wip rs pico

This commit is contained in:
Daniel Bulant 2026-05-03 15:22:22 +02:00
parent 792d46beb3
commit d5e033bd70
No known key found for this signature in database
13 changed files with 2382 additions and 0 deletions

9
pico/.cargo/config.toml Normal file
View file

@ -0,0 +1,9 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# runner = "probe-rs run --chip RP2040"
runner = "sudo picotool load -u -v -x -t elf"
[build]
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
[env]
DEFMT_LOG = "debug"

1
pico/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target

1981
pico/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

45
pico/Cargo.toml Normal file
View file

@ -0,0 +1,45 @@
[package]
name = "pico"
version = "0.1.0"
edition = "2024"
[dependencies]
hd44780-driver = { git = "https://github.com/JohnDoneth/hd44780-driver.git", rev = "9009f2c24771ba0a20f8f7534471c9869188f76c"}
embedded-hal-compat = "0.13.0"
embassy-usb-logger = "0.5.1"
log = "0.4"
liquid_crystal = "0.2"
ag-lcd="0.3"
embassy-executor = { version = "0.10.0", features = [
"platform-cortex-m",
"executor-thread",
"executor-interrupt",
"defmt",
] }
embassy-rp = { version = "0.10.0", features = [
"defmt",
"unstable-pac",
"time-driver",
"critical-section-impl",
"rp2040",
] }
embassy-time = { version = "0.5.1", features = [
"defmt",
"defmt-timestamp-uptime",
] }
cortex-m = { version = "0.7.7", features = ["inline-asm"] }
cortex-m-rt = "0.7.5"
critical-section = "1.2.0"
static_cell = "2.1.1"
portable-atomic = { version = "1.13.1", features = ["critical-section"] }
defmt = "1.0.1"
defmt-rtt = "1.1.0"
panic-probe = { version = "1.0.0", features = ["print-defmt"] }
cyw43 = { version = "0.7.0", features = ["defmt", "firmware-logs"] }
cyw43-pio = { version = "0.10.0", features = ["defmt"] }

36
pico/build.rs Normal file
View file

@ -0,0 +1,36 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
// By default, Cargo will re-run a build script whenever
// any file in the project changes. By specifying `memory.x`
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
}

26
pico/elf2uf2.nix Normal file
View file

@ -0,0 +1,26 @@
{ lib, stdenv, rustPlatform, fetchFromGitHub, pkg-config, libudev-zero }:
rustPlatform.buildRustPackage rec {
pname = "elf2uf2-rs";
version = "2038e9a199101ee8a16d046a87136be2a607001d";
src = fetchFromGitHub {
owner = "JoNil";
repo = pname;
rev = "${version}";
# sha256 = "sha256-CHuTpnAnD4I5PY47sceSxfjGdPZznXWfARE4YBfYoIg=";
# sha256 = "sha256-tvjncEmVHFurhRtWN6m8JT0XIgrti4QTnZ2XioBtqxo=";
sha256 = "sha256-CHuTpnAnD4I5PY47sceSxfjGdPZznXWfARE4YBfYoIg=";
};
nativeBuildInputs = [
pkg-config
];
buildInputs = [
libudev-zero
];
# cargoHash = "sha256-OefAKK5rvh4HSHd26Pac4BSbcNO3ntqBReYepulV8Dk=";
cargoHash = "sha256-tvjncEmVHFurhRtWN6m8JT0XIgrti4QTnZ2XioBtqxo=";
}

BIN
pico/firmware/43439A0.bin Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

61
pico/flake.lock Normal file
View file

@ -0,0 +1,61 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1777383249,
"narHash": "sha256-b6T90GXUr21iIu6Aw+KGy5uSiB/cVT/UecId1YSESys=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "61a7520db583d9b41be602b4c1e1b1b1b1faa1f4",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1744536153,
"narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1777346187,
"narHash": "sha256-oVxyGjpiIsrXhWTJVUOs38fZQkLjd0nZGOY9K7Kfot8=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "146e7bf7569b8288f24d41d806b9f584f7cfd5b5",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

40
pico/flake.nix Normal file
View file

@ -0,0 +1,40 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs =
{
self,
rust-overlay,
nixpkgs,
}:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
system = "x86_64-linux";
inherit overlays;
};
in
{
packages.x86_64-linux.elf2uf2-rs = pkgs.callPackage ./elf2uf2.nix { };
devShell.x86_64-linux = pkgs.mkShell {
buildInputs = [
(pkgs.rust-bin.selectLatestNightlyWith (
toolchain:
toolchain.default.override {
targets = [ "thumbv6m-none-eabi" ];
extensions = [ "rust-src" ];
}
))
pkgs.rust-analyzer
pkgs.flip-link
pkgs.probe-rs-tools
self.packages.x86_64-linux.elf2uf2-rs
pkgs.rustfmt
pkgs.picotool
];
};
};
}

17
pico/memory.x Normal file
View file

@ -0,0 +1,17 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
/* Pick one of the two options for RAM layout */
/* OPTION A: Use all RAM banks as one big block */
/* Reasonable, unless you are doing something */
/* really particular with DMA or other concurrent */
/* access that would benefit from striping */
RAM : ORIGIN = 0x20000000, LENGTH = 264K
/* OPTION B: Keep the unstriped sections separate */
/* RAM: ORIGIN = 0x20000000, LENGTH = 256K */
/* SCRATCH_A: ORIGIN = 0x20040000, LENGTH = 4K */
/* SCRATCH_B: ORIGIN = 0x20041000, LENGTH = 4K */
}

166
pico/src/main.rs Normal file
View file

@ -0,0 +1,166 @@
#![no_std]
#![no_main]
use ag_lcd::{Blink, Cursor, Display, LcdDisplay};
use cyw43::aligned_bytes;
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
use defmt::*;
use embassy_executor::Spawner;
use embassy_rp::gpio::{Level, Output};
use embassy_rp::peripherals::{DMA_CH0, PIO0};
use embassy_rp::pio::{InterruptHandler, Pio};
use embassy_rp::{bind_interrupts, dma};
use embassy_rp::{peripherals::USB, usb};
use embassy_time::{Delay, Duration, Timer};
use hd44780_driver::HD44780;
use hd44780_driver::bus::FourBitBusPins;
use hd44780_driver::memory_map::MemoryMap1602;
use hd44780_driver::setup::DisplayOptions4Bit;
use liquid_crystal::BusBits::Bus4Bits;
use liquid_crystal::SendType::Text;
use liquid_crystal::{LCD16X2, LiquidCrystal, Parallel, lcd_dummy};
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => InterruptHandler<PIO0>;
DMA_IRQ_0 => dma::InterruptHandler<DMA_CH0>;
USBCTRL_IRQ => usb::InterruptHandler<USB>;
});
#[embassy_executor::task]
async fn cyw43_task(
runner: cyw43::Runner<'static, cyw43::SpiBus<Output<'static>, PioSpi<'static, PIO0, 0>>>,
) -> ! {
runner.run().await
}
#[embassy_executor::task]
async fn logger_task(usb: embassy_rp::Peri<'static, embassy_rp::peripherals::USB>) {
let driver = embassy_rp::usb::Driver::new(usb, Irqs);
embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver);
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let fw = aligned_bytes!("../firmware/43439A0.bin");
let clm = aligned_bytes!("../firmware/43439A0_clm.bin");
let nvram = aligned_bytes!("../firmware/nvram_rp2040.bin");
// To make flashing faster for development, you may want to flash the firmwares independently
// at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
// probe-rs download ../../cyw43-firmware/43439A0.bin --binary-format bin --chip RP2040 --base-address 0x10100000
// probe-rs download ../../cyw43-firmware/43439A0_clm.bin --binary-format bin --chip RP2040 --base-address 0x10140000
//let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 230321) };
//let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
let pwr = Output::new(p.PIN_23, Level::Low);
let cs = Output::new(p.PIN_25, Level::High);
let mut pio = Pio::new(p.PIO0, Irqs);
let spi = PioSpi::new(
&mut pio.common,
pio.sm0,
DEFAULT_CLOCK_DIVIDER,
pio.irq0,
cs,
p.PIN_24,
p.PIN_29,
dma::Channel::new(p.DMA_CH0, Irqs),
);
static STATE: StaticCell<cyw43::State> = StaticCell::new();
let state = STATE.init(cyw43::State::new());
let (_net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw, nvram).await;
spawner.spawn(unwrap!(cyw43_task(runner)));
spawner.spawn(unwrap!(logger_task(p.USB)));
control.init(clm).await;
control
.set_power_management(cyw43::PowerManagementMode::PowerSave)
.await;
let edelay = &mut Delay;
let mut lcd: LcdDisplay<_, _> = LcdDisplay::new(
Output::new(p.PIN_18, Level::Low),
Output::new(p.PIN_19, Level::Low),
edelay,
)
.with_half_bus(
Output::new(p.PIN_13, Level::Low),
Output::new(p.PIN_14, Level::Low),
Output::new(p.PIN_15, Level::Low),
Output::new(p.PIN_16, Level::Low),
)
.with_display(Display::On)
.with_blink(Blink::On)
.with_cursor(Cursor::On)
.build();
// let mut lcd_interface = Parallel::new(
// Output::new(p.PIN_14, Level::Low),
// Output::new(p.PIN_15, Level::Low),
// Output::new(p.PIN_16, Level::Low),
// Output::new(p.PIN_17, Level::Low),
// Output::new(p.PIN_19, Level::Low),
// Output::new(p.PIN_20, Level::Low),
// lcd_dummy,
// );
// let mut lcd = LiquidCrystal::new(&mut lcd_interface, Bus4Bits, LCD16X2);
// let mut lcd = HD44780::new(
// DisplayOptions4Bit::new(MemoryMap1602::new()).with_pins(FourBitBusPins {
// rs: Output::new(p.PIN_19, Level::Low),
// en: Output::new(p.PIN_20, Level::Low),
// d4: Output::new(p.PIN_14, Level::Low),
// d5: Output::new(p.PIN_15, Level::Low),
// d6: Output::new(p.PIN_16, Level::Low),
// d7: Output::new(p.PIN_17, Level::Low),
// }),
// edelay,
// )
// .unwrap();
// lcd.begin(edelay);
lcd.set_cursor(Cursor::Off);
lcd.set_blink(Blink::Off);
// // Unshift display and set cursor to 0
// lcd.reset(edelay).unwrap();
// // Clear existing characters
// lcd.clear(edelay).unwrap();
// // Display the following string
// lcd.write_str("Hello, world!", edelay).unwrap();
// // Move the cursor to the second line
// lcd.set_cursor_xy((0, 1), edelay).unwrap();
// // Display the following string on the second line
// lcd.write_str("I'm on line 2!", edelay).unwrap();
//
let mut led = Output::new(p.PIN_1, Level::Low);
let delay = Duration::from_secs(1);
loop {
log::info!("led on!");
control.gpio_set(0, true).await;
led.set_high();
Timer::after(delay).await;
lcd.print("Test message!");
// lcd.write(edelay, Text("hello World!"));
log::info!("led off!");
control.gpio_set(0, false).await;
led.set_low();
Timer::after(delay).await;
lcd.print("Test message2!");
// lcd.write(edelay, Text("hello World2!"));
}
}