9 years, 9 months ago.

KL46z as I2C Slave

I have a problem about I2C slave and my FRDM board KL46z. I'm using the example code wich is in "I2CSlave.h" the only change I made is the ports definiton and use a PC serial output to handle the incoming data from I2C master (Adruino). Here is the code

#include "mbed.h"
Serial pc(USBTX,USBRX);
 I2CSlave slave(PTE0, PTE1);
 
int main() {
      char buf[10];
      char msg[] = "Slave!";
      slave.frequency(100000); 
      slave.address(0x0F);
      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, 1);
                  pc.printf("Read G: %s\n", buf);
                  break;
              case I2CSlave::WriteAddressed:
                  slave.read(buf, 1);
                  pc.printf("Read A: %s\n", buf);
                  break;
          }
          for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
      }
 }

Whenever I send something from the master, It is supposed that the KL46z does an output through the pc serial. In advance thank you very much.

1 Answer

9 years, 9 months ago.

What is the slave address that the arduino master is using? Note that arduino uses the 7bit format for I2C adresses, whereas mbed uses the 8bit format. The mbed should use the arduino address shifted to the left by 1 bit. You have a slave address of 0x0F in the mbed code. That looks like a 7bit format to me. On mbed the LSB of an I2C address should always be 0.