mbed official / mbed Featured

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

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.

Synchronization level: Not protected


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

 I2CSlave slave(p9, p10);

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

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

Definition at line 65 of file I2CSlave.h.