cancel
Showing results for 
Search instead for 
Did you mean: 

UART DMA multiple transmissions/receptions failure

Beppe101
Associate III

Hi all,
I'm facing an issue that has already been discussed several times in this and others forum, I attempted all the proposed solutions I could find but without success.
I want to read the characters user inputs via Tera Term and store them in a buffer, when 10 characters are entered or user press 'Enter' key, the received characters are sent out to the serial port.
This should go on forever, again, after 10 characters or 'Enter' key input from user the received characters are sent out to the serial port.
I'm using STM32G070RBTx, STM32CubeIDE v. 1.13.2 , this is my code:

 

 

 

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "crc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include <Serial.h>

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

int readyToSend = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* The UART2 debug serial port outbound buffer */
uint8_t outboundBuffer[512];
/* The UART2 debug serial port inbound character buffer */
uint8_t UART2_charRxBuffer;
/* The UART2 debug serial port inbound data buffer */
uint8_t UART2_rxBuffer[512];

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

	int rc = 0;

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */


  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_TIM6_Init();
  MX_ADC1_Init();
  MX_TIM14_Init();
  MX_USART1_UART_Init();
  MX_CRC_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */

    HAL_TIM_Base_Start_IT(&htim6);
    HAL_TIM_Base_Start_IT(&htim3);
    HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);

    HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);
    HAL_TIM_Base_Start(&htim14);

    HAL_UART_MspInit(&huart1);
    HAL_UART_MspInit(&huart2);

    __HAL_RCC_CRC_CLK_ENABLE();

    rc = HAL_UART_Receive_DMA( &huart2, &UART2_charRxBuffer, 1 );

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

    while (1) {

    /* USER CODE END WHILE */

    	while (!readyToSend);

    	uint16_t bufferSize = strlen( outboundBuffer );

    	rc = HAL_UART_Transmit_DMA( &huart2, outboundBuffer, bufferSize );

    /* USER CODE BEGIN 3 */
	}
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
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();
  }
}

/* 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.
  *   file: pointer to the source file name
  *   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 */

 

 

 

The file containing callback functions is:

 

 

 

/*
 * Serial.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Serial.h>
#include "main.h"
#include "usart.h"
#include "crc.h"
#include "adc.h"

/* The UART2 debug serial port inbound character buffer */
extern uint8_t UART2_charRxBuffer;

/* The UART2 debug serial port inbound data buffer */
extern uint8_t UART2_rxBuffer[512];

/* The UART2 debug serial port outbound buffer */
extern uint8_t outboundBuffer[512];

extern int readyToSend;

void HAL_UART_TxCpltCallback( UART_HandleTypeDef *huart )
{
    if( huart->Instance == USART2 )
    {
    	readyToSend = 0;
    }
}

/**
  * @brief  This function appends incoming characters from the debug serial port to receiving buffer.
  *  huart The UART port handle
  * @retval None
  */
void HAL_UART_RxCpltCallback( UART_HandleTypeDef *huart )
{
    if( huart->Instance == USART2 )
    {
        static int serialDebugCharsCounter = 0;

        if (!readyToSend ) // ignore incoming characters until retransmission completes
        {

            /* Add received byte to buffer */
            UART2_rxBuffer[serialDebugCharsCounter++] = UART2_charRxBuffer;

            if ( ( UART2_charRxBuffer == '\r' ) || ( serialDebugCharsCounter == 10 ) )
            {
                /* Add trailing '\0' */
                UART2_rxBuffer[serialDebugCharsCounter] = '\0';

                memcpy( UART2_rxBuffer, outboundBuffer, serialDebugCharsCounter );

                /* Reset the data counter */
                serialDebugCharsCounter = 0;

                readyToSend = 1;
            }
        }
    }
}

 

 

 

In the file stm32g0xx_it.c I see:

 

 

 

/**
  * @brief This function handles DMA1 channel 2 and channel 3 interrupts.
  */
void DMA1_Channel2_3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */

  /* USER CODE END DMA1_Channel2_3_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart2_tx);
  HAL_DMA_IRQHandler(&hdma_usart2_rx);
  /* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */

  /* USER CODE END DMA1_Channel2_3_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */

  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */

  /* USER CODE END USART2_IRQn 1 */
}

 

 

 

I verified the STM32 serial to USB PC port connection is working good.

Main problem is that the function HAL_UART_RxCpltCallback is never executed.

Please help, can somebody check my code and/or provide a full working example on how to accomplish my goal ?

1 ACCEPTED SOLUTION

Accepted Solutions

Need to use volatile for variables changed under interrupt context, ie also callbacks, and then relied upon elsewhere. The compiler/optimizer doesn't understand the inter-play.

Code fails to copy NUL in string. Adds it to the holding string, but doesn't move it.

Logic here is backward  

memcpy( UART2_rxBuffer, outboundBuffer, serialDebugCharsCounter );

Single characters, interrupt mode probably more efficient.

Would probably watch for reception errors that would stop further data

>>I'm facing an issue that has already been discussed several times in this and others forum, I attempted all the proposed solutions I could find but without success.

You should perhaps master the art of debugging code you have copied, or have written. It'll take time, but the lessons you learn will be invaluable.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5
Simon.T
ST Employee

Hello @Beppe101 ,

 

First of all if you want to have more detail about how the USART is working with DMA you can check this github: https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx.

 

Concerning your code, what is the DMA configuration for the RX line ? Is it in circular mode ? 

Also for the RX line, as the DMA buffer size is 1, I would not recommend to use DMA but just the interrupt line instead.

 

BR,


Simon

Bob S
Principal

+1 on using MaJerle's code.  Don't write your own, use what works and get on with your project.

Need to use volatile for variables changed under interrupt context, ie also callbacks, and then relied upon elsewhere. The compiler/optimizer doesn't understand the inter-play.

Code fails to copy NUL in string. Adds it to the holding string, but doesn't move it.

Logic here is backward  

memcpy( UART2_rxBuffer, outboundBuffer, serialDebugCharsCounter );

Single characters, interrupt mode probably more efficient.

Would probably watch for reception errors that would stop further data

>>I'm facing an issue that has already been discussed several times in this and others forum, I attempted all the proposed solutions I could find but without success.

You should perhaps master the art of debugging code you have copied, or have written. It'll take time, but the lessons you learn will be invaluable.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Karl Yamashita
Lead II

Did you enable the NVIC interrupt? 

If you find my answers useful, click the accept button so that way others can see the solution.

Oh yes, the terminating '\0' was added but not copied.

I'm receiving 1 byte per second more or less, I verified DMA is faster than using Interrupt, so I'm keeping my solution using DMA to receive single bytes.