Tool/software: Code Composer Studio
Hi , So I am trying to have a I2C communication between two Piccolo TMS320F28035 I2C communication. I am stuck with Slave side, and the document doesn't explain much. I would appreciate some help.I am pretty sure I am doing some thing wrong but don't know what.
P.S. : I am not using interrupts like polling the BB, XRDY, RRDY or NACK bits for now, just to keep it simple.
Following is my code:
########## MASTER CODE (Only Write)#####################################
#define I2C_SLAVE_ADDR 0x0055
void main(void)
{
InitSysCtrl();
InitI2CGpio();\
bool flag = true;
I2CA_Init_master();
for(;;)
{
while(GpioDataRegs.GPBDAT.bit.GPIO44 == 0 && flag )
{
GREENLED_ON ;
I2CA_WriteData();
GREENLED_OFF ;
flag = false;
//DELAY_US(1000000);
}
}
void I2CA_Init_master(void)
{
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; // Slave address -
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0;
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
DELAY_US(10);
return;
}
void I2CA_WriteData(void){
Uint16 i;
// while (I2caRegs.I2CMDR.bit.STP == 1)
// {
// };
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //slave address register
I2caRegs.I2CCNT = I2C_NUMBYTES ; //data count is 4 bytes
I2caRegs.I2CFFTX.all = 0x6000; //enable FIFO mode and reset TX FIFO
I2caRegs.I2CDXR = 0xf0;
I2caRegs.I2CMDR.bit.TRX = 1;
I2caRegs.I2CMDR.bit.MST = 1;
I2caRegs.I2CMDR.bit.FREE = 1;
I2caRegs.I2CMDR.bit.STP = 1; //creating a stop condition will free the BUS thus turning the BB == 0.
I2caRegs.I2CMDR.bit.STT = 1; // As soon as you start the Start Bit the BB == 1 becomes busy again.
return ;
}
###################################### SLAVE CODE(Only read) #################################
void main(void){
InitSysCtrl();
InitI2CGpio();
I2CASlave_init();
DELAY_US(1000000);
for(;;)
{
I2CA_ReceiveData();
}
}
void I2CASlave_init(void)
{
I2caRegs.I2COAR = I2C_SLAVE_ADDR; // Slave address
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x00; // Enable SCD, XRDY, RRDY, ARDY, NACK interrupts...sorry don't enable anything
I2caRegs.I2CMDR.bit.MST = 0; // Enter the slave mode
I2caRegs.I2CMDR.bit.TRX = 0; //receive mode
I2caRegs.I2CMDR.bit.FREE = 1 ; // Runs in free mode setting it to zero will stop when data is received.l
I2caRegs.I2CMDR.all = 0x0020 ; // enable the IRS
// I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
// I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
return;
}
void I2CA_ReceiveData(void)
{
I2caRegs.I2COAR = I2C_SLAVE_ADDR; // Slave address - EEPROM control code
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //slave address register
I2caRegs.I2CCNT = 1 ; //Receive count to 4 bytes
//while(I2caRegs.I2CSTR.bit.RRDY == 0){} ;
Receive_Data[0] = I2caRegs.I2CDRR ;
return;
}