11use crate :: pac:: flash:: vals:: Latency ;
22pub use crate :: pac:: pwr:: vals:: Vos as VoltageRange ;
33pub use crate :: pac:: rcc:: vals:: {
4- Hpre as AHBPrescaler , Pllm as PllPreDiv , Plln as PllMul , Pllp as PllPDiv , Pllq as PllQDiv , Pllr as PllRDiv ,
5- Pllsrc as PllSource , Ppre as APBPrescaler , Sw as Sysclk ,
4+ Hpre as AHBPrescaler , Hsidiv as HsiSysDiv , Pllm as PllPreDiv , Plln as PllMul , Pllp as PllPDiv , Pllq as PllQDiv ,
5+ Pllr as PllRDiv , Pllsrc as PllSource , Ppre as APBPrescaler , Sw as Sysclk ,
66} ;
77use crate :: pac:: { FLASH , PWR , RCC } ;
88use crate :: time:: Hertz ;
@@ -28,6 +28,12 @@ pub struct Hse {
2828 pub mode : HseMode ,
2929}
3030
31+ #[ derive( Clone , Copy , Eq , PartialEq ) ]
32+ pub struct Hsi {
33+ /// Division factor for HSISYS clock. Default is 1.
34+ pub sys_div : HsiSysDiv ,
35+ }
36+
3137/// PLL Configuration
3238///
3339/// Use this struct to configure the PLL source, input frequency, multiplication factor, and output
@@ -58,8 +64,8 @@ pub struct Pll {
5864#[ non_exhaustive]
5965#[ derive( Clone , Copy ) ]
6066pub struct Config {
61- /// HSI Enable
62- pub hsi : bool ,
67+ /// HSI Configuration
68+ pub hsi : Option < Hsi > ,
6369
6470 /// HSE Configuration
6571 pub hse : Option < Hse > ,
@@ -94,7 +100,9 @@ impl Default for Config {
94100 #[ inline]
95101 fn default ( ) -> Config {
96102 Config {
97- hsi : true ,
103+ hsi : Some ( Hsi {
104+ sys_div : HsiSysDiv :: DIV1 ,
105+ } ) ,
98106 hse : None ,
99107 sys : Sysclk :: HSI ,
100108 #[ cfg( crs) ]
@@ -119,17 +127,22 @@ pub struct PllFreq {
119127
120128pub ( crate ) unsafe fn init ( config : Config ) {
121129 // Turn on the HSI
122- RCC . cr ( ) . modify ( |w| w. set_hsion ( true ) ) ;
130+ RCC . cr ( ) . modify ( |w| {
131+ w. set_hsion ( true ) ;
132+ if let Some ( hsi) = config. hsi {
133+ w. set_hsidiv ( hsi. sys_div ) ;
134+ }
135+ } ) ;
123136 while !RCC . cr ( ) . read ( ) . hsirdy ( ) { }
124137
125138 // Use the HSI clock as system clock during the actual clock setup
126139 RCC . cfgr ( ) . modify ( |w| w. set_sw ( Sysclk :: HSI ) ) ;
127140 while RCC . cfgr ( ) . read ( ) . sws ( ) != Sysclk :: HSI { }
128141
129142 // Configure HSI
130- let hsi = match config. hsi {
131- false => None ,
132- true => Some ( HSI_FREQ ) ,
143+ let ( hsi, hsisys ) = match config. hsi {
144+ None => ( None , None ) ,
145+ Some ( hsi ) => ( Some ( HSI_FREQ ) , Some ( HSI_FREQ / hsi . sys_div ) ) ,
133146 } ;
134147
135148 // Configure HSE
@@ -222,7 +235,7 @@ pub(crate) unsafe fn init(config: Config) {
222235 . unwrap_or_default ( ) ;
223236
224237 let sys = match config. sys {
225- Sysclk :: HSI => unwrap ! ( hsi ) ,
238+ Sysclk :: HSI => unwrap ! ( hsisys ) ,
226239 Sysclk :: HSE => unwrap ! ( hse) ,
227240 Sysclk :: PLL1_R => unwrap ! ( pll. pll_r) ,
228241 _ => unreachable ! ( ) ,
@@ -264,7 +277,7 @@ pub(crate) unsafe fn init(config: Config) {
264277 while RCC . cfgr ( ) . read ( ) . sws ( ) != config. sys { }
265278
266279 // Disable HSI if not used
267- if ! config. hsi {
280+ if config. hsi . is_none ( ) {
268281 RCC . cr ( ) . modify ( |w| w. set_hsion ( false ) ) ;
269282 }
270283
0 commit comments