cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive bytes from SPI with interrupts using HAL?

Michel Keijzers
Associate III
Posted on May 25, 2018 at 13:45

Background

(note I asked this question too at

https://electronics.stackexchange.com/questions/376282/how-to-receive-bytes-from-spi-with-interrupts-using-hal-on-stm32

, but no answers yet).

I am trying to make a SPI connection between two STM32F103C8T6's.

Measurements

According to my logic analyzer I get a signal that looks ok (sending 10 bytes with values 0x55):

Results

My receiver's callback function for RX Complete also stops in the breakpoint (last line in code below):

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)

{

/* Prevent unused argument(s) compilation warning */

if (hspi1.RxXferCount == 10)

Problem

I sent 10 bytes, however, the RxXferCount has value 0. Instead, RxXferSize has value 10, but pRxBuffPtr is empty.

Question

How can I receive the correct bytes?

**Values from the register SPI1 and variable hspi1**

https://i.stack.imgur.com/xb5X5.png

Relevant code

Initialization (using a GPIO for defining Master and Slave):

/* SPI1 init function */

static void MX_SPI1_Init(void)

{

hspi1.Instance = SPI1;

hspi1.Init.Mode = (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_SET ?

SPI_MODE_MASTER : SPI_MODE_SLAVE);

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

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

}

}

Main function:

int main(void)

{

...

MX_SPI1_Init();

...

_transmitter = (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_SET ?

SPI_MODE_MASTER : SPI_MODE_SLAVE);

while (1)

{

if (_transmitter)

{

uint8_t data[] = { 0x55, 0x55, 0x55, 0x55, 0x55,

0x55, 0x55, 0x55, 0x55, 0x55 };

HAL_SPI_Transmit_IT(&hspi1, data, 10);

}

else

{

HAL_SPI_Receive_IT(&hspi1, _spi_data, 10);

}

}

}

1 ACCEPTED SOLUTION

Accepted Solutions
Michel Keijzers
Associate III
Posted on May 27, 2018 at 12:46

 The problem has been 'solved'... Or better said, there was no problem in the first place. I expected the results (received data) to be in the variable 

hspi in the interrupt handler, however, it is directly stored in the variable 

_spi_data (passed during the receive call). And hspi is cleared up already while in the interrupt handler.

@T J I saw your comment has been removed; thanks for your reaction though.

View solution in original post

1 REPLY 1
Michel Keijzers
Associate III
Posted on May 27, 2018 at 12:46

 The problem has been 'solved'... Or better said, there was no problem in the first place. I expected the results (received data) to be in the variable 

hspi in the interrupt handler, however, it is directly stored in the variable 

_spi_data (passed during the receive call). And hspi is cleared up already while in the interrupt handler.

@T J I saw your comment has been removed; thanks for your reaction though.