Compare commits

...

No commits in common. "92576855536436318aecb0dc99852dbc153e3253" and "cb3df1cade1ffad774d618a7c7cbde9236d19bff" have entirely different histories.

12 changed files with 1827 additions and 1 deletions

8
.cargo/config.toml Normal file
View file

@ -0,0 +1,8 @@
[build]
target = "thumbv6m-none-eabi"
[env]
DEFMT_LOG = "debug"
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "elf2uf2-rs -d"

1482
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

22
Cargo.toml Normal file
View file

@ -0,0 +1,22 @@
[package]
name = "picow"
version = "0.1.0"
edition = "2021"
[dependencies]
cortex-m-rt = "*"
cyw43 = "*"
cyw43-pio = "*"
defmt = "*"
defmt-rtt = "*"
embassy-executor = { version = "*", features = ["arch-cortex-m", "defmt", "executor-interrupt", "executor-thread", "integrated-timers"] }
embassy-rp = { version = "*", features = ["critical-section-impl", "defmt", "time-driver"] }
embassy-time = { version = "*", features = ["defmt", "defmt-timestamp-uptime"] }
panic-probe = { version = "*", features = ["print-defmt"] }
portable-atomic = { version = "*", features = ["critical-section"] }
static_cell = "*"
[profile.release]
debug = 2
lto = true
opt-level = "z"

View file

@ -1,2 +1,12 @@
# picow
# Picow
My first attempt on embedded system.
## Hardware
[Raspberry Pi Pico WH](https://www.raspberrypi.com/products/raspberry-pi-pico)
## Software
Rust [Embassy](https://embassy.dev/)

36
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");
}

130
flake.lock generated Normal file
View file

@ -0,0 +1,130 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1728344376,
"narHash": "sha256-lxTce2XE6mfJH8Zk6yBbqsbu9/jpwdymbSH5cCbiVOA=",
"owner": "ipetkov",
"repo": "crane",
"rev": "fd86b78f5f35f712c72147427b1eb81a9bd55d0b",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1728628307,
"narHash": "sha256-GRMRHZyU+R0RqKPFFgi7BBMDIRFPnHaAhOIxlqyvbZQ=",
"owner": "nix-community",
"repo": "fenix",
"rev": "b0a014d5b9dba793ebc205bcf12a93b5f6a4c66c",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1726560853,
"narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1728492678,
"narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1728538411,
"narHash": "sha256-f0SBJz1eZ2yOuKUr5CA9BHULGXVSn6miBuUWdTyhUhU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b69de56fac8c2b6f8fd27f2eca01dcda8e0a4221",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"crane": "crane",
"fenix": "fenix",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs_2"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1728505432,
"narHash": "sha256-QFPMazeiGLo7AGy4RREmTgko0Quch/toMVKhGUjDEeo=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "0fb804acb375b02a3beeaceeb75b71969ef37b15",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

33
flake.nix Normal file
View file

@ -0,0 +1,33 @@
{
inputs = {
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
fenix.url = "github:nix-community/fenix";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = {
self,
crane,
fenix,
flake-utils,
nixpkgs,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
fenixPkgs = fenix.packages.${system};
fenixToolchain = fenixPkgs.fromToolchainFile {
dir = ./.;
sha256 = "sha256-HAFn+jo7K/dwbCKRHNXQU+x9b+8LJ8xlQGL/tE0rNlE=";
};
craneLib = (crane.mkLib pkgs).overrideToolchain fenixToolchain;
in {
devShells.default = craneLib.devShell {
packages = with pkgs; [
elf2uf2-rs
fenixPkgs.rust-analyzer
];
RUST_SRC_PATH = "${fenixPkgs.complete.rust-src}/lib/rustlib/src/rust/library";
};
});
}

17
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 */
}

4
rust-toolchain.toml Normal file
View file

@ -0,0 +1,4 @@
[toolchain]
channel = "nightly"
targets = ["thumbv6m-none-eabi"]
profile = "complete"

BIN
src/firmware/43439A0.bin Normal file

Binary file not shown.

Binary file not shown.

84
src/main.rs Normal file
View file

@ -0,0 +1,84 @@
#![no_std]
#![no_main]
use cyw43_pio::PioSpi;
use defmt::*;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_rp::{
bind_interrupts,
gpio::{Level, Output},
peripherals::{DMA_CH0, PIO0},
pio::{InterruptHandler, Pio},
};
use embassy_time::Timer;
use panic_probe as _;
use static_cell::StaticCell;
// bind interrupt request to handler
bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => InterruptHandler<PIO0>;
});
// network task
#[embassy_executor::task]
async fn cyw43_task(
runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>,
) -> ! {
runner.run().await
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
// hardware abstraction layer
let hal = embassy_rp::init(Default::default());
// wireless firmware binary
let fw = include_bytes!("./firmware/43439A0.bin");
// wireless country locale matrix
let clm = include_bytes!("./firmware/43439A0_clm.bin");
// wireless power on signal
let pwr = Output::new(hal.PIN_23, Level::Low);
// wireless serial peripheral interface chip select
let cs = Output::new(hal.PIN_25, Level::High);
// programmed input/output
let mut pio = Pio::new(hal.PIO0, Irqs);
// programmed serial peripheral interface
let spi = PioSpi::new(
&mut pio.common,
pio.sm0,
pio.irq0,
cs,
hal.PIN_24,
hal.PIN_29,
hal.DMA_CH0,
);
// wireless driver state
static STATE: StaticCell<cyw43::State> = StaticCell::new();
let state = STATE.init(cyw43::State::new());
// spawn network task
let (_, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
unwrap!(spawner.spawn(cyw43_task(runner)));
// initialize wireless
control.init(clm).await;
control
.set_power_management(cyw43::PowerManagementMode::PowerSave)
.await;
// time to blink
let mut led = false;
loop {
info!("Blink!");
led = !led;
control.gpio_set(0, led).await;
Timer::after_secs(1).await;
}
}