@@ -125,13 +125,7 @@ pub trait TimerGroupInstance {
125125 fn register_block ( ) -> * const RegisterBlock ;
126126 #[ cfg( soc_has_clock_node_timg_function_clock) ]
127127 fn clock_instance ( ) -> clocks:: TimgInstance ;
128- fn enable_peripheral ( ) ;
129- fn reset_peripheral ( ) ;
130- #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
131- fn configure_wdt_src_clk ( src : TimgWdtClockConfig ) ;
132- #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
133- fn gate_wdt_src_clk ( enable : bool ) ;
134- fn wdt_src_frequency ( ) -> Rate ;
128+ fn peripheral ( ) -> crate :: system:: Peripheral ;
135129 fn wdt_interrupt ( ) -> Interrupt ;
136130}
137131
@@ -151,41 +145,8 @@ impl TimerGroupInstance for TIMG0<'_> {
151145 clocks:: TimgInstance :: Timg0
152146 }
153147
154- fn enable_peripheral ( ) {
155- PeripheralClockControl :: enable ( crate :: system:: Peripheral :: Timg0 ) ;
156- }
157-
158- fn reset_peripheral ( ) {
159- // FIXME: for TIMG0 do nothing for now because the reset breaks
160- // `time::Instant::now`
161- }
162-
163- #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
164- fn configure_wdt_src_clk ( src : TimgWdtClockConfig ) {
165- clocks:: ClockTree :: with ( |clocks| Self :: clock_instance ( ) . configure_wdt_clock ( clocks, src) ) ;
166- }
167-
168- #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
169- fn gate_wdt_src_clk ( enable : bool ) {
170- clocks:: ClockTree :: with ( |clocks| {
171- if enable {
172- Self :: clock_instance ( ) . request_wdt_clock ( clocks)
173- } else {
174- Self :: clock_instance ( ) . release_wdt_clock ( clocks)
175- }
176- } ) ;
177- }
178-
179- fn wdt_src_frequency ( ) -> Rate {
180- clocks:: ClockTree :: with ( |clocks| {
181- cfg_if:: cfg_if! {
182- if #[ cfg( soc_has_clock_node_timg_wdt_clock) ] {
183- Rate :: from_hz( Self :: clock_instance( ) . wdt_clock_frequency( clocks) )
184- } else {
185- Rate :: from_hz( clocks:: apb_clk_frequency( clocks) )
186- }
187- }
188- } )
148+ fn peripheral ( ) -> crate :: system:: Peripheral {
149+ crate :: system:: Peripheral :: Timg0
189150 }
190151
191152 fn wdt_interrupt ( ) -> Interrupt {
@@ -209,40 +170,8 @@ impl TimerGroupInstance for crate::peripherals::TIMG1<'_> {
209170 clocks:: TimgInstance :: Timg1
210171 }
211172
212- fn enable_peripheral ( ) {
213- PeripheralClockControl :: enable ( crate :: system:: Peripheral :: Timg1 ) ;
214- }
215-
216- fn reset_peripheral ( ) {
217- PeripheralClockControl :: reset ( crate :: system:: Peripheral :: Timg1 ) ;
218- }
219-
220- #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
221- fn configure_wdt_src_clk ( src : TimgWdtClockConfig ) {
222- clocks:: ClockTree :: with ( |clocks| Self :: clock_instance ( ) . configure_wdt_clock ( clocks, src) ) ;
223- }
224-
225- #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
226- fn gate_wdt_src_clk ( enable : bool ) {
227- clocks:: ClockTree :: with ( |clocks| {
228- if enable {
229- Self :: clock_instance ( ) . request_wdt_clock ( clocks)
230- } else {
231- Self :: clock_instance ( ) . release_wdt_clock ( clocks)
232- }
233- } ) ;
234- }
235-
236- fn wdt_src_frequency ( ) -> Rate {
237- clocks:: ClockTree :: with ( |clocks| {
238- cfg_if:: cfg_if! {
239- if #[ cfg( soc_has_clock_node_timg_wdt_clock) ] {
240- Rate :: from_hz( Self :: clock_instance( ) . wdt_clock_frequency( clocks) )
241- } else {
242- Rate :: from_hz( clocks:: apb_clk_frequency( clocks) )
243- }
244- }
245- } )
173+ fn peripheral ( ) -> crate :: system:: Peripheral {
174+ crate :: system:: Peripheral :: Timg1
246175 }
247176
248177 fn wdt_interrupt ( ) -> Interrupt {
@@ -256,8 +185,14 @@ where
256185{
257186 /// Construct a new instance of [`TimerGroup`] in blocking mode
258187 pub fn new ( _timer_group : T ) -> Self {
259- T :: reset_peripheral ( ) ;
260- T :: enable_peripheral ( ) ;
188+ // TODO: use PeripheralGuard
189+ if PeripheralClockControl :: enable ( T :: peripheral ( ) ) {
190+ PeripheralClockControl :: reset ( T :: peripheral ( ) ) ;
191+ } else {
192+ // Refcount was more than 0. Decrement to avoid overflow because we don't handle
193+ // dropping the driver.
194+ PeripheralClockControl :: disable ( T :: peripheral ( ) ) ;
195+ }
261196
262197 #[ cfg( soc_has_clock_node_timg_function_clock) ]
263198 clocks:: ClockTree :: with ( |clocks| {
@@ -665,7 +600,9 @@ where
665600
666601 this. set_write_protection ( false ) ;
667602 #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
668- TG :: configure_wdt_src_clk ( TimgWdtClockConfig :: default ( ) ) ;
603+ clocks:: ClockTree :: with ( |clocks| {
604+ TG :: clock_instance ( ) . configure_wdt_clock ( clocks, TimgWdtClockConfig :: default ( ) )
605+ } ) ;
669606 this. set_write_protection ( true ) ;
670607
671608 this
@@ -699,7 +636,7 @@ where
699636
700637 #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
701638 if enabled {
702- TG :: gate_wdt_src_clk ( true ) ;
639+ clocks :: ClockTree :: with ( |clocks| TG :: clock_instance ( ) . request_wdt_clock ( clocks ) ) ;
703640 }
704641
705642 reg_block
@@ -724,7 +661,7 @@ where
724661
725662 #[ cfg( soc_has_clock_node_timg_wdt_clock) ]
726663 if !enabled {
727- TG :: gate_wdt_src_clk ( false ) ;
664+ clocks :: ClockTree :: with ( |clocks| TG :: clock_instance ( ) . release_wdt_clock ( clocks ) ) ;
728665 }
729666
730667 self . set_write_protection ( true ) ;
@@ -753,7 +690,16 @@ where
753690
754691 /// Set the timeout, in microseconds, of the watchdog timer
755692 pub fn set_timeout ( & mut self , stage : MwdtStage , timeout : Duration ) {
756- let clk_src = TG :: wdt_src_frequency ( ) ;
693+ let clk_src = clocks:: ClockTree :: with ( |clocks| {
694+ cfg_if:: cfg_if! {
695+ if #[ cfg( soc_has_clock_node_timg_wdt_clock) ] {
696+ Rate :: from_hz( TG :: clock_instance( ) . wdt_clock_frequency( clocks) )
697+ } else {
698+ Rate :: from_hz( clocks:: apb_clk_frequency( clocks) )
699+ }
700+ }
701+ } ) ;
702+
757703 let timeout_ticks = timeout. as_micros ( ) * clk_src. as_mhz ( ) as u64 ;
758704
759705 let reg_block = unsafe { & * TG :: register_block ( ) } ;
0 commit comments