cancel
Showing results for 
Search instead for 
Did you mean: 

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

MNapi
Senior III

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

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));

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SofLit
ST Employee

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));

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.