Datasheet

Table Of Contents
25 sleep_ms(1000);
26 }
27 }
With the two PIO instances on RP2040, this could be extended to 8 additional UART TX interfaces, on 8 different pins, with
8 different baud rates.
3.6.4. UART RX
Recalling figure Figure 50 showing the format of an 8n1 UART:
We can recover the data by waiting for the start bit, sampling 8 times with the correct timing, and pushing the result to the
RX FIFO. Below is possibly the shortest program which can do this:
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/pio/uart_rx/uart_rx.pio Lines 7 - 18
Ê7 .program uart_rx_mini
Ê8
Ê9 ; Minimum viable 8n1 UART receiver. Wait for the start bit, then sample 8 bits
10 ; with the correct timing.
11 ; IN pin 0 is mapped to the GPIO used as UART RX.
12 ; Autopush must be enabled, with a threshold of 8.
13
14 wait 0 pin 0 ; Wait for start bit
15 set x, 7 [10] ; Preload bit counter, delay until eye of first data bit
16 bitloop: ; Loop 8 times
17 in pins, 1 ; Sample data
18 jmp x-- bitloop [6] ; Each iteration is 8 cycles
This works, but it has some annoying characteristics, like repeatedly outputting NUL characters if the line is stuck low.
Ideally, we would want to drop data that is not correctly framed by a start and stop bit (and set some sticky flag to
indicate this has happened), and pause receiving when the line is stuck low for long periods. We can add these to our
program, at the cost of a few more instructions.
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/pio/uart_rx/uart_rx.pio Lines 43 - 62
43 .program uart_rx
44
45 ; Slightly more fleshed-out 8n1 UART receiver which handles framing errors and
46 ; break conditions more gracefully.
47 ; IN pin 0 and JMP pin are both mapped to the GPIO used as UART RX.
48
49 start:
50 wait 0 pin 0 ; Stall until start bit is asserted
51 set x, 7 [10] ; Preload bit counter, then delay until halfway through
52 bitloop: ; the first data bit (12 cycles incl wait, set).
53 in pins, 1 ; Shift data bit into ISR
54 jmp x-- bitloop [6] ; Loop 8 times, each loop iteration is 8 cycles
55 jmp pin good_stop ; Check stop bit (should be high)
56
57 irq 4 rel ; Either a framing error or a break. Set a sticky flag,
58 wait 1 pin 0 ; and wait for line to return to idle state.
59 jmp start ; Don't push data if we didn't see good framing.
RP2040 Datasheet
3.6. Examples 352