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

Improve module 8 #125

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ impl Dial {
self.cols.iter_mut().for_each(|c| c.set_high());
}

pub fn light_only(&mut self, dir: Direction) {
pub fn set_light_direction(&mut self, dir: Direction) {
let (row, col) = dir.led_index();
self.clear();
self.rows[row].set_high();
self.cols[col].set_low();
}

/// Operate the dial. This function is useful for running
/// Operate the dial autonomously. This function is useful for running
/// in a separate task.
///
/// Useful for exercise 8.1.2
pub async fn run(
mut self,
receiver: embassy_sync::channel::Receiver<'_, NoopRawMutex, Direction, 4>,
Expand Down
12 changes: 8 additions & 4 deletions exercises/8-embedded/3-async-on-embedded/1-compass/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use dial::{dir_channel, Dial, Direction};
use embassy_nrf::{self as hal, peripherals::TWISPI0, twim::Twim};
use embassy_time::Delay;
use embedded_hal_async::delay::DelayNs;
use hal::twim;
use lsm303agr::{interface::I2cInterface, mode::MagOneShot, Lsm303agr, MagnetometerId};
Expand All @@ -19,7 +18,6 @@ hal::bind_interrupts!(struct Irqs {

#[embassy_executor::main]
async fn main(s: embassy_executor::Spawner) -> ! {

// Init RTT control block
rtt_init_print!();

Expand All @@ -31,21 +29,27 @@ async fn main(s: embassy_executor::Spawner) -> ! {

let config = twim::Config::default();
let twim0 = Twim::new(dp.TWISPI0, Irqs, dp.P0_16, dp.P0_08, config);
let delay = embassy_time::Delay;

let dial: Dial = todo!("Initialize Dial");

let mut sensor: Lsm303agr<I2cInterface<Twim<TWISPI0>>, MagOneShot> = todo!("Initialize LSM303AGR driver given the twim0 peripheral");
let mut sensor: Lsm303agr<I2cInterface<Twim<TWISPI0>>, MagOneShot> =
todo!("Initialize LSM303AGR driver given the twim0 peripheral");
let id: MagnetometerId = todo!("Read the magnetometer ID using the driver");
rprintln!("{:#02x?}", id);

todo!("Initialize the driver");
todo!("Set magnetometer mode to high resolution and output data rate to 100Hz");


todo!("Change the magnetometer to continuous mode");
todo!("Enable magnetometer offset cancellation");

loop {
todo!("Read data and update the dial accordingly");

// Steps:
// - Read the MagneticField data
// - Convert to a direction using: Direction::from(mag_data)
// - Update the dial: dial.set_light_direction(direction)
}
}
69 changes: 21 additions & 48 deletions slides/8_1-embedded-ecosystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ layout: three-slots
- Similar to CMSIS register definitions

::right::

## `cortex-m-rt`
<br/>

Expand Down Expand Up @@ -133,7 +132,7 @@ WDT->CONFIG.bit.PER = WDT_CONFIG_PER_16;
let dp = atmsamd21e::Peripherals::take().unwrap();

let is_8_cycles = dp.WDT.CONFIG.read().per().is_8();
dp.WDT.CONFIG.modify(|_, w| w.per()._8());
dp.WDT.CONFIG.modify(|_, w| w.per()._16());
```

---
Expand Down Expand Up @@ -183,31 +182,27 @@ layout: full
---

```rust
hal::bind_interrupts!(struct Irqs {
SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<hal::peripherals::TWISPI0>;
});

#[cortex_m_rt::entry]
fn main() -> ! {
// Init RTT control block
rtt_init_print!();

let _cp = cortex_m::Peripherals::take().unwrap();
// Use ``dp` to get a handle to the peripherals
let dp = hal::init(Default::default());
//! This example shows how to use UART (Universal asynchronous receiver-transmitter) in the RP2040 chip.

rprintln!("Starting");
#![no_std]
#![no_main]

let config = twim::Config::default();
let mut twim0 = Twim::new(dp.TWISPI0, Irqs, dp.P0_03, dp.P0_04, config);
use embassy_rp::uart;
use {defmt_rtt as _, panic_probe as _};

rprintln!("Reading...");
#[cortex_m_rt::entry]
fn main() -> ! {
// Init the hal
let p = embassy_rp::init(Default::default());

let mut buf = [0u8; 16];
twim0.blocking_write_read(0xAB, &mut [0x00], &mut buf).unwrap();
// Init the uart
let config = uart::Config::default();
let mut uart = uart::Uart::new_with_rtscts_blocking(p.UART0, p.PIN_0, p.PIN_1, p.PIN_3, p.PIN_2, config);

rprintln!("Read: {:02x?}", buf);
exit();
loop {
uart.blocking_write("hello there!\r\n".as_bytes()).unwrap();
cortex_m::asm::delay(1_000_000);
}
}
```

Expand Down Expand Up @@ -246,14 +241,15 @@ layout: with-footer
---
layout: with-footer
---

# `embedded-hal`

The glue of the entire ecosytem
- Contains abstractions for many common operations

<br/>

### SPI example trait:
### SPI trait:
```rust
pub trait SpiDevice<Word: Copy + 'static = u8>: ErrorType {
fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error>;
Expand Down Expand Up @@ -300,38 +296,15 @@ layout: with-footer

</table>

---
layout: with-footer
---

# Typestate

State encoded in the *type* of the variable

```rust
// https://github.com/embassy-rs/embassy/blob/main/examples/nrf52840/src/bin/blinky.rs

use nrf52833_hal::gpio::{Pin, p0::P0_04, Input, PullDown, Output, PushPull};

/// Take an nRF pin.
/// It must be:
/// - Port 0 pin 4 (Compile time constant)
/// - Configured as input
fn read_status(pin: Input<'_, P0_04>) -> bool {}

/// Take an nRF pin.
/// It must be:
/// - Any port and pin (Runtime variable)
/// - Configured as output
fn set_led_level(pin: Output<'_, AnyPin>, enabled: bool) {}
```

---
layout: with-footer
---

# Runtimes

<br/><br/><br/>

<table class="horizontal">
<tr>
<th><center>🔸<br/><strong>Bare metal<br/>+ interrupts</strong></center></th>
Expand Down
Loading