Skip to main content
Visitor II
May 5, 2019
Question

Unable to communicate to external peripheral device using I2C.

  • May 5, 2019
  • 6 replies
  • 4626 views

I am using STM8L Discovery board, and want to communicate with external memory and humidity sensor which requires I2C protocol for communication.

I am using STM8l15x standard peripheral library, but unable to communicate. I found the following problems while debug:

  1. If not connecting any pull-up resistor, not getting acknowledgment from slave device and getting stuck in while((I2C1->SR1 & I2C_SR1_ADDR) == 0) loop
  2. If connecting pull-up resistor on both SDA and SCL (1k and 4.7K tried), unable to generate start condition.

Please help me out how can I resolve it?

Thank you so much in advance.

    This topic has been closed for replies.

    6 replies

    Visitor II
    May 5, 2019

    The i2c bus needs pull-up resistors on both lines (SCL, SDA), it cannot work without them.

    I just checked the STM8L Disco. board schematics: your SB17 jumper needs to be open, so that the blue button is not functional anymore.

    How do you know it cannot generate the start condition? Post some more info and maybe the relevant part of your software.

    NPabaAuthor
    Visitor II
    May 19, 2019

    Hello Cristian,

    First of all thank you very much for your reply, and sorry for late reply from my side.

    How do you know it cannot generate the start condition?

    -> I am using standard peripheral library of stm8l152, where by putting breakpoint (code get stuck here:while(!(I2C1->SR1 & 0x02); I observed that start condition is not generated. But now I am able to generate start condition even with pull up resistor.

    Still there is no acknowledgement from slave.

    I just checked the STM8L Disco. board schematics: your SB17 jumper needs to be open, so that the blue button is not functional anymore.

    -> For this I need to desolder the jumper. Let me try this.

    Thank you ;)

    NPabaAuthor
    Visitor II
    May 19, 2019

    Hello Cristian,

    I tried by removing the SB17 jumper, but still the issue is same.

    Please help.

    NPabaAuthor
    Visitor II
    May 19, 2019

    I am using this file.

    Slave device: EEPROM - 24AA512, device address 0x50

    Visitor II
    May 19, 2019

    Dusting off my old ST7/STM8 I2C bit bang code from 2003...

    As salesforce can't import more than a single file (thank god, we don't need to comply with 8 ASCII . 3 ASCII filename...

    Visitor II
    May 19, 2019

    one more

    	#define I2C_DELAY 4 //2
     
    	#define GPIO_SM2_I2C_SCL 	 	GPIOC,GPIO_Pin_1
    	#define GPIO_SM2_I2C_SDA 	 	GPIOC,GPIO_Pin_0

    Visitor II
    May 19, 2019

    It's rustly and I've got something a bit cleaner for STM32. Used it with many humidity sensors (ST, Honeywell, Bosch, (Sensirion is not supported as it's the only sensor with clock stretching), etc...). Functionally rugged like a hummer.

    Visitor II
    May 21, 2019

    Hello Pabari!

    I only had a short view on the code you posted, I think you are mistaking addresses. ​First of all, in master mode only (this is your case if you only have the EEPROM on the bus) you don't use the MCU's Own Address as it is not addressed as a slave.

    The 0xA0 address is the slave's address, if you have more slaves on the same bus this is how you can access them, and if you always leftshift this address to add the R/W bit, than the 0x50 value was good.

    ​What you mention 2 byte addressing is actually the EEPROM's address where you want to write/read from.

    Try to understand the EEPROM's datasheet, I will try to provide you some sample code.​

    Visitor II
    May 21, 2019

    yeah, everybody is confused as I2C address can be said to be 7 bits 0x50 = 101 0000 or 0xA0/A1 if you add the read/write bit to make it 8 bit.

    In my case, when I boot, I sweep for all possible slave address and it gives me a list of detected slaves, faster than guessing how the datasheet presents it.