How to use HAL_UARTEx_ReceiveToIdle_DMA() and monitor when the data reception is complete
Greetings
I am trying to use 2 UART interfaces on the STM32L4xx series and echo what's received on 1 interface. I am receiving strings of unknown length on UART1 using the DMA and echo-ing it with the virtual com port (UART2).
I intend on using receiving one byte at a time and using the HAL_UARTEx_ReceiveToIdle_DMA() function, and be notified via the ISR HAL_UARTEx_RxEventCallback() function, when the UART1-reception is completed because UART1 will be idle, and I can form the array of bytes received and transmit it via UART2. However, HAL_UARTEx_RxEventCallback() only fires for each byte received. The brief of the function says it will fire when the uart line is idle.
I have also tried to use the HAL_UART_RxCpltCallback() function to notify me that reception is complete, but it never fires.
What am I doing wrong? My code is as follows
#define UART_RX_BUFFER_SIZE 1
uint8_t UART1_RxBuffer[UART_RX_BUFFER_SIZE] = {0};
uint16_t RxDataLen = 0;
uint16_t cnt = 0;
uint8_t final_buffer[1024];
bool busy_flag = false;
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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_USART3_UART_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/*
* Init of the reception using DMA
*
* */
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, UART1_RxBuffer, UART_RX_BUFFER_SIZE);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/*
* Looking to use this flag to transmit the constructed string from each byte/character received
*
* */
if(busy_flag == 1)
{
busy_flag = 0;
HAL_UART_Transmit(&huart2, final_buffer, strlen(final_buffer), 100);
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* ISR which runs when a byte is received but does not notify when the uart line is idle*/
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
final_buffer[cnt++] = UART1_RxBuffer[0];
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, UART1_RxBuffer, UART_RX_BUFFER_SIZE);
}
/*
* This ISR does not fire at all. One would think this ISR fires when the reception is complete
*
*
* */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
busy_flag = 1;
}
