6 years, 5 months ago.

I2C Master<>Slave communication - only two bytes sent

I'm ting to acheive transmission from I2C Master to Slave using below code:

NUCLEO_F042K6____MASTER

#include "mbed.h"

I2C i2c_port(PF_0, PF_1);
const int addr = 0xA0;
unsigned char buf[6];

int main()
{
    //i2c_port.frequency(25000);
    buf[0]=0x00;
    buf[1]=0x30;
    buf[2]=0x09;
    
    buf[3]=0x00;
    buf[4]=0x31;
    buf[5]=0x10;
    
    
    
    while(1) {
      
      wait(0.002);
      
        i2c_port.start();
        i2c_port.write(addr);
        i2c_port.write(buf[0]);
        i2c_port.write(buf[1]);
        i2c_port.write(buf[2]);
        i2c_port.stop();
        
        wait(0.002);
        
        i2c_port.start();
        i2c_port.write(addr);
        i2c_port.write(buf[3]);
        i2c_port.write(buf[4]);
        i2c_port.write(buf[5]);
        i2c_port.stop();
        
    }
}

NUCLEO_L053R8___SLAVE

#include <mbed.h>
I2CSlave slave(PB_9, PB_8);
//Serial pc(SERIAL_TX, SERIAL_RX);

char rec_buf[10];

int main() 
{
    slave.address(0xA0);
        while (1) 
        {
            int i = slave.receive();
            if (i == 3)
            { 
                rec_buf[0]=slave.read();  
                rec_buf[1]=slave.read(); 
                rec_buf[2]=slave.read(); 
                rec_buf[3]=slave.read(); 
                rec_buf[4]=slave.read();
                rec_buf[5]=slave.read();                 
            }
        
        }
}    

Results on bus are showed below:

/media/uploads/slawekm/1.png /media/uploads/slawekm/2.png

I'm tring to send data from master buf[] so I expect to have 3 bytes sent like on below picture (captured form eeprom)

/media/uploads/slawekm/3.png /media/uploads/slawekm/4.png

Please check my code and give me any advice to send and receive data like below: /media/uploads/slawekm/3.png

Be the first to answer this question.