2019-04-15 01:42 AM
I am trying to use asynchronous USART on stm32L4R5ZI nucleo board for sending and receiving data. Following is related part of code.
while (1)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin); //Toggle LED
HAL_Delay(1000); //Delay 1 Seconds
for(i = 0; i < 5; i++)
{
USART1->TDR = p[i];
while((USART1->ISR & 0x40) == 0);
}
while ((USART1->ISR & 0x20) == 0);
uint32_t receivedByte = (uint32_t)(USART1->RDR);
}
Problem is that I am able to send data to PC but I am not able to receive data. What can be the problem?
2019-04-15 01:55 AM
It is not wired properly.
You are not clearing any pending noise or overflow flags, etc on the receiver.
The code might work more effectively if you don't continually block, and check for TXE before sending a byte.
2019-04-15 03:14 AM
I have checked wiring is proper.
2019-04-15 05:41 AM
Or test you this code:
uint8_t getChar()
{
uint8_t InputData = 0;
while (1)
{
if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE))
__HAL_UART_CLEAR_OREFLAG(&huart1);
if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE))
{
InputData = huart1.Instance->RDR & 0x1FF;
return InputData;
}
}
}
2019-05-25 04:07 AM
This resolved my problem
2019-05-25 04:33 AM
This is polling based. If you want to keep it that way, add the DMA as cyclic buffer filling from RX side, then check the incoming buffer regularly.
Transmit can be polling as you are in control of sending data timeline.
Imagine that you add a HAL_Delay(10) somewhere in your code, you might lose incoming bytes from USART the way it is here. From example to application type example.