diff --git a/README.md b/README.md index bfe051eb7039b0348f0a45625ba70147a4623f21..101465f70649917e80838617ab12154416e098a6 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,11 @@ -# rp2040-ring-pio +# Ring oscillator with RP2040 PIO, MicroPython implementation This is a demonstration of the RP2040's PIO system, which let you run several state machines independently of the CPU. There's exactly 0 overhead and the state machines run a custom assembly language with simplified instructions. In this example implementation of a [ring oscillator](https://pub.pages.cba.mit.edu/ring/), pin 1 is configured as a PIO input, and pin 2 as an output. Reading, inverting and writing is as simple as: ``` -.program ring - mov pins, !pins -.wrap +mov pins, !pins ``` Where `pins` is a meta keyword, with a different meaning in the first and second operand (output VS input, respectively). @@ -19,23 +17,10 @@ Where `pins` is a meta keyword, with a different meaning in the first and second | 133 MHz | 16.667 MHz | | 250 MHz | 25 MHz | -## Binaries +## C implementation -You can find compiled binaries in [build/](./build/). Simply drang and drop the `.uf2` file on your raspberry pi pico's storage space after putting it in reset mode (boot button + power cycle). +The original C implementation can be found [here](./c). -## Build +## MicroPython implementation -Navigate to `build/` and generate the project using cmake: - -``` -cd build/ -cmake .. -``` - -This should create a Makefile, and you can simply build the project every time you update the code by running: - -``` -make -``` - -The output is `bin`, `elf`, `hex` and `uf2` files in `build/`. +A MicroPython version with the same performance can be found [here](./py). diff --git a/CMakeLists.txt b/c/CMakeLists.txt similarity index 100% rename from CMakeLists.txt rename to c/CMakeLists.txt diff --git a/c/README.md b/c/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a0eb1f270203e42e2c6d54cff701546ce5945fc1 --- /dev/null +++ b/c/README.md @@ -0,0 +1,24 @@ +# Ring oscillator with RP2040 PIO, C implementation + +This is a demonstration of the RP2040's PIO system, which let you run several state machines independently of the CPU. There's exactly 0 overhead and the state machines run a custom assembly language with simplified instructions. + +## Binaries + +You can find compiled binaries in [build/](./build/). Simply drang and drop the `.uf2` file on your raspberry pi pico's storage space after putting it in reset mode (boot button + power cycle). + +## Build + +Navigate to `build/` and generate the project using cmake: + +``` +cd build/ +cmake .. +``` + +This should create a Makefile, and you can simply build the project every time you update the code by running: + +``` +make +``` + +The output is `bin`, `elf`, `hex` and `uf2` files in `build/`. diff --git a/build/ring_133MHz.bin b/c/build/ring_133MHz.bin similarity index 100% rename from build/ring_133MHz.bin rename to c/build/ring_133MHz.bin diff --git a/build/ring_133MHz.elf b/c/build/ring_133MHz.elf similarity index 100% rename from build/ring_133MHz.elf rename to c/build/ring_133MHz.elf diff --git a/build/ring_133MHz.uf2 b/c/build/ring_133MHz.uf2 similarity index 100% rename from build/ring_133MHz.uf2 rename to c/build/ring_133MHz.uf2 diff --git a/build/ring_250MHz.bin b/c/build/ring_250MHz.bin similarity index 100% rename from build/ring_250MHz.bin rename to c/build/ring_250MHz.bin diff --git a/build/ring_250MHz.elf b/c/build/ring_250MHz.elf similarity index 100% rename from build/ring_250MHz.elf rename to c/build/ring_250MHz.elf diff --git a/build/ring_250MHz.uf2 b/c/build/ring_250MHz.uf2 similarity index 100% rename from build/ring_250MHz.uf2 rename to c/build/ring_250MHz.uf2 diff --git a/ring.RP2040PIO.c b/c/ring.RP2040PIO.c similarity index 100% rename from ring.RP2040PIO.c rename to c/ring.RP2040PIO.c diff --git a/ring.RP2040PIO.pio b/c/ring.RP2040PIO.pio similarity index 100% rename from ring.RP2040PIO.pio rename to c/ring.RP2040PIO.pio diff --git a/py/README.md b/py/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e66bb8f7b79474faae3ff3be59ceedd2c6f8a451 --- /dev/null +++ b/py/README.md @@ -0,0 +1,50 @@ +# Ring oscillator with RP2040 PIO, C implementation + +Here is a MicroPython implementation of using the RP2040's PIO system for a [ring oscillator](https://pub.pages.cba.mit.edu/ring/). The PIO asm is a one liner: + +``` +mov pins, !pins +``` + +The rest of the code simply sets up the state machine and optionally overclocks the RP2040 to 250MHz. + +## Installing MicroPython + +You can install MicroPython from [the official website](https://micropython.org/download/), and a good setup guide can be found [here](https://wiki.seeedstudio.com/XIAO-RP2040-with-MicroPython/). + +## Code + +The code can be found [here](./ring.RP2040PIO.py), but it's short enough to paste here: + +```py +# +# ring.RP2040PIO.py +# RP2040 ring oscillator with PIO +# connect P1 and P2 +# +# Quentin Bolsee 12/27/22 +# +# This work may be reproduced, modified, distributed, +# performed, and displayed for any purpose, but must +# acknowledge this project. Copyright is retained and +# must be preserved. The work is provided as is; no +# warranty is provided, and users accept all liability. +# + +from machine import Pin, freq +import rp2 + + +@rp2.asm_pio(out_init=rp2.PIO.OUT_LOW) +def ring(): + mov(pins, invert(pins)) + + +# clock speed +# freq(133000000) +freq(250000000) +pin_in = Pin(1, Pin.IN) +pin_out = Pin(2, Pin.OUT) +sm = rp2.StateMachine(0, ring, in_base=pin_in, out_base=pin_out) +sm.active(1) +``` diff --git a/py/ring.RP2040PIO.py b/py/ring.RP2040PIO.py new file mode 100644 index 0000000000000000000000000000000000000000..e2176b476609e4c4f7586471cdd528b53bd2de7e --- /dev/null +++ b/py/ring.RP2040PIO.py @@ -0,0 +1,30 @@ +# +# ring.RP2040PIO.py +# RP2040 ring oscillator with PIO +# connect P1 and P2 +# +# Quentin Bolsee 12/27/22 +# +# This work may be reproduced, modified, distributed, +# performed, and displayed for any purpose, but must +# acknowledge this project. Copyright is retained and +# must be preserved. The work is provided as is; no +# warranty is provided, and users accept all liability. +# + +from machine import Pin, freq +import rp2 + + +@rp2.asm_pio(out_init=rp2.PIO.OUT_LOW) +def ring(): + mov(pins, invert(pins)) + + +# clock speed +# freq(133000000) +freq(250000000) +pin_in = Pin(1, Pin.IN) +pin_out = Pin(2, Pin.OUT) +sm = rp2.StateMachine(0, ring, in_base=pin_in, out_base=pin_out) +sm.active(1)