Skip to main content
Associate II
January 16, 2025
Solved

HAL_SPI error is not reported even with failed cases in STML4 device

  • January 16, 2025
  • 10 replies
  • 1886 views

 

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?

This topic has been closed for replies.
Best answer by Andrew Neil

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

10 replies

Andrew Neil
Super User
January 16, 2025

@anandhram1988 wrote:

spiState must be error state 


Must it?

Why?

 

PS: How to insert source code 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate II
January 16, 2025

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. 

Andrew Neil
Super User
January 16, 2025

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.

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate II
January 16, 2025

i can understand this scenario , but in this case i have removed the slave device , in this case the error must be reported?

 

Andrew Neil
Super User
January 16, 2025

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!

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate II
January 16, 2025

for this test: removing slave i am using the below code:

        spiState1 = HAL_SPI_Transmit(&hspi1, (uint8_t*)buffer[0], 1, 10);
    
    uint32_t error = HAL_SPI_GetError(&hspi1);
    spiState = HAL_SPI_GetState(&hspi1);
Associate II
January 16, 2025

test 1:  L4 with slave device connected 

 

   spiState1 = HAL_SPI_Transmit(&hspi1, (uint8_t*)buffer[0], 1, 10);
    
    uint32_t error = HAL_SPI_GetError(&hspi1);
    spiState = HAL_SPI_GetState(&hspi1);
result : all good
 

test 2:  L4 with no  slave device connected 

 

   spiState1 = HAL_SPI_Transmit(&hspi1, (uint8_t*)buffer[0], 1, 10);
    
    uint32_t error = HAL_SPI_GetError(&hspi1);
    spiState = HAL_SPI_GetState(&hspi1);
result :no error reported.
 
test 3:  L4 with slave device connected but trying to simulate introduce error to get error codes
HAL_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
Andrew Neil
Andrew NeilBest answer
Super User
January 16, 2025

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
January 16, 2025

@Andrew Neil wrote:

how would the hardware detect that there is no slave attached?

It can't


To expand on that, consider how an SPI transfer works:

  1. The Master asserts NSS - this is unaffected by whether a slave is connected or not
     
  2. The Master clocks out data on MOSI  - this is unaffected by whether a slave is connected or not
     
  3. Simultaneously, the Master samples data in MISO - there is no way to tell if the signal at MISO is actually coming from a slave, or is just open-circuit, or tied high, or tied low
     
  4. The Master releases NSS - this is unaffected by whether a slave is connected or not

So you can see that there is no way for the SPI hardware itself to know whether a slave is connected or not.

Similarly for a UART.

But I2C is different - because the I2C protocol requires certain actions from the Slave

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.