4 years, 8 months ago.

connection issues with i2C KL25Z as master and KL05Z as slave

Hi, updated: I noticed that the slave is holding the clock line down once it receives the write address command. Hope this helps solving the problem.

I tried to use I2C to connect KL25Z as master and KL05Z as slave, but it seems like the KL25Z master is not writing the correct data to the bus. It'd be great if anyone notice an error in the code. The i2c_slave1.receive() only returns 3, which is I2CSlave::WriteAddressed: no matter what the master sends to the slave I'd really appreciate to know what is going on in the I2C here's my updated code

KL25Z master

#include "mbed.h"


namespace master{
//I2C setup
I2C i2c(PTC9, PTC8);
Serial pc(USBTX, USBRX);
}

int main()
{   const int slave_addr = 0x0A ;
    char cmd[1];
    master::pc.baud(9600);

    master::i2c.frequency(100000);

    while(1) {
        master::i2c.start();
        cmd[0] = 0x01;
        master::i2c.write(slave_addr,cmd,1,true);
        master::i2c.stop();
        cmd[0] = 0x02;
        master::i2c.write(slave_addr,cmd,1);
        master::i2c.stop();
        wait(2);
        cmd[0] = 0x03;
        master::i2c.write(slave_addr,cmd,1);
        
        master::i2c.stop();
    }

}

KL05Z slave

#include "mbed.h"


namespace slave1
{

Serial pc(USBTX, USBRX);



DigitalOut green(LED_GREEN);
DigitalOut red(LED_RED);
DigitalOut blue(LED_BLUE);

}

I2CSlave i2c_slave1(PTB4, PTB3);



//I2C setup

int main()
{   const int slave_addr = 0x0A;
    slave1::red = 1;
    slave1::green = 1;
    slave1::blue = 1;
    i2c_slave1.frequency(100000); //Set the clock frequency
    char buf[1];
    char msg[1];
    msg[0] = 0x00;
    i2c_slave1.address(slave_addr);
    while (1) {
        int i = i2c_slave1.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                slave1::green = 0;
                wait(1);
                break;
            case I2CSlave::WriteGeneral:
                slave1::blue = 0;
                wait(1);
                break;
            case I2CSlave::WriteAddressed:
                slave1::red = 0;
                wait(1);
                break;
        }
        slave1::pc.printf("%i",i2c_slave1.receive());
        for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
        
    }
    buf[0] = 0;    // Clear buffer
    i2c_slave1.stop();
}

i2c_slave1.receive(); only returns 3 no matter what the master sends /media/uploads/conrad2001/3.png

1 Answer

4 years, 8 months ago.

Hi Conrad,

Thanks for your questions and answer from you self (Good work!), I just link the post you gave and make sure anyone can find the answer to the same problem.

https://os.mbed.com/forum/mbed/topic/36122/?page=1#comment-63070

Regards, Desmond