cancel
Showing results for 
Search instead for 
Did you mean: 

SPI sending out 32-bit data when configured for 16-bit

Hrishikesh
Senior
Posted on June 30, 2018 at 17:32

I've configured the STM32F103C8 for 16-bit data transfers. On the scope I can see my 16-bit data which is followed by some garbage data that the MCU is not configured to send. Am I mis-configuring something? How do I ensure that only 16-bits are sent? Here is my code in main.c: I've not included all the other bits about clocks and gpios. Please let me know if you'd like to see those as well and I'll attach them here.

uint16_t tx_temp = 0xABCD;
 __HAL_SPI_ENABLE(&hspi1);
 while (1)
 {
 /* USER CODE END WHILE */
 /* USER CODE BEGIN 3 */
 HAL_SPI_Transmit(&hspi1, &tx_temp, sizeof(tx_temp), HAL_MAX_DELAY);
 HAL_Delay(10);
 }
 /* USER CODE END 3 */�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

This is how the SPI is configured:

SPI_HandleTypeDef hspi1;
/* SPI1 init function */
void MX_SPI1_Init(void)
{
 hspi1.Instance = SPI1;
 hspi1.Init.Mode = SPI_MODE_MASTER;
 hspi1.Init.Direction = SPI_DIRECTION_1LINE;
 hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

This is what I see on the scope:

0690X0000060LYfQAM.png #16-bit-spi #spi-communication #spi-master-mode #hal-spi
4 REPLIES 4
Posted on June 30, 2018 at 22:34

Try changing:

sizeof(tx_temp)

...to '1'.

Posted on June 30, 2018 at 22:34

Suggests that HAL_SPI_Transmit takes a count of units to transmit, not bytes, right

HAL_SPI_Transmit(&hspi1, &tx_temp, sizeof(tx_temp)/sizeof(uint16_t), HAL_MAX_DELAY);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 01, 2018 at 10:14

Thank you. That worked!

:)

Posted on July 01, 2018 at 10:15

Thank you for that clarification. I wasn't aware of that.