AnsweredAssumed Answered

STM32-MAT/TARGET. Tons Of Bugs: Jumping over NULL pointer if not all SPI callback functions are involved

Question asked by Dima Sagalov on Feb 26, 2018

Environment:

Windows10 (x64)

Matlab R2017a

STM32-MAT/TARGET-v4.4.2

STM32CubeMX-v4.24.0

STM32Cube MCU Package for STM32F0 Series -v1.9.0

STM32Cube MCU Package for STM32F3 Series -v1.9.0

SW4STM32-v2.4

TrueSTUDIO-v9.0.0

 

The SPI block uses the callback functions to handle events (TX, RX, ERROR).

 

 

If you do not use them all, then an unconditional jump over NULL pointer will occur, which in turn will cause a hardware error (see spi_null_callback_hw_fault).

 

In the file <project_name>/<project_name>_SPI.c, arrays of pointers to the callback functions are initialized with null pointers

 

 

 

/* SPI Rx Callback function pointer array */
void (*SPIx_Rx_Callback[1])(void) = {
NULL,
};

/* SPI Tx Callback function pointer array */
void (*SPIx_Tx_Callback[1])(void) = {
NULL,
};

/* SPI Timeout Error Callback function pointer array */
void (*SPIx_Er_Callback[1])(void) = {
NULL,
};

 

Further, in the function SPI2_Initialization they are reassigned to the actual calls, if those are used.

If the corresponding callback function for the corresponding SPIx interface is not used, then the corresponding pointer remains unassigned.

After that, in one of the functions HAL_SPI_RxCpltCallback, HAL_SPI_TxCpltCallback, HAL_SPI_TxRxCpltCallback, HAL_SPI_ErrorCallback, there is an unconditional jump over one of them occurs - a hardware error can not be avoided!

 

There are two ways to bypass

 

1. Use empty callback functions (see spi_empty_callback_work)!

 

2. Do not jump over the pointer, if that is NULL.

 

What is noteworthy, functions HAL_SPI_RxCpltCallback, HAL_SPI_TxCpltCallback, HAL_SPI_TxRxCpltCallback, HAL_SPI_ErrorCallback already contain conditional statements to work around this situation:

 

...

//if(SPIx_Tx_Callback[i] != NULL) {
(*SPIx_Tx_Callback[i])();

//}
//if(SPIx_Rx_Callback[i] != NULL) {
(*SPIx_Rx_Callback[i])();

//}

 

But for some reason they are commented out! Either the developers forgot to uncomment these lines in the template, or whether it was done on purpose, but then the reason is not clear to me!

Attachments

Outcomes