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?