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

WIP: sensor abstraction & hardware setup file #271

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
65ff467
fix(threads): add a 'static bound on the stack reference
ROMemories Apr 12, 2024
fa399c6
fix(macro): fix the `group_peripherals!()` macro
ROMemories Apr 11, 2024
cba30f6
WIP
ROMemories Apr 11, 2024
321309a
refactor: rename riot-rs-saga to riot-rs-sensors
ROMemories Apr 11, 2024
d39d5d5
WIP: for debugging
ROMemories Apr 12, 2024
a2640ca
fixup! refactor: rename riot-rs-saga to riot-rs-sensors
ROMemories Apr 12, 2024
ab4e653
feat(sensors): pub/sub working for thresholds
ROMemories Apr 12, 2024
7d1dadc
WIP
ROMemories Apr 12, 2024
795f5cf
WIP: pick the downcast + async route
ROMemories Apr 16, 2024
50d16da
refactor(sensors): do some cleaning and add some documentation
ROMemories Apr 16, 2024
c509492
feat(sensors): introduce a (private) macro for sensor downcasting
ROMemories Apr 17, 2024
495194f
WIP
ROMemories Apr 17, 2024
ecb1a41
WIP
ROMemories Apr 17, 2024
cd53b57
WIP: parse a HW config YAML file
ROMemories Apr 17, 2024
333697c
WIP: use a separate spawner for sensor init
ROMemories Apr 17, 2024
3ce334d
WIP: use a peripheral struct per sensor
ROMemories Apr 17, 2024
b5222ee
WIP: introduce a PushButton sensor
ROMemories Apr 18, 2024
0519037
WIP: use arch re-exports of gpio
ROMemories Apr 18, 2024
435900d
refactor(hwsetup): move hwsetup to its own crate
ROMemories Apr 19, 2024
2e88ae4
WIP: start generating the sensors module (incomplete)
ROMemories Apr 19, 2024
e4214f7
WIP: support conditional in generate_sensor
ROMemories Apr 19, 2024
36bc329
refactor(sensor)!: rename await_read_sensor_main
ROMemories Apr 22, 2024
f4955b2
chore(sensors): delete some unused code
ROMemories Apr 22, 2024
dbc23ea
WIP: introduce sensor categories
ROMemories Apr 23, 2024
1ece991
WIP: get unit and display name when reading a sensor value
ROMemories Apr 23, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
# TODO: we'll eventually want to enable relevant features
- name: Run crate tests
run: |
cargo test --no-default-features --features no-boards -p riot-rs -p riot-rs-embassy -p riot-rs-threads -p riot-rs-macros
cargo test --no-default-features --features no-boards -p riot-rs -p riot-rs-embassy -p riot-rs-hwsetup -p riot-rs-threads -p riot-rs-macros
cargo test -p rbi -p ringbuffer

lint:
Expand Down Expand Up @@ -136,7 +136,7 @@ jobs:
- name: clippy
uses: clechasseur/rs-clippy-check@v3
with:
args: --verbose --locked --features no-boards -p riot-rs -p riot-rs-boards -p riot-rs-buildinfo -p riot-rs-chips -p riot-rs-debug -p riot-rs-embassy -p riot-rs-macros -p riot-rs-rt -p riot-rs-utils
args: --verbose --locked --features no-boards -p riot-rs -p riot-rs-boards -p riot-rs-buildinfo -p riot-rs-chips -p riot-rs-debug -p riot-rs-embassy -p riot-rs-hwsetup -p riot-rs-macros -p riot-rs-rt -p riot-rs-sensors -p riot-rs-utils

- name: "rustdoc"
run: cargo rustdoc -p riot-rs --features no-boards,bench,threading -- -D warnings
Expand Down
53 changes: 48 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ members = [
"src/riot-rs-boards/nucleo-f401re",
"src/riot-rs-chips",
"src/riot-rs-debug",
"src/riot-rs-hwsetup",
"src/riot-rs-macros",
"src/riot-rs-sensors",
"tests/benchmarks/bench_sched_yield",
]

Expand All @@ -37,6 +39,7 @@ cortex-m-semihosting = { version = "0.5" }
critical-section = { version = "1.1.2" }

embassy-executor = { version = "0.5", default-features = false }
embassy-futures = { version = "0.1.1", default-features = false }
embassy-net = { version = "0.4", default-features = false }
embassy-net-driver-channel = { version = "0.2.0", default-features = false }
embassy-nrf = { version = "0.1", default-features = false }
Expand All @@ -55,14 +58,20 @@ riot-rs = { path = "src/riot-rs", default-features = false }
riot-rs-bench = { path = "src/riot-rs-bench", default-features = false }
riot-rs-boards = { path = "src/riot-rs-boards", default-features = false }
riot-rs-debug = { path = "src/riot-rs-debug", default-features = false }
riot-rs-hwsetup = { path = "src/riot-rs-hwsetup", default-features = false }
riot-rs-rt = { path = "src/riot-rs-rt" }
riot-rs-runqueue = { path = "src/riot-rs-runqueue" }
riot-rs-sensors = { path = "src/riot-rs-sensors" }
riot-rs-threads = { path = "src/riot-rs-threads" }

const_panic = { version = "0.2.8", default_features = false }
document-features = "0.2.8"
embedded-hal = { version = "1.0.0", default_features = false }
heapless = { version = "0.8.0", default-features = false }
konst = { version = "0.3.8", default_features = false }
ld-memory = { version = "0.2.9" }
serde = { version = "1.0.198", default_features = false }
serde_yaml = { version = "0.9.34" } # Replace with a maintained crate?
static_cell = { version = "2.0.0", features = ["nightly"] }

[profile.dev]
Expand Down
5 changes: 2 additions & 3 deletions examples/embassy-http-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ riot-rs-boards = { path = "../../src/riot-rs-boards" }
serde = { version = "1.0", default-features = false }
static_cell = { workspace = true }

embassy-nrf = { workspace = true, optional = true }

[target.'cfg(capability = "hw/usb-device-port")'.dependencies]
riot-rs = { path = "../../src/riot-rs", features = ["override-usb-config"] }

[features]
button-readings = ["dep:embassy-nrf"]
button-readings = []
internal-temp = ["riot-rs/internal-temp"]
21 changes: 21 additions & 0 deletions examples/embassy-http-server/hw-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sensors:
- name: TEMP_SENSOR
driver: riot_rs::embassy::arch::internal_temp::InternalTemp
on: nrf52

# - name: BUTTONS
# driver: riot_rs::sensors::push_buttons::PushButtons
# on: nrf52840dk
# when: button-readings
# peripherals:
# btn1: P0_11
# btn2: P0_12
# btn3: P0_24
# btn4: P0_25

- name: BUTTON_1
driver: riot_rs::embassy::arch::PushButtonNrf
on: nrf52840dk
when: button-readings
peripherals:
btn: P0_11 # FIXME: specify pull-up/down resistors
11 changes: 10 additions & 1 deletion examples/embassy-http-server/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ apps:
env:
global:
CARGO_ENV:
- CONFIG_ISR_STACKSIZE=16384
- CONFIG_ISR_STACKSIZE=65536
selects:
- ?release
- network
- ?button-readings
- ?internal-temp

modules:
- name: button-readings
Expand All @@ -17,3 +18,11 @@ modules:
global:
FEATURES:
- button-readings

- name: internal-temp
context:
- nrf52840
env:
global:
FEATURES:
- internal-temp
82 changes: 31 additions & 51 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,28 @@
#![no_std]
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]
// Both of these are required for sensors
#![feature(trait_upcasting)]
#![feature(impl_trait_in_assoc_type)]

mod pins;
mod routes;

#[riot_rs::hw_setup]
mod sensors {}

use riot_rs::{
debug::println,
embassy::{network, Spawner},
sensors::Sensor,
};

use embassy_net::tcp::TcpSocket;
use embassy_time::Duration;
use picoserve::routing::get;
use static_cell::make_static;

#[cfg(feature = "button-readings")]
use embassy_nrf::gpio::{Input, Pin, Pull};

struct AppState {
#[cfg(feature = "button-readings")]
buttons: ButtonInputs,
}

#[cfg(feature = "button-readings")]
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};

#[cfg(feature = "button-readings")]
#[derive(Copy, Clone)]
struct ButtonInputs(&'static Mutex<CriticalSectionRawMutex, Buttons>);

#[cfg(feature = "button-readings")]
struct Buttons {
button1: Input<'static>,
button2: Input<'static>,
button3: Input<'static>,
button4: Input<'static>,
}

#[cfg(feature = "button-readings")]
impl picoserve::extract::FromRef<AppState> for ButtonInputs {
fn from_ref(state: &AppState) -> Self {
state.buttons
}
}
struct AppState {}

type AppRouter = impl picoserve::routing::PathRouter<AppState>;

Expand Down Expand Up @@ -88,28 +67,22 @@ async fn web_task(
}

#[riot_rs::spawner(autostart, peripherals)]
fn main(spawner: Spawner, peripherals: pins::Peripherals) {
#[cfg(not(feature = "button-readings"))]
let _ = peripherals;

#[cfg(feature = "button-readings")]
let button_inputs = {
let buttons = peripherals.buttons;

let buttons = Buttons {
button1: Input::new(buttons.btn1.degrade(), Pull::Up),
button2: Input::new(buttons.btn2.degrade(), Pull::Up),
button3: Input::new(buttons.btn3.degrade(), Pull::Up),
button4: Input::new(buttons.btn4.degrade(), Pull::Up),
};

ButtonInputs(make_static!(Mutex::new(buttons)))
};
fn main(spawner: Spawner, _peripherals: pins::Peripherals) {
#[cfg(context = "nrf52")]
{
use riot_rs::sensors::sensor::{PhysicalValue, ThresholdKind};
let threshold = PhysicalValue::new(2300);
sensors::TEMP_SENSOR.set_threshold(ThresholdKind::Lower, threshold);
sensors::TEMP_SENSOR.set_threshold_enabled(ThresholdKind::Lower, true);
}

fn make_app() -> picoserve::Router<AppRouter, AppState> {
let router = picoserve::Router::new().route("/", get(routes::index));
#[cfg(feature = "button-readings")]
let router = router.route("/buttons", get(routes::buttons));
let router = router.route("/api/buttons", get(routes::buttons));
let router = router.route("/api/sensors", get(routes::sensors));
#[cfg(context = "nrf52840")]
let router = router.route("/api/temp", get(routes::temp));
router
}

Expand All @@ -122,14 +95,21 @@ fn main(spawner: Spawner, peripherals: pins::Peripherals) {
}));

for id in 0..WEB_TASK_POOL_SIZE {
let app_state = AppState {
#[cfg(feature = "button-readings")]
buttons: button_inputs,
};
let app_state = AppState {};
spawner.spawn(web_task(id, app, config, app_state)).unwrap();
}
}

#[riot_rs::task(autostart)]
async fn temp_subscriber() {
let rx = sensors::TEMP_SENSOR.subscribe();

loop {
let notification = rx.receive().await;
println!("{:#?}", notification);
}
}

#[riot_rs::config(network)]
fn network_config() -> embassy_net::Config {
use embassy_net::Ipv4Address;
Expand Down
15 changes: 2 additions & 13 deletions examples/embassy-http-server/src/pins.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
#[cfg(feature = "button-readings")]
#[allow(clippy::unused_imports)]
use riot_rs::embassy::arch::peripherals;

#[cfg(all(feature = "button-readings", builder = "nrf52840dk"))]
riot_rs::define_peripherals!(Buttons {
btn1: P0_11,
btn2: P0_12,
btn3: P0_24,
btn4: P0_25,
});

riot_rs::group_peripherals!(Peripherals {
#[cfg(feature = "button-readings")]
buttons: Buttons,
});
riot_rs::group_peripherals!(Peripherals {});
Loading