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.

C2000 I2C - Help

Other Parts Discussed in Thread: CONTROLSUITE

I am trying to read values from Magnetic Encoder using the IC2. So far no luck. Take a look at my code let me know if you see where I have gone wrong. Thanks

#include "DSP28x_Project.h" // Device Headerfile and Examples Include File

///////// Init for I2C comm. /////////////
void I2CA_Init(void);
void InitI2CGpio(void);
void I2CA_WriteData(void);

int32 Result;

#define I2C_SLAVE_ADDR 0xFE

#define I2C_NUMBYTES 2

Uint32 write_data[2];
Uint16 Count = 0;


void main(void)
{
InitI2CGpio();  // Init GPIO

I2CA_Init();      // Init I2C


for(;;)
{
I2CA_WriteData();
Count++;
}

}

/////////////////// I2C INIT ///////////////////////////

void I2CA_Init(void)
{
I2caRegs.I2CMDR.all = 0x0000;     // put I2C module in reset mode
I2caRegs.I2CPSC.all = 9;                 // prescaler
I2caRegs.I2CCLKL = 10;                  // clock-low period, MUST BE NON-ZERO
I2caRegs.I2CCLKH = 5;                    // clock-high period, MUST BE NON-ZERO
I2caRegs.I2CMDR.all = 0x0020;      // take I2C out of reset, stop I2C when emulation is suspended
I2caRegs.I2CFFTX.all = 0x6000;      // enable FIFO mode and TX_FIFO
I2caRegs.I2CFFRX.all = 0x2040;    // enable RX_FIFO, clear RXFFINT,

return;
}


/////////////////// GPIO INIT ////////////////////////

void InitI2CGpio(void)

{
EALLOW;

GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // Configure GPIO32 for SDAA operation
GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // Configure GPIO33 for SCLA operation

GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA)
GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO33 (SCLA)

GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA)
GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO33 (SCLA)

EDIS;
}

/////////////////////// Read/Write I2C //////////

void I2CA_WriteData(void)

{
unsigned int i; // counter

while(I2caRegs.I2CMDR.bit.STP == 1) {} // wait until the STP bit is cleared from any previous master communication
while(I2caRegs.I2CSTR.bit.BB == 1) {} // wait until bus not busy

I2caRegs.I2CSAR = I2C_SLAVE_ADDR; // slave address register set
I2caRegs.I2CCNT = I2C_NUMBYTES ; // setup number of bytes to send

for(i = 0; i < 2; i++) // setup data to send, # of bytes equals num_bytes
{
I2caRegs.I2CDXR = (unsigned int)(write_data[i]); // write data into Data Transmit FIFO
}
Result = ((unsigned int)(write_data[0] << 8 | write_data[1]));
return;           // end of function, return value is void

}  

  • Hi kenton,

    It looks like you are trying to write data to the encoder using the FIFO. I dont see where you have configured I2CMDR as Master transmitter and set the STT bit. Is this snippet the entirety of your code? You can take a look at the example I2C code in controlsuite and modify it to work with the encoder. You would have to change the slave address and modify the size of the message buffers to suit your needs