cancel
Showing results for 
Search instead for 
Did you mean: 

I2C setting problem on STM8L101

Edward Wong
Associate
Posted on April 24, 2017 at 08:39

Hi,

I have a #I2C setting problem on STM8L101, I am using the ST's ''STM8L10x_StdPeriph_Driver'' library and try to setup the I2C bus to run at standard mode 100KHz (with system clock of 2MHz) for reading a magnetic sensor. Unfortunately, it seems the I2C clock simply does not start properly.  However, if i slow down the I2C clock to 25KHz, the I2C bus becomes working normally and can get data from the magnetic sensor correctly.

Below is the program code that I used to configure the I2C interface of STM8L101:

====================================================

//Initialization of Peripheral and CPU clock

   CLK_PeripheralClockConfig(CLK_Peripheral_I2C, ENABLE);     //Activation of I2C_CTR clock

   CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv8); // System clock (fMaster clock) is 2MHz

 

// Setup I2C pin => PC0 = SDA, PC1 = SCL (Both pin has external 4.7KOhm pull-up resistor) 

    GPIO_SetBits(MAIN_SCL_MAG_GPIO_PORT, MAIN_SCL_MAG_GPIO_PIN); // set HIGH

    GPIO_SetBits(MAIN_SDA_MAG_GPIO_PORT, MAIN_SDA_MAG_GPIO_PIN);

    GPIO_Init(GPIOC, MAIN_SDA_MAG_GPIO_PIN & MAIN_SCL_MAG_GPIO_PIN, GPIO_Mode_Out_OD_HiZ_Fast);

 //Initialization of the integrated I2C peripheral

    I2C_Cmd(ENABLE); //Activation of I2C peripheral

    I2C_DeInit();    //Initialization the I2C peripheral registers to their default reset values.

    I2C_Init(100000, 0xA0, I2C_DutyCycle_2,I2C_Ack_Enable,I2C_AcknowledgedAddress_7bit); // Initialization of I2C peripheral

====================================================

If I use the below code (using 100KHz I2C speed), the I2C clock does NOT output:

  

I2C_Init(100000, 0xA0, I2C_DutyCycle_2,I2C_Ack_Enable,I2C_AcknowledgedAddress_7bit);

0690X00000603nFQAQ.jpg

When I changed it to below code (using 25KHz I2C speed), the I2C clock can work correctly:

  

I2C_Init(25000, 0xA0, I2C_DutyCycle_2,I2C_Ack_Enable,I2C_AcknowledgedAddress_7bit);

0690X00000603nKQAQ.jpg0690X00000606p6QAA.png

Questions:

1) Do you know what would be the possible cause of this I2C setting problem?

2) How can I use the standard mode 100KHz and what registers setting shall be used?

Thanks!

1 REPLY 1
Simon V.
ST Employee
Posted on May 03, 2017 at 10:51

Hi,

I would suggest to review the system clock configuration. Regarding the product datasheet, the I2C peripheral can work up to 8MHz. I would advise you to use the internal HSI @ 16MHz.

Datasheet link :

http://www.st.com/content/ccc/resource/technical/document/datasheet/cb/08/7a/32/32/d8/4d/23/CD00220591.pdf/files/CD00220591.pdf/jcr:content/translations/en.CD00220591.pdf

 

You can also read the following AN3281 that describes optimized I2C examples.

http://www.st.com/en/embedded-software/stsw-stm8004.html

 

Regards,

Simon

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.