99//! ```
1010//! # use cortex_m_rt::entry;
1111//! # use stm32f3xx_hal::prelude::*;
12+ //! #use hal::time::rate::*;
13+ //!
1214//! # #[entry]
1315//! # fn main() -> ! {
1416//! // Get our peripherals
3335//! let clocks = rcc.cfgr
3436//! // Using the external oscillator
3537//! // Set the frequency to that of the external oscillator
36- //! .use_hse(8.mhz ())
38+ //! .use_hse(8.MHz ())
3739//! // Set the frequency for the AHB bus,
3840//! // which the root of every following clock peripheral
39- //! .hclk(48.mhz ())
41+ //! .hclk(48.MHz ())
4042//! // The sysclk is equivalent to the core clock
41- //! .sysclk(48.mhz ())
43+ //! .sysclk(48.MHz ())
4244//! // The following are peripheral clocks, which are both
4345//! // needed to configure specific peripherals.
4446//! // Looking at the peripheral function parameters
4547//! // should give more insight, which peripheral clock is needed.
46- //! .pclk1(12.mhz ())
47- //! .pclk2(12.mhz ())
48+ //! .pclk1(12.MHz ())
49+ //! .pclk2(12.MHz ())
4850//! // Freeze / apply the configuration and setup all clocks
4951//! .freeze(&mut flash.acr);
5052//! # }
@@ -59,8 +61,10 @@ use crate::pac::{
5961 RCC ,
6062} ;
6163
64+ use core:: convert:: TryInto ;
65+
6266use crate :: flash:: ACR ;
63- use crate :: time:: rate:: Hertz ;
67+ use crate :: time:: rate:: * ;
6468
6569/// Extension trait that constrains the `RCC` peripheral
6670pub trait RccExt {
@@ -333,7 +337,12 @@ impl CFGR {
333337 ///
334338 /// Will result in a hang if an external oscillator is not connected or it fails to start,
335339 /// unless [css](CFGR::enable_css) is enabled.
336- pub fn use_hse ( mut self , freq : Hertz ) -> Self {
340+ ///
341+ /// # Panics
342+ ///
343+ /// Panics if conversion from `Megahertz` to `Hertz` produces a value greater then `u32::MAX`.
344+ pub fn use_hse ( mut self , freq : Megahertz ) -> Self {
345+ let freq: Hertz = crate :: expect!( freq. try_into( ) , "ConversionError" ) ;
337346 self . hse = Some ( freq. 0 ) ;
338347 self
339348 }
@@ -360,8 +369,13 @@ impl CFGR {
360369 self
361370 }
362371
363- /// Sets a frequency for the AHB bus
364- pub fn hclk ( mut self , freq : Hertz ) -> Self {
372+ /// Sets a frequency for the AHB bus.
373+ ///
374+ /// # Panics
375+ ///
376+ /// Panics if conversion from `Megahertz` to `Hertz` produces a value greater then `u32::MAX`.
377+ pub fn hclk ( mut self , freq : Megahertz ) -> Self {
378+ let freq: Hertz = crate :: expect!( freq. try_into( ) , "ConversionError" ) ;
365379 self . hclk = Some ( freq. 0 ) ;
366380 self
367381 }
@@ -372,7 +386,12 @@ impl CFGR {
372386 ///
373387 /// If not manually set, it will be set to [`CFGR::sysclk`] frequency
374388 /// or [`CFGR::sysclk`] frequency / 2, if [`CFGR::sysclk`] > 36 Mhz
375- pub fn pclk1 ( mut self , freq : Hertz ) -> Self {
389+ ///
390+ /// # Panics
391+ ///
392+ /// Panics if conversion from `Megahertz` to `Hertz` produces a value greater then `u32::MAX`.
393+ pub fn pclk1 ( mut self , freq : Megahertz ) -> Self {
394+ let freq: Hertz = crate :: expect!( freq. try_into( ) , "ConversionError" ) ;
376395 self . pclk1 = Some ( freq. 0 ) ;
377396 self
378397 }
@@ -390,7 +409,11 @@ impl CFGR {
390409 ///
391410 /// [stm32f302xd,stm32f302xe,stm32f303xd,stm32f303xe,stm32f398]
392411 ///
393- pub fn pclk2 ( mut self , freq : Hertz ) -> Self {
412+ /// # Panics
413+ ///
414+ /// Panics if conversion from `Megahertz` to `Hertz` produces a value greater then `u32::MAX`.
415+ pub fn pclk2 ( mut self , freq : Megahertz ) -> Self {
416+ let freq: Hertz = crate :: expect!( freq. try_into( ) , "ConversionError" ) ;
394417 self . pclk2 = Some ( freq. 0 ) ;
395418 self
396419 }
@@ -410,7 +433,12 @@ impl CFGR {
410433 /// even when using the internal oscillator:
411434 ///
412435 /// [stm32f302xd,stm32f302xe,stm32f303xd,stm32f303xe,stm32f398]
413- pub fn sysclk ( mut self , freq : Hertz ) -> Self {
436+ ///
437+ /// # Panics
438+ ///
439+ /// Panics if conversion from `Megahertz` to `Hertz` produces a value greater then `u32::MAX`.
440+ pub fn sysclk ( mut self , freq : Megahertz ) -> Self {
441+ let freq: Hertz = crate :: expect!( freq. try_into( ) , "ConversionError" ) ;
414442 self . sysclk = Some ( freq. 0 ) ;
415443 self
416444 }
@@ -645,7 +673,7 @@ impl CFGR {
645673
646674 // This ensures, that no panic happens, when
647675 // pclk1 is not manually set.
648- // As hclk highest value is 72.mhz ()
676+ // As hclk highest value is 72.MHz ()
649677 // dividing by 2 should always be sufficient
650678 if self . pclk1 . is_none ( ) && pclk1 > 36_000_000 {
651679 ppre1_bits = cfgr:: PPRE1_A :: DIV2 ;
0 commit comments