Part Number: TMS320F28027
I'm trying to use the I2C and SPI buses on my project.
The problem is that when I attempt to use both together by uncommenting each respective block of code, the SPI and I2C don't work right.
Both blocks work perfectly when the other is commented out. I use the SPI bus to program a DAC and PGA, and use the I2C bus to program a bank of LEDs.
I'll paste the relevant code below. As an example the code labeled "ERROR: HANGS HERE" gets stuck waiting for an ACK when the SPI block is also compiled in but always works when the SPI code is commented out.
I realize it's difficult to make suggestions based on code snippets, but does anyone have an idea where I should look for the problem? This program is running from flash and I copy time-critical functions to RAM.
// Using the I2C bus. Works fine if SPI is not compiled in.
void set_leds(Uint16 slave_addr, Uint16 on_off)
{
int i;
I2caRegs.I2CSAR = slave_addr;
I2caRegs.I2CMDR.all = 0x4620; // FREE, MST, TRX, IRS
I2caRegs.I2CCNT = LEN_ON_MSG;
I2caRegs.I2CMDR.bit.STT = 1; // Start condition
I2caRegs.I2CMDR.bit.STP = 1; // Stop condition will be generated when CNT=0
if (on_off == ON)
{
for (i=0; i<LEN_ON_MSG; i++)
{
I2caRegs.I2CDXR = on_msg[i];
while (I2caRegs.I2CSTR.bit.XRDY == 0); // ERROR: HANGS HERE
}
}
else
{
for (i=0; i<LEN_ON_MSG; i++)
{
I2caRegs.I2CDXR = off_msg[i];
while (I2caRegs.I2CSTR.bit.XRDY == 0);
}
}
}
...
...
...
// I2C initialization.
void i2c_init(void)
{
I2caRegs.I2CMDR.all = 0; // Disable I2C module and clear all options
I2caRegs.I2CFFTX.all = 0;
I2caRegs.I2CFFRX.all = 0;
// Set the clock rates:
// Module clock rate: (CPU clock)/(IPSC + 1) = 10 MHz.
// I2C bus clock divider registers: 10 MHz/((ICCL+d)+(ICCH+d))
// where'd'=5 (see sprufz9d.pdf) produces bus clock at 300 KHz, 50% duty cycle.
I2caRegs.I2CPSC.all = 5; // 60 MHz/(IPSC+1) = 10 MHz
I2caRegs.I2CCLKL = 3; // ICCL+d = 6
I2caRegs.I2CCLKH = 3; // ICCH+d = 6
CLK_enableI2cClock(myClk);
I2caRegs.I2CSAR = TLC59116_SLAVE_ADDR_60;
I2caRegs.I2CMDR.bit.IRS = 1; // Enable I2C module
while (I2caRegs.I2CMDR.bit.STP != 0);
return;
}
...
...
...
// SPI initialization. Works fine if I2C is not compiled in.
void spi_init()
{
CLK_enableSpiaClock(myClk);
SPI_resetChannels(mySpi);
SPI_setCharLength(mySpi, SPI_CharLength_16_Bits);
SPI_setMode(mySpi, SPI_Mode_Master);
SPI_setBaudRate(mySpi, SPI_BaudRate_500_KBaud);
SPI_setPriority(mySpi, SPI_Priority_FreeRun);
SPI_enableChannels(mySpi);
SPI_disableLoopBack(mySpi);
SPI_enableTx(mySpi);
SPI_setTriWire(mySpi, SPI_TriWire_ThreeWire);
SPI_setClkPhase(mySpi, SPI_ClkPhase_Delayed);
SPI_setClkPolarity(mySpi, SPI_ClkPolarity_OutputRisingEdge_InputFallingEdge);
SPI_enable(mySpi);
return;
}
void spi_fifo_init(void)
{
SPI_enableFifoEnh(mySpi);
SPI_resetTxFifo(mySpi);
SPI_clearTxFifoInt(mySpi);
return;
}