Joris Aerts / I2CSlaveX
Embed: (wiki syntax)

« Back to documentation index

I2CSlaveX Class Reference

I2CSlaveX Class Reference

An I2C Slave, used for communicating with an I2C Master device with support for Interrupts and multiple slave addresses. More...

#include <I2CSlaveX.h>

Public Member Functions

 I2CSlaveX (PinName sda, PinName scl)
 Create an I2C Slave interface, connected to the specified pins.
int receive (char *data=NULL)
 Checks to see if this I2C Slave has been addressed.
int read (char *data, int length)
 Read from an I2C master.
int write (const char *data, int length)
 Write to an I2C master.
void address (int address, int idx=0)
 Sets the I2C slave address.
void attach (void(*fptr)(void))
 Attach a function to call when state changed event occured on the I2C peripheral.
template<typename T >
void attach (T *tptr, void(T::*mptr)(void))
 Attach a member function to call when state changed event occured on the I2C peripheral.
void enable_irq ()
 Enable IRQ.
void disable_irq ()
 Disable IRQ.

Detailed Description

An I2C Slave, used for communicating with an I2C Master device with support for Interrupts and multiple slave addresses.

Warning: Currently only somewhat tested with LPC1347. This should be concidered a proof of concept. Ideally this would be merged into the original I2CSlave library.

Example:

 // Simple Simulated I2C Read-Only EEPROM
 #include <mbed.h>
 #include "I2CSlaveX.h"
 
 I2CSlaveX slave(p9, p10);
 
 uint8_t eeprom_data[16] = {0);
 uint8_t eeprom_addr = 0;
 
 void receive_handler() {
     char addr;
     int i = slave.receive(&addr);
     switch (i) {
         case I2CSlave::ReadAddressed:
             slave.write(&eeprom_data[eeprom_addr], 1);
             printf("Write @ %02X [%02X] : %02X\n", addr, eeprom_addr, eeprom_data[eeprom_addr]);
             break;
         case I2CSlave::WriteAddressed:
             slave.read(&eeprom_addr, 1);
             printf("Read  @ %02X [%02X]\n", addr, eeprom_addr);
             break;
     }
 }
 
 int main() {
     slave.address(0xA0, 0);
     slave.address(0x30, 1);
     slave.attach(&receive_handler);
     
     while(1) {
         printf("Nothing to do really...");
         eeprom_data[0]++;
         wait(1);
     }
 }

Definition at line 52 of file I2CSlaveX.h.


Constructor & Destructor Documentation

I2CSlaveX ( PinName  sda,
PinName  scl 
)

Create an I2C Slave interface, connected to the specified pins.

Parameters:
sdaI2C data line pin
sclI2C clock line pin

Definition at line 9 of file I2CSlaveX.cpp.


Member Function Documentation

void address ( int  address,
int  idx = 0 
)

Sets the I2C slave address.

Parameters:
addressThe address to set for the slave (ignoring the least signifcant bit). If set to 0, the slave will only respond to the general call address.

Definition at line 14 of file I2CSlaveX.cpp.

void attach ( void(*)(void)  fptr )

Attach a function to call when state changed event occured on the I2C peripheral.

Parameters:
fptrA pointer to a void function, or 0 to set as none

Definition at line 37 of file I2CSlaveX.cpp.

void attach ( T *  tptr,
void(T::*)(void)  mptr 
)

Attach a member function to call when state changed event occured on the I2C peripheral.

Parameters:
tptrpointer to the object to call the member function on
mptrpointer to the member function to be called

Definition at line 115 of file I2CSlaveX.h.

void disable_irq (  )

Disable IRQ.

This method depends on hw implementation, might disable one port interrupts. For further information, check gpio_irq_disable().

Definition at line 57 of file I2CSlaveX.cpp.

void enable_irq (  )

Enable IRQ.

This method depends on hw implementation, might enable one port interrupts. For further information, check gpio_irq_enable().

Definition at line 50 of file I2CSlaveX.cpp.

int read ( char *  data,
int  length 
)

Read from an I2C master.

Parameters:
datapointer to the byte array to read data in to
lengthmaximum number of bytes to read
Returns:
0 on success, non-0 otherwise

Definition at line 28 of file I2CSlaveX.cpp.

int receive ( char *  data = NULL )

Checks to see if this I2C Slave has been addressed.

Returns:
A status indicating if the device has been addressed, and how
  • NoData - the slave has not been addressed
  • ReadAddressed - the master has requested a read from this slave
  • WriteAddressed - the master is writing to this slave
  • WriteGeneral - the master is writing to all slave

Definition at line 20 of file I2CSlaveX.cpp.

int write ( const char *  data,
int  length 
)

Write to an I2C master.

Parameters:
datapointer to the byte array to be transmitted
lengththe number of bytes to transmite
Returns:
0 on success, non-0 otherwise

Definition at line 33 of file I2CSlaveX.cpp.