Skip to content

rxuart: Receive-only 8N1 UART #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions examples/rxuart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)