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.

Bug in CSL?

Guru 15580 points

I have written a function to initialize SPI0 as follows:

Int16 spi_init()
{
result = SPI_init();

if(CSL_SOK != result)
{
status = CSL_TEST_FAILED;
return (status);
}
else
{
printf ("SPI Instance Initialize successfully\n");
}

hSpi = SPI_open(SPI_CS_NUM_0, SPI_POLLING_MODE);

if(NULL == hSpi)
{
return (CSL_TEST_FAILED);
}
else
{
printf ("SPI Instance Opened successfully\n");
}

/** Set the hardware configuration */
hwConfig.spiClkDiv = SPI_CLK_DIV;
hwConfig.wLen = SPI_WORD_LENGTH_16;
hwConfig.frLen = SPI_FRAME_LENGTH;
hwConfig.wcEnable = SPI_WORD_IRQ_ENABLE;
hwConfig.fcEnable = SPI_FRAME_IRQ_DISABLE;
hwConfig.csNum = SPI_CS_NUM_0;
hwConfig.dataDelay = SPI_DATA_DLY_0;
hwConfig.csPol = SPI_CSP_ACTIVE_LOW;
hwConfig.clkPol = SPI_CLKP_LOW_AT_IDLE;
hwConfig.clkPh = SPI_CLK_PH_FALL_EDGE;

result = SPI_config(hSpi, &hwConfig);

if(CSL_SOK != result)
{
return (CSL_TEST_FAILED);
}
else
{
printf ("SPI Instance Configured successfully\n");
}
return result;
}

However, every other time I run this initialization the SPI_open() returns a failure
 since hSpi->configured was set==TRUE on the previous pass through the code.
Notice in the SPI_open() function below, if hSpi was previously configured the 
function returns hSpi==NULL.
CSL_SpiHandle SPI_open(SPI_HwMode	csNum, SPI_OperMode opMode)
{
CSL_SpiHandle hSpi;

hSpi = &SPI_Instance;

if(hSpi->configured == TRUE)
{
hSpi->configured = FALSE;
hSpi = NULL;
return (hSpi);
}

hSpi->mode = csNum;
hSpi->opMode = opMode;
return(hSpi);
}
Shouldn't the following code be added to SPI_init() to clear any pre-initialized handles?

if(hSpi->configured == TRUE)
{
hSpi->configured = FALSE;
hSpi = NULL;
}
I'm not sure why SPI_open() would need to return an error if hSpi were already configured. 
Can someone clarify?

  • Mike,

    The SPI_close(hSpi) function does exactly that. Ideally, you should call this function after you have completed the task for which you use the SPI. Please see the SPI example in the  "...\C55 Low Power Chip Support Library v2.50.00\c55xx_csl\ccs_v4.0_examples\spi\CSL_SPI_Example"

    Regards,

    Sunil D. Kamath

  • Sunil,

    Thanks. Is there any downside to adding the code per my previous post? I don't always "gracefully" exit my application while debugging...:)

  • Mike,

    Ideally your task/program should know if the SPI is available before you start to initialize/reprogram it, otherwise you might interrupt an ongoing transfer. This can be a very valid scenario especially if you have multiple tasks that may use the SPI.

    Sunil