Skip to content

Commit 94dccca

Browse files
authored
Merge pull request #11 from adafruit/jepler-examples-improvements
Add some examples, improve the assembler
2 parents a631797 + 9399b44 commit 94dccca

File tree

6 files changed

+154
-5
lines changed

6 files changed

+154
-5
lines changed

adafruit_pioasm.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
def assemble(text_program):
3333
"""Converts pioasm text to encoded instruction bytes"""
34-
# pylint: disable=too-many-branches,too-many-statements
34+
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
3535
assembled = []
3636
program_name = None
3737
labels = {}
@@ -55,7 +55,10 @@ def assemble(text_program):
5555
elif line.startswith(".side_set"):
5656
sideset_count = int(line.split()[1])
5757
elif line.endswith(":"):
58-
labels[line[:-1]] = len(instructions)
58+
label = line[:-1]
59+
if label in labels:
60+
raise SyntaxError(f"Duplicate label {repr(label)}")
61+
labels[label] = len(instructions)
5962
elif line:
6063
# Only add as an instruction if the line isn't empty
6164
instructions.append(line)
@@ -85,10 +88,13 @@ def assemble(text_program):
8588
elif instruction[0] == "jmp":
8689
# instr delay cnd addr
8790
assembled.append(0b000_00000_000_00000)
88-
if instruction[-1] in labels:
89-
assembled[-1] |= labels[instruction[-1]]
91+
target = instruction[-1]
92+
if target[:1] in "0123456789":
93+
assembled[-1] |= int(target)
94+
elif instruction[-1] in labels:
95+
assembled[-1] |= labels[target]
9096
else:
91-
assembled[-1] |= int(instruction[-1])
97+
raise SyntaxError(f"Invalid jmp target {repr(target)}")
9298

9399
if len(instruction) > 2:
94100
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5

examples/getting-started/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2020 Jeff Epler, written for Adafruit Industries
3+
4+
SPDX-License-Identifier: MIT
5+
-->
6+
7+
These examples are adapted from [Getting started with MicroPython on the Raspberry Pi Pico](https://www.adafruit.com/product/4898). You can also get an [electonic copy](https://hackspace.raspberrypi.org/books/micropython-pico/). PIO is covered in Appendix C.
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adapted from the an example in Appendix C of RPi_PiPico_Digital_v10.pdf
6+
7+
import time
8+
import board
9+
import rp2pio
10+
import adafruit_pioasm
11+
12+
led_quarter_brightness = adafruit_pioasm.assemble(
13+
"""
14+
set pins, 0 [2]
15+
set pins, 1
16+
"""
17+
)
18+
19+
led_half_brightness = adafruit_pioasm.assemble(
20+
"""
21+
set pins, 0
22+
set pins, 1
23+
"""
24+
)
25+
26+
led_full_brightness = adafruit_pioasm.assemble(
27+
"""
28+
set pins, 1
29+
"""
30+
)
31+
32+
while True:
33+
sm = rp2pio.StateMachine(
34+
led_quarter_brightness, frequency=10000, first_set_pin=board.LED
35+
)
36+
time.sleep(1)
37+
sm.deinit()
38+
39+
sm = rp2pio.StateMachine(
40+
led_half_brightness, frequency=10000, first_set_pin=board.LED
41+
)
42+
time.sleep(1)
43+
sm.deinit()
44+
45+
sm = rp2pio.StateMachine(
46+
led_full_brightness, frequency=10000, first_set_pin=board.LED
47+
)
48+
time.sleep(1)
49+
sm.deinit()

examples/pico-examples/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2020 Jeff Epler, written for Adafruit Industries
3+
4+
SPDX-License-Identifier: MIT
5+
-->
6+
7+
These examples are adapted from the pio folder of the [Raspberry Pi Pico SDK Examples](https://github.com/raspberrypi/pico-examples).

examples/pico-examples/blink.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/pio_blink
6+
7+
import array
8+
import time
9+
import board
10+
import rp2pio
11+
import adafruit_pioasm
12+
13+
blink = adafruit_pioasm.assemble(
14+
"""
15+
.program blink
16+
pull block ; These two instructions take the blink duration
17+
out y, 32 ; and store it in y
18+
forever:
19+
mov x, y
20+
set pins, 1 ; Turn LED on
21+
lp1:
22+
jmp x-- lp1 ; Delay for (x + 1) cycles, x is a 32 bit number
23+
mov x, y
24+
set pins, 0 ; Turn LED off
25+
lp2:
26+
jmp x-- lp2 ; Delay for the same number of cycles again
27+
jmp forever ; Blink forever!
28+
"""
29+
)
30+
31+
32+
while True:
33+
for freq in [5, 8, 30]:
34+
with rp2pio.StateMachine(
35+
blink,
36+
frequency=125_000_000,
37+
first_set_pin=board.LED,
38+
wait_for_txstall=False,
39+
) as sm:
40+
data = array.array("I", [sm.frequency // freq])
41+
sm.write(data)
42+
time.sleep(3)
43+
time.sleep(0.5)

examples/pico-examples/hello.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/hello_pio
6+
7+
import time
8+
import board
9+
import rp2pio
10+
import adafruit_pioasm
11+
12+
hello = """
13+
.program hello
14+
loop:
15+
pull
16+
out pins, 1
17+
; This program uses a 'jmp' at the end to follow the example. However,
18+
; in a many cases (including this one!) there is no jmp needed at the end
19+
; and the default "wrap" behavior will automatically return to the "pull"
20+
; instruction at the beginning.
21+
jmp loop
22+
"""
23+
24+
assembled = adafruit_pioasm.assemble(hello)
25+
26+
sm = rp2pio.StateMachine(
27+
assembled,
28+
frequency=2000,
29+
first_out_pin=board.LED,
30+
)
31+
print("real frequency", sm.frequency)
32+
33+
while True:
34+
sm.write(bytes((1,)))
35+
time.sleep(0.5)
36+
sm.write(bytes((0,)))
37+
time.sleep(0.5)

0 commit comments

Comments
 (0)