Skip to content
Snippets Groups Projects
Commit 1f54462a authored by Quentin Bolsee's avatar Quentin Bolsee
Browse files

py implementation

parent 9e1b9171
No related branches found
No related tags found
No related merge requests found
# 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).
File moved
# 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/`.
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
# 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)
```
#
# 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment