Initial commit
This commit is contained in:
commit
7f42dac8bf
9 changed files with 2716 additions and 0 deletions
8
.cargo/config.toml
Normal file
8
.cargo/config.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
runner = "probe-rs run --chip RP2040"
|
||||
|
||||
[build]
|
||||
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||
|
||||
[env]
|
||||
DEFMT_LOG = "debug"
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "embassy"]
|
||||
path = embassy
|
||||
url = https://github.com/embassy-rs/embassy.git
|
2362
Cargo.lock
generated
Normal file
2362
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
86
Cargo.toml
Normal file
86
Cargo.toml
Normal file
|
@ -0,0 +1,86 @@
|
|||
[package]
|
||||
edition = "2024"
|
||||
name = "oolong-water"
|
||||
version = "0.1.0"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
|
||||
[dependencies]
|
||||
embassy-time = { version = "0.4.0", features = [
|
||||
"defmt",
|
||||
"defmt-timestamp-uptime",
|
||||
] }
|
||||
embassy-rp = { version = "0.4.0", features = [
|
||||
"defmt",
|
||||
"unstable-pac",
|
||||
"time-driver",
|
||||
"critical-section-impl",
|
||||
"rp2040",
|
||||
"intrinsics",
|
||||
] }
|
||||
embassy-executor = { version = "0.7.0", features = [
|
||||
"arch-cortex-m",
|
||||
"executor-thread",
|
||||
"executor-interrupt",
|
||||
"defmt",
|
||||
"task-arena-size-8192",
|
||||
] }
|
||||
embassy-sync = { version = "0.6.2", features = ["defmt"] }
|
||||
# embassy-embedded-hal = { version = "0.3.0", path = "../../embassy-embedded-hal", features = ["defmt"] }
|
||||
# embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||
# embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] }
|
||||
# embassy-net = { version = "0.7.0", path = "../../embassy-net", features = ["defmt", "icmp", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] }
|
||||
# embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] }
|
||||
embassy-futures = "0.1.1"
|
||||
# embassy-usb-logger = { version = "0.4.0", path = "../../embassy-usb-logger" }
|
||||
cyw43 = { version = "0.3.0", features = ["defmt", "firmware-logs"] }
|
||||
cyw43-pio = { version = "0.4.0", features = ["defmt"] }
|
||||
|
||||
defmt = "0.3"
|
||||
defmt-rtt = "0.4"
|
||||
fixed = "1.23.1"
|
||||
fixed-macro = "1.2"
|
||||
|
||||
# for web request example
|
||||
reqwless = { version = "0.13.0", features = ["defmt"] }
|
||||
serde = { version = "1.0.203", default-features = false, features = ["derive"] }
|
||||
serde-json-core = "0.5.1"
|
||||
|
||||
# for assign resources example
|
||||
# assign-resources = { git = "https://github.com/adamgreig/assign-resources", rev = "bd22cb7a92031fb16f74a5da42469d466c33383e" }
|
||||
|
||||
#cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
|
||||
cortex-m = { version = "0.7.6", features = ["inline-asm"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
critical-section = "1.1"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
display-interface-spi = "0.5.0"
|
||||
embedded-graphics = "0.8.1"
|
||||
mipidsi = "0.8.0"
|
||||
display-interface = "0.5.0"
|
||||
byte-slice-cast = { version = "1.2.0", default-features = false }
|
||||
smart-leds = "0.4.0"
|
||||
heapless = "0.8"
|
||||
usbd-hid = "0.8.1"
|
||||
rand_core = "0.6.4"
|
||||
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
|
||||
embedded-hal-async = "1.0"
|
||||
embedded-hal-bus = { version = "0.1", features = ["async"] }
|
||||
embedded-io-async = { version = "0.6.1", features = ["defmt-03"] }
|
||||
embedded-storage = { version = "0.3" }
|
||||
static_cell = "2.1"
|
||||
portable-atomic = { version = "1.5", features = ["critical-section"] }
|
||||
log = "0.4"
|
||||
rand = { version = "0.8.5", default-features = false }
|
||||
embedded-sdmmc = "0.7.0"
|
||||
|
||||
[profile.release]
|
||||
debug = 2
|
||||
lto = true
|
||||
opt-level = 'z'
|
||||
|
||||
[profile.dev]
|
||||
debug = 2
|
||||
lto = true
|
||||
opt-level = "z"
|
36
build.rs
Normal file
36
build.rs
Normal 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");
|
||||
}
|
1
embassy
Submodule
1
embassy
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit e8b1ea14c7fb151aa5e296ca8f9724f175bdeaef
|
17
memory.x
Normal file
17
memory.x
Normal 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 */
|
||||
}
|
202
src/main.rs
Normal file
202
src/main.rs
Normal file
|
@ -0,0 +1,202 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use cyw43::Control;
|
||||
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
||||
use defmt::{info, unwrap};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::select::{Either, select};
|
||||
use embassy_rp::bind_interrupts;
|
||||
use embassy_rp::config::Config;
|
||||
use embassy_rp::gpio::{self, Input};
|
||||
use embassy_rp::peripherals::{DMA_CH0, PIO0};
|
||||
use embassy_rp::pio::{InterruptHandler, Pio};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::channel::{Channel, Sender};
|
||||
use embassy_time::{Duration, Instant, Timer};
|
||||
use gpio::{Level, Output};
|
||||
use static_cell::StaticCell;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
const ONE_MINUTE_SECONDS: u64 = 60;
|
||||
const ONE_HOUR_SECONDS: u64 = ONE_MINUTE_SECONDS * 60;
|
||||
const ONE_DAY_SECONDS: u64 = ONE_HOUR_SECONDS * 24;
|
||||
|
||||
const FRESH_TO_STALE_DURATION: Duration = Duration::from_secs(ONE_DAY_SECONDS);
|
||||
const STALE_TO_OLD_DURATION: Duration = Duration::from_secs(ONE_DAY_SECONDS);
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
PIO0_IRQ_0 => InterruptHandler<PIO0>;
|
||||
});
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn cyw43_task(
|
||||
runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>,
|
||||
) -> ! {
|
||||
runner.run().await
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum WaterState {
|
||||
Fresh,
|
||||
Stale,
|
||||
Old,
|
||||
}
|
||||
|
||||
impl WaterState {
|
||||
fn decay(&mut self) {
|
||||
match self {
|
||||
Self::Fresh => *self = Self::Stale,
|
||||
Self::Stale => *self = Self::Old,
|
||||
Self::Old => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn refill(&mut self) {
|
||||
*self = Self::Fresh;
|
||||
}
|
||||
|
||||
fn timer(self) -> Timer {
|
||||
match self {
|
||||
WaterState::Fresh => Timer::after(FRESH_TO_STALE_DURATION),
|
||||
WaterState::Stale => Timer::after(STALE_TO_OLD_DURATION),
|
||||
// I hope oolong has been watered after this long...
|
||||
WaterState::Old => Timer::at(Instant::MAX),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum ChannelMessage {
|
||||
Refilled,
|
||||
}
|
||||
static CHANNEL: Channel<CriticalSectionRawMutex, ChannelMessage, 8> = Channel::new();
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn button(
|
||||
mut input: Input<'static>,
|
||||
sender: Sender<'static, CriticalSectionRawMutex, ChannelMessage, 8>,
|
||||
) {
|
||||
loop {
|
||||
info!("Waiting for falling edge");
|
||||
input.wait_for_falling_edge().await;
|
||||
sender.send(ChannelMessage::Refilled).await;
|
||||
input.wait_for_high().await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn heartbeat(mut control: Control<'static>) {
|
||||
loop {
|
||||
info!("Heartbeating!");
|
||||
control.gpio_set(0, true).await;
|
||||
Timer::after(Duration::from_millis(1)).await;
|
||||
control.gpio_set(0, false).await;
|
||||
Timer::after(Duration::from_secs(10)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
// let p = embassy_rp::init(Default::default());
|
||||
// let mut led = Output::new(p.PIN_18, Level::Low);
|
||||
|
||||
// loop {
|
||||
// info!("led on!");
|
||||
// led.set_high();
|
||||
// Timer::after_secs(1).await;
|
||||
|
||||
// info!("led off!");
|
||||
// led.set_low();
|
||||
// Timer::after_secs(1).await;
|
||||
// }
|
||||
|
||||
let p = embassy_rp::init(Config::default());
|
||||
let fw = include_bytes!("../embassy/cyw43-firmware/43439A0.bin");
|
||||
let clm = include_bytes!("../embassy/cyw43-firmware/43439A0_clm.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,
|
||||
p.DMA_CH0,
|
||||
);
|
||||
|
||||
let (_net_device, mut control, runner) = {
|
||||
static STATE: StaticCell<cyw43::State> = StaticCell::new();
|
||||
let state = STATE.init(cyw43::State::new());
|
||||
cyw43::new(state, pwr, spi, fw).await
|
||||
};
|
||||
|
||||
unwrap!(spawner.spawn(cyw43_task(runner)));
|
||||
|
||||
control.init(clm).await;
|
||||
control
|
||||
.set_power_management(cyw43::PowerManagementMode::Aggressive)
|
||||
.await;
|
||||
unwrap!(spawner.spawn(heartbeat(control)));
|
||||
|
||||
unwrap!(spawner.spawn(button(
|
||||
{
|
||||
let input = Input::new(p.PIN_15, gpio::Pull::Up);
|
||||
input
|
||||
},
|
||||
CHANNEL.sender()
|
||||
)));
|
||||
|
||||
let recv = CHANNEL.receiver();
|
||||
|
||||
let mut water_state = WaterState::Fresh;
|
||||
let mut timer = water_state.timer();
|
||||
let mut red_led = Output::new(p.PIN_13, Level::Low);
|
||||
let mut yellow_led = Output::new(p.PIN_18, Level::Low);
|
||||
|
||||
loop {
|
||||
match select(&mut timer, recv.receive()).await {
|
||||
Either::First(()) => {
|
||||
info!("Timer decay received");
|
||||
water_state.decay();
|
||||
set_leds(water_state, &mut yellow_led, &mut red_led);
|
||||
timer = water_state.timer();
|
||||
}
|
||||
Either::Second(ChannelMessage::Refilled) => {
|
||||
info!("Water refilled received");
|
||||
water_state.refill();
|
||||
set_leds(water_state, &mut yellow_led, &mut red_led);
|
||||
timer = water_state.timer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_leds(water_state: WaterState, yellow_led: &mut Output, red_led: &mut Output) {
|
||||
match water_state {
|
||||
WaterState::Fresh => {
|
||||
yellow_led.set_low();
|
||||
red_led.set_low();
|
||||
}
|
||||
WaterState::Stale => {
|
||||
yellow_led.set_high();
|
||||
red_led.set_low();
|
||||
}
|
||||
WaterState::Old => {
|
||||
yellow_led.set_low();
|
||||
red_led.set_high();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue