File tree 1 file changed +59
-0
lines changed 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ # SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ import rp2pio
6
+ import adafruit_pioasm
7
+
8
+ code = adafruit_pioasm .assemble (
9
+ """
10
+ .program uart_rx_mini
11
+
12
+ ; Minimum viable 8n1 UART receiver. Wait for the start bit, then sample 8 bits
13
+ ; with the correct timing.
14
+ ; IN pin 0 is mapped to the GPIO used as UART RX.
15
+ ; Autopush must be enabled, with a threshold of 8.
16
+
17
+ wait 0 pin 0 ; Wait for start bit
18
+ set x, 7 [10] ; Preload bit counter, delay until eye of first data bit
19
+ bitloop: ; Loop 8 times
20
+ in pins, 1 ; Sample data
21
+ jmp x-- bitloop [6] ; Each iteration is 8 cycles
22
+
23
+ """
24
+ )
25
+
26
+
27
+ class RXUART :
28
+ def __init__ (self , pin , baudrate = 9600 ):
29
+ self .pio = rp2pio .StateMachine (
30
+ code ,
31
+ first_in_pin = pin ,
32
+ frequency = 8 * baudrate ,
33
+ auto_push = True ,
34
+ push_threshold = 8 ,
35
+ )
36
+
37
+ @property
38
+ def timeout (self ):
39
+ return 0
40
+
41
+ @property
42
+ def baudrate (self ):
43
+ return self .pio .frequency // 8
44
+
45
+ @baudrate .setter
46
+ def baudrate (self , frequency ):
47
+ self .pio .frequency = freqency * 8
48
+
49
+ @property
50
+ def in_waiting (self ):
51
+ return self .pio .in_waiting
52
+
53
+ def read (self , n ):
54
+ b = bytearray (n )
55
+ n = self .pio .readinto (b )
56
+ return b [:n ]
57
+
58
+ def readinto (self , buf ):
59
+ return self .pio .readinto (n )
You can’t perform that action at this time.
0 commit comments