cancel
Showing results for 
Search instead for 
Did you mean: 

How to transmit five byte data transmit in UART_DMA?

mrsmile
Associate III

I already try for send five byte data in UART DMA. I am using the controller in STM32G030K6T6. Data transmition is working properly but output is did not taken properly. The problem was am transmit data five bytes for UART_DMA but DMA transmit only two bytes data. HAL_UART_Transmit_DMA call in timer interrupt handler to check the output for just now. The output is came but this is not proper output. this method for just call in to the interrupt handler output is continuously came for five bytes data. I need a output for 500 millisec once Five Bytes data transmit.  I attach the output for picture and code. please tell what can I do for next step.


_legacyfs_online_stmicro_images_0693W00000bVgJtQAK.png
_legacyfs_online_stmicro_images_0693W00000bVgJjQAK.png

#include "main.h"
 
 TIM_HandleTypeDef htim3;
 
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_tx;
 
/* USER CODE BEGIN PV */
 
uint16_t tim=0;
 
uint8_t tx_buf[5]= {0x01,0x02,0x03,0x04,0x05};
 
 
/* USER CODE END PV */
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM3_Init(void);
 
int main(void)
{
 
  HAL_Init();
 
 
  SystemClock_Config();
 
 
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	
	HAL_TIM_Base_Start_IT(&htim3);
	HAL_UART_Transmit_DMA(&huart2, tx_buf, sizeof(tx_buf));  // 500 msec once
  /* USER CODE END 2 */
 
  while (1)
  {
 
  }
}
 
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
 
  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */
		 tim++;	
	
	 if(tim==500)
	 {
		__HAL_DMA_ENABLE(huart2.hdmatx);
		 tx_buf[0] = 0x01;
		 tx_buf[1] = 0x02;
		 tx_buf[2] = 0x03;
		 tx_buf[3] = 0x04;
		 tx_buf[4] = 0x05;
  	 		tim=0; 	
		__HAL_DMA_DISABLE(huart2.hdmatx);
	 }
  /* USER CODE END TIM3_IRQn 1 */
}

1 ACCEPTED SOLUTION

Accepted Solutions
RomainR.
ST Employee

it is already declared and initialized as global.

volatile uint16_t prevTick=0;

But it's not correct, you must declare it as uint32_t because Systick timer is a 24bit down counter size.

And it should not be volatile.

Take a look at step 2°) in my example. and apply it:

int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t prevTick = 0;
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

Then put a breakpoint into stm32g0xx_it.c into following Systick Interrupt handler at line HAL_IncTick(); This to validate Systick interrupt is enabled and is working.

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
 
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
 
  /* USER CODE END SysTick_IRQn 1 */
}

Good luck

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

21 REPLIES 21
RomainR.
ST Employee

Hello @mrsmile (Community Member)

To better understand your use case and according your diagram, you want to send 5 bytes with UART and DMA, then wait a delay of 500ms ... send again 5 bytes and so on ?  

Regards,

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

yes, I want to send 5 bytes wit UART and DMA, then wait a delay 500 ms. if you know guide me.

RomainR.
ST Employee

Yes here my example in 4 steps:

1°) Create this global variable after your buffer: dmaXferCptl 

/* USER CODE BEGIN PV */
uint8_t tx_buf[5]= {0x01 ,0x02 ,0x03, 0x04, 0x05};
volatile uint8_t dmaXferCptl = 0;
/* USER CODE END PV */

2°) Create a local variable in main function.

/* USER CODE BEGIN 1 */
  uint32_t prevTick = 0;
/* USER CODE END 1 */

3°) Create the UART callback to update this variables somewhere in user code begin 4.

/* USER CODE BEGIN 4 */
/**
  * @brief Tx Transfer completed callback.
  * @param huart UART handle.
  * @retval None
  */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart->Instance == USART2)
  {
    dmaXferCptl = 1;
  }
}
/* USER CODE END 4 */

4°) Implement while loop in main function without using GP Timer, you can use Systick.

/* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* Wait 500ms before sending UART bufer */
    if ((HAL_GetTick() - prevTick ) > 500)
    {
      if (HAL_UART_Transmit_DMA(&huart2, (uint8_t *)tx_buf, 5) != HAL_OK)
      {
        /* Manage UART DMA Transfert here */
        Error_Handler();
      }
      /* Wait DMA Transfert complete */
      while (dmaXferCptl == 0){;}
      dmaXferCptl = 0;
      /* Toggle a Led for fun */
      HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_5);
      prevTick = HAL_GetTick();
 
    }
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Can you try it and let me know?

Regards,

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

How can I generate (HAL_GetTick() - prevTick ) > 500 this delay. out is continuously send the data.

#include "main.h"
 
 TIM_HandleTypeDef htim3;
 
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_tx;
 
/* USER CODE BEGIN PV */
 
uint16_t tim=0;
 
uint8_t tx_buf[5]= {0x01,0x02,0x03,0x04,0x05};
volatile uint8_t test_flag=0;
 
/* USER CODE END PV */
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM3_Init(void);
 
int main(void)
{
 
  HAL_Init();
 
 
  SystemClock_Config();
 
 
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	
	HAL_TIM_Base_Start_IT(&htim3);
//	HAL_UART_Transmit_DMA(&huart2, tx_buf, sizeof(tx_buf));  // 500 msec once
  /* USER CODE END 2 */
 
  while (1)
  {
		if ((HAL_GetTick() - tim ) > 500)
    {
      if (HAL_UART_Transmit_DMA(&huart2, (uint8_t *)tx_buf, 5) != HAL_OK)
      {
        Error_Handler();
      }
     }
		 while (test_flag == 0){;}
      test_flag = 0;
      tim = HAL_GetTick();	}}


_legacyfs_online_stmicro_images_0693W00000bViA0QAK.png.

RomainR.
ST Employee

This delay is generated by cpu CM0 Systick Timer and it is normally enabled when you generate a STM32CubeMX project.

The Systick is configured by HAL_Init() below to generate an interrupt every ms.

Do you use STM32CubeMX, STM32G0 HAL library?

In other case, share your full code.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

ok I will share. how initialize prevtick variable and condition can not enter the uart call back function. flag is not enable.

#include "main.h"
 
 TIM_HandleTypeDef htim3;
 
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_tx;
 
/* USER CODE BEGIN PV */
 
uint16_t tim=0;
 
uint8_t tx_buf[5]= {0x01,0x02,0x03,0x04,0x05};
volatile uint8_t test_flag=0; uint16_t prevTick=0;
 
/* USER CODE END PV */
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM3_Init(void);
 
int main(void)
{
 
  HAL_Init();
 
 
  SystemClock_Config();
 
 
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	
	HAL_TIM_Base_Start_IT(&htim3);
//	HAL_UART_Transmit_DMA(&huart2, tx_buf, sizeof(tx_buf));  // 500 msec once
  /* USER CODE END 2 */
 
  while (1)
  {
		if ((HAL_GetTick() - prevTick  ) > 500)
    {
      if (HAL_UART_Transmit_DMA(&huart2, (uint8_t *)tx_buf, 5) != HAL_OK)
      {
        Error_Handler();
      }
     }
		 while (test_flag == 0){;}
      test_flag = 0;
      prevTick  = HAL_GetTick();		 
		
		
//	  if(tim==500)
//	 {
//		 
//		  if (HAL_UART_Transmit_DMA(&huart2, (uint8_t *)tx_buf, 5) != HAL_OK)
//      {
//        Error_Handler();
//      }
//		 		 while (test_flag == 0){;}
//        test_flag = 0;
//					 tim=0;
					 
					 
//		__HAL_DMA_ENABLE(huart2.hdmatx);
//		 tx_buf[0] = 0x01;
//		 tx_buf[1] = 0x02;
//		 tx_buf[2] = 0x03;
//		 tx_buf[3] = 0x04;
//		 tx_buf[4] = 0x05;
//  	 		tim=0; 	
//		__HAL_DMA_DISABLE(huart2.hdmatx);
		 }
//	 }
		
		
}
 
void TIM3_IRQHandler(void)
{
  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */
		 tim++;	    // 500 ms timer increament
	
 
  /* USER CODE END TIM3_IRQn 1 */
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART2)
	{
		test_flag =1;
	}
	
}
 
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 8;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{
 
  /* USER CODE BEGIN TIM3_Init 0 */
 
  /* USER CODE END TIM3_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM3_Init 1 */
 
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 2;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 22999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */
 
  /* USER CODE END TIM3_Init 2 */
 
}
 
/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{
 
  /* USER CODE BEGIN USART2_Init 0 */
 
  /* USER CODE END USART2_Init 0 */
 
  /* USER CODE BEGIN USART2_Init 1 */
 
  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
 
  /* USER CODE END USART2_Init 2 */
 
}
 
/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{
 
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();
 
  /* DMA interrupt init */
  /* DMA1_Channel1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
 
  /*Configure GPIO pin : PC15 */
  GPIO_InitStruct.Pin = GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF1_OSC;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

RomainR.
ST Employee

it is already declared and initialized as global.

volatile uint16_t prevTick=0;

But it's not correct, you must declare it as uint32_t because Systick timer is a 24bit down counter size.

And it should not be volatile.

Take a look at step 2°) in my example. and apply it:

int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t prevTick = 0;
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

Then put a breakpoint into stm32g0xx_it.c into following Systick Interrupt handler at line HAL_IncTick(); This to validate Systick interrupt is enabled and is working.

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
 
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
 
  /* USER CODE END SysTick_IRQn 1 */
}

Good luck

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

same problem continued. uart call back function does not run and flag is not enable.

RomainR.
ST Employee

So, zip your full project and share it here, I will look it.

What is your board?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.