cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Transmit only last word of byffer

MAnik
Associate

Hi

I'm trying to transmit 4 16-bit words via SPI as slave using interrupts and HAL.

I'm using STM32F107VCT6

So I specified a buffer:

uint16_t buf[] = {0x000F, 0x00F0, 0x0F00, 0x0004};

And started to transmit in main:

MX_SPI2_Init();
HAL_SPI_Transmit_IT(&hspi2, (uint8_t *)buf, 4);
 
while (1) 
{
      //some logic
}

Spi was initialized with CubeMX:

static void MX_SPI2_Init(void)
{
 
  /* USER CODE BEGIN SPI2_Init 0 */
 
  /* USER CODE END SPI2_Init 0 */
 
  /* USER CODE BEGIN SPI2_Init 1 */
 
  /* USER CODE END SPI2_Init 1 */
  /* SPI2 parameter configuration*/
  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_SLAVE;
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi2.Init.NSS = SPI_NSS_HARD_INPUT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI2_Init 2 */
 
  /* USER CODE END SPI2_Init 2 */
 
}

That is what I get:

0693W000000XIgzQAG.png

What am I doing wrong?

3 REPLIES 3

Your code sends the 4 halfwords ONCE.

Is the LA picture you posted the FIRST transaction after the slave has been reset?

JW

MAnik
Associate

Thanks for the answer!

You are right, first transaction is correct.

What is the correct way to transmit data "by request" using interrupts?

Is there any way to get interrupt by hardware NSS?

>What is the correct way 

There's no "one correct way", it depends on your application.

> Is there any way to get interrupt by hardware NSS?

Use EXTI interrupt - Cube will get into your way. It's a recurring theme her, search, there's among others also a quote recent related thread.

JW