AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
I2cSlave.h
Go to the documentation of this file.
1 /*
2  I2cSlave.h - An I2C slave library
3  For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4  This is part of the AVRTools library.
5  Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 
23 
24 
59 #ifndef I2cSlave_h
60 #define I2cSlave_h
61 
62 
63 #ifdef I2cMaster_h
64 #error "You cannot use both I2cMaster and I2cSlave in the same application"
65 #endif
66 
67 
68 #include <stdint.h>
69 
70 #ifdef DEBUG_I2cSlaveDiary
71 #include "USART0.h"
72 #endif
73 
74 
75 
76 #ifndef I2C_SLAVE_BUFFER_SIZE
77 #define I2C_SLAVE_BUFFER_SIZE 32
78 #endif
79 
80 #if I2C_SLAVE_BUFFER_SIZE > 255
81 #error "I2C_SLAVE_BUFFER_SIZE exceeds size of a uint8_t"
82 #endif
83 
84 
85 
86 
87 
107 namespace I2cSlave
108 {
109 
110 
138  uint8_t processI2cMessage( uint8_t* buffer, uint8_t len );
139 
140 
141 
142 
149  {
152  };
153 
154 
155 
160  {
161 /*
162  kI2cCompletedOk = 0x00,
163  kI2cNotStarted = 0x01,
164  kI2cInProgress = 0x02,
165  kI2cError = 0x04,
166  kI2cBusError = 0x07
167 */
169  kI2cError = 0x01,
170  kI2cTxPartial = 0x02,
171  kI2cRxOverflow = 0x04,
173  };
174 
175 
176 
183  {
186  };
187 
188 
189 
190 
204  void start( uint8_t ownAddress, uint8_t speed = kI2cBusFast, bool answerGeneralCall = false );
205 
206 
212  void stop();
213 
214 
223  void pullups( uint8_t set = kPullupsOn );
224 
225 
232  bool busy();
233 
234 
235 #ifdef DEBUG_I2cSlaveDiary
236 
237  void setDebugSout( Serial0* s );
238 
239  void clearDebugI2cDiary();
240  void dumpDebugI2cDiary();
241 
242 #endif
243 
244 };
245 
246 
247 #endif
248 
This file provides functions that offer high-level interfaces to USART0 hardware, which is available ...
I2C communications completed with no error.
Definition: I2cSlave.h:168
This namespace bundles the I2C-protocol-based interface to the TWI hardware. It provides logical cohe...
Definition: I2cSlave.h:107
I2C communications encountered an error.
Definition: I2cSlave.h:169
Enable the built-in TWI hardware pullups.
Definition: I2cSlave.h:185
I2cStatusCodes
This enum lists I2C status codes reported by the various transmit functions.
Definition: I2cSlave.h:159
bool busy()
Reports whether the TWI hardware is busy communicating (either transmitting or receiving).
Definition: I2cSlave.cpp:186
I2C fast mode: 400 KHz.
Definition: I2cSlave.h:151
uint8_t processI2cMessage(uint8_t *buffer, uint8_t len)
This function must be defined by the user. It is called by the TWI interrupt function installed as pa...
Provides a high-end interface to serial communications using USART0.
Definition: USART0.h:295
void stop()
Terminates the I2C communications using the TWI hardware, and disables the TWI interrupts.
Definition: I2cSlave.cpp:163
Disable the built-in TWI hardware pullups.
Definition: I2cSlave.h:184
I2C slow (standard) mode: 100 KHz.
Definition: I2cSlave.h:150
I2cBusSpeed
This enum lists I2C bus speed configurations.
Definition: I2cSlave.h:148
I2C communications on this message still in progress.
Definition: I2cSlave.h:172
void start(uint8_t ownAddress, uint8_t speed=kI2cBusFast, bool answerGeneralCall=false)
Configures the TWI hardware for I2C communications in Slave mode. You must call this function before ...
Definition: I2cSlave.cpp:112
I2cPullups
This enum lists the options for controlling the built-in pullups in the TWI hardware.
Definition: I2cSlave.h:182
void pullups(uint8_t set=kPullupsOn)
Sets the state of the internal pullups that are part of the TWI hardware.
Definition: I2cSlave.cpp:170
I2C Master terminated transmission before all data were sent.
Definition: I2cSlave.h:170
Recieved a message larger than can be held in the receive buffer.
Definition: I2cSlave.h:171