1
1
use crate :: pac:: flash:: vals:: Latency ;
2
2
pub use crate :: pac:: pwr:: vals:: Vos as VoltageRange ;
3
3
pub 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 ,
6
6
} ;
7
7
use crate :: pac:: { FLASH , PWR , RCC } ;
8
8
use crate :: time:: Hertz ;
@@ -28,6 +28,12 @@ pub struct Hse {
28
28
pub mode : HseMode ,
29
29
}
30
30
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
+
31
37
/// PLL Configuration
32
38
///
33
39
/// Use this struct to configure the PLL source, input frequency, multiplication factor, and output
@@ -58,8 +64,8 @@ pub struct Pll {
58
64
#[ non_exhaustive]
59
65
#[ derive( Clone , Copy ) ]
60
66
pub struct Config {
61
- /// HSI Enable
62
- pub hsi : bool ,
67
+ /// HSI Configuration
68
+ pub hsi : Option < Hsi > ,
63
69
64
70
/// HSE Configuration
65
71
pub hse : Option < Hse > ,
@@ -94,7 +100,9 @@ impl Default for Config {
94
100
#[ inline]
95
101
fn default ( ) -> Config {
96
102
Config {
97
- hsi : true ,
103
+ hsi : Some ( Hsi {
104
+ sys_div : HsiSysDiv :: DIV1 ,
105
+ } ) ,
98
106
hse : None ,
99
107
sys : Sysclk :: HSI ,
100
108
#[ cfg( crs) ]
@@ -119,17 +127,22 @@ pub struct PllFreq {
119
127
120
128
pub ( crate ) unsafe fn init ( config : Config ) {
121
129
// 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
+ } ) ;
123
136
while !RCC . cr ( ) . read ( ) . hsirdy ( ) { }
124
137
125
138
// Use the HSI clock as system clock during the actual clock setup
126
139
RCC . cfgr ( ) . modify ( |w| w. set_sw ( Sysclk :: HSI ) ) ;
127
140
while RCC . cfgr ( ) . read ( ) . sws ( ) != Sysclk :: HSI { }
128
141
129
142
// 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 ) ) ,
133
146
} ;
134
147
135
148
// Configure HSE
@@ -222,7 +235,7 @@ pub(crate) unsafe fn init(config: Config) {
222
235
. unwrap_or_default ( ) ;
223
236
224
237
let sys = match config. sys {
225
- Sysclk :: HSI => unwrap ! ( hsi ) ,
238
+ Sysclk :: HSI => unwrap ! ( hsisys ) ,
226
239
Sysclk :: HSE => unwrap ! ( hse) ,
227
240
Sysclk :: PLL1_R => unwrap ! ( pll. pll_r) ,
228
241
_ => unreachable ! ( ) ,
@@ -264,7 +277,7 @@ pub(crate) unsafe fn init(config: Config) {
264
277
while RCC . cfgr ( ) . read ( ) . sws ( ) != config. sys { }
265
278
266
279
// Disable HSI if not used
267
- if ! config. hsi {
280
+ if config. hsi . is_none ( ) {
268
281
RCC . cr ( ) . modify ( |w| w. set_hsion ( false ) ) ;
269
282
}
270
283
0 commit comments