Using pdk_am57xx_1_0_2 on an AM5728 processor using TI-RTOS.
Trying to use the McSpi driver transaction feature to pass a pointer to the callback function.
The doxygen docs say "A SPI_Transaction data structure is used with SPI_transfer(). It indicates how many SPI_FrameFormat frames are sent and received from the buffers pointed to txBuf and rxBuf. The arg variable is an user-definable argument which gets passed to the SPI_CallbackFxn when the SPI driver is in SPI_MODE_CALLBACK".
I am specifically trying to use the arg variable.
The code looks like this:
Initialize the SPI Driver:
/* Init SPI driver */
SPI_init();
/* Default SPI configuration parameters */
SPI_Params_init(&(this->spiParams));
this->spiParams.frameFormat = SPI_POL1_PHA0;
this->spiParams.transferMode = SPI_MODE_CALLBACK;
this->spiParams.transferCallbackFxn = MCSPICallbackFxn;
this->spiParams.mode = SPI_MASTER;
this->spiParams.bitRate = 12500000;
this->spiParams.dataSize = 8;
this->spiHandle = SPI_open(this->spiNum,&(this->spiParams));
Start the transfer:
static SPI_Transaction transaction; /* SPI transaction must stay until complete */
transaction.count = count;
transaction.txBuf = buffer; // Transmit buffer is reused for receive
transaction.rxBuf = buffer; // Transmit buffer is reused for receive
transaction.arg = this;
retVal = SPI_transfer(this->spiHandle, &transaction);
Callback function:
void MCSPICallbackFxn(SPI_Handle handle, SPI_Transaction * transaction)
{
if (transaction == NULL)
{
printf("ERROR");
}
else
{
AM57xxMcSpi* thisMcSpi = (AM57xxMcSpi*) transaction->arg;
if (transaction->status != SPI_TRANSFER_COMPLETED)
{
printf("MCSPICallbackFxn Transaction status %d not completed.",transaction->status);
}
thisMcSpi->semRxComplete->post();
}
}
So, I was crashing and after several hours of debugging I finally figured out that the transaction pointer was NULL, so I add the check for NULL and sure enough the crashing stopped. So, in the callback function the transaction pointer is indeed null.
Then, much to my surprise, I discover this in the spi driver code, SPI_v1.c, function SPI_v1_hwiFxn:
object->transaction = NULL;
object->transferCallbackFxn((SPI_Handle)arg, object->transaction);
So, it would appear that transaction is intentionally being to NULL, and this was never supposed to work. Is this a bug in the driver code or a mistake in the documentation?