add template with gaps to fill

This commit is contained in:
Mirabellensaft 2023-02-20 18:25:48 +01:00
parent 923b850102
commit 9e46b5732e

View file

@ -6,38 +6,31 @@
use core::{ use core::{
ops, ops,
fmt,
sync::atomic::{self, Ordering}, sync::atomic::{self, Ordering},
time::Duration, time::Duration,
}; };
use cortex_m::asm; use cortex_m::asm;
use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin}; use embedded_hal::digital::v2::{OutputPin as _, StatefulOutputPin};
pub use hal::pac::{
UARTE1, uarte0::{
baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}};
use hal::{ use hal::{
gpio::{p0, Level, Output, Input, PullUp, Pin, Port, PushPull}, gpio::{p0, Level, Output, Pin, Port, PushPull},
timer::OneShot, prelude::InputPin, timer::OneShot,
}; };
use defmt; use defmt;
use defmt_rtt as _; // global logger use defmt_rtt as _; // global logger
/// Components on the board /// Components on the boarde
// Add structs for the peripheral you want to implement. First for the buttons, later UARTE
pub struct Board { pub struct Board {
/// LEDs /// LEDs
pub leds: Leds, pub leds: Leds,
// --- Exercise --- 🔽
/// Buttons
pub buttons: Buttons,
// --- Exercise --- 🔼
/// Timer /// Timer
pub timer: Timer, pub timer: Timer,
// --- Exercise --- 🔽
/// uarte interface
pub uarte: Uarte,
// --- Exercise --- 🔼
} }
/// All LEDs on the board /// All LEDs on the board
@ -101,31 +94,21 @@ impl Led {
} }
} }
} }
// --- Exercise --- 🔽
/// All buttons on the board /// All buttons on the board
pub struct Buttons { // todo! Add a struct that represents all buttons of the board.
/// BUTTON1: pin P0.11, green LED // ...
pub b_1: Button,
/// BUTTON2: pin P0.12, green LED
pub b_2: Button,
/// BUTTON3: pin P0.24, green LED
pub b_3: Button,
/// BUTTON4: pin P0.25, green LED
pub b_4: Button,
}
/// A single button /// A single button
pub struct Button { // todo! Add a struct that represents a single button.
inner: Pin<Input<PullUp>>, // ...
}
// Add an impl block for the Button struct
// todo! Add a method that returns true, if the button is pushed.
// ...
impl Button {
/// returns true if button is pushed
pub fn is_pushed(&self) -> bool {
self.inner.is_low() == Ok(true)
}
}
// --- Exercise --- 🔼
/// A timer for creating blocking delays /// A timer for creating blocking delays
pub struct Timer { pub struct Timer {
@ -183,24 +166,12 @@ impl ops::DerefMut for Timer {
} }
/// Uarte peripheral /// Uarte peripheral
pub struct Uarte { // todo! Add a struct that represents the Uarte
inner: hal::Uarte<hal::pac::UARTE0>, // ...
}
impl fmt::Write for Uarte {
fn write_str(&mut self, s: &str) -> fmt::Result { // todo! Implement the fmt::Write Trait for Uarte
// Copy all data into an on-stack buffer so we never try to EasyDMA from // ...
// flash.
let mut buf: [u8; 16] = [0; 16];
for block in s.as_bytes().chunks(16) {
buf[..block.len()].copy_from_slice(block);
self.inner.write(&buf[..block.len()]).map_err(|_| fmt::Error)?;
}
Ok(())
}
}
/// Initializes the board /// Initializes the board
/// ///
@ -216,30 +187,26 @@ pub fn init() -> Result<Board, ()> {
let led_3 = pins.p0_15.degrade().into_push_pull_output(Level::High); let led_3 = pins.p0_15.degrade().into_push_pull_output(Level::High);
let led_4 = pins.p0_16.degrade().into_push_pull_output(Level::High); let led_4 = pins.p0_16.degrade().into_push_pull_output(Level::High);
// --- Exercise --- 🔽
// Buttons // Buttons
let b_1 = pins.p0_11.degrade().into_pullup_input(); // todo! Assign the pins of the buttons
let b_2 = pins.p0_12.degrade().into_pullup_input(); // ...
let b_3 = pins.p0_24.degrade().into_pullup_input();
let b_4 = pins.p0_25.degrade().into_pullup_input();
// --- Exercise --- 🔼
defmt::debug!("I/O pins have been configured for digital output"); defmt::debug!("I/O pins have been configured for digital output");
let timer = hal::Timer::new(periph.TIMER0); let timer = hal::Timer::new(periph.TIMER0);
// --- Exercise --- 🔽
// Uarte // Uarte
let pins = hal::uarte::Pins { // todo! Assign the pins of the UARTE peripheral
rxd: pins.p0_08.degrade().into_floating_input(), // ...
txd: pins.p0_06.degrade().into_push_pull_output(Level::High),
cts: Some(pins.p0_07.degrade().into_floating_input()),
rts: Some(pins.p0_05.degrade().into_push_pull_output(Level::High)),
};
// todo! Instantiate the UARTE peripheral
let uarte = hal::uarte::Uarte::new(periph.UARTE0, pins, Parity::INCLUDED, Baudrate::BAUD115200); // ...
// --- Exercise --- 🔼
Ok(Board { Ok(Board {
leds: Leds { leds: Leds {
@ -249,20 +216,13 @@ pub fn init() -> Result<Board, ()> {
led_4: Led { inner: led_4 }, led_4: Led { inner: led_4 },
}, },
// --- Exercise --- 🔽 // todo! Create an instance of the struct that contains all the single buttons.
buttons: Buttons { // ...
b_1: Button { inner: b_1},
b_2: Button { inner: b_2},
b_3: Button { inner: b_3},
b_4: Button { inner: b_4},
},
// --- Exercise --- 🔼
timer: Timer { inner: timer }, timer: Timer { inner: timer },
// --- Exercise --- 🔽 // todo! Create an instance of the UARTE struct
uarte: Uarte { inner: uarte }, // ...
// --- Exercise --- 🔼
}) })
} else { } else {
Err(()) Err(())