AnsweredAssumed Answered

Problem with I2C on STM8L151

Question asked by winix on Nov 4, 2014
Latest reply on Nov 6, 2014 by ozbayraktar.selcuk
Hello, I tried distilling the peripheral library sample on i2c to make it simpler for my own use.  However, strangely I cannot get pass a the send address part, and can't figure out why. Can someone give me some direction?  Attached is the code I used, and the line with problem is at 
"while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) " inside readRegistry function.  

Thanks. 

#include "stm8l15x.h"
 
void initI2C(void);
uint16_t readRegistry(uint8_t address, uint8_t reg);
 
#define HMC5883_ADDR        (0x1E)  
#define I2C_SPEED           100000 /*!< I2C Speed */
#define I2C_SCL_PIN         GPIO_Pin_1                  /* PC.01 */
#define I2C_SCL_GPIO_PORT   GPIOC                       /* GPIOC */
#define I2C_SDA_PIN         GPIO_Pin_0                  /* PC.00 */
#define I2C_SDA_GPIO_PORT   GPIOC                       /* GPIOC */
 
void main(void)
{
    CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
    enableInterrupts();
       
    initI2C();
    readRegistry(HMC5883_ADDR, 4);
     
    while (1)
    {
    }
}
 
void initI2C(void)
{
    GPIO_Init(GPIOC, GPIO_Pin_0, GPIO_Mode_Out_OD_HiZ_Fast);
    GPIO_Init(GPIOC, GPIO_Pin_1, GPIO_Mode_Out_OD_HiZ_Fast);
    CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);
    I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
    I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); 
    I2C_Cmd(I2C1, ENABLE);
}
 
 
uint16_t readRegistry(uint8_t address, uint8_t reg)
{
  uint16_t RegValue=0;
   
  I2C_AcknowledgeConfig(I2C1, DISABLE);
  I2C_GenerateSTART(I2C1, ENABLE);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))  /* EV5 */
  {
  }
 
  I2C_Send7bitAddress(I2C1, address, I2C_Direction_Transmitter);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6    <--- EVERYTHING STOPS HERE  */
  {
  }
   
  I2C_SendData(I2C1, reg);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
  {
  }
 
  I2C_GenerateSTART(I2C1, ENABLE);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))  /* EV5 */
  {
  }
 
  I2C_Send7bitAddress(I2C1, address, I2C_Direction_Receiver);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))  /* EV6 */
  {
  }
 
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))  /* EV7 */
  {
  }
 
  RegValue = I2C_ReceiveData(I2C1);
  I2C_AcknowledgeConfig(I2C1, DISABLE);
  I2C_GenerateSTOP(I2C1, ENABLE);
  while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
  {}
   
  return RegValue;
}




Outcomes