The official mbed C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

Embed: (wiki syntax)

« Back to documentation index

I2CSlave Class Reference

I2CSlave Class Reference
[Drivers]

An I2C Slave, used for communicating with an I2C Master device. More...

#include <I2CSlave.h>

Public Member Functions

 I2CSlave (PinName sda, PinName scl)
 Create an I2C Slave interface, connected to the specified pins.
void frequency (int hz)
 Set the frequency of the I2C interface.
int receive (void)
 Checks to see if this I2C Slave has been addressed.
int read (char *data, int length)
 Read from an I2C master.
int read (void)
 Read a single byte from an I2C master.
int write (const char *data, int length)
 Write to an I2C master.
int write (int data)
 Write a single byte to an I2C master.
void address (int address)
 Sets the I2C slave address.
void stop (void)
 Reset the I2C slave back into the known ready receiving state.

Detailed Description

An I2C Slave, used for communicating with an I2C Master device.

Note:
Synchronization level: Not protected

Example:

 // Simple I2C responder
 #include <mbed.h>

 I2CSlave slave(p9, p10);

 int main() {
     char buf[10];
     char msg[] = "Slave!";

     slave.address(0xA0);
     while (1) {
         int i = slave.receive();
         switch (i) {
             case I2CSlave::ReadAddressed:
                 slave.write(msg, strlen(msg) + 1); // Includes null char
                 break;
             case I2CSlave::WriteGeneral:
                 slave.read(buf, 10);
                 printf("Read G: %s\n", buf);
                 break;
             case I2CSlave::WriteAddressed:
                 slave.read(buf, 10);
                 printf("Read A: %s\n", buf);
                 break;
         }
         for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
     }
 }

Definition at line 65 of file I2CSlave.h.


Constructor & Destructor Documentation

I2CSlave ( PinName  sda,
PinName  scl 
)

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

Parameters:
sdaI2C data line pin
sclI2C clock line pin

Member Function Documentation

void address ( int  address )

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.
void frequency ( int  hz )

Set the frequency of the I2C interface.

Parameters:
hzThe bus frequency in hertz
int read ( void   )

Read a single byte from an I2C master.

Returns:
the byte read
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
int receive ( void   )

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
void stop ( void   )

Reset the I2C slave back into the known ready receiving state.

int write ( int  data )

Write a single byte to an I2C master.

Parameters:
datathe byte to write
Returns:
'1' if an ACK was received, '0' otherwise
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