AnsweredAssumed Answered

STM8S001J3 I2C: Nothing happens

Question asked by karl.maximilian.001 on Sep 11, 2017

I'm trying to get I2C to work, but nothing happens on either SDA nor SCL.

I've connected the STM8S001J3 like so:

Pin 5   SDA

Pin 6   SCL

(There are pull-ups on both of the lines)

 

1. I initialize the clocks:

     CLK_DeInit();
     
     CLK_HSECmd(DISABLE);
     CLK_LSICmd(DISABLE);
     CLK_HSICmd(ENABLE);
     while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
     
     CLK_ClockSwitchCmd(ENABLE);
     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
     CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV2);
     
     CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
     DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
     
     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);

Then I set the pins:

/*SDA, SCL*/
GPIO_Init(GPIOB, GPIO_PIN_4, GPIO_MODE_OUT_OD_HIZ_FAST);
GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_OD_HIZ_FAST);

Inititalize I2C:

void Init_I2C1(uint32_t clockSpeed, uint8_t ownAddress)
{
     I2C_Cmd(DISABLE);
     I2C_Init(clockSpeed,
               ownAddress,
               I2C_DUTYCYCLE_2,
               I2C_ACK_CURR,
            I2C_ADDMODE_7BIT,
               16
               );
     I2C_Cmd(ENABLE);
}

And then I (try to) write:

DS442x_ReturnState returnState = { .state = (uint8_t)1 };
     uint16_t timeout = DAC_DS442x_Timeout;
     
     I2C_GenerateSTART(ENABLE);
     
     while(!I2C_GetFlagStatus(I2C_FLAG_STARTDETECTION))
     {
          timeout--;
          if(timeout == 0)
          {
               returnState.state = 0;
               return returnState;
          }
     }
     
     I2C->DR = (uint8_t)(address << 1);
     
     timeout = DAC_DS442x_Timeout;
     while(!I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED))
     {
          timeout--;
          if(timeout == 0)
          {
               returnState.state = 0;
               return returnState;
          }
     }
     
     I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
     
     timeout = DAC_DS442x_Timeout;
     while(!I2C_GetFlagStatus(I2C_FLAG_TXEMPTY))
     {
          timeout--;
          if(timeout == 0)
          {
               returnState.state = 0;
               return returnState;
          }
     }
     
     I2C->DR = registerAddress;
     
     timeout = DAC_DS442x_Timeout;
     while(!I2C_GetFlagStatus(I2C_FLAG_TXEMPTY))
     {
          timeout--;
          if(timeout == 0)
          {
               returnState.state = 0;
               return returnState;
          }
     }
     
     I2C->DR = data;
     
     timeout = DAC_DS442x_Timeout;
     while(!I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED))
     {
          timeout--;
          if(timeout == 0)
          {
               returnState.state = 0;
               return returnState;
          }
     }
     
     I2C_GenerateSTOP(ENABLE);
     
     return returnState;

But nothing happens - as in, there are no signals on the lines.

Am I missing something?

Thanks in advance!

Outcomes