2024-11-01 01:25 PM - last edited on 2024-11-02 07:08 AM by SofLit
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 ?
Solved! Go to Solution.
2024-11-02 07:07 AM - edited 2024-11-02 07:09 AM
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));
2024-11-01 02:00 PM
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
2024-11-02 07:07 AM - edited 2024-11-02 07:09 AM
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));