@@ -46,6 +46,16 @@ pub enum PllSrc {
46
46
HSE_BYPASS ( Hertz ) ,
47
47
}
48
48
49
+ impl PllSrc {
50
+ pub const fn frequency ( self ) -> Hertz {
51
+ match self {
52
+ PllSrc :: HSI => Hertz :: MHz ( 16 ) ,
53
+ PllSrc :: HSE ( f) => f,
54
+ PllSrc :: HSE_BYPASS ( f) => f,
55
+ }
56
+ }
57
+ }
58
+
49
59
/// Divider for the PLL clock input (M)
50
60
/// This must be set based on the input clock to keep the PLL input frequency within the limits
51
61
/// specified in the datasheet.
@@ -70,11 +80,11 @@ pub enum PllMDiv {
70
80
}
71
81
72
82
impl PllMDiv {
73
- pub fn divisor ( & self ) -> u32 {
83
+ pub const fn divisor ( & self ) -> u32 {
74
84
( * self as u32 ) + 1
75
85
}
76
86
77
- pub fn register_setting ( & self ) -> u8 {
87
+ pub const fn register_setting ( & self ) -> u8 {
78
88
* self as u8
79
89
}
80
90
}
@@ -89,11 +99,11 @@ pub enum PllQDiv {
89
99
}
90
100
91
101
impl PllQDiv {
92
- pub fn divisor ( & self ) -> u32 {
102
+ pub const fn divisor ( & self ) -> u32 {
93
103
( ( * self as u32 ) + 1 ) * 2
94
104
}
95
105
96
- pub fn register_setting ( & self ) -> u8 {
106
+ pub const fn register_setting ( & self ) -> u8 {
97
107
* self as u8
98
108
}
99
109
}
@@ -108,11 +118,11 @@ pub enum PllRDiv {
108
118
}
109
119
110
120
impl PllRDiv {
111
- pub fn divisor ( & self ) -> u32 {
121
+ pub const fn divisor ( & self ) -> u32 {
112
122
( ( * self as u32 ) + 1 ) * 2
113
123
}
114
124
115
- pub fn register_setting ( & self ) -> u8 {
125
+ pub const fn register_setting ( & self ) -> u8 {
116
126
* self as u8
117
127
}
118
128
}
@@ -157,11 +167,11 @@ pub enum PllPDiv {
157
167
}
158
168
159
169
impl PllPDiv {
160
- pub fn divisor ( & self ) -> u32 {
170
+ pub const fn divisor ( & self ) -> u32 {
161
171
* self as u32
162
172
}
163
173
164
- pub fn register_setting ( & self ) -> u8 {
174
+ pub const fn register_setting ( & self ) -> u8 {
165
175
* self as u8
166
176
}
167
177
}
@@ -292,11 +302,11 @@ pub enum PllNMul {
292
302
}
293
303
294
304
impl PllNMul {
295
- pub fn multiplier ( & self ) -> u32 {
305
+ pub const fn multiplier ( & self ) -> u32 {
296
306
* self as u32
297
307
}
298
308
299
- pub fn register_setting ( & self ) -> u8 {
309
+ pub const fn register_setting ( & self ) -> u8 {
300
310
* self as u8
301
311
}
302
312
}
@@ -312,8 +322,8 @@ pub struct PllConfig {
312
322
pub p : Option < PllPDiv > ,
313
323
}
314
324
315
- impl Default for PllConfig {
316
- fn default ( ) -> PllConfig {
325
+ impl PllConfig {
326
+ pub const fn new ( ) -> Self {
317
327
PllConfig {
318
328
mux : PllSrc :: HSI ,
319
329
m : PllMDiv :: DIV_2 ,
@@ -325,6 +335,12 @@ impl Default for PllConfig {
325
335
}
326
336
}
327
337
338
+ impl Default for PllConfig {
339
+ fn default ( ) -> PllConfig {
340
+ Self :: new ( )
341
+ }
342
+ }
343
+
328
344
/// FDCAN Clock Source
329
345
#[ allow( clippy:: upper_case_acronyms) ]
330
346
pub enum FdCanClockSource {
@@ -352,64 +368,68 @@ pub struct Config {
352
368
}
353
369
354
370
impl Config {
355
- pub fn new ( mux : SysClockSrc ) -> Self {
356
- Config :: default ( ) . clock_src ( mux)
371
+ pub const fn new ( sys_mux : SysClockSrc ) -> Self {
372
+ Config {
373
+ sys_mux,
374
+ pll_cfg : PllConfig :: new ( ) ,
375
+ ahb_psc : Prescaler :: NotDivided ,
376
+ apb1_psc : Prescaler :: NotDivided ,
377
+ apb2_psc : Prescaler :: NotDivided ,
378
+ enable_boost : false ,
379
+ fdcansel : FdCanClockSource :: HSE ,
380
+ }
357
381
}
358
382
359
- pub fn pll ( ) -> Self {
360
- Config :: default ( ) . clock_src ( SysClockSrc :: PLL )
383
+ pub const fn const_default ( ) -> Self {
384
+ Self :: new ( SysClockSrc :: HSI )
361
385
}
362
386
363
- pub fn hsi ( ) -> Self {
364
- Config :: default ( ) . clock_src ( SysClockSrc :: HSI )
387
+ pub const fn pll ( ) -> Self {
388
+ Config :: const_default ( ) . clock_src ( SysClockSrc :: PLL )
365
389
}
366
390
367
- pub fn clock_src ( mut self , mux : SysClockSrc ) -> Self {
391
+ pub const fn hsi ( ) -> Self {
392
+ Config :: const_default ( ) . clock_src ( SysClockSrc :: HSI )
393
+ }
394
+
395
+ pub const fn clock_src ( mut self , mux : SysClockSrc ) -> Self {
368
396
self . sys_mux = mux;
369
397
self
370
398
}
371
399
372
- pub fn pll_cfg ( mut self , cfg : PllConfig ) -> Self {
400
+ pub const fn pll_cfg ( mut self , cfg : PllConfig ) -> Self {
373
401
self . pll_cfg = cfg;
374
402
self
375
403
}
376
404
377
- pub fn ahb_psc ( mut self , psc : Prescaler ) -> Self {
405
+ pub const fn ahb_psc ( mut self , psc : Prescaler ) -> Self {
378
406
self . ahb_psc = psc;
379
407
self
380
408
}
381
409
382
- pub fn apb1_psc ( mut self , psc : Prescaler ) -> Self {
410
+ pub const fn apb1_psc ( mut self , psc : Prescaler ) -> Self {
383
411
self . apb1_psc = psc;
384
412
self
385
413
}
386
414
387
- pub fn apb2_psc ( mut self , psc : Prescaler ) -> Self {
415
+ pub const fn apb2_psc ( mut self , psc : Prescaler ) -> Self {
388
416
self . apb2_psc = psc;
389
417
self
390
418
}
391
419
392
- pub fn boost ( mut self , enable_boost : bool ) -> Self {
420
+ pub const fn boost ( mut self , enable_boost : bool ) -> Self {
393
421
self . enable_boost = enable_boost;
394
422
self
395
423
}
396
424
397
- pub fn fdcan_src ( mut self , mux : FdCanClockSource ) -> Self {
425
+ pub const fn fdcan_src ( mut self , mux : FdCanClockSource ) -> Self {
398
426
self . fdcansel = mux;
399
427
self
400
428
}
401
429
}
402
430
403
431
impl Default for Config {
404
432
fn default ( ) -> Config {
405
- Config {
406
- sys_mux : SysClockSrc :: HSI ,
407
- pll_cfg : PllConfig :: default ( ) ,
408
- ahb_psc : Prescaler :: NotDivided ,
409
- apb1_psc : Prescaler :: NotDivided ,
410
- apb2_psc : Prescaler :: NotDivided ,
411
- enable_boost : false ,
412
- fdcansel : FdCanClockSource :: HSE ,
413
- }
433
+ Config :: const_default ( )
414
434
}
415
435
}
0 commit comments