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.

TM4C 1292 I2C SWITCH iNTERFACE

Hello,
I am using TM4C1292 I2C to communicate with Ethernet Switch(Read and write Switch registers).Right now i am unable to read and write the registers(switch data sheet tells that accessing the registers is similar to accessing EEPROM AT24C02).
(I went through peripheral examples  of I2Cand forum and i made the code)
I will be happy if any body can validate my code.
//*****************************************************************************
#define SLAVE_ADDRESS_READ 0xBF
#define SLAVE_ADDRESS_WRITE 0xBE
#define NUM_I2C_DATA 3
unsigned short *sram =NULL;
uint32_t ui32Index;
uint16_t device_register = 0;
uint16_t data;
uint8_t data_low = 0, data_high = 0;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//------------------------------------------//
void Delay( unsigned long count)
{
//udelay(count);
while ( --count );
}
//------------------------------------------//
uint8_t readI2C8(uint16_t device_address, uint16_t device_register)
{
//specify that we want to communicate to device address with an intended write to bus
I2CMasterSlaveAddrSet(I2C8_BASE, device_address, false);
//the register to be read
I2CMasterDataPut(I2C8_BASE, device_register);
SysCtlDelay(50);
//send control byte and register address byte to slave device
I2CMasterControl(I2C8_BASE, I2C_MASTER_CMD_SINGLE_SEND);
SysCtlDelay(50);
//wait for MCU to complete send transaction
while(!(I2CMasterBusy(I2C8_BASE)));
while ((I2CMasterBusy(I2C8_BASE))); //Wait till end of transaction
SysCtlDelay(50);
//while (I2CMasterBusy(I2C0_BASE)); //Wait till end of transaction
//read from the specified slave device
I2CMasterSlaveAddrSet(I2C8_BASE, device_address, true);
SysCtlDelay(50);
//send control byte and read from the register from the MCU
//I2CMasterControl(I2C8_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
I2CMasterControl (I2C6_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); //Receive
SysCtlDelay(50);
//wait while checking for MCU to complete the transaction
while(!(I2CMasterBusy(I2C8_BASE)));
while ((I2CMasterBusy(I2C8_BASE))); //Wait till end of transaction
SysCtlDelay(50);
//while (I2CMasterBusy(I2C0_BASE)); //Wait till end of transaction
//Get the data from the MCU register and return to caller
data_high = I2CMasterDataGet(I2C8_BASE);
SysCtlDelay(50);
I2CMasterControl (I2C8_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); //Receive
//while(!(I2CMasterBusy(I2C8_BASE)));
while (I2CMasterBusy(I2C8_BASE)); //Wait till end of transaction
SysCtlDelay(50);
data_low = I2CMasterDataGet (I2C8_BASE); //Read from FIFO
SysCtlDelay(50);
data = (data_high << 8) | data_low;
return( I2CMasterDataGet(I2C8_BASE));
}
void writeI2C8(uint16_t device_address, uint16_t device_register, uint8_t device_data)
{
//specify that we want to communicate to device address with an intended write to bus
I2CMasterSlaveAddrSet(I2C8_BASE, device_address, false);
//register to be read
I2CMasterDataPut(I2C8_BASE, device_register);
//send control byte and register address byte to slave device
I2CMasterControl(I2C8_BASE, I2C_MASTER_CMD_BURST_SEND_START);
//wait for MCU to finish transaction
while(I2CMasterBusy(I2C8_BASE));
I2CMasterSlaveAddrSet(I2C8_BASE, device_address, true);
//specify data to be written to the above mentioned device_register
I2CMasterDataPut(I2C8_BASE, device_data);
//wait while checking for MCU to complete the transaction
I2CMasterControl(I2C8_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
//wait for MCU & device to complete transaction
while(I2CMasterBusy(I2C8_BASE));
}
//
//
//*****************************************************************************
int
main(void)
{
    uint32_t ui32SysClock;
    uint32_t pui32DataRx[NUM_I2C_DATA];
    sram =(unsigned short *)0x20000500;
unsigned long Data;
unsigned char readData;
unsigned char Reg;
    //
    // Run from the PLL at 120 MHz.
    //
ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000);
//
// The I2C0 peripheral must be enabled before use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C8);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
//
// Configure the pin muxing for I2C0 functions on port D2 and D3.
// This step is not necessary if your part does not support pin muxing.
GPIOPinConfigure(GPIO_PD2_I2C8SCL);
GPIOPinConfigure(GPIO_PD3_I2C8SDA);
// Select the I2C function for these pins.  This function will also
GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_3);
//
// Enable and initialize the I2C0 master module.  Use the system clock for
// the I2C0 module.  The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps.  For this example we will use a data rate of 100kbps.
//
I2CMasterInitExpClk(I2C8_BASE, SysCtlClockGet(), false);
SysCtlDelay(50);
// Enable interrupts to the processor.
//
IntMasterEnable();
readI2C8(0xBF, 0x00);
readI2C8(0xBF, 0x01);
readI2C8(0xBF, 0x02);
while (1) {
}
}
Thanks in advance.
Regards,
Krishnan
  • Hello Krishnan

    First of all SysCtlClockGet is not a valid function to be used on the TM4C129 platform

    Second of all if the delay loop is being used then the second line of waiting for the ! I2CMasterBusy is not required.

    SysCtlDelay(50);
    //wait for MCU to complete send transaction
    while(!(I2CMasterBusy(I2C8_BASE)));

    Regards
    Amit
  • Hello,

    Thank you for the replay.

    1->SysCtlClockGet , is now replaced properly

    2->while(!(I2CMasterBusy(I2C8_BASE))); is removed .

    Two more changes i made according to data sheet

    1->Open Drain Configuration for data pin (GPIOPinTypeGPIOOutputOD(GPIO_PORTD_BASE, GPIO_PIN_3);//Open drain)

    2->I2C8_MTPR_R = 0x24;This register is configured as switch required i2c-slave frequency is around 2.5mhz.(yet to probe and verify)

    3->I2C8_MTPR_R  register configuration i have a doubt as i am using (I2CMasterInitExpClk(I2C8_BASE, ui32SysClock, false); will this configuration helps me to generate SCL Clock to 2.5 mhz.Any other configurations require..............

    4->In continuous run code is going to FaultISR,But while using braekpoints its not occuring....How to achieve this stability..

    Thank you in advance and for past suggestions.

    Regards,

    Krishnan

  • Hello Amit,

    I have one more doubt

    According to datasheet

    SCL_PERIOD = 2 × (1 + TIMER_PRD) × (SCL_LP + SCL_HP) × CLK_PRD

    For example:  in my case 120MHZ SYSTEM CLOCK  SO

    CLK_PRD = 8.33 ns,systemclock is 120mhz

    TIMER_PRD = 1,default value

    SCL_LP=6,constant

    SCL_HP=constant

    SCL frequency =1/SCL_PERIOD

    SCL_PERIOD  =2 × (1 + 1) × (10) × 8.33 =333.2

    SCL frequency  =3MHZ

    But while probing we are getting 77.7 khz.

    My configuration is

    ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C8);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

    GPIOPinConfigure(GPIO_PD2_I2C8SCL);

    GPIOPinConfigure(GPIO_PD3_I2C8SDA);

    // Select the I2C function for these pins.  This function will also

    GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_2);

    GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_3);

    GPIOPinTypeGPIOOutputOD(GPIO_PORTD_BASE, GPIO_PIN_3);//Open drain

    //

    I2CMasterInitExpClk(I2C8_BASE, ui32SysClock, false);

    ui32SysClock is correctly returning 120mhz.

    Where i am going wrong.

    Regards,

    Krishnan

  • Hello Krishnan

    1. The Open Drain configuration is handled by the GPIO Function call for I2C
    2. The SCL of 2.5MHz requires very strong pull up on the bus. Be sure to check the pull up strength. Also note that the use of SysCtlClockGet may have affected the register. It would be better to reset the peripheral before initializing.
    3. Check the source of the Fault

    Regards
    Amit
  • Hello Krishnan

    Just configuring the MTPR for a frequency does not mean that it will run at the frequency. Do go through the I2C Application Note to understand how the I2C SCL is dependent on the pull up resistor

    Regards
    Amit