I am trying to build a simple baud rate changer with a Blue pill stm32f103 using the HAL API.
What I'm hoping to achieve: One old terminal to that's locked at 9600 baud. One old thermal printer that has a max of 300 baud.
The project consist of 2 "Blue pills", one that just puts of a character stream at 9600. The other Blue pill takes the stream in at 9600 and dumps every 4 bytes into a register for UART 3. Using hardware flow control, the contents are dumped into UART2 which is 300 baud.
It works well as long as its a continuous stream but when I stop the primary transmitter I need the downstream UARTs to stop transmitting. I have tried using the RXNE, TXE and TC flags but they do not work as expected. When I stop the transmission from primary stream, UART2 ( 300 baud), goes into "TIME OUT" and just continuously prints whatever is left in its buffer.
while (1) {
if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE) == 0) { // 0 = Empty
HAL_GPIO_WritePin(BluePill_LED_GPIO_Port, BluePill_LED_Pin,GPIO_PIN_SET);
HAL_Delay(100);
}
HAL_UART_Receive(&huart3, (uint8_t*) uart1_buf, uart_buf_len, 500);
strcpy(uart2_buf, uart1_buf); // copy the contents of UART1 buffer to UART2 buffer
if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE) == RESET) {
HAL_GPIO_WritePin(BluePill_LED_GPIO_Port, BluePill_LED_Pin,GPIO_PIN_SET);
HAL_Delay(200);
}
HAL_UART_Transmit(&huart2, (uint8_t*) uart2_buf, uart_buf_len, 1000);
if (!(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE))) {
HAL_GPIO_WritePin(BluePill_LED_GPIO_Port, BluePill_LED_Pin,GPIO_PIN_RESET);
HAL_Delay(200);
}
}
This code is modified for testing purposes. As you can see I was thinking I could get the LED to come on when the flag conditions were met but the LED doesn't come on. One other odd thing is the although the LED doesn't come on, the HAL_Delay is for sure being executed.
Maybe someone could shed some light on what I'm doing wrong. Thanks