6 years, 7 months ago.

Nucleo F103RB i2c Problem

Good Day everyone!

I have this setup here:

/media/uploads/hgg_lester/max30102_simulator_bb.jpg Note: I just use the battery image as reference for power supply. Power supply I used here is MiWi 5V/3A

I ran this code on the Master Device.

F103RB_I2C_Master

#include "mbed.h"

I2C i2cMaster(PB_9, PB_8);// SDA, SCL
Serial pc(USBTX, USBRX);

int main() {
    
    char writeData[2];
    pc.baud(57600);
    pc.printf("Starting I2CMaster\r\n");
    i2cMaster.frequency(400000);
    

    while(1) {
        int count = 0;
        for (int address=0; address<256; address+=2) {
            if (!i2cMaster.write( address , NULL, 0)) { // 0 returned is ok                
                pc.printf("I2C address 0x%02X\r\n", address);
                count++;
            }
        }        
        pc.printf("%d devices found\r\n", count);
        wait(2);       
    }
}

And this code on Slave Device

F103RB_I2C_Master

#include <mbed.h>

I2CSlave slave(PB_11, PB_10);
Serial pc(USBTX, USBRX);
 
#define ADDRESS 0x50
int main() {
   pc.baud(57600);
   pc.printf("Starting slave device at address:0x%02x\r\n", ADDRESS);
   slave.address(ADDRESS);
   slave.frequency(400000); //Set the clock frequency
   char buf[2];
   char msg[1];
   msg[0] = 0x00;
   int j = 1;
   
   while (1) {
       myled = 1;
       int i = slave.receive();
       if(i > 0)
            pc.printf("rcv:%d\r\n", i);
         
       switch (i) {
           case I2CSlave::ReadAddressed:
               pc.printf("I2CSlave::ReadAddressed\r\n");
               //slave.write(msg, strlen(msg) + 1); // Includes null char
               break;
           case I2CSlave::WriteAddressed:
               pc.printf("I2CSlave::WriteAddressed\r\n");
               slave.read(buf, 2);
               printf("Read A: 0x%02x, 0x%02x\r\n", buf[0], buf[1]);
               break;
       }
       buf[0] = 0;    // Clear buffer
       buf[1] = 0;
       msg[0] = j;
       j++;
       
   }
}

The problem is the Master Device cannot detect the Slave device(address = 0x50). It already scan address from 0 to 256. Is there something with my code or on my hardware setup?

Here is the logic analyzer result for more info:

/media/uploads/hgg_lester/nucleo_logic_analyzer.png

I'm hoping somebody can help me on this issue. I already doing this for 3 days but the issue is still there. I already also tried the other I2C pins but still no success.

Best Regards, Lester

2 Answers

6 years, 7 months ago.

Hello Lester,

In your code for the slave device rather than first setting address and then frequency:

   slave.address(ADDRESS);
   slave.frequency(400000); //Set the clock frequency

try to first set frequency and then address:

   slave.frequency(400000); //Set the clock frequency
   slave.address(ADDRESS);

Accepted Answer

The i2c voltage i used before is 5v, it should be 3v. Also the printf before the reading of data in slave can also affect and hang the slave device. Thanks for your help.

posted by Lester Lecong 23 Apr 2018
6 years, 7 months ago.

Hi Lester. I think the issue may be with your hardware setup. Believe you are not mating with the proper I2C pins on the (Master - left side) Nucleo board.

Shift the YELLOW wire up by one position.

Then do the same with the GREEN wire up by one position.

The top 2 pins on the board are the I2C lines and the diagram appears to be shifted down by one pin. Please post your results for future readers.

Also, for your I2C slave, after the above change to the wiring, consider to use an I2C address of 0xA0 (ie. 0x50 << 1) due to this similar topic thread:

https://os.mbed.com/questions/672/I2C-master-and-slave/

BTW: We are also working with I2C and the same target but as I2C Master only as of yesterday and is working fine using the same I2C port pins.

-

Update - ignore my comments on the I2C port pins. You do have that correct as the CN10 pins are shifted down by a pin so working with pins 3 & 5.

The issue could be just your I2C address value - try the 0xA0 on your slave and post your results.

/media/uploads/juthi/nucleo_i2c.png

Update # 2

Now I recall why this sounds so familiar...do review this lengthy thread and hope it helps you:

https://os.mbed.com/forum/platform-34-ST-Nucleo-F401RE-community/topic/26679/?page=1#comment-52936

The i2c voltage i used before is 5v, it should be 3v. Also the printf before the reading of data in slave can also affect and hang the slave device. Thanks for your help.

posted by Lester Lecong 23 Apr 2018