From 6ab5634c54c5a72f17cd7f79aa4260aeb24fd3e8 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Tue, 11 Jan 2022 13:24:55 -0800 Subject: [PATCH] Various small improvements, formatting, making things more consistent --- esp-hal-common/src/delay.rs | 8 +++- esp-hal-common/src/serial.rs | 81 +++++++++++++++++++++--------------- esp-hal-common/src/timer.rs | 47 ++++++++++++--------- 3 files changed, 81 insertions(+), 55 deletions(-) diff --git a/esp-hal-common/src/delay.rs b/esp-hal-common/src/delay.rs index b2763b002..b81f59434 100644 --- a/esp-hal-common/src/delay.rs +++ b/esp-hal-common/src/delay.rs @@ -47,12 +47,16 @@ mod delay { } impl Delay { - /// Instantiate the `Delay` driver, taking ownership of the `SYSTIMER` - /// peripheral struct + /// Create a new Delay instance pub fn new(systimer: SYSTIMER) -> Self { Self { systimer } } + /// Return the raw interface to the underlying SYSTIMER instance + pub fn free(self) -> SYSTIMER { + self.systimer + } + /// Delay for the specified number of microseconds pub fn delay(&self, us: u32) { let t0 = self.unit0_value(); diff --git a/esp-hal-common/src/serial.rs b/esp-hal-common/src/serial.rs index 4025d2371..39bce3f0e 100644 --- a/esp-hal-common/src/serial.rs +++ b/esp-hal-common/src/serial.rs @@ -18,6 +18,7 @@ pub struct Serial { } impl Serial { + /// Create a new UART instance pub fn new(uart: T) -> Result { let mut serial = Serial { uart }; serial.uart.disable_rx_interrupts(); @@ -25,6 +26,11 @@ impl Serial { Ok(serial) } + + /// Return the raw interface to the underlying UART instance + pub fn free(self) -> T { + self.uart + } } /// UART peripheral instance @@ -112,7 +118,44 @@ pub trait Instance { } } -impl Write for Serial { +impl Instance for UART0 { + #[inline(always)] + fn register_block(&self) -> &RegisterBlock { + self + } +} + +impl Instance for UART1 { + #[inline(always)] + fn register_block(&self) -> &RegisterBlock { + self + } +} + +#[cfg(any(feature = "esp32", feature = "esp32s3"))] +impl Instance for UART2 { + #[inline(always)] + fn register_block(&self) -> &RegisterBlock { + self + } +} + +impl core::fmt::Write for Serial +where + T: Instance, +{ + fn write_str(&mut self, s: &str) -> core::fmt::Result { + s.as_bytes() + .iter() + .try_for_each(|c| nb::block!(self.write(*c))) + .map_err(|_| core::fmt::Error) + } +} + +impl Write for Serial +where + T: Instance, +{ type Error = Error; fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { @@ -137,7 +180,10 @@ impl Write for Serial { } } -impl Read for Serial { +impl Read for Serial +where + T: Instance, +{ type Error = Error; fn read(&mut self) -> nb::Result { @@ -156,34 +202,3 @@ impl Read for Serial { } } } - -impl core::fmt::Write for Serial { - fn write_str(&mut self, s: &str) -> core::fmt::Result { - s.as_bytes() - .iter() - .try_for_each(|c| nb::block!(self.write(*c))) - .map_err(|_| core::fmt::Error) - } -} - -impl Instance for UART0 { - #[inline(always)] - fn register_block(&self) -> &RegisterBlock { - self - } -} - -impl Instance for UART1 { - #[inline(always)] - fn register_block(&self) -> &RegisterBlock { - self - } -} - -#[cfg(any(feature = "esp32", feature = "esp32s3"))] -impl Instance for UART2 { - #[inline(always)] - fn register_block(&self) -> &RegisterBlock { - self - } -} diff --git a/esp-hal-common/src/timer.rs b/esp-hal-common/src/timer.rs index aa1a34117..2d266bf1a 100644 --- a/esp-hal-common/src/timer.rs +++ b/esp-hal-common/src/timer.rs @@ -8,26 +8,33 @@ use void::Void; use crate::pac::{timg0::RegisterBlock, TIMG0, TIMG1}; -/// General-purpose timer -pub struct Timer { - timg: T, -} - /// Custom timer error type +#[derive(Debug)] pub enum Error { TimerActive, TimerInactive, AlarmInactive, } +/// General-purpose timer +pub struct Timer { + timg: T, +} + /// Timer driver impl Timer where T: Instance, { + /// Create a new timer instance pub fn new(timg: T) -> Self { Self { timg } } + + /// Return the raw interface to the underlying timer instance + pub fn free(self) -> T { + self.timg + } } /// Timer peripheral instance @@ -119,6 +126,20 @@ pub trait Instance { } } +impl Instance for TIMG0 { + #[inline(always)] + fn register_block(&self) -> &RegisterBlock { + self + } +} + +impl Instance for TIMG1 { + #[inline(always)] + fn register_block(&self) -> &RegisterBlock { + self + } +} + impl CountDown for Timer where T: Instance, @@ -143,7 +164,7 @@ where fn wait(&mut self) -> nb::Result<(), Void> { if !self.timg.is_counter_active() { - panic!("Called wait on an inactive timer!"); + panic!("Called wait on an inactive timer!") } let reg_block = self.timg.register_block(); @@ -188,17 +209,3 @@ where self.timg.set_wdt_enabled(false); } } - -impl Instance for TIMG0 { - #[inline(always)] - fn register_block(&self) -> &RegisterBlock { - self - } -} - -impl Instance for TIMG1 { - #[inline(always)] - fn register_block(&self) -> &RegisterBlock { - self - } -}