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

Restructure folders, add pmic abstraction #29

Merged
merged 1 commit into from
May 31, 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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ stm32h7xx-hal = { version = "0.16.0", features = [
defmt = "=0.3.8"
defmt-rtt = "0.4.1"
panic-probe = { version = "0.3", features = ["print-defmt"] }
embedded-hal-v1 = { version = "1.0.0", package = "embedded-hal" }
embedded-hal-v0 = { version = "0.2.6", package = "embedded-hal", features = ["unproven"] }
embedded-hal-async = "1.0.0"

[dev-dependencies]
rtic = { version = "2.1.1", features = ["thumbv7-backend"] }
Expand Down
11 changes: 4 additions & 7 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
#![no_main]

use defmt::info;
use portenta_h7::{
board::{self, Board},
led,
};
use portenta_h7::board::{self, Board, LedBlue, LedGreen, LedRed};
use rtic::app;
use rtic_monotonics::systick::prelude::*;

Expand All @@ -25,9 +22,9 @@ mod app {

#[local]
struct Local {
led_red: led::user::Red,
led_green: led::user::Green,
led_blue: led::user::Blue,
led_red: LedRed,
led_green: LedGreen,
led_blue: LedBlue,
}

#[init]
Expand Down
15 changes: 6 additions & 9 deletions examples/usb_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
#![no_main]

use defmt::{error, info};
use portenta_h7::{
board::{self, Board, UsbBus, USB},
led::{self, Led},
};
use portenta_h7::board::{self, Board, LedBlue, UsbBusImpl};
use rtic::app;
use rtic_monotonics::systick::prelude::*;
use static_cell::StaticCell;
Expand All @@ -27,16 +24,16 @@ mod app {
const APP_BUFF_SIZE: usize = 64;
const USB_BUS_BUFFER_SIZE: usize = 1024;
static USB_BUS_BUFFER: StaticCell<[u32; USB_BUS_BUFFER_SIZE]> = StaticCell::new();
static USB_ALLOCATOR: StaticCell<UsbBusAllocator<UsbBus<USB>>> = StaticCell::new();
static USB_ALLOCATOR: StaticCell<UsbBusAllocator<UsbBusImpl>> = StaticCell::new();

#[shared]
struct Shared {}

#[local]
struct Local {
led_blue: led::user::Blue,
usb_dev: UsbDevice<'static, UsbBus<USB>>,
usb_serial_port: CdcAcmClass<'static, UsbBus<USB>>,
led_blue: LedBlue,
usb_dev: UsbDevice<'static, UsbBusImpl>,
usb_serial_port: CdcAcmClass<'static, UsbBusImpl>,
}

#[init]
Expand All @@ -49,7 +46,7 @@ mod app {
let Board { led_blue, usb, .. } = Board::take();

// Init USB stack
let usb_bus = USB_ALLOCATOR.init(UsbBus::new(
let usb_bus = USB_ALLOCATOR.init(UsbBusImpl::new(
usb,
USB_BUS_BUFFER.init([0; USB_BUS_BUFFER_SIZE]),
));
Expand Down
55 changes: 46 additions & 9 deletions src/board.rs → src/board/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
//! board

use crate::{hal, led, sys};
use crate::{
drivers::{led, pmic},
hal, sys,
};
use core::sync::atomic::{AtomicBool, Ordering};
pub use hal::usb_hs::{UsbBus, USB1_ULPI as USB};
use hal::{gpio::PinState, pac, prelude::*, rcc, time::Hertz, usb_hs::USB1_ULPI};
use defmt::debug;
use hal::{
gpio::{Output, Pin, PinState, PushPull},
pac,
prelude::*,
rcc,
time::Hertz,
usb_hs::{UsbBus, USB1_ULPI},
};

type DigitalOutputPin<const P: char, const N: u8> = Pin<P, N, Output<PushPull>>;
pub type LedRed = led::Led<DigitalOutputPin<'K', 5>>;
pub type LedGreen = led::Led<DigitalOutputPin<'K', 6>>;
pub type LedBlue = led::Led<DigitalOutputPin<'K', 7>>;
pub type UsbPer = USB1_ULPI;
pub type UsbBusImpl = UsbBus<UsbPer>;
pub const CORE_FREQUENCY: Hertz = Hertz::from_raw(480_000_000);

pub struct Board {
pub led_red: led::user::Red,
pub led_green: led::user::Green,
pub led_blue: led::user::Blue,
pub usb: USB1_ULPI,
pub led_red: LedRed,
pub led_green: LedGreen,
pub led_blue: LedBlue,
pub usb: UsbPer,
}

impl Board {
Expand Down Expand Up @@ -41,7 +57,7 @@ impl Board {
debug_assert_eq!(sys::Clk::get_source(), Some(sys::ClkSource::Pll1));
debug_assert_eq!(sys::Clk::get_pll_source(), sys::PllSourceVariant::Hse);

// USB
// GPIOs
let (gpioa, gpiob, gpioc, gpioh, gpioi, gpioj) = {
(
dp.GPIOA.split(ccdr.peripheral.GPIOA),
Expand Down Expand Up @@ -78,12 +94,33 @@ impl Board {

// User LEDs
let gpiok = dp.GPIOK.split(ccdr.peripheral.GPIOK);
let (led_red, led_green, led_blue) = (
let (output_k5, output_k6, output_k7) = (
gpiok.pk5.into_push_pull_output_in_state(PinState::High),
gpiok.pk6.into_push_pull_output_in_state(PinState::High),
gpiok.pk7.into_push_pull_output_in_state(PinState::High),
);

let led_red = led::Led::new(output_k5);
let led_green = led::Led::new(output_k6);
let led_blue = led::Led::new(output_k7);

// PMIC
let (i2c1_scl, i2c1_sda) = (
gpiob.pb6.into_alternate_open_drain(),
gpiob.pb7.into_alternate_open_drain(),
);
let i2c1 = dp.I2C1.i2c(
(i2c1_scl, i2c1_sda),
400.kHz(),
ccdr.peripheral.I2C1,
&ccdr.clocks,
);
let mut pmic = pmic::Pmic::new(i2c1);
match pmic.device_id() {
Ok(id) => debug!("PMIC device ID: {:X}", id),
Err(_) => debug!("PMIC device ID read error"),
}

Board {
led_red,
led_green,
Expand Down
27 changes: 27 additions & 0 deletions src/drivers/led.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! led

pub struct Led<PIN> {
pin: PIN,
}

impl<PIN> Led<PIN> {
pub fn new(pin: PIN) -> Self {
Self { pin }
}
}

impl<PIN: embedded_hal_v0::digital::v2::OutputPin> Led<PIN> {
pub fn on(&mut self) {
let _ = self.pin.set_low();
}

pub fn off(&mut self) {
let _ = self.pin.set_high();
}
}

impl<PIN: embedded_hal_v0::digital::v2::ToggleableOutputPin> Led<PIN> {
pub fn toggle(&mut self) {
let _ = self.pin.toggle();
}
}
2 changes: 2 additions & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod led;
pub mod pmic;
39 changes: 39 additions & 0 deletions src/drivers/pmic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Pmic WIP

const PMIC_ADDR: u8 = 0x08;

#[derive(Clone, Copy, Debug)]
pub enum Reg {
DeviceId = 0x00,
}

impl Reg {
pub const fn as_u8(&self) -> u8 {
*self as u8
}
}

#[derive(Clone, Copy, Debug)]
pub enum Error {
I2cError,
}

pub struct Pmic<I2C> {
i2c: I2C,
}

impl<I2C> Pmic<I2C> {
pub fn new(i2c: I2C) -> Self {
Self { i2c }
}
}

impl<I2C: embedded_hal_v0::blocking::i2c::WriteRead> Pmic<I2C> {
pub fn device_id(&mut self) -> Result<u8, Error> {
let mut data = [0u8];
self.i2c
.write_read(PMIC_ADDR, &[Reg::DeviceId.as_u8()], &mut data)
.map_err(|_| Error::I2cError)?;
Ok(data[0])
}
}
28 changes: 0 additions & 28 deletions src/interrupt.rs

This file was deleted.

44 changes: 0 additions & 44 deletions src/led.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![no_std]

pub mod board;
pub mod interrupt;
pub mod led;
pub mod drivers;
mod sys;
pub use cortex_m_rt::entry;
#[allow(unused)]
Expand Down
Loading