Skip to content

Commit a04621b

Browse files
committed
timer & fixes
1 parent d835d76 commit a04621b

File tree

9 files changed

+266
-222
lines changed

9 files changed

+266
-222
lines changed

src/gpio/alt/c0.rs

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,103 @@ pub mod tim1 {
396396
}
397397
}
398398

399+
pub mod tim3 {
400+
use super::*;
401+
402+
pin! {
403+
<Ch1> default:PushPull for [
404+
PA6<1>,
405+
406+
#[cfg(feature = "gpio-c0xx_453")]
407+
PB4<1>,
408+
409+
PB6<12>,
410+
411+
PB7<11>,
412+
413+
#[cfg(feature = "gpio-c0xx_453")]
414+
PB8<3>,
415+
416+
#[cfg(feature = "gpio-c0xx_453")]
417+
PC6<1>,
418+
],
419+
420+
<Ch2> default:PushPull for [
421+
PA7<1>,
422+
423+
#[cfg(feature = "gpio-c0xx_453")]
424+
PB3<3>,
425+
426+
#[cfg(feature = "gpio-c0xx_453")]
427+
PB5<1>,
428+
429+
PB6<13>,
430+
431+
#[cfg(feature = "gpio-c0xx_453")]
432+
PB9<3>,
433+
434+
#[cfg(feature = "gpio-c0xx_453")]
435+
PC7<1>,
436+
437+
PC14<11>,
438+
],
439+
440+
<Ch3> default:PushPull for [
441+
PA8<11>,
442+
443+
#[cfg(feature = "gpio-c0xx_453")]
444+
PB0<1>,
445+
446+
#[cfg(feature = "gpio-c0xx_453")]
447+
PB5<3>,
448+
449+
PB6<3>,
450+
451+
PC15<3>,
452+
],
453+
454+
<Ch4> default:PushPull for [
455+
PA8<12>,
456+
457+
#[cfg(feature = "gpio-c0xx_453")]
458+
PB1<1>,
459+
460+
PB7<3>,
461+
],
462+
}
463+
464+
pin! {
465+
<Etr, PushPull> for [
466+
PA2<3>,
467+
468+
PA9<3>,
469+
470+
PA13<3>,
471+
472+
#[cfg(feature = "gpio-c0xx_453")]
473+
PD2<1>,
474+
],
475+
476+
}
477+
478+
use crate::pac::TIM3 as TIM;
479+
impl TimCPin<0> for TIM {
480+
type Ch<Otype> = Ch1<Otype>;
481+
}
482+
impl TimCPin<1> for TIM {
483+
type Ch<Otype> = Ch2<Otype>;
484+
}
485+
impl TimCPin<2> for TIM {
486+
type Ch<Otype> = Ch3<Otype>;
487+
}
488+
impl TimCPin<3> for TIM {
489+
type Ch<Otype> = Ch4<Otype>;
490+
}
491+
impl TimEtr for TIM {
492+
type Etr = Etr;
493+
}
494+
}
495+
399496
pub mod tim14 {
400497
use super::*;
401498

@@ -518,103 +615,6 @@ pub mod tim17 {
518615
}
519616
}
520617

521-
pub mod tim3 {
522-
use super::*;
523-
524-
pin! {
525-
<Ch1> default:PushPull for [
526-
PA6<1>,
527-
528-
#[cfg(feature = "gpio-c0xx_453")]
529-
PB4<1>,
530-
531-
PB6<12>,
532-
533-
PB7<11>,
534-
535-
#[cfg(feature = "gpio-c0xx_453")]
536-
PB8<3>,
537-
538-
#[cfg(feature = "gpio-c0xx_453")]
539-
PC6<1>,
540-
],
541-
542-
<Ch2> default:PushPull for [
543-
PA7<1>,
544-
545-
#[cfg(feature = "gpio-c0xx_453")]
546-
PB3<3>,
547-
548-
#[cfg(feature = "gpio-c0xx_453")]
549-
PB5<1>,
550-
551-
PB6<13>,
552-
553-
#[cfg(feature = "gpio-c0xx_453")]
554-
PB9<3>,
555-
556-
#[cfg(feature = "gpio-c0xx_453")]
557-
PC7<1>,
558-
559-
PC14<11>,
560-
],
561-
562-
<Ch3> default:PushPull for [
563-
PA8<11>,
564-
565-
#[cfg(feature = "gpio-c0xx_453")]
566-
PB0<1>,
567-
568-
#[cfg(feature = "gpio-c0xx_453")]
569-
PB5<3>,
570-
571-
PB6<3>,
572-
573-
PC15<3>,
574-
],
575-
576-
<Ch4> default:PushPull for [
577-
PA8<12>,
578-
579-
#[cfg(feature = "gpio-c0xx_453")]
580-
PB1<1>,
581-
582-
PB7<3>,
583-
],
584-
}
585-
586-
pin! {
587-
<Etr, PushPull> for [
588-
PA2<3>,
589-
590-
PA9<3>,
591-
592-
PA13<3>,
593-
594-
#[cfg(feature = "gpio-c0xx_453")]
595-
PD2<1>,
596-
],
597-
598-
}
599-
600-
use crate::pac::TIM3 as TIM;
601-
impl TimCPin<0> for TIM {
602-
type Ch<Otype> = Ch1<Otype>;
603-
}
604-
impl TimCPin<1> for TIM {
605-
type Ch<Otype> = Ch2<Otype>;
606-
}
607-
impl TimCPin<2> for TIM {
608-
type Ch<Otype> = Ch3<Otype>;
609-
}
610-
impl TimCPin<3> for TIM {
611-
type Ch<Otype> = Ch4<Otype>;
612-
}
613-
impl TimEtr for TIM {
614-
type Etr = Etr;
615-
}
616-
}
617-
618618
pub mod usart1 {
619619
use super::*;
620620

src/rcc/enable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ macro_rules! bus_reset {
9999
macro_rules! bus {
100100
($($PER:ident => ($busX:ty, $($en:ident)?, $($smen:ident)?, $($rst:ident)?),)+) => {
101101
$(
102+
impl crate::Sealed for crate::pac::$PER {}
102103
impl RccBus for crate::stm32::$PER {
103104
type Bus = $busX;
104105
}

src/serial/usart.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use core::fmt;
21
use core::marker::PhantomData;
2+
use core::{fmt, ops::Deref};
33

44
use crate::gpio::{
55
alt::{SerialAsync as CommonPins, SerialRs485 as Rs485},
@@ -72,14 +72,27 @@ impl Event {
7272
}
7373
}
7474

75-
impl crate::Sealed for pac::USART1 {}
76-
impl crate::Sealed for pac::USART2 {}
77-
78-
pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + CommonPins + Rs485 {
79-
type RegisterBlock;
75+
impl Instance for pac::USART1 {
76+
fn ptr() -> *const pac::usart1::RegisterBlock {
77+
pac::USART1::ptr()
78+
}
79+
}
80+
impl Instance for pac::USART2 {
81+
fn ptr() -> *const pac::usart1::RegisterBlock {
82+
pac::USART2::ptr()
83+
}
84+
}
8085

86+
pub trait Instance:
87+
crate::Sealed
88+
+ rcc::Enable
89+
+ rcc::Reset
90+
+ CommonPins
91+
+ Rs485
92+
+ Deref<Target = pac::usart1::RegisterBlock>
93+
{
8194
#[doc(hidden)]
82-
fn ptr() -> *const Self::RegisterBlock;
95+
fn ptr() -> *const pac::usart1::RegisterBlock;
8396
}
8497

8598
/// Serial receiver
@@ -149,7 +162,7 @@ impl<USART: Instance> SerialExt for USART {
149162
}
150163
}
151164

152-
impl<USART> fmt::Write for Serial<USART>
165+
impl<USART: Instance> fmt::Write for Serial<USART>
153166
where
154167
Serial<USART>: hal::serial::Write<u8>,
155168
{
@@ -159,7 +172,7 @@ where
159172
}
160173
}
161174

162-
impl<USART> fmt::Write for Tx<USART>
175+
impl<USART: Instance> fmt::Write for Tx<USART>
163176
where
164177
Tx<USART>: hal::serial::Write<u8>,
165178
{

src/spi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<SPI: Instance> Spi<SPI> {
140140
}
141141

142142
pub fn release(self) -> (SPI, (SPI::Sck, SPI::Miso, SPI::Mosi)) {
143-
(self.spi, self.pins.release())
143+
(self.spi, self.pins)
144144
}
145145
}
146146

src/timer/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use crate::rcc::*;
33
use crate::stm32::*;
44
use crate::time::{Hertz, MicroSecond};
5-
use core::marker::PhantomData;
65
use cortex_m::peripheral::syst::SystClkSource;
76
use cortex_m::peripheral::SYST;
87
use hal::timer::{CountDown, Periodic};
@@ -21,10 +20,14 @@ pub struct Timer<TIM> {
2120
tim: TIM,
2221
}
2322

24-
pub struct Channel1;
25-
pub struct Channel2;
26-
pub struct Channel3;
27-
pub struct Channel4;
23+
#[allow(non_upper_case_globals)]
24+
pub const Channel1: u8 = 0;
25+
#[allow(non_upper_case_globals)]
26+
pub const Channel2: u8 = 1;
27+
#[allow(non_upper_case_globals)]
28+
pub const Channel3: u8 = 2;
29+
#[allow(non_upper_case_globals)]
30+
pub const Channel4: u8 = 3;
2831

2932
/// System timer
3033
impl Timer<SYST> {

src/timer/opm.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! # One-pulse Mode
2+
use crate::gpio::alt::TimCPin;
3+
use crate::gpio::PushPull;
24
use crate::rcc::*;
35
use crate::stm32::*;
46
use crate::time::{Hertz, MicroSecond};
5-
use crate::timer::pins::TimerPin;
67
use crate::timer::*;
78
use core::marker::PhantomData;
89
use fugit::RateExtU32;
@@ -11,9 +12,8 @@ pub trait OpmExt: Sized {
1112
fn opm(self, period: MicroSecond, rcc: &mut Rcc) -> Opm<Self>;
1213
}
1314

14-
pub struct OpmPin<TIM, CH> {
15+
pub struct OpmPin<TIM, const CH: u8> {
1516
tim: PhantomData<TIM>,
16-
channel: PhantomData<CH>,
1717
delay: u32,
1818
}
1919

@@ -23,14 +23,13 @@ pub struct Opm<TIM> {
2323
}
2424

2525
impl<TIM> Opm<TIM> {
26-
pub fn bind_pin<PIN>(&self, pin: PIN) -> OpmPin<TIM, PIN::Channel>
26+
pub fn bind_pin<const C: u8, PIN>(&self, pin: impl Into<PIN>) -> OpmPin<TIM, C>
2727
where
28-
PIN: TimerPin<TIM>,
28+
TIM: TimCPin<C, Ch<PushPull> = PIN>,
2929
{
30-
pin.setup();
30+
let _ = pin.into();
3131
OpmPin {
3232
tim: PhantomData,
33-
channel: PhantomData,
3433
delay: 1,
3534
}
3635
}

src/timer/pins.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
/*
12
use crate::gpio::*;
23
use crate::gpio::{AltFunction, DefaultMode};
34
use crate::stm32::*;
45
use crate::timer::*;
56
67
pub trait TimerPin<TIM> {
7-
type Channel;
8+
#[allow(non_upper_case_globals)]
9+
const Channel: u8;
810
911
fn setup(&self);
1012
fn release(self) -> Self;
@@ -28,10 +30,11 @@ impl<TIM, PIN: TimerPin<TIM>> TriggerPin<TIM, PIN> {
2830
}
2931
3032
macro_rules! timer_pins {
31-
($TIMX:ident, [ $(($ch:ty, $pin:ty, $af_mode:expr),)+ ]) => {
33+
($TIMX:ident, [ $(($ch:ident, $pin:ty, $af_mode:expr),)+ ]) => {
3234
$(
3335
impl TimerPin<$TIMX> for $pin {
34-
type Channel = $ch;
36+
#[allow(non_upper_case_globals)]
37+
const Channel: u8 = $ch;
3538
3639
fn setup(&self) {
3740
self.set_alt_mode($af_mode);
@@ -173,3 +176,4 @@ timer_pins!(TIM17, [
173176
timer_pins!(TIM17, [
174177
(Channel1, PB7<DefaultMode>, AltFunction::AF2),
175178
]);
179+
*/

0 commit comments

Comments
 (0)