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