Skip to main content
Graduate
January 16, 2025
Solved

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

  • January 16, 2025
  • 5 replies
  • 1817 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

    5 replies

    Super User
    January 16, 2025

    @anandhram1988 wrote:

    spiState must be error state 


    Must it?

    Why?

     

    PS: How to insert source code 

    Graduate
    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. 

    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.

     

    Graduate
    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?

     

    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!

    Graduate
    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);
    Graduate
    January 16, 2025

    test 1:  L4 with slave device connected 

     

       spiState1 = HAL_SPI_Transmit(&hspi1, (uint8_t*)buffer[0], 110);
        
        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], 110);
        
        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
    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

    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