Skip to content

Commit 9a1d5c6

Browse files
committed
Add sideset assembly support.
You must also set `sideset_enable` when creating the rp2pio.StateMachine object. Fixes adafruit#16 and fixes adafruit#21.
1 parent 0f3df84 commit 9a1d5c6

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

adafruit_pioasm.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def assemble(text_program):
5555
pass
5656
elif line.startswith(".side_set"):
5757
sideset_count = int(line.split()[1])
58+
sideset_enable = 1 if "opt" in line else 0
5859
elif line.endswith(":"):
5960
label = line[:-1]
6061
if label in labels:
@@ -64,7 +65,7 @@ def assemble(text_program):
6465
# Only add as an instruction if the line isn't empty
6566
instructions.append(line)
6667

67-
max_delay = 2 ** (5 - sideset_count) - 1
68+
max_delay = 2 ** (5 - sideset_count - sideset_enable) - 1
6869
assembled = []
6970
for instruction in instructions:
7071
# print(instruction)
@@ -76,10 +77,13 @@ def assemble(text_program):
7677
raise RuntimeError("Delay too long:", delay)
7778
instruction.pop()
7879
if len(instruction) > 1 and instruction[-2] == "side":
80+
if sideset_count == 0:
81+
raise RuntimeError("No side_set count set")
7982
sideset_value = int(instruction[-1])
8083
if sideset_value > 2 ** sideset_count:
8184
raise RuntimeError("Sideset value too large")
82-
delay |= sideset_value << (5 - sideset_count)
85+
delay |= sideset_value << (5 - sideset_count - sideset_enable)
86+
delay |= sideset_enable << 4
8387
instruction.pop()
8488
instruction.pop()
8589

@@ -186,6 +190,6 @@ def assemble(text_program):
186190
else:
187191
raise RuntimeError("Unknown instruction:" + instruction[0])
188192
assembled[-1] |= delay << 8
189-
# print(hex(assembled[-1]))
193+
# print(bin(assembled[-1]))
190194

191195
return array.array("H", assembled)

examples/txuart.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_tx
11+
.side_set 1 opt
12+
13+
; An 8n1 UART transmit program.
14+
; OUT pin 0 and side-set pin 0 are both mapped to UART TX pin.
15+
16+
pull side 1 [7] ; Assert stop bit, or stall with line in idle state
17+
set x, 7 side 0 [7] ; Preload bit counter, assert start bit for 8 clocks
18+
bitloop: ; This loop will run 8 times (8n1 UART)
19+
out pins, 1 ; Shift 1 bit from OSR to the first OUT pin
20+
jmp x-- bitloop [6] ; Each loop iteration is 8 cycles.
21+
22+
"""
23+
)
24+
25+
class TXUART:
26+
def __init__(self, *, tx, baudrate=9600):
27+
self.pio = rp2pio.StateMachine(
28+
code,
29+
first_out_pin=tx,
30+
first_sideset_pin=tx,
31+
frequency=8 * baudrate,
32+
initial_sideset_pin_state=1,
33+
initial_sideset_pin_direction=1,
34+
initial_out_pin_state=1,
35+
initial_out_pin_direction=1,
36+
sideset_enable=True
37+
)
38+
39+
@property
40+
def timeout(self):
41+
return 0
42+
43+
@property
44+
def baudrate(self):
45+
return self.pio.frequency // 8
46+
47+
@baudrate.setter
48+
def baudrate(self, frequency):
49+
self.pio.frequency = frequency * 8
50+
51+
def write(self, buf):
52+
return self.pio.write(buf)

0 commit comments

Comments
 (0)