@@ -65,10 +65,7 @@ pub(crate) struct PeripheralGuard {
6565
6666impl PeripheralGuard {
6767 pub ( crate ) fn new_with ( p : Peripheral , init : fn ( ) ) -> Self {
68- if PeripheralClockControl :: enable ( p) {
69- PeripheralClockControl :: reset ( p) ;
70- init ( ) ;
71- }
68+ PeripheralClockControl :: request_peripheral ( p, init) ;
7269
7370 Self { peripheral : p }
7471 }
@@ -100,14 +97,8 @@ pub(crate) struct GenericPeripheralGuard<const P: u8> {}
10097
10198impl < const P : u8 > GenericPeripheralGuard < P > {
10299 pub ( crate ) fn new_with ( init : fn ( ) ) -> Self {
103- let peripheral = const { Peripheral :: try_from ( P ) . unwrap ( ) } ;
104-
105- PERIPHERAL_REF_COUNT . with ( |ref_counts| {
106- if PeripheralClockControl :: enable_with_counts ( peripheral, ref_counts) {
107- unsafe { PeripheralClockControl :: reset_racey ( peripheral) } ;
108- init ( ) ;
109- }
110- } ) ;
100+ let p = const { Peripheral :: try_from ( P ) . unwrap ( ) } ;
101+ PeripheralClockControl :: request_peripheral ( p, init) ;
111102
112103 Self { }
113104 }
@@ -139,6 +130,15 @@ impl<const P: u8> Drop for GenericPeripheralGuard<P> {
139130pub ( crate ) struct PeripheralClockControl ;
140131
141132impl PeripheralClockControl {
133+ fn request_peripheral ( p : Peripheral , init : fn ( ) ) {
134+ PERIPHERAL_REF_COUNT . with ( |ref_counts| {
135+ if Self :: enable_with_counts ( p, ref_counts) {
136+ unsafe { Self :: reset_racey ( p) } ;
137+ init ( ) ;
138+ }
139+ } ) ;
140+ }
141+
142142 /// Enables the given peripheral.
143143 ///
144144 /// This keeps track of enabling a peripheral - i.e. a peripheral
@@ -165,8 +165,6 @@ impl PeripheralClockControl {
165165 /// gets disabled when the number of enable/disable attempts is balanced.
166166 ///
167167 /// Returns `true` if it actually disabled the peripheral.
168- ///
169- /// Before disabling a peripheral it will also get reset
170168 pub ( crate ) fn disable ( peripheral : Peripheral ) -> bool {
171169 PERIPHERAL_REF_COUNT . with ( |ref_counts| {
172170 Self :: enable_forced_with_counts ( peripheral, false , false , ref_counts)
@@ -200,10 +198,6 @@ impl PeripheralClockControl {
200198 assert ! ( * ref_count == 0 ) ;
201199 }
202200
203- if !enable {
204- unsafe { Self :: reset_racey ( peripheral) } ;
205- }
206-
207201 debug ! ( "Enable {:?} {}" , peripheral, enable) ;
208202 unsafe { enable_internal_racey ( peripheral, enable) } ;
209203
0 commit comments