Datasheet

Table Of Contents
57 uart_get_hw(uart)->cr = UART_UARTCR_UARTEN_BITS | UART_UARTCR_TXE_BITS |
Ê UART_UARTCR_RXE_BITS;
58 // Enable FIFOs
59 hw_set_bits(&uart_get_hw(uart)->lcr_h, UART_UARTLCR_H_FEN_BITS);
60 // Always enable DREQ signals -- no harm in this if DMA is not listening
61 uart_get_hw(uart)->dmacr = UART_UARTDMACR_TXDMAE_BITS | UART_UARTDMACR_RXDMAE_BITS;
62
63 return baud;
64 }
4.2.7.1. Baud Rate Calculation
The uart baud rate is derived from dividing clk_peri.
If the required baud rate is 115200 and UARTCLK = 125MHz then:
Baud Rate Divisor = (125 * 10^6)/(16 * 115200) ~= 67.817
Therefore, BRDI = 67 and BRDF = 0.817,
Therefore, fractional part, m = integer((0.817 * 64) + 0.5) = 52
Generated baud rate divider = 67 + 52/64 = 67.8125
Generated baud rate = (125 * 10^6)/(16 * 67.8125) ~= 115207
Error = (abs(115200 - 115207) / 115200) * 100 ~= 0.006%
SDK: https://github.com/raspberrypi/pico-sdk/tree/master/src/rp2_common/hardware_uart/uart.c Lines 73 - 99
73 uint uart_set_baudrate(uart_inst_t *uart, uint baudrate) {
74 invalid_params_if(UART, baudrate == 0);
75 uint32_t baud_rate_div = (8 * clock_get_hz(clk_peri) / baudrate);
76 uint32_t baud_ibrd = baud_rate_div >> 7;
77 uint32_t baud_fbrd;
78
79 if (baud_ibrd == 0) {
80 baud_ibrd = 1;
81 baud_fbrd = 0;
82 } else if (baud_ibrd >= 65535) {
83 baud_ibrd = 65535;
84 baud_fbrd = 0;
85 } else {
86 baud_fbrd = ((baud_rate_div & 0x7f) + 1) / 2;
87 }
88
89 // Load PL011's baud divisor registers
90 uart_get_hw(uart)->ibrd = baud_ibrd;
91 uart_get_hw(uart)->fbrd = baud_fbrd;
92
93 // PL011 needs a (dummy) line control register write to latch in the
94 // divisors. We don't want to actually change LCR contents here.
95 hw_set_bits(&uart_get_hw(uart)->lcr_h, 0);
96
97 // See datasheet
98 return (4 * clock_get_hz(clk_peri)) / (64 * baud_ibrd + baud_fbrd);
99 }
RP2040 Datasheet
4.2. UART 446