Sending Serial Data from one UART to another. HAL_UART_Receive not reading new bytes.
My Board is the STM32-G431KB Nucleo-32. I have a Mavlink Serial Tx/Rx (From a PX4 Autopilot) connected to PA10/PA9.
I am attempting to receive serial data at 115200 8N1 on UART1 and then send it at the same rate on UART2. I am just doing a basic polling implementation of HAL_UART_Receive() grabbing a single byte at a time.
Unfortunately, HAL_UART_Receive only gets the first byte and transmits it. After the first loop, HAL_UART_Receive doesn't get any new data from the serial line. I can see there is good data coming in on UART1 with a logic analyzer. If I don't set to 0 in the while loop, it just transmits out the last byte received over and over.
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* Infinite loop */
while (1)
{
uint8_t c = 0;
HAL_UART_Receive(&huart1, &c, 1, 1);
HAL_UART_Transmit(&huart2, &c, sizeof(c), 0xFFFF);
}
}Can anyone point out where I'm going wrong? I expected HAL_UART_Receive to work like Arduino's 'serial->read'. Is there another step to force HAL_UART to grab a new byte?
Edit:
Using @KnarfB 's example below and the "uart forwarding" works fine. Incoming bytes on UART1 are transmitted out UART2 - as I intended.
while (1)
{
if( USART1->ISR & USART_ISR_RXNE ) {
char c = USART1->RDR & 0xFF;
while( (USART2->ISR & USART_ISR_TXE) == 0 );
USART2->TDR = c;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//HAL_Delay(20);
}Can anyone explain to me what I'm doing wrong with HAL_UART_Receive?
Edit: I believe I solved this by setting the parameter 'Fifo Mode' to 'Enable'
