cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G030 UART_DMA

Sudhakar.R
Associate III

I'm trying to 500ms once transmit 5 byte of data. but 2 byte of data only transmit . below i given my code . i don't know where i made mistake if any one know help me.

UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_tx;
DMA_HandleTypeDef hdma_usart2_rx;
 
 uint8_t Tx_data[5] = {0, 0, 0, 0, 0};
 uint8_t Rx_data[1];
 
 uint32_t Aout[3];
 int status = 0,  test = 0;
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
 
 void TIM1_init(void)
{
	RCC->APBENR2 |= RCC_APBENR2_TIM1EN;  // TIM1 CLOCK ENABLE
	
	TIM1->PSC = 1-1;   //PERSCALER VALUE IS 0-65535
	TIM1->ARR = 2000-1;     //ARR VALUE IS 0-65535
	TIM1->RCR = 0;
	TIM1->BDTR |= TIM_BDTR_MOE;  // Main output enable
	TIM1->EGR = TIM_EGR_UG;  // Update generation
	TIM1->DIER  |= (TIM_DIER_UIE);  // Update interrupt enable 
	NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
	TIM1->CNT = 0;   //COUNT START IN 0
	TIM1->CR1 |= (TIM_CR1_CEN);  // COUNTER ENABLE
}
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
 
	HAL_UART_Receive_DMA(&huart2, Rx_data, 1);
	HAL_UART_Transmit_DMA(&huart2, Tx_data, sizeof(Tx_data));
	
	TIM1_init();
  while (1)
  {   
		HAL_UART_TxCpltCallback(&huart2);
  }
}
 
static void MX_USART2_UART_Init(void)
{
  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_RX;
  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();
  }
}
 
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);
  /* DMA1_Channel2_3_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
}
 
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
	TIM1->SR &= ~TIM_SR_UIF;
	test++;
	if(test == 8000)
	{
		__HAL_DMA_ENABLE(huart2.hdmatx);
		status++;
	}	
}
 
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	if(status == 1)
	{
		test=0;
		Tx_data[0] = 0x04;
		Tx_data[1] = 0x00;
		Tx_data[2] = 0x09;
		Tx_data[3] = 0x05;
		Tx_data[4] = 0x66;
		if((USART2->ISR & USART_ISR_TC) != 1)
		{
			__HAL_DMA_DISABLE(huart2.hdmatx);
			status = 0;
		}
	}
}

2 REPLIES 2
RomainR.
ST Employee

Hello @Sudhakar.R (Community Member) asked a question.

Maybe this same question (the same exercise of a same promotion...?) will help you.

https://community.st.com/s/question/0D53W00002I831eSAB/how-to-transmit-five-byte-data-transmit-in-uartdma

Best 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.

Sudhakar.R
Associate III

Thank you for your reply.