AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Namespaces | Enumerations | Functions
I2cSlave.h File Reference

This file provides functions that interface to the TWI (two-wire serial interface) hardware of the Arduino Uno (ATmega328) and Arduino Mega (ATmega2560), providing a high-level interface to I2C protocol communications. Include this file if you want your application will operate in Slave mode as defined in the I2C protocol. More...

#include <stdint.h>
Include dependency graph for I2cSlave.h:

Go to the source code of this file.

Namespaces

 I2cSlave
 This namespace bundles the I2C-protocol-based interface to the TWI hardware. It provides logical cohesion for functions implement the Slave portions of the I2C protocol and prevents namespace collisions.
 

Enumerations

enum  I2cSlave::I2cBusSpeed { I2cSlave::kI2cBusSlow, I2cSlave::kI2cBusFast }
 This enum lists I2C bus speed configurations. More...
 
enum  I2cSlave::I2cStatusCodes {
  I2cSlave::kI2cCompletedOk = 0x00, I2cSlave::kI2cError = 0x01, I2cSlave::kI2cTxPartial = 0x02, I2cSlave::kI2cRxOverflow = 0x04,
  I2cSlave::kI2cInProgress = 0x06
}
 This enum lists I2C status codes reported by the various transmit functions. More...
 
enum  I2cSlave::I2cPullups { I2cSlave::kPullupsOff, I2cSlave::kPullupsOn }
 This enum lists the options for controlling the built-in pullups in the TWI hardware. More...
 

Functions

uint8_t I2cSlave::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 part of I2cSlave.cpp whenever it receives a message from the Master. The user should implement this function to respond to the data in the buffer, taking actions and as appropriate returning data to the buffer (for asynchronous transmission to the Master). More...
 
void I2cSlave::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 conducting any I2C communications using the functions in this module. More...
 
void I2cSlave::stop ()
 Terminates the I2C communications using the TWI hardware, and disables the TWI interrupts. More...
 
void I2cSlave::pullups (uint8_t set=kPullupsOn)
 Sets the state of the internal pullups that are part of the TWI hardware. More...
 
bool I2cSlave::busy ()
 Reports whether the TWI hardware is busy communicating (either transmitting or receiving). More...
 

Detailed Description

This file provides functions that interface to the TWI (two-wire serial interface) hardware of the Arduino Uno (ATmega328) and Arduino Mega (ATmega2560), providing a high-level interface to I2C protocol communications. Include this file if you want your application will operate in Slave mode as defined in the I2C protocol.

To use these functions, include I2cSlave.h and link against I2cSlave.cpp.

These interfaces are buffered for recieving and sending data and operate using interrupts associated with the TWI hardware. This means data from the Master is received asynchronously and when reception is complete, a user-supplied function is called. That function has the option of placing data in a buffer to be transmitted asynchronously back to the Master.

The Slave buffer is a simple array. The size of the Slave buffer can be set at compile time via the macro constant I2C_SLAVE_BUFFER_SIZE. The default size of the Slave buffer is 32 bytes. You can change the default by defining the macro I2C_SLAVE_BUFFER_SIZE prior to including the file I2cSlave.h, each time it is included. So you should define it using a compiler option (e.g., -DI2C_SLAVE_BUFFER_SIZE=64) to ensure it is consistently defined throughout your project.

This interface assumes your application will operator in I2C Slave mode as defined in the I2C protocol. If you wish your application to operate in I2C Master mode, then instead include I2cMaster.h and link against I2cMaster.cpp.

Note
Only one of I2cMaster.cpp and I2cSlave.cpp can be linking into your application. These two files install different, incompatible versions of the TWI interrupt function. AVRTools does not support building an application that functions both as a Master and as a Slave under the I2C protocol. This limitation allows the corresponding TWI interrupt functions to be significantly leaner and faster.