- Notifications
You must be signed in to change notification settings - Fork24
Support crate for Raspberry Pi's PIO architecture.
License
rp-rs/pio-rs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Support for the Raspberry Silicon RP2040'sPIO State Machines.
Seehttps://www.raspberrypi.com/news/what-is-pio/. You can also read the PIOsection in the (very well written) RP2040 datasheet:https://datasheets.raspberrypi.org/rp2040/rp2040-datasheet.pdf.
PIO programs must be compiled from PIO Assembly Language into a special PIOmachine code. This machine code is then stored in your C or Rust program, andprogram is copied to the PIO hardware at the relevant point in time.
Raspberry Pi provide a PIO assembler called pioasm, and it lives athttps://github.com/raspberrypi/pico-sdk/tree/master/tools/pioasm. This is anexcellent choice if you want use the Raspberry Pi Pico SDK(https://github.com/raspberrypi/pico-sdk) to write C or C++ programs for yourRP2040.
The pio-rs project provides an alternative implementation of pioasm. The mainbenefits are:
- It's easier to integrate into an Embedded Rust program for the RP2040 than
pioasm
- It provides a
pio_asm!
macro that can assemble PIO at compile-time. - The compiler itself can be included in your Embedded Rust program as a library,so you can compile PIO code on the RP2040, at run-time!
- Writing an assembler was a good way to test our understanding of thespecification.
- It's written in Rust :)
If you want to write a program in Rust that uses the RP2040's PIO block, thereare three ways to use pio-rs:
There is a macro calledpio!
which allows you to place PIO assembly languagesource into your Rust program. This source code is assembled into a PIO programat compile time.
YourCargo.toml
file should include:
[dependencies]pio ="0.2"
Your Rust program should contain your PIO program, as follows with PIO asm directly in the file:
let program_with_defines = pio::pio_asm!("set pindirs, 1",".wrap_target","set pins, 0 [31]","set pins, 1 [31]",".wrap", options(max_program_size =32)// Optional, defaults to 32);let program = program_with_defines.program;
Or you can assemble a stand-alone PIO file from disk:
let program_with_defines = pio::pio_file!("./tests/test.pio", select_program("test"),// Optional if only one program in the file options(max_program_size =32)// Optional, defaults to 32);let program = program_with_defines.program;
The syntax should be the same as supported by the official pioasm tool.
You can callpio::Assembler::new()
and construct a PIO program usingthe 'builder pattern' - effectively you are compiling a PIO program atrun-time on the RP2040 itself!
// Define some simple PIO program.constMAX_DELAY:u8 =31;letmut assembler = pio::Assembler::<32>::new();letmut wrap_target = assembler.label();letmut wrap_source = assembler.label();// Set pin as Outassembler.set(pio::SetDestination::PINDIRS,1);// Define begin of program loopassembler.bind(&mut wrap_target);// Set pin lowassembler.set_with_delay(pio::SetDestination::PINS,0,MAX_DELAY);// Set pin highassembler.set_with_delay(pio::SetDestination::PINS,1,MAX_DELAY);// Define end of program loopassembler.bind(&mut wrap_source);// The labels wrap_target and wrap_source, as set above,// define a loop which is executed repeatedly by the PIO// state machine.let program = assembler.assemble_with_wrap(wrap_source, wrap_target);
Each line startingassembler.
adds a new line to the program. The completedprogram can be passed to the PIO driver in the Rust-languageRP2040HAL.
This crate is just the PIO assembler. If you want to see some fully-featuredPIO examples integrated with Embedded Rust on the RP2040, check out therp-hal examples.
NOTE This tool is under active development. As such, it is likely to remainvolatile until a 1.0.0 release.
See theopen issues for a list ofproposed features (and known issues).
Contributions are what make the open source community such an amazing place tobe learn, inspire, and create. Any contributions you make aregreatlyappreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. SeeLICENSE
for more information.
Project Link:https://github.com/rp-rs/pio-rs/issuesMatrix:#rp-rs:matrix.org
About
Support crate for Raspberry Pi's PIO architecture.