cancel
Showing results for 
Search instead for 
Did you mean: 

problem send byte via spi dma tx

SDall
Associate II

hi I'm trying to send a byte to arduino with spi2 in circular dma tx in circular mode,

at the beginning the dma starts then I don't understand why the cpu

crashes in the while (1), here the code:

 MX_GPIO_Init();

 MX_DMA_Init();

 MX_SPI2_Init();

 MX_USART2_UART_Init();

 /* USER CODE BEGIN 2 */

 HAL_SPI_Transmit_DMA(&hspi2,&i, 1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  

 i++;

   

HAL_Delay(200);

}}

here dma set:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(hspi->Instance==SPI2)

 {

 /* USER CODE BEGIN SPI2_MspInit 0 */

 /* USER CODE END SPI2_MspInit 0 */

  /* Peripheral clock enable */

  __HAL_RCC_SPI2_CLK_ENABLE();

  

  __HAL_RCC_GPIOC_CLK_ENABLE();

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**SPI2 GPIO Configuration   

  PC2   ------> SPI2_MISO

  PC3   ------> SPI2_MOSI

  PB10   ------> SPI2_SCK

  PB12   ------> SPI2_NSS 

  */

  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* SPI2 DMA Init */

  /* SPI2_TX Init */

  hdma_spi2_tx.Instance = DMA1_Stream4;

  hdma_spi2_tx.Init.Channel = DMA_CHANNEL_0;

  hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;

  hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;

  hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;

  hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

  hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

  hdma_spi2_tx.Init.Mode = DMA_CIRCULAR;

  hdma_spi2_tx.Init.Priority = DMA_PRIORITY_LOW;

  hdma_spi2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

  if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK)

  {

   Error_Handler();

  }

thanks

5 REPLIES 5
TDK
Guru

> crashes in the while (1),

Why do you think it's crashing? What does "crashing" mean?

Every time your transfer completes, the transfer complete interrupt is called.

Since you're sending 1 byte in circular mode, this is probably quite frequent, although you don't specify what the SPI clock is.

Disable the transfer complete interrupt. It could be set for the SPI and/or the relevant DMA stream.

If you feel a post has answered your question, please click "Accept as Solution".
SDall
Associate II

what i mean is that it executes the dma_transmitt instruction and starts transmitting the value of the variable i = 0 then continues transmitting i = 0 the i does not increase, it gets stuck in the loop! now the question is how can I disable the complete transfer interrupts , with what instruction?

TDK
Guru

Why would i increase? You told it to transmit 1 bit forever, and that's exactly what it's doing.

If you're trying to send 0, then 1, then 2, you need to put those values in a buffer and send them.

What are you trying to do?

If you feel a post has answered your question, please click "Accept as Solution".
SDall
Associate II

the problem is the circular dma which blocks the execution of the program when I debug, if instead I use it in normal mode the execution takes place regularly! now I need circular dma but i don't understand why it gets stuck ....

SDall
Associate II

I have to command a stepper motor driver via spi (tmc5130), what I wanted to do is change the parameters in the while cycle and let the dma send the data automatically without calling the function hal_spi_transmit_dma every time!