Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to embedded-hal 1.0 and embedded-hal async 1.0 #6

Merged
merged 9 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/embassy-rp-async/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "probe-rs run --chip RP2040 --protocol swd --speed 16000"

[build]
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+

[env]
DEFMT_LOG = "debug"
78 changes: 78 additions & 0 deletions examples/embassy-rp-async/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// The format of this file is specified in https://probe.rs/docs/tools/vscode/#start-a-debug-session-with-minimum-configuration
{
"version": "0.2.0",
"configurations": [
{
"preLaunchTask": "rust: cargo build",
"type": "probe-rs-debug",
"request": "launch",
"name": "embassy_rp_async launch",
"cwd": "${workspaceFolder}",
"chip": "rp2040",
// RP2040 doesn't support connectUnderReset
"connectUnderReset": false,
"speed": 4000,
"runtimeExecutable": "probe-rs",
"runtimeArgs": [
"dap-server"
],
"flashingConfig": {
"flashingEnabled": true,
"resetAfterFlashing": true,
"haltAfterReset": true,
},
"coreConfigs": [
{
"coreIndex": 0,
"programBinary": "target/thumbv6m-none-eabi/debug/embassy_rp_async",
"chip": "RP2040",
// Uncomment this if you've downloaded the SVD from
// https://github.com/raspberrypi/pico-sdk/raw/1.3.1/src/rp2040/hardware_regs/rp2040.svd
// and placed it in the .vscode directory
// "svdFile": "./.vscode/rp2040.svd",
"rttEnabled": true,
"options": {
"env": {
"DEFMT_LOG": "debug"
}
},
}
],
"consoleLogLevel": "Info", //Error, Warn, Info, Debug, Trace
"wireProtocol": "Swd"
},
{
"type": "probe-rs-debug",
"request": "attach",
"name": "embassy_rp_async attach",
"cwd": "${workspaceFolder}",
"chip": "rp2040",
// RP2040 doesn't support connectUnderReset
"connectUnderReset": false,
"speed": 4000,
"runtimeExecutable": "probe-rs",
"runtimeArgs": [
"dap-server"
],
"coreConfigs": [
{
"coreIndex": 0,
"programBinary": "target/thumbv6m-none-eabi/debug/embassy_rp_async",
"chip": "RP2040",
// Uncomment this if you've downloaded the SVD from
// https://github.com/raspberrypi/pico-sdk/raw/1.5.1/src/rp2040/hardware_regs/rp2040.svd
// and placed it in the .vscode directory
// "svdFile": "./.vscode/rp2040.svd",
"rttEnabled": true,
"options": {
"env": {
"DEFMT_LOG": "debug"
}
},
}
],
"consoleLogLevel": "Info", //Error, Warn, Info, Debug, Trace
"wireProtocol": "Swd"
}
]
}
6 changes: 6 additions & 0 deletions examples/embassy-rp-async/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rust-analyzer.cargo.target": "thumbv6m-none-eabi",
"rust-analyzer.check.allTargets": false,
"rust-analyzer.checkOnSave": false,
"editor.formatOnSave": true
}
97 changes: 97 additions & 0 deletions examples/embassy-rp-async/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
[package]
edition = "2021"
name = "embassy_rp_async"
version = "0.1.0"
license = "MIT OR Apache-2.0"
resolver = "2"


[dependencies]
wii-ext = { version = "0.3.0", features = [
"defmt_print",
], path = "../../wii-ext" }

cortex-m = { version = "0.7.6", features = ["inline-asm"] }
cortex-m-rt = "0.7.0"

defmt = "0.3"
defmt-rtt = "0.4"
panic-probe = { version = "0.3", features = ["print-defmt"] }

embassy-embedded-hal = { version = "0.1.0", features = ["defmt"] }

embassy-executor = { version = "0.5.0", features = [
"arch-cortex-m",
"executor-thread",
"executor-interrupt",
"defmt",
"integrated-timers",
"task-arena-size-32768",
] }
embassy-futures = { version = "0.1.0" }
embassy-sync = { version = "0.5.0", features = ["defmt"] }
embassy-time = { version = "0.3.0", features = [
"defmt",
"defmt-timestamp-uptime",
] }

embassy-rp = { version = "0.1.0", features = [
"defmt",
"unstable-pac",
"time-driver",
"critical-section-impl",
] }
portable-atomic = { version = "1.5.1", features = ["critical-section"] }

# cargo build/run
[profile.dev]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 1
overflow-checks = true
lto = "off"

# cargo build/run --release
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
lto = true
opt-level = 's'
overflow-checks = false

# do not optimize proc-macro crates = faster builds from scratch
[profile.dev.build-override]
codegen-units = 8
debug = false
debug-assertions = false
opt-level = 0
overflow-checks = false

[profile.release.build-override]
codegen-units = 8
debug = false
debug-assertions = false
opt-level = 0
overflow-checks = false

# cargo test
[profile.test]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 's'
overflow-checks = true

# cargo test --release
[profile.bench]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
lto = true
opt-level = 's'
36 changes: 36 additions & 0 deletions examples/embassy-rp-async/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
5 changes: 5 additions & 0 deletions examples/embassy-rp-async/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
RAM : ORIGIN = 0x20000000, LENGTH = 256K
}
84 changes: 84 additions & 0 deletions examples/embassy-rp-async/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#![no_std]
#![no_main]

use defmt::*;
use embassy_rp::gpio;
use gpio::{Level, Output};
use wii_ext::classic_async;
use {defmt_rtt as _, panic_probe as _};

use embassy_executor::Spawner;
use embassy_rp::bind_interrupts;
use embassy_rp::gpio::AnyPin;
use embassy_rp::i2c::{self, Config, InterruptHandler};
use embassy_rp::peripherals::I2C0;
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_time::{Delay, Duration, Ticker};

bind_interrupts!(struct Irqs {
I2C0_IRQ => InterruptHandler<I2C0>;
});

type LedType = Mutex<ThreadModeRawMutex, Option<Output<'static, AnyPin>>>;
static LED: LedType = Mutex::new(None);

#[embassy_executor::main]
async fn main(spawner: Spawner) {
info!("Program start");
let p = embassy_rp::init(Default::default());

// Configure Pico LED to blink once a second
let led = Output::new(AnyPin::from(p.PIN_25), Level::High);
{
*(LED.lock().await) = Some(led);
}
unwrap!(spawner.spawn(toggle_led(&LED, Duration::from_millis(500))));

let sda = p.PIN_8;
let scl = p.PIN_9;

info!("set up i2c");
let i2c = i2c::I2c::new_async(p.I2C0, scl, sda, Irqs, Config::default());

// Create, initialise and calibrate the controller
info!("initialising controller");
let mut controller = classic_async::ClassicAsync::new(i2c, Delay);
controller.init().await.unwrap();

let hi_res = false;

// Enable hi-resolution mode. This also updates calibration
// Don't really need it for this single stick mode. Plus it might make recovery easier...
if hi_res {
info!("enabling hi-res mode");
controller.enable_hires().await.unwrap();
}

info!("begin polling controller");
loop {
let input = controller.read().await.unwrap();
debug!("{:?}", input);
}
}

#[embassy_executor::task(pool_size = 1)]
async fn toggle_led(led: &'static LedType, delay: Duration) {
let mut ticker = Ticker::every(delay);
loop {
{
let mut led_unlocked = led.lock().await;
if let Some(pin_ref) = led_unlocked.as_mut() {
pin_ref.toggle();
}
}
ticker.next().await;
}
}

#[cortex_m_rt::pre_init]
unsafe fn before_main() {
// Soft-reset doesn't clear spinlocks. Clear the one used by critical-section
// before we hit main to avoid deadlocks when using a debugger
embassy_rp::pac::SIO.spinlock(31).write_value(1);
}
4 changes: 2 additions & 2 deletions examples/rp2040-hal-blocking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use defmt::*;
use defmt_rtt as _;
use panic_probe as _;

use rp_pico as bsp;
use bsp::hal::{
self,
entry,
clocks::{init_clocks_and_plls, Clock},
entry,
gpio::FunctionI2C,
pac,
sio::Sio,
watchdog::Watchdog,
};
use fugit::RateExtU32;
use rp_pico as bsp;
use wii_ext::{classic_sync::Classic, core::classic::ClassicReadingCalibrated};

use usb_device::class_prelude::*;
Expand Down
9 changes: 3 additions & 6 deletions wii-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ repository = "https://github.com/9names/wii-ext-rs"
license = "MIT OR Apache-2.0"

[dependencies]
embedded-hal = { version = "0.2.5", features = ["unproven"] }
embedded-hal = "1"
embedded-hal-async = { version = "1"}
defmt = { version = "0.3.0", optional = true }
embedded-hal-async = { version ="0.2.0-alpha.2", optional = true }
embassy-futures = { version ="0.1.0", optional = true }
embassy-time = { version ="0.1.1", optional = true }

[dev-dependencies]
embedded-hal-mock = "0.8.0"
embedded-hal-mock = "0.10.0"
paste = "1.0.6"

[features]
default = ["defmt_print"]
defmt_print = ["defmt"]
async = ["embedded-hal-async", "embassy-futures", "embassy-time"]
Loading
Loading