2025-01-16 01:06 AM - last edited on 2025-01-16 01:10 AM by Andrew Neil
HAL_SPI_Transmit(&hspi1, NULL, 0, 0);
uint32_t error = HAL_SPI_GetError(&hspi1);
HAL_SPI_StateTypeDef spiState = HAL_SPI_GetState(&hspi1);
above lines of code i expect error = error code
spiState must be error state , but i still gets error = 0, spistate = Ready
anyone knows the reason?
2025-01-16 01:12 AM
2025-01-16 01:28 AM
thats the problem i am facing, but not sure why if any error module to be additionally included, but i have added the necessary driver code.
2025-01-16 01:35 AM
But why do you think that error should be anything other than zero ?
And why do you think that spistate should be anything other than Ready?
On what do you base those assumptions?
AIUI, error only relates to faults in the actual SPI hardware operation.
In your case, no hardware operation happens - so .error remains at zero, and spistate remains Ready.
2025-01-16 01:51 AM
i can understand this scenario , but in this case i have removed the slave device , in this case the error must be reported?
2025-01-16 02:10 AM
for this test: removing slave i am using the below code:
2025-01-16 02:11 AM
That's irrelevant - the code you showed never accesses the SPI at all:
HAL_SPI_Transmit( &hspi1, NULL, 0, 0 );
// ^^^^^^^ Look!!
You've given a NULL buffer pointer, and zero length - there is nothing to transmit!
This is an invalid call - it does nothing!
2025-01-16 02:33 AM
test 1: L4 with slave device connected
test 2: L4 with no slave device connected
HAL_SPI_Transmit(&hspi1, NULL, 0, 0); uint32_t error = HAL_SPI_GetError(&hspi1); HAL_SPI_StateTypeDef spiState = HAL_SPI_GetState(&hspi1);
2025-01-16 02:54 AM
Again, please see How to insert source code
@anandhram1988 wrote:test 2: L4 with no slave device connected
Think about it: how would the hardware detect that there is no slave attached?
It can't.
You will have to add that in your application.
@anandhram1988 wrote:test 3: L4 with slave device connected but trying to simulate introduce error to get error codes
So what, exactly, did you do for this "simulation"?
Again, think about whether it produces anything that the SPI hardware could actually detect.
These are the possible error codes which can be reported by HAL_SPI_GetError:
/** @defgroup SPI_Error_Code SPI Error Code
* @{
*/
#define HAL_SPI_ERROR_NONE (0x00000000U) /*!< No error */
#define HAL_SPI_ERROR_MODF (0x00000001U) /*!< MODF error */
#define HAL_SPI_ERROR_CRC (0x00000002U) /*!< CRC error */
#define HAL_SPI_ERROR_OVR (0x00000004U) /*!< OVR error */
#define HAL_SPI_ERROR_FRE (0x00000008U) /*!< FRE error */
#define HAL_SPI_ERROR_DMA (0x00000010U) /*!< DMA transfer error */
#define HAL_SPI_ERROR_FLAG (0x00000020U) /*!< Error on RXNE/TXE/BSY/FTLVL/FRLVL Flag */
#define HAL_SPI_ERROR_ABORT (0x00000040U) /*!< Error during SPI Abort procedure */
#if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
#define HAL_SPI_ERROR_INVALID_CALLBACK (0x00000080U) /*!< Invalid Callback error */
#endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
/**
* @}
*/
See stm32l4xx_hal_spi.h
2025-01-16 03:03 AM - edited 2025-01-16 03:04 AM
@anandhram1988 wrote:test 3: L4 with slave device connected but trying to simulate introduce error to get error codesHAL_SPI_Transmit(&hspi1, NULL, 0, 0); uint32_t error = HAL_SPI_GetError(&hspi1); HAL_SPI_StateTypeDef spiState = HAL_SPI_GetState(&hspi1);
result : no error reported
We've already been through that one - I explained that the code is invalid, and the results are as expected.