1
\$\begingroup\$

Background

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

enter image description here

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

enter image description here

enter image description here

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);
      }
   }
}
\$\endgroup\$
1
\$\begingroup\$

Feeling stupid, but found the answer. Just want to mention it for others:

The result is NOT inside the argument hsp (.pRxBuffPtr) of the interrupt handler

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)

but in the variable _spi_data provided by the receive command:

HAL_SPI_Receive_IT(&hspi1, _spi_data, 10);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.