1
- use core:: cmp;
2
1
use crate :: time:: Hertz ;
3
2
4
3
/// Extension trait that constrains the `RCC` peripheral
@@ -112,7 +111,7 @@ impl CFGR {
112
111
} else {
113
112
src_clk_freq = HSI ; // If no clock source is selected use HSI.
114
113
}
115
-
114
+
116
115
// Pll check
117
116
if sysclk == src_clk_freq {
118
117
// Bypass pll if src clk and requested sysclk are the same, to save power.
@@ -121,8 +120,9 @@ impl CFGR {
121
120
pllmul_bits = None ;
122
121
r_sysclk = src_clk_freq;
123
122
} else {
124
- let pllmul = ( 4 * self . sysclk . unwrap_or ( src_clk_freq) + src_clk_freq) / src_clk_freq / 2 ;
125
- let pllmul = cmp:: min ( cmp:: max ( pllmul, 2 ) , 16 ) ;
123
+ let pllmul =
124
+ ( 4 * self . sysclk . unwrap_or ( src_clk_freq) + src_clk_freq) / src_clk_freq / 2 ;
125
+ let pllmul = core:: cmp:: min ( core:: cmp:: max ( pllmul, 2 ) , 16 ) ;
126
126
r_sysclk = pllmul * src_clk_freq / 2 ;
127
127
128
128
pllmul_bits = if pllmul == 2 {
@@ -182,17 +182,17 @@ impl CFGR {
182
182
let rcc = unsafe { & * crate :: stm32:: RCC :: ptr ( ) } ;
183
183
184
184
// Set up rcc based on above calculated configuration.
185
-
185
+
186
186
// Enable requested clock sources
187
187
// HSI
188
188
if self . enable_hsi {
189
189
rcc. cr . write ( |w| w. hsion ( ) . set_bit ( ) ) ;
190
- while rcc. cr . read ( ) . hsirdy ( ) . bit_is_clear ( ) { }
190
+ while rcc. cr . read ( ) . hsirdy ( ) . bit_is_clear ( ) { }
191
191
}
192
192
// HSI48
193
193
if self . enable_hsi48 {
194
194
rcc. cr2 . modify ( |_, w| w. hsi48on ( ) . set_bit ( ) ) ;
195
- while rcc. cr2 . read ( ) . hsi48rdy ( ) . bit_is_clear ( ) { }
195
+ while rcc. cr2 . read ( ) . hsi48rdy ( ) . bit_is_clear ( ) { }
196
196
}
197
197
198
198
// Enable PLL
@@ -201,15 +201,18 @@ impl CFGR {
201
201
202
202
// Set PLL source based on configuration.
203
203
if self . enable_hsi48 {
204
- rcc. cfgr . modify ( |_, w| w. pllsrc ( ) . bits ( PllSource :: HSI48 as u8 ) ) ;
204
+ rcc. cfgr
205
+ . modify ( |_, w| w. pllsrc ( ) . bits ( PllSource :: HSI48 as u8 ) ) ;
205
206
} else if self . enable_hsi {
206
- rcc. cfgr . modify ( |_, w| w. pllsrc ( ) . bits ( PllSource :: HSI_DIV2 as u8 ) ) ;
207
+ rcc. cfgr
208
+ . modify ( |_, w| w. pllsrc ( ) . bits ( PllSource :: HSI_DIV2 as u8 ) ) ;
207
209
} else {
208
- rcc. cfgr . modify ( |_, w| w. pllsrc ( ) . bits ( PllSource :: HSI_DIV2 as u8 ) ) ;
210
+ rcc. cfgr
211
+ . modify ( |_, w| w. pllsrc ( ) . bits ( PllSource :: HSI_DIV2 as u8 ) ) ;
209
212
}
210
213
211
214
rcc. cr . write ( |w| w. pllon ( ) . set_bit ( ) ) ;
212
- while rcc. cr . read ( ) . pllrdy ( ) . bit_is_clear ( ) { }
215
+ while rcc. cr . read ( ) . pllrdy ( ) . bit_is_clear ( ) { }
213
216
214
217
rcc. cfgr . modify ( |_, w| unsafe {
215
218
w. ppre ( )
@@ -219,30 +222,40 @@ impl CFGR {
219
222
. sw ( )
220
223
. bits ( SysClkSource :: PLL as u8 )
221
224
} ) ;
222
-
223
- } else { // No PLL required.
225
+ } else {
226
+ // No PLL required.
224
227
// Setup requested clocks.
225
228
if self . enable_hsi48 {
226
- rcc. cfgr . modify ( |_, w| unsafe {
227
- w. ppre ( ) . bits ( ppre_bits)
228
- . hpre ( ) . bits ( hpre_bits)
229
- . sw ( ) . bits ( SysClkSource :: HSI48 as u8 )
229
+ rcc. cfgr . modify ( |_, w| unsafe {
230
+ w. ppre ( )
231
+ . bits ( ppre_bits)
232
+ . hpre ( )
233
+ . bits ( hpre_bits)
234
+ . sw ( )
235
+ . bits ( SysClkSource :: HSI48 as u8 )
230
236
} ) ;
231
237
} else if self . enable_hsi {
232
- rcc. cfgr . modify ( |_, w| unsafe {
233
- w. ppre ( ) . bits ( ppre_bits)
234
- . hpre ( ) . bits ( hpre_bits)
235
- . sw ( ) . bits ( SysClkSource :: HSI as u8 )
238
+ rcc. cfgr . modify ( |_, w| unsafe {
239
+ w. ppre ( )
240
+ . bits ( ppre_bits)
241
+ . hpre ( )
242
+ . bits ( hpre_bits)
243
+ . sw ( )
244
+ . bits ( SysClkSource :: HSI as u8 )
236
245
} ) ;
237
- } else { // Default to HSI
246
+ } else {
247
+ // Default to HSI
238
248
rcc. cfgr . modify ( |_, w| unsafe {
239
- w. ppre ( ) . bits ( ppre_bits)
240
- . hpre ( ) . bits ( hpre_bits)
241
- . sw ( ) . bits ( SysClkSource :: HSI as u8 )
249
+ w. ppre ( )
250
+ . bits ( ppre_bits)
251
+ . hpre ( )
252
+ . bits ( hpre_bits)
253
+ . sw ( )
254
+ . bits ( SysClkSource :: HSI as u8 )
242
255
} ) ;
243
256
}
244
257
}
245
-
258
+
246
259
Clocks {
247
260
hclk : Hertz ( hclk) ,
248
261
pclk : Hertz ( pclk) ,
0 commit comments