From 3a64ca716b5861eed2044faa7bc6c379f4e183a8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 24 Feb 2021 10:09:06 -0600 Subject: [PATCH 01/12] hello pio example --- examples/pico-examples/hello.py | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/pico-examples/hello.py diff --git a/examples/pico-examples/hello.py b/examples/pico-examples/hello.py new file mode 100644 index 0000000..7b4cd9a --- /dev/null +++ b/examples/pico-examples/hello.py @@ -0,0 +1,35 @@ +# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries +# +# SPDX-License-Identifier: MIT +# +# Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/hello_pio + +import adafruit_pioasm +import board +import rp2pio +import time + +hello = """ +.program hello +loop: + pull + out pins, 1 + jmp loop +""" + +assembled = adafruit_pioasm.assemble(hello) + +sm = rp2pio.StateMachine( + assembled, + frequency=80, + init=adafruit_pioasm.assemble("set pindirs 1"), + first_set_pin=board.LED, + first_out_pin=board.LED, +) +print("real frequency", sm.frequency) + +while True: + sm.write(bytes((1,))) + time.sleep(0.5) + sm.write(bytes((0,))) + time.sleep(0.5) From 72c2324ff52433996f0ac9116285359ddfc9b01d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 08:44:11 -0600 Subject: [PATCH 02/12] Adapt first example from the Micropython book --- examples/getting-started/led_brightness.py | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/getting-started/led_brightness.py diff --git a/examples/getting-started/led_brightness.py b/examples/getting-started/led_brightness.py new file mode 100644 index 0000000..4294d0f --- /dev/null +++ b/examples/getting-started/led_brightness.py @@ -0,0 +1,40 @@ +# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries +# +# SPDX-License-Identifier: MIT +# +# Adapted from the an example in Appendix C of file:///tmp/mozilla_jepler0/RPi_PiPico_Digital_v10.pdf + +import adafruit_pioasm +import board +import rp2pio +import time + +led_quarter_brightness = adafruit_pioasm.assemble(""" + set pins, 0 [2] + set pins, 1 +""") + +led_half_brightness = adafruit_pioasm.assemble(""" + set pins, 0 + set pins, 1 +""") + +led_full_brightness = adafruit_pioasm.assemble(""" + set pins, 1 +""") + +while True: + sm = rp2pio.StateMachine(led_quarter_brightness, + frequency=10000, first_set_pin=board.LED) + time.sleep(1) + sm.deinit() + + sm = rp2pio.StateMachine(led_half_brightness, + frequency=10000, first_set_pin=board.LED) + time.sleep(1) + sm.deinit() + + sm = rp2pio.StateMachine(led_full_brightness, + frequency=10000, first_set_pin=board.LED) + time.sleep(1) + sm.deinit() From ebdf4e3d6b826d10960b23377e186ce20b8ac5f1 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 09:18:57 -0600 Subject: [PATCH 03/12] assemble: Diagnose duplicate labels Closes #10 --- adafruit_pioasm.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index 22bf47b..2ab37fd 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -55,7 +55,10 @@ def assemble(text_program): elif line.startswith(".side_set"): sideset_count = int(line.split()[1]) elif line.endswith(":"): - labels[line[:-1]] = len(instructions) + label = line[:-1] + if label in labels: + raise SyntaxError(f"Duplicate label {repr(label)}") + labels[label] = len(instructions) elif line: # Only add as an instruction if the line isn't empty instructions.append(line) From 3ad9fae9f76bb4df82c6e2cd2c02c7361459de37 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 09:19:08 -0600 Subject: [PATCH 04/12] assemble: better diagnose jump to undefined label --- adafruit_pioasm.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index 2ab37fd..336c5c4 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -88,10 +88,13 @@ def assemble(text_program): elif instruction[0] == "jmp": # instr delay cnd addr assembled.append(0b000_00000_000_00000) - if instruction[-1] in labels: - assembled[-1] |= labels[instruction[-1]] + target = instruction[-1] + if target[:1] in "0123456789": + assembled[-1] |= int(target) + elif instruction[-1] in labels: + assembled[-1] |= labels[target] else: - assembled[-1] |= int(instruction[-1]) + raise SyntaxError(f"Invalid jmp target {repr(target)}") if len(instruction) > 2: assembled[-1] |= CONDITIONS.index(instruction[1]) << 5 From 0d56e195f42a9714712161e1e82f6fa047cf552f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 10:01:23 -0600 Subject: [PATCH 05/12] run black --- examples/getting-started/led_brightness.py | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/examples/getting-started/led_brightness.py b/examples/getting-started/led_brightness.py index 4294d0f..455510e 100644 --- a/examples/getting-started/led_brightness.py +++ b/examples/getting-started/led_brightness.py @@ -9,32 +9,41 @@ import rp2pio import time -led_quarter_brightness = adafruit_pioasm.assemble(""" +led_quarter_brightness = adafruit_pioasm.assemble( + """ set pins, 0 [2] set pins, 1 -""") +""" +) -led_half_brightness = adafruit_pioasm.assemble(""" +led_half_brightness = adafruit_pioasm.assemble( + """ set pins, 0 set pins, 1 -""") +""" +) -led_full_brightness = adafruit_pioasm.assemble(""" +led_full_brightness = adafruit_pioasm.assemble( + """ set pins, 1 -""") +""" +) while True: - sm = rp2pio.StateMachine(led_quarter_brightness, - frequency=10000, first_set_pin=board.LED) + sm = rp2pio.StateMachine( + led_quarter_brightness, frequency=10000, first_set_pin=board.LED + ) time.sleep(1) sm.deinit() - sm = rp2pio.StateMachine(led_half_brightness, - frequency=10000, first_set_pin=board.LED) + sm = rp2pio.StateMachine( + led_half_brightness, frequency=10000, first_set_pin=board.LED + ) time.sleep(1) sm.deinit() - sm = rp2pio.StateMachine(led_full_brightness, - frequency=10000, first_set_pin=board.LED) + sm = rp2pio.StateMachine( + led_full_brightness, frequency=10000, first_set_pin=board.LED + ) time.sleep(1) sm.deinit() From 0635463a786b6c994cd5f3e77fa8b63283d3b325 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 10:03:57 -0600 Subject: [PATCH 06/12] disable a pylint diagnostic --- adafruit_pioasm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index 336c5c4..e225a7d 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -31,7 +31,7 @@ def assemble(text_program): """Converts pioasm text to encoded instruction bytes""" - # pylint: disable=too-many-branches,too-many-statements + # pylint: disable=too-many-branches,too-many-statements,too-many-locals assembled = [] program_name = None labels = {} From 9cf38ebea5c0d82e02409f5766f58a551b169117 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 10:09:53 -0600 Subject: [PATCH 07/12] Pylint changes --- examples/getting-started/led_brightness.py | 6 +++--- examples/pico-examples/hello.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/getting-started/led_brightness.py b/examples/getting-started/led_brightness.py index 455510e..71ba925 100644 --- a/examples/getting-started/led_brightness.py +++ b/examples/getting-started/led_brightness.py @@ -2,12 +2,12 @@ # # SPDX-License-Identifier: MIT # -# Adapted from the an example in Appendix C of file:///tmp/mozilla_jepler0/RPi_PiPico_Digital_v10.pdf +# Adapted from the an example in Appendix C of RPi_PiPico_Digital_v10.pdf -import adafruit_pioasm +import time import board import rp2pio -import time +import adafruit_pioasm led_quarter_brightness = adafruit_pioasm.assemble( """ diff --git a/examples/pico-examples/hello.py b/examples/pico-examples/hello.py index 7b4cd9a..2ca40d0 100644 --- a/examples/pico-examples/hello.py +++ b/examples/pico-examples/hello.py @@ -4,10 +4,10 @@ # # Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/hello_pio -import adafruit_pioasm +import time import board import rp2pio -import time +import adafruit_pioasm hello = """ .program hello From 8db157b7bb437facc0a1a292639bcca31f2c53e5 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 15:03:14 -0600 Subject: [PATCH 08/12] Explain where these examples come from --- examples/getting-started/README.md | 7 +++++++ examples/pico-examples/README.md | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 examples/getting-started/README.md create mode 100644 examples/pico-examples/README.md diff --git a/examples/getting-started/README.md b/examples/getting-started/README.md new file mode 100644 index 0000000..639ed26 --- /dev/null +++ b/examples/getting-started/README.md @@ -0,0 +1,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. diff --git a/examples/pico-examples/README.md b/examples/pico-examples/README.md new file mode 100644 index 0000000..e6f42e1 --- /dev/null +++ b/examples/pico-examples/README.md @@ -0,0 +1,7 @@ + + +These examples are adapted from the pio folder of the [Raspberry Pi Pico SDK Examples](https://github.com/raspberrypi/pico-examples). From b8e8c9279eaa4bae91a48ddf1fc6875f58062859 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 15:05:28 -0600 Subject: [PATCH 09/12] Remove unneeded 'set pindirs', fix minimum frequency --- examples/pico-examples/hello.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/pico-examples/hello.py b/examples/pico-examples/hello.py index 2ca40d0..71bb138 100644 --- a/examples/pico-examples/hello.py +++ b/examples/pico-examples/hello.py @@ -21,9 +21,7 @@ sm = rp2pio.StateMachine( assembled, - frequency=80, - init=adafruit_pioasm.assemble("set pindirs 1"), - first_set_pin=board.LED, + frequency=2000, first_out_pin=board.LED, ) print("real frequency", sm.frequency) From d727e55a9da0d70cfe9d5394673436590c49446e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 15:05:40 -0600 Subject: [PATCH 10/12] Explain why a needless jmp is here --- examples/pico-examples/hello.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/pico-examples/hello.py b/examples/pico-examples/hello.py index 71bb138..ade87ad 100644 --- a/examples/pico-examples/hello.py +++ b/examples/pico-examples/hello.py @@ -14,6 +14,10 @@ loop: pull out pins, 1 +; This program uses a 'jmp' at the end to follow the example. However, +; in a many cases (including this one!) there is no jmp needed at the end +; and the default "wrap" behavior will automatically return to the "pull" +; instruction at the beginning. jmp loop """ From b5b8e35798e4349ed6cf750a9c5ddeaf5544c907 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 16:04:32 -0600 Subject: [PATCH 11/12] add blink example --- examples/pico-examples/blink.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/pico-examples/blink.py diff --git a/examples/pico-examples/blink.py b/examples/pico-examples/blink.py new file mode 100644 index 0000000..be8955b --- /dev/null +++ b/examples/pico-examples/blink.py @@ -0,0 +1,43 @@ +# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries +# +# SPDX-License-Identifier: MIT +# +# Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/pio_blink + +import adafruit_pioasm +import board +import array +import rp2pio +import time + +blink = adafruit_pioasm.assemble( + """ +.program blink + pull block ; These two instructions take the blink duration + out y, 32 ; and store it in y +forever: + mov x, y + set pins, 1 ; Turn LED on +lp1: + jmp x-- lp1 ; Delay for (x + 1) cycles, x is a 32 bit number + mov x, y + set pins, 0 ; Turn LED off +lp2: + jmp x-- lp2 ; Delay for the same number of cycles again + jmp forever ; Blink forever! +""" +) + + +while True: + for freq in [5, 8, 30]: + with rp2pio.StateMachine( + blink, + frequency=125_000_000, + first_set_pin=board.LED, + wait_for_txstall=False, + ) as sm: + data = array.array("I", [sm.frequency // freq]) + sm.write(data) + time.sleep(3) + time.sleep(0.5) From 9399b4496aae01a8f89b2af1062f6114aa84692a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 25 Feb 2021 16:08:42 -0600 Subject: [PATCH 12/12] pylint --- examples/pico-examples/blink.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/pico-examples/blink.py b/examples/pico-examples/blink.py index be8955b..3d2fc97 100644 --- a/examples/pico-examples/blink.py +++ b/examples/pico-examples/blink.py @@ -4,11 +4,11 @@ # # Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/pio_blink -import adafruit_pioasm -import board import array -import rp2pio import time +import board +import rp2pio +import adafruit_pioasm blink = adafruit_pioasm.assemble( """