3838
3939use crate :: rcc:: Clocks ;
4040use crate :: time:: MicroSecond ;
41- pub use cortex_m:: delay:: * ;
41+ pub use cortex_m:: delay:: Delay ;
4242use cortex_m:: peripheral:: SYST ;
4343use fugit:: ExtU32Ceil ;
4444
@@ -52,12 +52,69 @@ pub trait CountDown: embedded_hal_old::timer::CountDown {
5252}
5353
5454pub trait SYSTDelayExt {
55- fn delay ( self , clocks : & Clocks ) -> Delay ;
55+ fn delay ( self , clocks : & Clocks ) -> SystDelay ;
5656}
5757
5858impl SYSTDelayExt for SYST {
59- fn delay ( self , clocks : & Clocks ) -> Delay {
60- Delay :: new ( self , clocks. ahb_clk . raw ( ) )
59+ fn delay ( self , clocks : & Clocks ) -> SystDelay {
60+ SystDelay ( Delay :: new ( self , clocks. ahb_clk . raw ( ) ) )
61+ }
62+ }
63+
64+ /// Delay provider
65+ pub struct SystDelay ( Delay ) ;
66+
67+ impl DelayNs for SystDelay {
68+ // TODO: the current fallback to 1us resolution is a stopgap until the module is reworked
69+ fn delay_ns ( & mut self , ns : u32 ) {
70+ self . 0 . delay_us ( ns. div_ceil ( 1000 ) )
71+ }
72+ fn delay_us ( & mut self , us : u32 ) {
73+ self . 0 . delay_us ( us) ;
74+ }
75+ fn delay_ms ( & mut self , mut ms : u32 ) {
76+ const MAX_MILLIS : u32 = u32:: MAX / 1000 ;
77+ while ms > MAX_MILLIS {
78+ ms -= MAX_MILLIS ;
79+ DelayNs :: delay_us ( self , MAX_MILLIS * 1000 ) ;
80+ }
81+ DelayNs :: delay_us ( self , ms * 1000 ) ;
82+ }
83+ }
84+
85+ impl DelayUs < u32 > for SystDelay {
86+ fn delay_us ( & mut self , us : u32 ) {
87+ DelayNs :: delay_us ( self , us) ;
88+ }
89+ }
90+
91+ impl DelayUs < u16 > for SystDelay {
92+ fn delay_us ( & mut self , us : u16 ) {
93+ DelayNs :: delay_us ( self , us as u32 ) ;
94+ }
95+ }
96+
97+ impl DelayUs < u8 > for SystDelay {
98+ fn delay_us ( & mut self , us : u8 ) {
99+ DelayNs :: delay_us ( self , us as u32 ) ;
100+ }
101+ }
102+
103+ impl DelayMs < u32 > for SystDelay {
104+ fn delay_ms ( & mut self , ms : u32 ) {
105+ DelayNs :: delay_ms ( self , ms) ;
106+ }
107+ }
108+
109+ impl DelayMs < u16 > for SystDelay {
110+ fn delay_ms ( & mut self , ms : u16 ) {
111+ DelayNs :: delay_ms ( self , ms as u32 ) ;
112+ }
113+ }
114+
115+ impl DelayMs < u8 > for SystDelay {
116+ fn delay_ms ( & mut self , ms : u8 ) {
117+ DelayNs :: delay_ms ( self , ms as u32 ) ;
61118 }
62119}
63120
@@ -67,12 +124,12 @@ pub trait DelayExt {
67124 T : Into < MicroSecond > ;
68125}
69126
70- impl DelayExt for Delay {
127+ impl DelayExt for SystDelay {
71128 fn delay < T > ( & mut self , delay : T )
72129 where
73130 T : Into < MicroSecond > ,
74131 {
75- self . delay_us ( delay. into ( ) . ticks ( ) )
132+ self . 0 . delay_us ( delay. into ( ) . ticks ( ) )
76133 }
77134}
78135
0 commit comments