mbed OS-5 I2C Slave using Nucleo-F401RE board.

main.cpp

Committer:
jingxizhang
Date:
2016-08-22
Revision:
0:96840f1cc413

File content as of revision 0:96840f1cc413:

#include "mbed.h"

I2CSlave slave(PB_9, PB_8);

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

   slave.address(0x0A);
   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
   }
}