This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2538 I2C Issue

Other Parts Discussed in Thread: CC2538

Hello TI,

I am working on I2C protocol for TI CC2538 with EEPROM ( OxA0). I have made I2C write function and I2C read function. I am not able to perform the I2C communication with EEPROM.

Kindly have a look at the below code and let me know what could be wrong.

//***************************************************************************/


#include <stdbool.h>
#include <stdint.h>
#include "hw_ints.h"
#include "hw_memmap.h"
#include "gpio.h"
#include "interrupt.h"
#include "ioc.h"
#include "hw_ioc.h"
#include "sys_ctrl.h"
#include "hw_i2cm.h"
#include "hw_i2cs.h"
#include "i2c.h"
#include "uartstdio.h"

#include "bsp.h"

#define EXAMPLE_PIN_UART_RXD            GPIO_PIN_0
#define EXAMPLE_PIN_UART_TXD            GPIO_PIN_1
#define EXAMPLE_GPIO_UART_BASE          GPIO_A_BASE


#define EXAMPLE_PIN_I2C_SCL             GPIO_PIN_3
#define EXAMPLE_PIN_I2C_SDA             GPIO_PIN_4
#define EXAMPLE_GPIO_I2C_BASE           GPIO_D_BASE


#define SLAVE_ADDRESS 0xA0

//uint8_t rtc_arr_val[]={ 0,0,0,

void
InitConsole(void)
{
    //
    // Map UART signals to the correct GPIO pins and configure them as
    // hardware controlled.
    //
    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD,
                             IOC_MUX_OUT_SEL_UART0_TXD);
    GPIOPinTypeUARTOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD);
    
    IOCPinConfigPeriphInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD,
                            IOC_UARTRXD_UART0);
    GPIOPinTypeUARTInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD);
     
    //
    // Initialize the UART (UART0) for console I/O.
    //
    UARTStdioInit(0);
}

//*****************************************************************************
//
// Configure the I2C0 master and slave and connect them using loopback mode.
//
//*****************************************************************************
int
main(void)
{

    SysCtrlClockSet(false, false, SYS_CTRL_SYSDIV_16MHZ); //! 1. The clock is set for system I2C block

    SysCtrlIOClockSet(SYS_CTRL_SYSDIV_16MHZ);           //! 1. The clock is set for I2C bus
    
    bspLedInit();
    
    //! 2. The GPIO is being set for I2C bus communication
    SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_I2C);    
    SysCtrlPeripheralReset(SYS_CTRL_PERIPH_I2C);
    GPIOPinTypeI2C(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL);
    GPIOPinTypeI2C(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA);
    IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL,
                            IOC_I2CMSSCL);
    IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA,
                            IOC_I2CMSSDA);    
    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL,
                             IOC_MUX_OUT_SEL_I2C_CMSSCL);
    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA,
                            IOC_MUX_OUT_SEL_I2C_CMSSDA);          
       
    //! The UART is initailize  
    InitConsole();    
    UARTprintf("\n Tranferring from : Master -> Slave \n");
    
    EERPROM_write();
    EEPROM_read();
    
}


void EERPROM_write()
{    
    uint32_t RTC_Int_Flag,Seconds;  
     
    UARTprintf("------------- RTC TIME SET -------------- \n");    
    while(1){
    I2CMasterInitExpClk(SysCtrlClockGet(), false); //! 4. Set the SCL clock frequency in I2CM_TPR     
    I2CMasterEnable();                                //! 3. Enable the Master mode in I2C Module                
 
    I2CMasterSlaveAddrSet(SLAVE_ADDRESS, false);  // 6. Write Slave address  // Address written in I2CM_SA      
 
    
    I2CMasterDataPut(0);     //! RTC Second address
    I2CMasterControl(I2C_MASTER_CMD_BURST_SEND_START);
    while(!(I2CMasterIntStatus(false)));
    I2CMasterIntClear(); //! Clear Master interrupt source.
     while(I2CMasterBusy())                          //! 8. BUSY bit of the I2CM_STAT -- I2CM_STAT = 0x4E -> Error!
     {
     }
     while(I2CMasterErr())                            // ! 9. Check the ERROR bit in the I2CM_STAT
     {
     }
 
       
 /**************************************************/     
    I2CMasterDataPut(0);
    I2CMasterControl(I2C_MASTER_CMD_BURST_SEND_START);
    while(!(I2CMasterIntStatus(false)));
    I2CMasterIntClear(); //! Clear Master interrupt source.
    while(I2CMasterBusy())                          //! 8. BUSY bit of the I2CM_STAT -- I2CM_STAT = 0x4E -> Error!
     {
     }
     while(I2CMasterErr())                            // ! 9. Check the ERROR bit in the I2CM_STAT
     {
     }     
  /*************************************************/
     I2CMasterDataPut(2);
    I2CMasterControl(I2C_MASTER_CMD_BURST_SEND_START);
    while(!(I2CMasterIntStatus(false)));
    I2CMasterIntClear(); //! Clear Master interrupt source.
    while(I2CMasterBusy())                          //! 8. BUSY bit of the I2CM_STAT -- I2CM_STAT = 0x4E -> Error!
     {
     }
     while(I2CMasterErr())                            // ! 9. Check the ERROR bit in the I2CM_STAT
     {
     }    
    }}


uint32_t EEPROM_read()
{
 
  uint32_t RTC_Int_Flag,Seconds;  
     
    UARTprintf("\n ------------- RTC TIME READ -------------- \n");    
    I2CMasterInitExpClk(SysCtrlClockGet(), false); //! 4. Set the SCL clock frequency in I2CM_TPR     
    I2CMasterEnable();                                //! 3. Enable the Master mode in I2C Module                
 
    I2CMasterSlaveAddrSet(SLAVE_ADDRESS, false);  // 6. Write Slave address  // Address written in I2CM_SA      
 

     I2CMasterDataPut(0);
    I2CMasterControl(I2C_MASTER_CMD_SINGLE_SEND);
    while(!(I2CMasterIntStatus(false)));
    I2CMasterIntClear(); //! Clear Master interrupt source.
    while(I2CMasterBusy())                          //! 8. BUSY bit of the I2CM_STAT -- I2CM_STAT = 0x4E -> Error!
     {
     }
     while(I2CMasterErr())                            // ! 9. Check the ERROR bit in the I2CM_STAT
     {
     }      
      I2CMasterSlaveAddrSet(SLAVE_ADDRESS, true);  
    I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE);
    while(!(I2CMasterIntStatus(false)));
    I2CMasterIntClear(); //! Clear Master interrupt source.
      
     while(I2CMasterBusy())                          //! 8. BUSY bit of the I2CM_STAT -- I2CM_STAT = 0x4E -> Error!
     {
     }
     while(I2CMasterErr())                            // ! 9. Check the ERROR bit in the I2CM_STAT
     {
     }
     RTC_Int_Flag=I2CMasterDataGet();     //! RTC Second address
     UARTprintf("Recieved data : %d ",RTC_Int_Flag);
       
 
                
 
}
    
void callbackFunc()
{
 
  while(!(I2CMasterIntStatus(false)));
   I2CMasterIntClear(); //! Clear Master interrupt source.
 
}  


  • Hi Gaurav, 

    I would recommend using an Oscilloscope to check first if the signals on the transmit operation are correct and then you can check if receive signals are as expected or not. Also, as mentioned in the reference I2C loopback example are you using the external pull ups?

    // NOTE: For external I2C operation you will need to use external pullups
    // that are stronger than the internal pullups. Refer to the datasheet for
    // more information.

    Regards

  • Hi Suyash,

    I checked the I2C bus on the oscilloscope and write function is working properly, as per I2C specification. But read function is not working.Perhaps, I am not able to generate the START condition for the read function .

    Please have a look once.

    With Regards

    Gaurav 

  • I have same issue on CC2538.
    1. IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL,
    IOC_I2CMSSCL);

    This call may be caused while(I2CMasterBusy()){} stick when write or read i2c,reference loopback i2c example.

    2.The i2c write may be not work correct.