stm8s0003k3 I2c Slave

Thread Starter

Anup Sharma

Joined May 21, 2015
2
hi ,
i am working with camera sensor and stm8s003k3 MCU. i am communicating with the camera sensor using the I2C protocol. My MCU is the slave and sensor is master. there is issue in the communication.
MCU is able to detect the start bit and the slave address as a result it enters the interrupt part. but after acknowledgment from the MCU, sensor sends some random data along with restart command. I am unable to to read this random data as it never enters the interrupt again.

i have used the debugger in order to confirm this action. Moreover the SR3 register always show bus busy state. unable to understand this part . Please help.

this is my I2c init function
Code:
void Init_I2C (void)
{
    #ifdef I2C_slave_7Bits_Address
        /* Set I2C registers for 7Bits Address */
   I2C->CR1 |= 0x01;                            // I2C ENABLE
        I2C->CR2 = 0x04;                                  // Enable I2C acknowledgement
        I2C->FREQR =4;                               // Set I2C Freq value (16MHz)
        I2C->OARL = (SLAVE_ADDRESS<<1) ;    // set slave address to 0x51 (put 0xA2 for the register dues to7bit address)
        I2C->OARH = 0x40;                              // Set 7bit address mode

    #endif
    #ifdef I2C_slave_10Bits_Address
      /* Set I2C registers for 10Bits Address */
      I2C->CR1 |= 0x01;                  // Enable I2C peripheral
      I2C->CR2 = 0x04;                    // Enable I2C acknowledgement
      I2C->FREQR = 16;                     // Set I2C Freq value (16MHz)
      I2C->OARL = (SLAVE_ADDRESS & 0xFF) ;                            // set slave address LSB
      I2C->OARH = 0xC0 | ((SLAVE_ADDRESS & 0x300) >> 7);    // Set 10bits address mode and address MSB
    #endif
               // Enable I2C peripheral
    I2C->ITR    = 0x07;                          // all I2C interrupt enable 
}
and interrupt function
Code:
#ifdef _RAISONANCE_
void I2C_Slave_check_event(void) interrupt 19 {
#endif
#ifdef _COSMIC_
@far @interrupt void I2C_Slave_check_event(void) {
#endif


// save the I2C registers configuration

sr1 = I2C->SR1;
sr2 = I2C->SR2;
sr3 = I2C->SR3;


//GPIO_WriteLow(GPIOD,GPIO_PIN_2);
/* Communication error? */


  if (sr2 & (I2C_SR2_WUFH | I2C_SR2_OVR |I2C_SR2_ARLO |I2C_SR2_BERR))
  {     
    I2C->CR2|= I2C_CR2_STOP;  // stop communication - release the lines
    I2C->SR2= 0;                       // clear all error flags
    }
/* More bytes received ? */
  if ((sr1 & (I2C_SR1_RXNE | I2C_SR1_BTF)) == (I2C_SR1_RXNE | I2C_SR1_BTF))
  {
    I2C_byte_received(I2C->DR);
  I2C->CR2 |= I2C_CR2_ACK;
    }
/* Byte received ? */
  if (sr1 & I2C_SR1_RXNE)
  {
    I2C_byte_received(I2C->DR);
I2C->CR2 |= I2C_CR2_ACK;
}
/* NAK? (=end of slave transmit comm) */
  if (sr2 & I2C_SR2_AF)
  { 
    I2C->SR2 &= ~I2C_SR2_AF;     // clear AF
        I2C_transaction_end();
    }
/* Stop bit from Master  (= end of slave receive comm) */
  if (sr1 & I2C_SR1_STOPF)
  {
    I2C->CR2 |= I2C_CR2_ACK;     // CR2 write to clear STOPF
        I2C_transaction_end();
    }
/* Slave address matched (= Start Comm) */
  if (sr1 & I2C_SR1_ADDR)
  {   
    // I2C_transaction_begin();
        //I2C->CR2 |= I2C_CR2_ACK;
    }
/* More bytes to transmit ? */
  if ((sr1 & (I2C_SR1_TXE | I2C_SR1_BTF)) == ( I2C_SR1_TXE | I2C_SR1_BTF ))
  {
        I2C->DR = I2C_byte_write();
  }
/* Byte to transmit ? */
  if (sr1 & I2C_SR1_TXE)
  {
        I2C->DR = I2C_byte_write();
  } 

sr1 = I2C->SR1;
sr2 = I2C->SR2;
sr3 = I2C->SR3;
 

Thread Starter

Anup Sharma

Joined May 21, 2015
2
I have tested the type of data on the scope. Master is sending salve address with write bit and waiting for the acknowledgement bit and after the bit it goes 00 for 8 clock pulses. This should be the data. This is not being getting detected by the mcu.
 
Top