Skip to content

Commit

Permalink
timer initial & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Aug 22, 2023
1 parent d835d76 commit b8a7632
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 222 deletions.
194 changes: 97 additions & 97 deletions src/gpio/alt/c0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,103 @@ pub mod tim1 {
}
}

pub mod tim3 {
use super::*;

pin! {
<Ch1> default:PushPull for [
PA6<1>,

#[cfg(feature = "gpio-c0xx_453")]
PB4<1>,

PB6<12>,

PB7<11>,

#[cfg(feature = "gpio-c0xx_453")]
PB8<3>,

#[cfg(feature = "gpio-c0xx_453")]
PC6<1>,
],

<Ch2> default:PushPull for [
PA7<1>,

#[cfg(feature = "gpio-c0xx_453")]
PB3<3>,

#[cfg(feature = "gpio-c0xx_453")]
PB5<1>,

PB6<13>,

#[cfg(feature = "gpio-c0xx_453")]
PB9<3>,

#[cfg(feature = "gpio-c0xx_453")]
PC7<1>,

PC14<11>,
],

<Ch3> default:PushPull for [
PA8<11>,

#[cfg(feature = "gpio-c0xx_453")]
PB0<1>,

#[cfg(feature = "gpio-c0xx_453")]
PB5<3>,

PB6<3>,

PC15<3>,
],

<Ch4> default:PushPull for [
PA8<12>,

#[cfg(feature = "gpio-c0xx_453")]
PB1<1>,

PB7<3>,
],
}

pin! {
<Etr, PushPull> for [
PA2<3>,

PA9<3>,

PA13<3>,

#[cfg(feature = "gpio-c0xx_453")]
PD2<1>,
],

}

use crate::pac::TIM3 as TIM;
impl TimCPin<0> for TIM {
type Ch<Otype> = Ch1<Otype>;
}
impl TimCPin<1> for TIM {
type Ch<Otype> = Ch2<Otype>;
}
impl TimCPin<2> for TIM {
type Ch<Otype> = Ch3<Otype>;
}
impl TimCPin<3> for TIM {
type Ch<Otype> = Ch4<Otype>;
}
impl TimEtr for TIM {
type Etr = Etr;
}
}

pub mod tim14 {
use super::*;

Expand Down Expand Up @@ -518,103 +615,6 @@ pub mod tim17 {
}
}

pub mod tim3 {
use super::*;

pin! {
<Ch1> default:PushPull for [
PA6<1>,

#[cfg(feature = "gpio-c0xx_453")]
PB4<1>,

PB6<12>,

PB7<11>,

#[cfg(feature = "gpio-c0xx_453")]
PB8<3>,

#[cfg(feature = "gpio-c0xx_453")]
PC6<1>,
],

<Ch2> default:PushPull for [
PA7<1>,

#[cfg(feature = "gpio-c0xx_453")]
PB3<3>,

#[cfg(feature = "gpio-c0xx_453")]
PB5<1>,

PB6<13>,

#[cfg(feature = "gpio-c0xx_453")]
PB9<3>,

#[cfg(feature = "gpio-c0xx_453")]
PC7<1>,

PC14<11>,
],

<Ch3> default:PushPull for [
PA8<11>,

#[cfg(feature = "gpio-c0xx_453")]
PB0<1>,

#[cfg(feature = "gpio-c0xx_453")]
PB5<3>,

PB6<3>,

PC15<3>,
],

<Ch4> default:PushPull for [
PA8<12>,

#[cfg(feature = "gpio-c0xx_453")]
PB1<1>,

PB7<3>,
],
}

pin! {
<Etr, PushPull> for [
PA2<3>,

PA9<3>,

PA13<3>,

#[cfg(feature = "gpio-c0xx_453")]
PD2<1>,
],

}

use crate::pac::TIM3 as TIM;
impl TimCPin<0> for TIM {
type Ch<Otype> = Ch1<Otype>;
}
impl TimCPin<1> for TIM {
type Ch<Otype> = Ch2<Otype>;
}
impl TimCPin<2> for TIM {
type Ch<Otype> = Ch3<Otype>;
}
impl TimCPin<3> for TIM {
type Ch<Otype> = Ch4<Otype>;
}
impl TimEtr for TIM {
type Etr = Etr;
}
}

pub mod usart1 {
use super::*;

Expand Down
1 change: 1 addition & 0 deletions src/rcc/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ macro_rules! bus_reset {
macro_rules! bus {
($($PER:ident => ($busX:ty, $($en:ident)?, $($smen:ident)?, $($rst:ident)?),)+) => {
$(
impl crate::Sealed for crate::pac::$PER {}
impl RccBus for crate::stm32::$PER {
type Bus = $busX;
}
Expand Down
31 changes: 22 additions & 9 deletions src/serial/usart.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use core::marker::PhantomData;
use core::{fmt, ops::Deref};

use crate::gpio::{
alt::{SerialAsync as CommonPins, SerialRs485 as Rs485},
Expand Down Expand Up @@ -72,14 +72,27 @@ impl Event {
}
}

impl crate::Sealed for pac::USART1 {}
impl crate::Sealed for pac::USART2 {}

pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + CommonPins + Rs485 {
type RegisterBlock;
impl Instance for pac::USART1 {
fn ptr() -> *const pac::usart1::RegisterBlock {
pac::USART1::ptr()
}
}
impl Instance for pac::USART2 {
fn ptr() -> *const pac::usart1::RegisterBlock {
pac::USART2::ptr()
}
}

pub trait Instance:
crate::Sealed
+ rcc::Enable
+ rcc::Reset
+ CommonPins
+ Rs485
+ Deref<Target = pac::usart1::RegisterBlock>
{
#[doc(hidden)]
fn ptr() -> *const Self::RegisterBlock;
fn ptr() -> *const pac::usart1::RegisterBlock;
}

/// Serial receiver
Expand Down Expand Up @@ -149,7 +162,7 @@ impl<USART: Instance> SerialExt for USART {
}
}

impl<USART> fmt::Write for Serial<USART>
impl<USART: Instance> fmt::Write for Serial<USART>
where
Serial<USART>: hal::serial::Write<u8>,
{
Expand All @@ -159,7 +172,7 @@ where
}
}

impl<USART> fmt::Write for Tx<USART>
impl<USART: Instance> fmt::Write for Tx<USART>
where
Tx<USART>: hal::serial::Write<u8>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<SPI: Instance> Spi<SPI> {
}

pub fn release(self) -> (SPI, (SPI::Sck, SPI::Miso, SPI::Mosi)) {
(self.spi, self.pins.release())
(self.spi, self.pins)
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::rcc::*;
use crate::stm32::*;
use crate::time::{Hertz, MicroSecond};
use core::marker::PhantomData;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use hal::timer::{CountDown, Periodic};
Expand All @@ -21,10 +20,14 @@ pub struct Timer<TIM> {
tim: TIM,
}

pub struct Channel1;
pub struct Channel2;
pub struct Channel3;
pub struct Channel4;
#[allow(non_upper_case_globals)]
pub const Channel1: u8 = 0;
#[allow(non_upper_case_globals)]
pub const Channel2: u8 = 1;
#[allow(non_upper_case_globals)]
pub const Channel3: u8 = 2;
#[allow(non_upper_case_globals)]
pub const Channel4: u8 = 3;

/// System timer
impl Timer<SYST> {
Expand Down
13 changes: 6 additions & 7 deletions src/timer/opm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! # One-pulse Mode
use crate::gpio::alt::TimCPin;
use crate::gpio::PushPull;
use crate::rcc::*;
use crate::stm32::*;
use crate::time::{Hertz, MicroSecond};
use crate::timer::pins::TimerPin;
use crate::timer::*;
use core::marker::PhantomData;
use fugit::RateExtU32;
Expand All @@ -11,9 +12,8 @@ pub trait OpmExt: Sized {
fn opm(self, period: MicroSecond, rcc: &mut Rcc) -> Opm<Self>;
}

pub struct OpmPin<TIM, CH> {
pub struct OpmPin<TIM, const CH: u8> {
tim: PhantomData<TIM>,
channel: PhantomData<CH>,
delay: u32,
}

Expand All @@ -23,14 +23,13 @@ pub struct Opm<TIM> {
}

impl<TIM> Opm<TIM> {
pub fn bind_pin<PIN>(&self, pin: PIN) -> OpmPin<TIM, PIN::Channel>
pub fn bind_pin<const C: u8, PIN>(&self, pin: impl Into<PIN>) -> OpmPin<TIM, C>
where
PIN: TimerPin<TIM>,
TIM: TimCPin<C, Ch<PushPull> = PIN>,
{
pin.setup();
let _ = pin.into();
OpmPin {
tim: PhantomData,
channel: PhantomData,
delay: 1,
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/timer/pins.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
use crate::gpio::*;
use crate::gpio::{AltFunction, DefaultMode};
use crate::stm32::*;
use crate::timer::*;
pub trait TimerPin<TIM> {
type Channel;
#[allow(non_upper_case_globals)]
const Channel: u8;
fn setup(&self);
fn release(self) -> Self;
Expand All @@ -28,10 +30,11 @@ impl<TIM, PIN: TimerPin<TIM>> TriggerPin<TIM, PIN> {
}
macro_rules! timer_pins {
($TIMX:ident, [ $(($ch:ty, $pin:ty, $af_mode:expr),)+ ]) => {
($TIMX:ident, [ $(($ch:ident, $pin:ty, $af_mode:expr),)+ ]) => {
$(
impl TimerPin<$TIMX> for $pin {
type Channel = $ch;
#[allow(non_upper_case_globals)]
const Channel: u8 = $ch;
fn setup(&self) {
self.set_alt_mode($af_mode);
Expand Down Expand Up @@ -173,3 +176,4 @@ timer_pins!(TIM17, [
timer_pins!(TIM17, [
(Channel1, PB7<DefaultMode>, AltFunction::AF2),
]);
*/
Loading

0 comments on commit b8a7632

Please sign in to comment.