Receiving data

To enable the receiver on the same UART, the initialization function should also turn on the receiver using the corresponding switch in the UART_CR1 register:

UART3_CR1 |= UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE;

This ensures that the receiving side of the transceiver is enabled too. To read data in polling mode, blocking until a character is received, we can use the following function, which will return the value of the byte read:

char uart3_read(void){    char c;    volatile uint32_t reg;    do {        reg = UART3_SR;    } while ((reg & UART_SR_RX_NOTEMPTY) == 0);    c = (char)(UART3_DR & 0xff);    return c;}

This way we can, for example, echo back to the console each character received from the host:

void main(void) {    char c[2]; flash_set_waitstates(); ...

Get Embedded Systems Architecture now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.