diff --git a/Cargo.toml b/Cargo.toml index d1b1c4c..5bd9a47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/examples/blinky.rs b/examples/blinky.rs index e13de23..587f099 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -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::*; @@ -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] diff --git a/examples/usb_echo.rs b/examples/usb_echo.rs index 45a36c8..69e4da8 100644 --- a/examples/usb_echo.rs +++ b/examples/usb_echo.rs @@ -7,14 +7,11 @@ #![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; -use usb_device::{class_prelude::UsbBusAllocator, prelude::*}; +use usb_device::{bus::UsbBus, class_prelude::UsbBusAllocator, prelude::*}; use usbd_serial::CdcAcmClass; systick_monotonic!(Mono, 1000); @@ -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>> = StaticCell::new(); + static USB_ALLOCATOR: StaticCell> = StaticCell::new(); #[shared] struct Shared {} #[local] struct Local { - led_blue: led::user::Blue, - usb_dev: UsbDevice<'static, UsbBus>, - usb_serial_port: CdcAcmClass<'static, UsbBus>, + led_blue: LedBlue, + usb_dev: UsbDevice<'static, UsbBusImpl>, + usb_serial_port: CdcAcmClass<'static, UsbBusImpl>, } #[init] @@ -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]), )); diff --git a/src/board.rs b/src/board/mod.rs similarity index 67% rename from src/board.rs rename to src/board/mod.rs index 84caefd..21f3e13 100644 --- a/src/board.rs +++ b/src/board/mod.rs @@ -1,17 +1,35 @@ //! board -use crate::{hal, led, sys}; +#![allow(unused)] + +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 = Pin>; +pub type LedRed = led::Led>; +pub type LedGreen = led::Led>; +pub type LedBlue = led::Led>; +pub type UsbPer = USB1_ULPI; +pub type UsbBusImpl = UsbBus; 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 { @@ -41,7 +59,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), @@ -78,12 +96,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, diff --git a/src/drivers/led.rs b/src/drivers/led.rs new file mode 100644 index 0000000..0f0f809 --- /dev/null +++ b/src/drivers/led.rs @@ -0,0 +1,27 @@ +//! led + +pub struct Led { + pin: PIN, +} + +impl Led { + pub fn new(pin: PIN) -> Self { + Self { pin } + } +} + +impl Led { + pub fn on(&mut self) { + let _ = self.pin.set_low(); + } + + pub fn off(&mut self) { + let _ = self.pin.set_high(); + } +} + +impl Led { + pub fn toggle(&mut self) { + let _ = self.pin.toggle(); + } +} diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs new file mode 100644 index 0000000..6db2d64 --- /dev/null +++ b/src/drivers/mod.rs @@ -0,0 +1,2 @@ +pub mod led; +pub mod pmic; diff --git a/src/drivers/pmic.rs b/src/drivers/pmic.rs new file mode 100644 index 0000000..4778e30 --- /dev/null +++ b/src/drivers/pmic.rs @@ -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, +} + +impl Pmic { + pub fn new(i2c: I2C) -> Self { + Self { i2c } + } +} + +impl Pmic { + pub fn device_id(&mut self) -> Result { + let mut data = [0u8]; + self.i2c + .write_read(PMIC_ADDR, &[Reg::DeviceId.as_u8()], &mut data) + .map_err(|_| Error::I2cError)?; + Ok(data[0]) + } +} diff --git a/src/interrupt.rs b/src/interrupt.rs deleted file mode 100644 index f17c40d..0000000 --- a/src/interrupt.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! interrupt - -pub use crate::hal::pac::interrupt; -use crate::hal::{self, usb_hs::USB1_ULPI}; - -type CorePeripherals = cortex_m::Peripherals; - -pub trait InterruptEnabler { - fn enable_interrupt(&self); - fn disable_interrupt(&self); - fn set_interrupt_priority(&self, cp: &mut CorePeripherals, prio: u8); -} - -impl InterruptEnabler for USB1_ULPI { - fn enable_interrupt(&self) { - unsafe { cortex_m::peripheral::NVIC::unmask(hal::pac::Interrupt::OTG_HS) }; - } - - fn disable_interrupt(&self) { - cortex_m::peripheral::NVIC::mask(hal::pac::Interrupt::OTG_HS); - } - - fn set_interrupt_priority(&self, cp: &mut CorePeripherals, prio: u8) { - unsafe { - cp.NVIC.set_priority(hal::pac::Interrupt::OTG_HS, prio); - } - } -} diff --git a/src/led.rs b/src/led.rs deleted file mode 100644 index 4a3a9c4..0000000 --- a/src/led.rs +++ /dev/null @@ -1,44 +0,0 @@ -//! led - -pub use crate::hal::gpio::{gpiok::*, Output, Pin, PinState, PushPull}; -type DigitalOutputPin = Pin>; - -pub trait Led { - fn on(&mut self); - fn off(&mut self); - fn toggle(&mut self); -} - -pub mod user { - use super::*; - - pub type Red = DigitalOutputPin<'K', 5>; - pub type Green = DigitalOutputPin<'K', 6>; - pub type Blue = DigitalOutputPin<'K', 7>; - - // Marker trait - trait BoardLed {} - impl BoardLed for Red {} - impl BoardLed for Green {} - impl BoardLed for Blue {} - - impl Led for DigitalOutputPin - where - DigitalOutputPin: BoardLed, - { - #[inline] - fn on(&mut self) { - self.set_low(); - } - - #[inline] - fn off(&mut self) { - self.set_high(); - } - - #[inline] - fn toggle(&mut self) { - self.toggle(); - } - } -} diff --git a/src/lib.rs b/src/lib.rs index a3d775f..f9cfef6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)]