Mistake on this page?
Report an issue in GitHub or email us

I2CSlave

Use I2C Slave to communicate with I2C Master.

Synchronization level: not protected.

I2CSlave class reference

Public Types
enum  RxStatus { NoData = 0, ReadAddressed = 1, WriteGeneral = 2, WriteAddressed = 3 }
Public Member Functions
 I2CSlave (PinName sda, PinName scl)
void frequency (int hz)
int receive (void)
int read (char *data, int length)
int read (void)
int write (const char *data, int length)
int write (int data)
void address (int address)
void stop (void)
Protected Attributes
i2c_t _i2c

I2CSlave example

Try this example to see how an I2C responder works.

#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
   }
}
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.