@@ -46,6 +46,16 @@ pub enum PllSrc {
4646 HSE_BYPASS ( Hertz ) ,
4747}
4848
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+
4959/// Divider for the PLL clock input (M)
5060/// This must be set based on the input clock to keep the PLL input frequency within the limits
5161/// specified in the datasheet.
@@ -70,11 +80,11 @@ pub enum PllMDiv {
7080}
7181
7282impl PllMDiv {
73- pub fn divisor ( & self ) -> u32 {
83+ pub const fn divisor ( & self ) -> u32 {
7484 ( * self as u32 ) + 1
7585 }
7686
77- pub fn register_setting ( & self ) -> u8 {
87+ pub const fn register_setting ( & self ) -> u8 {
7888 * self as u8
7989 }
8090}
@@ -89,11 +99,11 @@ pub enum PllQDiv {
8999}
90100
91101impl PllQDiv {
92- pub fn divisor ( & self ) -> u32 {
102+ pub const fn divisor ( & self ) -> u32 {
93103 ( ( * self as u32 ) + 1 ) * 2
94104 }
95105
96- pub fn register_setting ( & self ) -> u8 {
106+ pub const fn register_setting ( & self ) -> u8 {
97107 * self as u8
98108 }
99109}
@@ -108,11 +118,11 @@ pub enum PllRDiv {
108118}
109119
110120impl PllRDiv {
111- pub fn divisor ( & self ) -> u32 {
121+ pub const fn divisor ( & self ) -> u32 {
112122 ( ( * self as u32 ) + 1 ) * 2
113123 }
114124
115- pub fn register_setting ( & self ) -> u8 {
125+ pub const fn register_setting ( & self ) -> u8 {
116126 * self as u8
117127 }
118128}
@@ -157,11 +167,11 @@ pub enum PllPDiv {
157167}
158168
159169impl PllPDiv {
160- pub fn divisor ( & self ) -> u32 {
170+ pub const fn divisor ( & self ) -> u32 {
161171 * self as u32
162172 }
163173
164- pub fn register_setting ( & self ) -> u8 {
174+ pub const fn register_setting ( & self ) -> u8 {
165175 * self as u8
166176 }
167177}
@@ -292,11 +302,11 @@ pub enum PllNMul {
292302}
293303
294304impl PllNMul {
295- pub fn multiplier ( & self ) -> u32 {
305+ pub const fn multiplier ( & self ) -> u32 {
296306 * self as u32
297307 }
298308
299- pub fn register_setting ( & self ) -> u8 {
309+ pub const fn register_setting ( & self ) -> u8 {
300310 * self as u8
301311 }
302312}
@@ -312,8 +322,8 @@ pub struct PllConfig {
312322 pub p : Option < PllPDiv > ,
313323}
314324
315- impl Default for PllConfig {
316- fn default ( ) -> PllConfig {
325+ impl PllConfig {
326+ pub const fn new ( ) -> Self {
317327 PllConfig {
318328 mux : PllSrc :: HSI ,
319329 m : PllMDiv :: DIV_2 ,
@@ -325,6 +335,12 @@ impl Default for PllConfig {
325335 }
326336}
327337
338+ impl Default for PllConfig {
339+ fn default ( ) -> PllConfig {
340+ Self :: new ( )
341+ }
342+ }
343+
328344/// FDCAN Clock Source
329345#[ allow( clippy:: upper_case_acronyms) ]
330346pub enum FdCanClockSource {
@@ -352,64 +368,68 @@ pub struct Config {
352368}
353369
354370impl 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+ }
357381 }
358382
359- pub fn pll ( ) -> Self {
360- Config :: default ( ) . clock_src ( SysClockSrc :: PLL )
383+ pub const fn const_default ( ) -> Self {
384+ Self :: new ( SysClockSrc :: HSI )
361385 }
362386
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 )
365389 }
366390
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 {
368396 self . sys_mux = mux;
369397 self
370398 }
371399
372- pub fn pll_cfg ( mut self , cfg : PllConfig ) -> Self {
400+ pub const fn pll_cfg ( mut self , cfg : PllConfig ) -> Self {
373401 self . pll_cfg = cfg;
374402 self
375403 }
376404
377- pub fn ahb_psc ( mut self , psc : Prescaler ) -> Self {
405+ pub const fn ahb_psc ( mut self , psc : Prescaler ) -> Self {
378406 self . ahb_psc = psc;
379407 self
380408 }
381409
382- pub fn apb1_psc ( mut self , psc : Prescaler ) -> Self {
410+ pub const fn apb1_psc ( mut self , psc : Prescaler ) -> Self {
383411 self . apb1_psc = psc;
384412 self
385413 }
386414
387- pub fn apb2_psc ( mut self , psc : Prescaler ) -> Self {
415+ pub const fn apb2_psc ( mut self , psc : Prescaler ) -> Self {
388416 self . apb2_psc = psc;
389417 self
390418 }
391419
392- pub fn boost ( mut self , enable_boost : bool ) -> Self {
420+ pub const fn boost ( mut self , enable_boost : bool ) -> Self {
393421 self . enable_boost = enable_boost;
394422 self
395423 }
396424
397- pub fn fdcan_src ( mut self , mux : FdCanClockSource ) -> Self {
425+ pub const fn fdcan_src ( mut self , mux : FdCanClockSource ) -> Self {
398426 self . fdcansel = mux;
399427 self
400428 }
401429}
402430
403431impl Default for Config {
404432 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 ( )
414434 }
415435}
0 commit comments