cancel
Showing results for 
Search instead for 
Did you mean: 

I2C peripheral (STM8S005C6) not working

Darryl Weimers
Associate II
Posted on August 29, 2017 at 16:39

*******************************I2C peripheral of STM8S005C6 not working******************************************

I2C peripheral of STM8S005C6 does not jump to interrupt service routine:

                  INTERRUPT_HANDLER(I2C_IRQHandler, 19)

and busy flag is always set. Using an external device (verified to be working), I generate an I2C signal (100KHz) to communicate with STM8S005C6 program as a slave receiver with the 7-bit address of 0x24.

0690X000006083WQAQ.png 

 I have read out all the I2C registers to ensure all registers are properly configured. 

0690X000006083NQAQ.png

For hardware side, SCL and SDA line is pulled up to 5v (verified with an oscilloscope). 

For firmware side, I am using the I2C example code with modifications provided by STM :

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

 

**********************************************Code Below*******************************

&sharpinclude ''stm8s.h''

&sharpinclude ''stm8s_clk.h''

&sharpinclude ''stm8s_i2c.h''

&sharpinclude ''stm8s_gpio.h''

&sharpinclude ''stm8s_it.h''

void main(void){

//

**************************************

Clock setup (16 MHz Internal clock + I2C peripheral clock) 

CLK_DeInit();

CLK_HSECmd(DISABLE);

CLK_LSICmd(DISABLE);

CLK_ClockSwitchCmd(DISABLE);

CLK_HSICmd(ENABLE);

while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, ENABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);

CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);

//**************************************GPIO setup

GPIO_DeInit(GPIOB);

GPIO_Init(GPIOB, GPIO_PIN_4, GPIO_MODE_OUT_OD_HIZ_FAST);

GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_OD_HIZ_FAST);

//

**************************************

I2C setup (100KHz, 7 bit address mode, all I2C interrupt enable, address: 0x24)  

I2C_DeInit();

I2C_Init(100000,

             (0x24<<1),

             I2C_DUTYCYCLE_2,

             I2C_ACK_CURR,

             I2C_ADDMODE_7BIT,

            (CLK_GetClockFreq() / 1000000));

I2C_Cmd(ENABLE);

I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);

// ***********************************************Enable general interrupts

enableInterrupts();

while(1); 

}

// Excerpt from file ''stm8s_it.c''

INTERRUPT_HANDLER(I2C_IRQHandler, 19)

{

/* Read SR2 register to get I2C error */

if ((I2C->SR2) != 0)

{

/* Clears SR2 register */

I2C->SR2 = 0;

/* Set LED2 */

//STM_EVAL_LEDOn(LED2);

}

Event = I2C_GetLastEvent();

switch (Event)

{

/******* Slave transmitter ******/

/* check on EV1 */

case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:

Tx_Idx = 0;

break;

/* check on EV3 */

case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:

/* Transmit data */

I2C_SendData(Slave_Buffer_Rx[Tx_Idx++]);

break;

/******* Slave receiver **********/

/* check on EV1*/

case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:

break;

/* Check on EV2*/

case I2C_EVENT_SLAVE_BYTE_RECEIVED:

Slave_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData();

break;

/* Check on EV4 */

case (I2C_EVENT_SLAVE_STOP_DETECTED):

/* write to CR2 to clear STOPF flag */

I2C->CR2 |= I2C_CR2_ACK;

break;

default:

break;

}

}

#i2c-peripheral-of-stm8s005c6-not-working
5 REPLIES 5
Franco Lazzarotti
Associate II
Posted on September 04, 2017 at 18:28

Hi Darryl,

have you enabled the I2C peripheral by setting its bit on OPT bytes?

Posted on September 05, 2017 at 15:03

Yes, I did enable the I2C peripheral. The register I2C_CR1 (Control Register 1) bit 0 has been set to 1. 

Posted on September 05, 2017 at 15:18

I meant the AFR bits on OPT bytes

Posted on September 05, 2017 at 15:25

The AFR6 bit enables the alternate function (I2C) on the &sharpPB5/&sharpPB4 port pin

Posted on September 05, 2017 at 15:44

Hi Franco, 

No, I did not enable the alternate function option byte. After enabling it, I2C interrupt works.

Thanks a lot. 

Darryl