STM8S003F3 I2c master-slave problem

Discussion in 'Embedded Systems and Microcontrollers' started by aamirali, Oct 30, 2014.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    I have two STM8 boards. One is master & other is slave.
    What master does is: send 4 bytes to slave & then read 4 bytes from slave.

    Problem: Sometimes the cycle work & many times code is stuck. I don't know what exactly is the problem.


    Master code:

    Code (Text):
    1.  
    2. #define FAST_I2C_MODE
    3. #ifdef FAST_I2C_MODE
    4. #define I2C_SPEED 300000
    5. #else
    6. #define I2C_SPEED 100000
    7. #endif
    8. /* definition of 10-bit or default 7-bit slave address */
    9. /* #define TEN_BITS_ADDRESS */
    10. #define OWN_ADDRESS    0xa0    
    11. #define SLAVE_ADD_1    0x30                                            
    12.                                                  
    13. /* This define is used in master receiver */
    14. /* Uncomment the line below if you want to use the safe procedure */
    15. #define SAFE_PROCEDURE
    16. #define BUFFERSIZE  4
    17. volatile uint8_t TxBuffer[BUFFERSIZE];
    18. volatile uint8_t RxBuffer[BUFFERSIZE];
    19. volatile uint8_t NumOfBytes;
    20. volatile uint8_t NumByteToRead;
    21. volatile uint8_t slave_add;
    22. volatile uint8_t Rx_Idx;
    23. volatile uint8_t Tx_Idx;
    24. int main(void)
    25. {
    26.     uint8_t test_i2c[4] = {0xaaU , 0x55U , 0xf0U , 0x0fU};
    27. /* init sys clock to internal 16Mhz HSI & peripheral clock divider = 1 */
    28.     set_system_clock();
    29.     I2C_DeInit();
    30.  
    31. /* I2C Initialize */
    32.     I2C_Init(I2C_SPEED, OWN_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);
    33.    
    34.     enableInterrupts();
    35.    
    36.     i2c_send_receive_4_bytes(SLAVE_ADD_1 , test_i2c);
    37.    
    38. }
    39. void i2c_send_receive_4_bytes(uint8_t temp_slave_add , uint8_t tx_data[])
    40. {
    41.     uint8_t cnt;
    42.  
    43. /* set vars */
    44.     Tx_Idx = 0U;
    45.     Rx_Idx = 0U;
    46.     NumOfBytes = BUFFERSIZE;
    47.     NumByteToRead = BUFFERSIZE;
    48.    
    49. /* fill vars */
    50.     slave_add = temp_slave_add;
    51.     for(cnt = 0U ; cnt < BUFFERSIZE ; cnt++)
    52.     {
    53.         TxBuffer[cnt] = tx_data[cnt];
    54.     }
    55.      
    56. /* Enable Buffer and Event Interrupt*/
    57.     I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);      
    58.    
    59. /* Send START condition */
    60.     I2C_GenerateSTART(ENABLE);  
    61.     while(NumOfBytes);
    62.     while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
    63.    
    64. /* Add a delay to be sure that communication is finished */
    65.     Delay(0xffffU);  
    66.    
    67. /*****  reception phase ***/
    68. /*  Wait while the bus is busy */
    69.     while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
    70.    
    71. /* Send START condition */
    72.     I2C_GenerateSTART(ENABLE);  
    73.    
    74. /* Test on EV5 and clear it */
    75.     while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
    76.    
    77. /* Send slave Address for write */
    78.     I2C_Send7bitAddress(slave_add, I2C_DIRECTION_RX);
    79. /* Test on EV6 and clear it */
    80.     while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));  
    81.    
    82. /* While there is data to be read */
    83.     while(NumByteToRead)
    84.     {
    85. #ifdef SAFE_PROCEDURE
    86.         if(3U != NumByteToRead)   /* Receive bytes from first byte until byte N-3 */
    87.         {
    88.             while((RESET == I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED)));        /* Poll on BTF */
    89.         /* Read a byte from the Slave */
    90.             RxBuffer[Rx_Idx] = I2C_ReceiveData();
    91.         /* Point to the next location where the byte read will be saved */
    92.             Rx_Idx++;
    93.         /* Decrement the read bytes counter */
    94.             NumByteToRead--;
    95.         }
    96.         if(3U == NumByteToRead)    /* it remains to read three data: data N-2, data N-1, Data N */
    97.         {
    98.         /* Data N-2 in DR and data N -1 in shift register */
    99.             while((RESET == I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED)));    /* Poll on BTF */
    100.         /* Clear ACK */
    101.             I2C_AcknowledgeConfig(I2C_ACK_NONE);
    102.         /* Disable general interrupts */
    103.             disableInterrupts();
    104.         /* Read Data N-2 */
    105.             RxBuffer[Rx_Idx] = I2C_ReceiveData();
    106.         /* Point to the next location where the byte read will be saved */
    107.             Rx_Idx++;
    108.         /* Program the STOP */
    109.             I2C_GenerateSTOP(ENABLE);
    110.         /* Read DataN-1 */
    111.             RxBuffer[Rx_Idx] = I2C_ReceiveData();
    112.         /* Enable General interrupts */
    113.             enableInterrupts();
    114.         /* Point to the next location where the byte read will be saved */
    115.             Rx_Idx++;
    116.             while((RESET == I2C_GetFlagStatus(I2C_FLAG_RXNOTEMPTY))); /* Poll on RxNE */
    117.         /* Read DataN */
    118.             RxBuffer[Rx_Idx] = I2C_ReceiveData();
    119.         /* Reset the number of bytes to be read by master */
    120.             NumByteToRead = 0U;
    121.         }
    122. #else
    123.         if(1U == NumByteToRead)
    124.         {
    125.         /* Disable Acknowledgement */
    126.             I2C_AcknowledgeConfig(I2C_ACK_NONE);
    127.         /* Send STOP Condition */
    128.             I2C_GenerateSTOP(ENABLE);
    129.         /* Poll on RxNE Flag */
    130.             while((RESET == I2C_GetFlagStatus(I2C_FLAG_RXNOTEMPTY)));
    131.      
    132.         /* Read a byte from the Slave */
    133.             RxBuffer[Rx_Idx] = I2C_ReceiveData();
    134.         /* Point to the next location where the byte read will be saved */
    135.             Rx_Idx++;
    136.         /* Decrement the read bytes counter */
    137.             NumByteToRead--;
    138.         }
    139.         /* Test on EV7 and clear it */
    140.         if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED) )
    141.         {
    142.         /* Read a byte from the EEPROM */
    143.             RxBuffer[Rx_Idx] = I2C_ReceiveData();
    144.         /* Point to the next location where the byte read will be saved */
    145.             Rx_Idx++;
    146.         /* Decrement the read bytes counter */
    147.             NumByteToRead--;
    148.         }
    149. #endif /* SAFE_PROCEDURE */
    150.     }  
    151.    
    152. /* Add a delay to be sure that communication is finished */
    153.     Delay(0xffffU);    
    154. }
    155. INTERRUPT_HANDLER(I2C_IRQHandler, 19)
    156. {
    157.     switch(I2C_GetLastEvent())
    158.     {
    159.     /* EV5 */
    160.         case I2C_EVENT_MASTER_MODE_SELECT :
    161.         /* Send slave Address for write */
    162.             I2C_Send7bitAddress(slave_add, I2C_DIRECTION_TX);        
    163.         break;
    164.        
    165.        
    166.     /* EV6 */
    167.         case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:
    168.             if(0U != NumOfBytes)
    169.             {
    170.             /* Send the first Data */
    171.                 I2C_SendData(TxBuffer[Tx_Idx++]);
    172.             /* Decrement number of bytes */
    173.                 NumOfBytes--;
    174.             }
    175.             if(0U == NumOfBytes)
    176.             {
    177.                 I2C_ITConfig(I2C_IT_BUF, DISABLE);
    178.             }
    179.         break;    
    180.        
    181.        
    182.     /* EV8 */
    183.         case I2C_EVENT_MASTER_BYTE_TRANSMITTING:
    184.         /* Transmit Data */
    185.             I2C_SendData(TxBuffer[Tx_Idx++]);
    186.         /* Decrement number of bytes */
    187.             NumOfBytes--;
    188.             if(0U == NumOfBytes)
    189.             {
    190.                 I2C_ITConfig(I2C_IT_BUF, DISABLE);
    191.             }
    192.         break;
    193.        
    194.     /* EV8_2 */
    195.         case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
    196.         /* Send STOP condition */
    197.             I2C_GenerateSTOP(ENABLE);
    198.             I2C_ITConfig(I2C_IT_EVT, DISABLE);
    199.         break;
    200.      
    201.         default:
    202.         break;      
    203.        
    204.     }
    205. }



    Slave code:
    Code (Text):
    1. #define FAST_I2C_MODE
    2. #ifdef FAST_I2C_MODE
    3. #define I2C_SPEED 300000
    4. #else
    5. #define I2C_SPEED 100000
    6. #endif
    7. /* definition of 10-bit or default 7-bit slave address */
    8. /* #define TEN_BITS_ADDRESS */
    9. #define OWN_ADDRESS    0xa0    
    10. #define SLAVE_ADD_1    0x30
    11. #define SLAVE_ADD_2    0x31
    12. #define SLAVE_ADD_3    0x32
    13. #define SLAVE_ADD_4    0x33                                              
    14.                                                  
    15. /* This define is used in master receiver */
    16. /* Uncomment the line below if you want to use the safe procedure */
    17. #define SAFE_PROCEDURE
    18. #define BUFFERSIZE  4
    19. volatile uint8_t TxBuffer[BUFFERSIZE];
    20. volatile uint8_t RxBuffer[BUFFERSIZE];
    21. volatile uint8_t Rx_Idx;
    22. volatile uint8_t Tx_Idx;
    23. volatile uint16_t Event;
    24. volatile uint8_t comm_complete;
    25. int main(void)
    26. {
    27.     uint8_t test_i2c[4] = {0xaaU , 0x55U , 0xf0U , 0x0fU};
    28.     uint8_t test_rx[4];
    29. /* init sys clock to internal 16Mhz HSI & peripheral clock divider = 1 */
    30.     set_system_clock();
    31.     I2C_DeInit();
    32.  
    33. /* I2C Initialize */
    34.     I2C_Init(I2C_SPEED, SLAVE_ADD_1, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, MHZ_SYS_FREQ);
    35. /* Enable Error Interrupt*/
    36.     I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);  
    37.    
    38.     enableInterrupts();
    39.    
    40.     i2c_send_receive_4_bytes(test_i2c , test_rx);
    41.    
    42. }
    43. void i2c_send_receive_4_bytes(uint8_t tx_data[] , uint8_t rx_data[])
    44. {
    45.     uint8_t cnt;
    46.  
    47. /* fill vars */
    48.     for(cnt = 0U ; cnt < BUFFERSIZE ; cnt++)
    49.     {
    50.         TxBuffer[cnt] = tx_data[cnt];
    51.     }
    52.     comm_complete = 0U;
    53.    
    54.    
    55. /* wait while comm complete */
    56.     while(0U == comm_complete);
    57.    
    58. /* disable interrupt */
    59.     disableInterrupts();
    60.     comm_complete = 0U;
    61.    
    62. /* fill the rx arr */  
    63.     for(cnt = 0U ; cnt < BUFFERSIZE ; cnt++)
    64.     {
    65.         rx_data[cnt] = RxBuffer[cnt];
    66.     }  
    67.    
    68. /* Enable general interrupts */
    69.     enableInterrupts();  
    70.    
    71. } /* function ends here */
    72. INTERRUPT_HANDLER(I2C_IRQHandler, 19)
    73. {
    74. /* Read SR2 register to get I2C error */
    75.     if(0U != (I2C->SR2))
    76.     {
    77.     /* Clears SR2 register */
    78.         I2C->SR2 = 0U;
    79.     }
    80.  
    81.     Event = I2C_GetLastEvent();
    82.     switch(Event)
    83.     {
    84.     /******* Slave transmitter ******/
    85.     /* check on EV1 */
    86.         case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
    87.             Tx_Idx = 0U;
    88.         break;
    89.      
    90.     /* check on EV3 */
    91.         case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
    92.         /* Transmit data */
    93.             I2C_SendData(TxBuffer[Tx_Idx++]);
    94.            
    95.             if(BUFFERSIZE == Tx_Idx)
    96.             {
    97.                 comm_complete = 1U;
    98.             }
    99.            
    100.         break;
    101.        
    102.        
    103.        
    104.        
    105.     /******* Slave receiver **********/
    106.     /* check on EV1*/
    107.         case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
    108.             Rx_Idx = 0U;
    109.         break;
    110.      
    111.     /* Check on EV2*/
    112.         case I2C_EVENT_SLAVE_BYTE_RECEIVED:
    113.             RxBuffer[Rx_Idx++] = I2C_ReceiveData();
    114.         break;
    115.        
    116.     /* Check on EV4 */
    117.         case (I2C_EVENT_SLAVE_STOP_DETECTED):
    118.         /* write to CR2 to clear STOPF flag */
    119.             I2C->CR2 |= I2C_CR2_ACK;
    120.         break;
    121.         default:
    122.         break;
    123.        
    124.     }
    125. }
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    We would need a better description then simply saying the code "is stuck." What sticks? The master or slave?

    What is the bus condition when stuck? If the "stick"is due to the I2C the bus will show the error: are both SCL and SDA hi low or what? If one or the other is low does it clear if you reset (MCLR) either or both devices?

    That much should show you which chip is sticking.

    When you know that you can put that chip on an in circuit debugger (such as a PICkit) and code walk to see what is happening when the stick ocures.

    (I would suspect the slave is the problem as slave code is the harder side to write.)
     
  3. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    There is no perfect sequence.
    Some everyting works fine.

    Sometimes master don't get out of:

    Code (Text):
    1. while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
    I added this line before sending start condition.



    Edit: Anyone have I2c code of slave by bit banging in C.
    I have written for master
     
    Last edited: Nov 1, 2014
Loading...