Skip to content

Commit

Permalink
Cleanup device initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rblaze committed Jan 20, 2025
1 parent 6b6216d commit 62285e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
20 changes: 13 additions & 7 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ use crate::pac::adc::chselr1;
use crate::pac::ADC;
use crate::rcc::{Rcc, ResetEnable};

pub trait AdcExt {
fn constrain(self, rcc: &Rcc) -> Adc;
}

pub struct Adc {
adc: ADC,
vref_cache: Option<u16>,
}

impl Adc {
pub fn new(adc: ADC, rcc: &Rcc) -> Self {
impl AdcExt for ADC {
fn constrain(self, rcc: &Rcc) -> Adc {
ADC::enable(rcc);
ADC::reset(rcc);

// Enable ADC voltage regulator.
adc.cr().modify(|_, w| w.advregen().enabled());
self.cr().modify(|_, w| w.advregen().enabled());

// RM0444 15.3.3 Calibration can only be initiated when the ADC voltage regulator is
// enabled (ADVREGEN = 1 and tADCVREG_SETUP has elapsed) and the ADC is disabled
Expand All @@ -24,18 +28,20 @@ impl Adc {
// Round up for a safety margin.
cortex_m::asm::delay(2000);

adc.cfgr1().modify(|_, w| w.chselrmod().sequence());
self.cfgr1().modify(|_, w| w.chselrmod().sequence());

// Set clock to PCLK/2.
// TODO: make this configurable.
adc.cfgr2().modify(|_, w| w.ckmode().pclk_div2());
self.cfgr2().modify(|_, w| w.ckmode().pclk_div2());

Self {
adc,
Adc {
adc: self,
vref_cache: None,
}
}
}

impl Adc {
pub fn calibrate(&mut self) {
self.adc.isr().write(|w| w.eocal().clear());
self.adc.cr().modify(|_, w| w.adcal().start_calibration());
Expand Down
4 changes: 2 additions & 2 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Config {

/// Extension trait to create I2C bus from a raw device
pub trait I2cExt<I2C> {
fn i2c<SDA, SCL>(self, sda: SDA, scl: SCL, config: &Config, rcc: &Rcc) -> I2c<I2C>
fn constrain<SDA, SCL>(self, sda: SDA, scl: SCL, config: &Config, rcc: &Rcc) -> I2c<I2C>
where
SDA: SdaPin<I2C>,
SCL: SclPin<I2C>;
Expand Down Expand Up @@ -132,7 +132,7 @@ macro_rules! i2c {
)+

impl I2cExt<$I2C> for $I2C {
fn i2c<SDA, SCL>(self, sda: SDA, scl: SCL, config: &Config, rcc: &Rcc) -> I2c<$I2C>
fn constrain<SDA, SCL>(self, sda: SDA, scl: SCL, config: &Config, rcc: &Rcc) -> I2c<$I2C>
where
SDA: SdaPin<$I2C>,
SCL: SclPin<$I2C>,
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#![no_std]
#![deny(unsafe_code)]

// TODO: cleanup HAL modules
// 1. use constrain() for creating peripherals via extension traits
// 2. except for GPIO, which is canonically split()
// 3. and except for timers, which can be turned into counter, pwm, etc

pub mod adc;
pub mod exti;
pub mod gpio;
Expand Down
14 changes: 11 additions & 3 deletions src/timer/lptim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use crate::pac::{LPTIM1, LPTIM2};
use crate::rcc::lptim::{LptimClock, LptimClockExt};
use crate::rcc::{Rcc, ResetEnable};

pub trait LptimExt {
fn constrain(self) -> LowPowerTimer<Self>
where
Self: Sized;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LptimPrescaler {
Div1 = 0b000,
Expand Down Expand Up @@ -50,11 +56,13 @@ pub struct LptimCounter<TIM, STATE> {

macro_rules! low_power_timer {
($TIM:ident) => {
impl LowPowerTimer<$TIM> {
pub fn new(timer: $TIM) -> Self {
Self { timer }
impl LptimExt for $TIM {
fn constrain(self) -> LowPowerTimer<Self> {
LowPowerTimer { timer: self }
}
}

impl LowPowerTimer<$TIM> {
pub fn upcounter(
self,
clock: LptimClock,
Expand Down
14 changes: 11 additions & 3 deletions src/timer/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use crate::gpio::gpioe::{PE3, PE4, PE5, PE6};
#[cfg(feature = "stm32g0b1")]
use crate::pac::TIM4;

pub trait TimerExt {
fn constrain(self) -> Timer<Self>
where
Self: Sized;
}

/// Wrapper for timer peripheral.
#[derive(Debug)]
pub struct Timer<TIM> {
Expand All @@ -34,11 +40,13 @@ pub struct Pwm<TIM> {

macro_rules! general_purpose_timer {
($TIM:ident, $REG:tt) => {
impl Timer<$TIM> {
pub fn new(timer: $TIM) -> Self {
Self { timer }
impl TimerExt for $TIM {
fn constrain(self) -> Timer<Self> {
Timer { timer: self }
}
}

impl Timer<$TIM> {
pub fn upcounter(self, prescaler: u16, limit: $REG, rcc: &Rcc) -> Counter<$TIM> {
$TIM::enable(rcc);
$TIM::reset(rcc);
Expand Down

0 comments on commit 62285e2

Please # to comment.