embedded-trainings-2020/advanced/firmware/src/bin/usb-1-solution.rs

68 lines
1.6 KiB
Rust
Raw Normal View History

2020-06-09 09:52:27 +00:00
#![no_main]
#![no_std]
2022-01-07 16:24:21 +00:00
2021-04-14 09:49:34 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
2020-06-09 09:52:27 +00:00
2022-01-07 16:24:21 +00:00
#[rtic::app(device = dk, peripherals = false)]
mod app {
2022-01-07 18:01:58 +00:00
use cortex_m::asm;
2022-01-07 16:24:21 +00:00
use dk::{
peripheral::USBD,
usbd::{self, Event},
};
#[local]
struct MyLocalResources {
2020-06-09 09:52:27 +00:00
usbd: USBD,
}
2022-01-07 16:24:21 +00:00
#[shared]
struct MySharedResources {
}
2020-06-09 09:52:27 +00:00
#[init]
2022-01-07 16:24:21 +00:00
fn init(_cx: init::Context) -> (MySharedResources, MyLocalResources, init::Monotonics) {
2020-06-09 09:52:27 +00:00
let board = dk::init().unwrap();
2022-01-07 16:24:21 +00:00
// initialize the USBD peripheral
// NOTE this will block if the USB cable is not connected to port J3
dk::usbd::init(board.power, &board.usbd);
defmt::println!("USBD initialized");
2020-06-09 09:52:27 +00:00
2022-01-07 16:24:21 +00:00
(MySharedResources {}, MyLocalResources {usbd: board.usbd }, init::Monotonics())
2020-06-09 09:52:27 +00:00
}
2022-01-07 16:24:21 +00:00
#[task(binds = USBD, local = [usbd])]
fn main(cx: main::Context) {
2022-01-07 16:24:21 +00:00
let usbd = cx.local.usbd;
2020-06-09 09:52:27 +00:00
while let Some(event) = usbd::next_event(usbd) {
on_event(usbd, event)
}
}
2022-01-07 16:24:21 +00:00
fn on_event(_usbd: &USBD, event: Event) {
2022-01-12 16:33:45 +00:00
defmt::println!("USB: {}", event);
2022-01-07 16:24:21 +00:00
match event {
Event::UsbReset => {
// going from the Default state to the Default state is a no-operation
2022-01-07 18:01:58 +00:00
defmt::println!("returning to the Default state");
2022-01-07 16:24:21 +00:00
}
Event::UsbEp0DataDone => todo!(),
Event::UsbEp0Setup => {
2022-01-07 18:01:58 +00:00
defmt::println!("goal reached; move to the next section");
2022-01-12 16:33:45 +00:00
dk::exit();
2022-01-07 16:24:21 +00:00
}
}
2020-06-09 09:52:27 +00:00
}
}
2022-01-07 16:24:21 +00:00