Skip to main content
Graduate II
November 1, 2024
Solved

HAL_SPI_Transmit, dose it transfer data or address of the data?

  • November 1, 2024
  • 2 replies
  • 1203 views

what does exactly happen here ?

static void WriteCommand(uint8_t cmd)
{
 HAL_SPI_Transmit(&hspi2, &cmd, sizeof(cmd), HAL_MAX_DELAY);
}

&cmd is address of the variable

I wonder if HAL is actually transferring data at that address ?

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    You are transmitting someting from the stack. 

    1- &cmd is not the address of the variable but pointing  to something in the stack.

    This is the declaration of HAL_SPI_Transmit() in stm32h7xx_hal_spi.c:

     

    HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size, uint32_t Timeout)

     

    2- sizeof(cmd) will be always = 1 as it's a byte.

     

    So if you want to send the command with a flexible data input and size, this how to implement it:

     

    uint8_t command[2] = {0xAA, 0x55};
    
    static void WriteCommand(uint8_t* cmd, uint16_t cmd_size)
    {
     HAL_SPI_Transmit(&hspi2, cmd, cmd_size, HAL_MAX_DELAY);
    }
    
    /* call WriteCommand */
    WriteCommand(command, sizeof(command));

     

    2 replies

    Graduate II
    November 1, 2024

    It's a pointer to the bytes you want to send. Could be a WORD count if SPI configured in 16-bit mode.

    WriteCommand is sending the BYTE value that you pass to the function

    mƎALLEmAnswer
    Technical Moderator
    November 2, 2024

    Hello,

    You are transmitting someting from the stack. 

    1- &cmd is not the address of the variable but pointing  to something in the stack.

    This is the declaration of HAL_SPI_Transmit() in stm32h7xx_hal_spi.c:

     

    HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size, uint32_t Timeout)

     

    2- sizeof(cmd) will be always = 1 as it's a byte.

     

    So if you want to send the command with a flexible data input and size, this how to implement it:

     

    uint8_t command[2] = {0xAA, 0x55};
    
    static void WriteCommand(uint8_t* cmd, uint16_t cmd_size)
    {
     HAL_SPI_Transmit(&hspi2, cmd, cmd_size, HAL_MAX_DELAY);
    }
    
    /* call WriteCommand */
    WriteCommand(command, sizeof(command));