mirror of
https://github.com/ferrous-systems/embedded-trainings-2020.git
synced 2025-01-25 07:18:08 +00:00
update book
This commit is contained in:
parent
ca262675e8
commit
ae36a3c578
5 changed files with 74 additions and 44 deletions
|
@ -1,14 +1,24 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use cortex_m::asm;
|
||||
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
const APP: () = {
|
||||
#[rtic::app(device = dk, peripherals = false)]
|
||||
mod app {
|
||||
use cortex_m::asm;
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) {
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
let board = dk::init().unwrap();
|
||||
|
||||
// `POWER` is a peripheral, or register block
|
||||
|
@ -39,10 +49,12 @@ const APP: () = {
|
|||
let vbusdetect: bool = power.usbregstatus.read().vbusdetect().bits();
|
||||
// ^^^^^^^^^^ bitfield name
|
||||
defmt::println!("USBREGSTATUS.VBUSDETECT: {}", vbusdetect);
|
||||
|
||||
(MySharedResources {}, MyLocalResources {}, init::Monotonics())
|
||||
}
|
||||
|
||||
#[idle]
|
||||
fn main(_cx: main::Context) -> ! {
|
||||
fn idle(_cx: idle::Context) -> ! {
|
||||
defmt::println!("idle: going to sleep");
|
||||
|
||||
// sleep in the background
|
||||
|
@ -56,4 +68,4 @@ const APP: () = {
|
|||
defmt::println!("POWER event occurred");
|
||||
asm::bkpt();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,20 +1,26 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use cortex_m::asm;
|
||||
use dk::peripheral::POWER;
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
const APP: () = {
|
||||
struct Resources {
|
||||
#[rtic::app(device = dk, peripherals = false)]
|
||||
mod app {
|
||||
use cortex_m::asm;
|
||||
use dk::peripheral::POWER;
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
power: POWER,
|
||||
counter: usize, // <- new resource
|
||||
counter: usize,
|
||||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> init::LateResources {
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
let board = dk::init().unwrap();
|
||||
|
||||
let power = board.power;
|
||||
|
@ -23,14 +29,19 @@ const APP: () = {
|
|||
|
||||
defmt::println!("USBDETECTED interrupt enabled");
|
||||
|
||||
init::LateResources {
|
||||
power,
|
||||
counter: 0, // <- initialize the new resource
|
||||
}
|
||||
(
|
||||
MySharedResources {},
|
||||
MyLocalResources {
|
||||
power,
|
||||
counter: 0, // <- initialize the new resource
|
||||
},
|
||||
init::Monotonics()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
#[idle]
|
||||
fn main(_cx: main::Context) -> ! {
|
||||
fn idle(_cx: idle::Context) -> ! {
|
||||
loop {
|
||||
defmt::println!("idle: going to sleep");
|
||||
asm::wfi();
|
||||
|
@ -38,13 +49,13 @@ const APP: () = {
|
|||
}
|
||||
}
|
||||
|
||||
#[task(binds = POWER_CLOCK, resources = [power, counter])]
|
||||
// ^^^^^^^ we want to access the resource from here
|
||||
#[task(binds = POWER_CLOCK, local = [power, counter])]
|
||||
// ^^^^^^^ we want to access the resource from here
|
||||
fn on_power_event(cx: on_power_event::Context) {
|
||||
defmt::debug!("POWER event occurred");
|
||||
|
||||
let power = cx.resources.power;
|
||||
let counter = cx.resources.counter;
|
||||
let power = cx.local.power;
|
||||
let counter = cx.local.counter;
|
||||
|
||||
*counter += 1;
|
||||
let n = *counter;
|
||||
|
@ -57,4 +68,4 @@ const APP: () = {
|
|||
// clear the interrupt flag; otherwise this task will run again after it returns
|
||||
power.events_usbdetected.reset();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use cortex_m::asm;
|
||||
use dk::peripheral::POWER;
|
||||
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||
use firmware as _;
|
||||
|
||||
#[rtic::app(device = dk)]
|
||||
const APP: () = {
|
||||
struct Resources {
|
||||
power: POWER, // <- resource declaration
|
||||
#[rtic::app(device = dk, peripherals = false)]
|
||||
mod app {
|
||||
use cortex_m::asm;
|
||||
use dk::peripheral::POWER;
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
power: POWER,
|
||||
}
|
||||
|
||||
#[shared]
|
||||
struct MySharedResources {
|
||||
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_cx: init::Context) -> init::LateResources {
|
||||
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
|
||||
let board = dk::init().unwrap();
|
||||
|
||||
let power = board.power;
|
||||
|
@ -22,13 +29,11 @@ const APP: () = {
|
|||
|
||||
defmt::println!("USBDETECTED interrupt enabled");
|
||||
|
||||
init::LateResources {
|
||||
power, // <- resource initialization
|
||||
}
|
||||
(MySharedResources {}, MyLocalResources {power}, init::Monotonics())
|
||||
}
|
||||
|
||||
#[idle]
|
||||
fn main(_cx: main::Context) -> ! {
|
||||
fn idle(_cx: idle::Context) -> ! {
|
||||
loop {
|
||||
defmt::println!("idle: going to sleep");
|
||||
asm::wfi();
|
||||
|
@ -36,13 +41,13 @@ const APP: () = {
|
|||
}
|
||||
}
|
||||
|
||||
#[task(binds = POWER_CLOCK, resources = [power])]
|
||||
#[task(binds = POWER_CLOCK, local = [power])]
|
||||
// ^^^^^^^ resource access list
|
||||
fn on_power_event(cx: on_power_event::Context) {
|
||||
defmt::println!("POWER event occurred");
|
||||
|
||||
// resources available to this task
|
||||
let resources = cx.resources;
|
||||
let resources = cx.local;
|
||||
|
||||
// the POWER peripheral can be accessed through a reference
|
||||
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
|
||||
power.events_usbdetected.reset();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ use firmware as _;
|
|||
#[rtic::app(device = dk, peripherals = false)]
|
||||
mod app {
|
||||
use cortex_m::asm;
|
||||
use dk::Peripherals;
|
||||
|
||||
#[local]
|
||||
struct MyLocalResources {
|
||||
|
|
|
@ -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.
|
||||
|
||||
``` console
|
||||
(..)
|
||||
INFO:resource -- on_power_event: cable connected 1 time
|
||||
(..)
|
||||
INFO:resource -- on_power_event: cable connected 2 times
|
||||
(..)
|
||||
INFO:resource -- on_power_event: cable connected 3 times
|
||||
USBDETECTED interrupt enabled
|
||||
idle: going to sleep
|
||||
on_power_event: cable connected 1 time
|
||||
idle: woke up
|
||||
idle: going to sleep
|
||||
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.
|
Loading…
Reference in a new issue