AnsweredAssumed Answered

I2C peripheral (STM8S005C6) not working

Question asked by Darryl Weimers on Aug 29, 2017
Latest reply on Sep 5, 2017 by Darryl Weimers

*******************************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.

 

 

 

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

 

 

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 :

STSW-STM8069 - STM8S/A Standard peripheral library - STMicroelectronics - STMicroelectronics 

 

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

#include "stm8s.h"
#include "stm8s_clk.h"
#include "stm8s_i2c.h"
#include "stm8s_gpio.h"
#include "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;
}

}

Outcomes