update book

This commit is contained in:
Mirabellensaft 2022-01-07 18:23:35 +01:00
parent ca262675e8
commit ae36a3c578
5 changed files with 74 additions and 44 deletions

View file

@ -1,14 +1,24 @@
#![no_main] #![no_main]
#![no_std] #![no_std]
use cortex_m::asm;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _; use firmware as _;
#[rtic::app(device = dk)] #[rtic::app(device = dk, peripherals = false)]
const APP: () = { mod app {
use cortex_m::asm;
#[local]
struct MyLocalResources {
}
#[shared]
struct MySharedResources {
}
#[init] #[init]
fn init(_cx: init::Context) { fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
let board = dk::init().unwrap(); let board = dk::init().unwrap();
// `POWER` is a peripheral, or register block // `POWER` is a peripheral, or register block
@ -39,10 +49,12 @@ const APP: () = {
let vbusdetect: bool = power.usbregstatus.read().vbusdetect().bits(); let vbusdetect: bool = power.usbregstatus.read().vbusdetect().bits();
// ^^^^^^^^^^ bitfield name // ^^^^^^^^^^ bitfield name
defmt::println!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect); defmt::println!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
(MySharedResources {}, MyLocalResources {}, init::Monotonics())
} }
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn idle(_cx: idle::Context) -> ! {
defmt::println!("idle: going to sleep"); defmt::println!("idle: going to sleep");
// sleep in the background // sleep in the background
@ -56,4 +68,4 @@ const APP: () = {
defmt::println!("POWER event occurred"); defmt::println!("POWER event occurred");
asm::bkpt(); asm::bkpt();
} }
}; }

View file

@ -1,20 +1,26 @@
#![no_main] #![no_main]
#![no_std] #![no_std]
use cortex_m::asm;
use dk::peripheral::POWER;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _; use firmware as _;
#[rtic::app(device = dk)] #[rtic::app(device = dk, peripherals = false)]
const APP: () = { mod app {
struct Resources { use cortex_m::asm;
use dk::peripheral::POWER;
#[local]
struct MyLocalResources {
power: POWER, power: POWER,
counter: usize, // <- new resource counter: usize,
}
#[shared]
struct MySharedResources {
} }
#[init] #[init]
fn init(_cx: init::Context) -> init::LateResources { fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
let board = dk::init().unwrap(); let board = dk::init().unwrap();
let power = board.power; let power = board.power;
@ -23,14 +29,19 @@ const APP: () = {
defmt::println!("USBDETECTED interrupt enabled"); defmt::println!("USBDETECTED interrupt enabled");
init::LateResources { (
power, MySharedResources {},
counter: 0, // <- initialize the new resource MyLocalResources {
} power,
counter: 0, // <- initialize the new resource
},
init::Monotonics()
)
} }
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn idle(_cx: idle::Context) -> ! {
loop { loop {
defmt::println!("idle: going to sleep"); defmt::println!("idle: going to sleep");
asm::wfi(); asm::wfi();
@ -38,13 +49,13 @@ const APP: () = {
} }
} }
#[task(binds = POWER_CLOCK, resources = [power, counter])] #[task(binds = POWER_CLOCK, local = [power, counter])]
// ^^^^^^^ we want to access the resource from here // ^^^^^^^ we want to access the resource from here
fn on_power_event(cx: on_power_event::Context) { fn on_power_event(cx: on_power_event::Context) {
defmt::debug!("POWER event occurred"); defmt::debug!("POWER event occurred");
let power = cx.resources.power; let power = cx.local.power;
let counter = cx.resources.counter; let counter = cx.local.counter;
*counter += 1; *counter += 1;
let n = *counter; let n = *counter;
@ -57,4 +68,4 @@ const APP: () = {
// clear the interrupt flag; otherwise this task will run again after it returns // clear the interrupt flag; otherwise this task will run again after it returns
power.events_usbdetected.reset(); power.events_usbdetected.reset();
} }
}; }

View file

@ -1,19 +1,26 @@
#![no_main] #![no_main]
#![no_std] #![no_std]
use cortex_m::asm;
use dk::peripheral::POWER;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _; use firmware as _;
#[rtic::app(device = dk)] #[rtic::app(device = dk, peripherals = false)]
const APP: () = { mod app {
struct Resources { use cortex_m::asm;
power: POWER, // <- resource declaration use dk::peripheral::POWER;
#[local]
struct MyLocalResources {
power: POWER,
}
#[shared]
struct MySharedResources {
} }
#[init] #[init]
fn init(_cx: init::Context) -> init::LateResources { fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
let board = dk::init().unwrap(); let board = dk::init().unwrap();
let power = board.power; let power = board.power;
@ -22,13 +29,11 @@ const APP: () = {
defmt::println!("USBDETECTED interrupt enabled"); defmt::println!("USBDETECTED interrupt enabled");
init::LateResources { (MySharedResources {}, MyLocalResources {power}, init::Monotonics())
power, // <- resource initialization
}
} }
#[idle] #[idle]
fn main(_cx: main::Context) -> ! { fn idle(_cx: idle::Context) -> ! {
loop { loop {
defmt::println!("idle: going to sleep"); defmt::println!("idle: going to sleep");
asm::wfi(); asm::wfi();
@ -36,13 +41,13 @@ const APP: () = {
} }
} }
#[task(binds = POWER_CLOCK, resources = [power])] #[task(binds = POWER_CLOCK, local = [power])]
// ^^^^^^^ resource access list // ^^^^^^^ resource access list
fn on_power_event(cx: on_power_event::Context) { fn on_power_event(cx: on_power_event::Context) {
defmt::println!("POWER event occurred"); defmt::println!("POWER event occurred");
// resources available to this task // resources available to this task
let resources = cx.resources; let resources = cx.local;
// the POWER peripheral can be accessed through a reference // the POWER peripheral can be accessed through a reference
let power: &mut POWER = resources.power; let power: &mut POWER = resources.power;
@ -50,4 +55,4 @@ const APP: () = {
// clear the interrupt flag; otherwise this task will run again after it returns // clear the interrupt flag; otherwise this task will run again after it returns
power.events_usbdetected.reset(); power.events_usbdetected.reset();
} }
}; }

View file

@ -8,7 +8,6 @@ use firmware as _;
#[rtic::app(device = dk, peripherals = false)] #[rtic::app(device = dk, peripherals = false)]
mod app { mod app {
use cortex_m::asm; use cortex_m::asm;
use dk::Peripherals;
#[local] #[local]
struct MyLocalResources { struct MyLocalResources {

View file

@ -21,12 +21,15 @@ Also note that in the starter code the `idle` function has been modified. Pay at
✅ Modify the program so that it prints the number of times the USB cable has been connected to the DK every time the cable is connected, as shown below. ✅ Modify the program so that it prints the number of times the USB cable has been connected to the DK every time the cable is connected, as shown below.
``` console ``` console
(..) USBDETECTED interrupt enabled
INFO:resource -- on_power_event: cable connected 1 time idle: going to sleep
(..) on_power_event: cable connected 1 time
INFO:resource -- on_power_event: cable connected 2 times idle: woke up
(..) idle: going to sleep
INFO:resource -- on_power_event: cable connected 3 times on_power_event: cable connected 2 times
idle: woke up
idle: going to sleep
on_power_event: cable connected 3 times
``` ```
You can find a solution to this exercise in the `resource-solution.rs` file. You can find a solution to this exercise in the `resource-solution.rs` file.