stm8s0003k3 I2c Slave

Discussion in 'Embedded Systems and Microcontrollers' started by Anup Sharma, May 21, 2015.

  1. Anup Sharma

    Thread Starter New Member

    May 21, 2015
    2
    0
    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 (Text):
    1. void Init_I2C (void)
    2. {
    3.     #ifdef I2C_slave_7Bits_Address
    4.         /* Set I2C registers for 7Bits Address */
    5.    I2C->CR1 |= 0x01;                            // I2C ENABLE
    6.         I2C->CR2 = 0x04;                                  // Enable I2C acknowledgement
    7.         I2C->FREQR =4;                               // Set I2C Freq value (16MHz)
    8.         I2C->OARL = (SLAVE_ADDRESS<<1) ;    // set slave address to 0x51 (put 0xA2 for the register dues to7bit address)
    9.         I2C->OARH = 0x40;                              // Set 7bit address mode
    10.  
    11.     #endif
    12.     #ifdef I2C_slave_10Bits_Address
    13.       /* Set I2C registers for 10Bits Address */
    14.       I2C->CR1 |= 0x01;                  // Enable I2C peripheral
    15.       I2C->CR2 = 0x04;                    // Enable I2C acknowledgement
    16.       I2C->FREQR = 16;                     // Set I2C Freq value (16MHz)
    17.       I2C->OARL = (SLAVE_ADDRESS & 0xFF) ;                            // set slave address LSB
    18.       I2C->OARH = 0xC0 | ((SLAVE_ADDRESS & 0x300) >> 7);    // Set 10bits address mode and address MSB
    19.     #endif
    20.                // Enable I2C peripheral
    21.     I2C->ITR    = 0x07;                          // all I2C interrupt enable
    22. }
    23.  
    and interrupt function
    Code (Text):
    1. #ifdef _RAISONANCE_
    2. void I2C_Slave_check_event(void) interrupt 19 {
    3. #endif
    4. #ifdef _COSMIC_
    5. @far @interrupt void I2C_Slave_check_event(void) {
    6. #endif
    7.  
    8.  
    9. // save the I2C registers configuration
    10.  
    11. sr1 = I2C->SR1;
    12. sr2 = I2C->SR2;
    13. sr3 = I2C->SR3;
    14.  
    15.  
    16. //GPIO_WriteLow(GPIOD,GPIO_PIN_2);
    17. /* Communication error? */
    18.  
    19.  
    20.   if (sr2 & (I2C_SR2_WUFH | I2C_SR2_OVR |I2C_SR2_ARLO |I2C_SR2_BERR))
    21.   {    
    22.     I2C->CR2|= I2C_CR2_STOP;  // stop communication - release the lines
    23.     I2C->SR2= 0;                       // clear all error flags
    24.     }
    25. /* More bytes received ? */
    26.   if ((sr1 & (I2C_SR1_RXNE | I2C_SR1_BTF)) == (I2C_SR1_RXNE | I2C_SR1_BTF))
    27.   {
    28.     I2C_byte_received(I2C->DR);
    29.   I2C->CR2 |= I2C_CR2_ACK;
    30.     }
    31. /* Byte received ? */
    32.   if (sr1 & I2C_SR1_RXNE)
    33.   {
    34.     I2C_byte_received(I2C->DR);
    35. I2C->CR2 |= I2C_CR2_ACK;
    36. }
    37. /* NAK? (=end of slave transmit comm) */
    38.   if (sr2 & I2C_SR2_AF)
    39.   {
    40.     I2C->SR2 &= ~I2C_SR2_AF;     // clear AF
    41.         I2C_transaction_end();
    42.     }
    43. /* Stop bit from Master  (= end of slave receive comm) */
    44.   if (sr1 & I2C_SR1_STOPF)
    45.   {
    46.     I2C->CR2 |= I2C_CR2_ACK;     // CR2 write to clear STOPF
    47.         I2C_transaction_end();
    48.     }
    49. /* Slave address matched (= Start Comm) */
    50.   if (sr1 & I2C_SR1_ADDR)
    51.   {  
    52.     // I2C_transaction_begin();
    53.         //I2C->CR2 |= I2C_CR2_ACK;
    54.     }
    55. /* More bytes to transmit ? */
    56.   if ((sr1 & (I2C_SR1_TXE | I2C_SR1_BTF)) == ( I2C_SR1_TXE | I2C_SR1_BTF ))
    57.   {
    58.         I2C->DR = I2C_byte_write();
    59.   }
    60. /* Byte to transmit ? */
    61.   if (sr1 & I2C_SR1_TXE)
    62.   {
    63.         I2C->DR = I2C_byte_write();
    64.   }
    65.  
    66. sr1 = I2C->SR1;
    67. sr2 = I2C->SR2;
    68. sr3 = I2C->SR3;
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Since the camera sensor is the master you should start there and see what format it is sending the data.
     
  3. Anup Sharma

    Thread Starter New Member

    May 21, 2015
    2
    0
    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.
     
Loading...