Because of the Thanksgiving holiday in the U.S., TI E2E™ design support forum responses may be delayed the week of November 23.

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.

LM4F120XL cant control AIC3254 codec by SPI

Hi, I try to us LM4F120XL to control AIC3254 codec by SPI. Every time I read the register in AIC3254, I always get "255". Can anyone help? Thanks a lot.

Howard

//*****************************************************************************
//
// This function sets up DAC.
//
//*****************************************************************************
void
DacInit(void)
{
uint32_t pui32DataRx[NUM_SSI_DATA];
//
// The SSI0 peripheral must be enabled for use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);

//
// For this example SSI0 is used with PortA[5:2]. The actual port and
// pins used may be different on your part, consult the data sheet for
// more information. GPIO port A needs to be enabled so these pins can
// be used.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

//
// Configure the pin muxing for SSI0 functions on port A2, A3, A4, and A5.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);

//
// Configure the GPIO settings for the SSI pins. This function also gives
// control of these pins to the SSI hardware. Consult the data sheet to
// see which functions are allocated per pin.
// The pins are assigned as follows:
// PA5 - SSI0Tx
// PA4 - SSI0Rx
// PA3 - SSI0Fss
// PA2 - SSI0CLK
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
GPIO_PIN_2);

//
// Configure and enable the SSI port for SPI master mode. Use SSI0,
// system clock supply, idle clock level low and active low clock in
// freescale SPI mode, master mode, 1MHz SSI frequency, and 8-bit data.
// For SPI mode, you can set the polarity of the SSI clock when the SSI
// unit is idle. You can also configure what clock edge you want to
// capture data on. Please reference the datasheet for more information on
// the different SPI modes.
//
//SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_1, SSI_MODE_MASTER, 1000000, 8);
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_1, SSI_MODE_MASTER, 1000, 8);
//
// Enable the SSI0 module.
//
SSIEnable(SSI0_BASE);

//
// Read any residual data from the SSI port. This makes sure the receive
// FIFOs are empty, so we don't read any unwanted junk. This is done here
// because the TI SSI mode is full-duplex, which allows you to send and
// receive at the same time. The SSIDataGetNonBlocking function returns
// "true" when data was returned, and "false" when no data was returned.
// The "non-blocking" function checks if there is any data in the receive
// FIFO and does not "hang" if there isn't.
//
while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
{
}

DACReadRegister(0x0);

}

void DACReadRegister(unsigned int ucRegister)
{
uint32_t pui32Data[1];

unsigned int reg;

while(SSIDataGetNonBlocking(SSI0_BASE, &pui32Data[0]))
{
}

pui32Data[0] = 0x0;

//
// Write the first byte to the controller (register)
//
//I2CMasterDataPut(DAC_I2C_MASTER_BASE, ucRegister);
reg = (ucRegister << 1) | 1;
SSIDataPut(SSI0_BASE, reg);

//
// Wait until the current byte has been transferred.
while(SSIBusy(SSI0_BASE))
{
}

//
// Read Data Byte.
//
SSIDataGet(SSI0_BASE, &pui32Data[0]);

//
// Wait until the current byte has been transferred.
while(SSIBusy(SSI0_BASE))
{
}
UARTprintf("read:%d\n", ucRegister);
UARTprintf("%d\n", pui32Data[0]);
}

void DACWriteRegister(unsigned char ucRegister, unsigned long ulData)
{
unsigned int reg;

IntMasterDisable();
//
// Write the first byte to the controller (register)
//
//I2CMasterDataPut(DAC_I2C_MASTER_BASE, ucRegister);
reg = (ucRegister << 1);
SSIDataPut(SSI0_BASE, reg);

//
// Wait until the current byte has been transferred.
//
//while(I2CMasterBusy(DAC_I2C_MASTER_BASE))
while(SSIBusy(SSI0_BASE))
{
}

//
// Write the data byte to the controller.
//
//I2CMasterDataPut(DAC_I2C_MASTER_BASE, ulData);
SSIDataPut(SSI0_BASE, ulData);

//
// Wait until the current byte has been transferred.
//
//while(I2CMasterBusy(DAC_I2C_MASTER_BASE))
while(SSIBusy(SSI0_BASE))
{
}

IntMasterEnable();

}

  • There's one very important thing you are not doing in this code and it could be that it's just not clear from the datasheet (we've just added a note about this, actually). When using SSI in master mode as you are, you need to transmit an 0xFF byte for every byte you want to receive from the slave. The act of transmitting the 0xFF generates the clocks that the slave needs to actually send you back the data.

    In your code, before every call to SSIDataGet() which you expect to return a value from the slave, add an SSIDataGet(SSI0_BASE, 0xFF) to ensure that you have actually generated the clocks needed to get the data back from the slave.

    Note, also, that you will need to read a dummy byte from the SSI data register every time you transmit something to the slave because the receiver will clock data into the FIFO whenever the clock runs - it has no idea whether you intend to receive something from the slave or just transmit something to it.

    I was going to suggest you look at the SSI examples in TivaWare under examples/peripherals/ssi but, looking at these, I see they don't really make this clear at all. The only place I can think of which shows that you need to transmit bytes for every byte you receive is the CC3000 SPI driver which you can find in cc3000/src/spi/spi-tm4c123.c which uses DMA and so is rather more complex than you would need.

  • Hi Dave,

    Thank you so so much for the quick response and it helps a lot.

    There is a little typo that SSIDataGet(SSI0_BASE, 0xFF) should be SSIDataPut(SSI0_BASE, 0xFF). And I don't why the /SS pin of controller can't work therefore I just set a GPIO as /SS.

    I got stuck at this problem for weeks. Thanks again.

    Howa