@@ -45,6 +45,143 @@ pub struct Rcc {
4545 pub ( crate ) rb : RCC ,
4646}
4747
48+ impl Rcc {
49+ /// Applies the clock configuration and returns a `Clocks` struct that signifies that the
50+ /// clocks are frozen, and contains the frequencies used. After this function is called,
51+ /// the clocks can not change
52+ ///
53+ /// Usage:
54+ ///
55+ /// ```rust
56+ /// let dp = pac::Peripherals::take().unwrap();
57+ /// let mut flash = dp.FLASH.constrain();
58+ /// let cfg = rcc::Config::hse(8.MHz()).sysclk(72.MHz());
59+ /// let mut rcc = dp.RCC.constrain().freeze(cfg, &mut flash.acr);
60+ /// ```
61+ #[ allow( unused_variables) ]
62+ #[ inline( always) ]
63+ pub fn freeze ( self , cfg : impl Into < RawConfig > , acr : & mut ACR ) -> Self {
64+ let cfg = cfg. into ( ) ;
65+ let clocks = cfg. get_clocks ( ) ;
66+ // adjust flash wait states
67+ #[ cfg( any( feature = "stm32f103" , feature = "connectivity" ) ) ]
68+ unsafe {
69+ acr. acr ( ) . write ( |w| {
70+ w. latency ( ) . bits ( if clocks. sysclk <= MHz ( 24 ) {
71+ 0b000
72+ } else if clocks. sysclk <= MHz ( 48 ) {
73+ 0b001
74+ } else {
75+ 0b010
76+ } )
77+ } ) ;
78+ }
79+
80+ let rcc = unsafe { & * RCC :: ptr ( ) } ;
81+
82+ if cfg. hse . is_some ( ) {
83+ // enable HSE and wait for it to be ready
84+
85+ rcc. cr ( ) . modify ( |_, w| {
86+ if cfg. hse_bypass {
87+ w. hsebyp ( ) . bypassed ( ) ;
88+ }
89+ w. hseon ( ) . set_bit ( )
90+ } ) ;
91+
92+ while rcc. cr ( ) . read ( ) . hserdy ( ) . bit_is_clear ( ) { }
93+ }
94+
95+ if let Some ( pllmul_bits) = cfg. pllmul {
96+ // enable PLL and wait for it to be ready
97+
98+ #[ allow( unused_unsafe) ]
99+ rcc. cfgr ( ) . modify ( |_, w| unsafe {
100+ w. pllmul ( ) . bits ( pllmul_bits) . pllsrc ( ) . bit ( cfg. hse . is_some ( ) )
101+ } ) ;
102+
103+ rcc. cr ( ) . modify ( |_, w| w. pllon ( ) . set_bit ( ) ) ;
104+
105+ while rcc. cr ( ) . read ( ) . pllrdy ( ) . bit_is_clear ( ) { }
106+ }
107+
108+ // set prescalers and clock source
109+ #[ cfg( feature = "connectivity" ) ]
110+ rcc. cfgr ( ) . modify ( |_, w| unsafe {
111+ w. adcpre ( ) . variant ( cfg. adcpre ) ;
112+ w. ppre2 ( ) . bits ( cfg. ppre2 as u8 ) ;
113+ w. ppre1 ( ) . bits ( cfg. ppre1 as u8 ) ;
114+ w. hpre ( ) . bits ( cfg. hpre as u8 ) ;
115+ w. otgfspre ( ) . variant ( cfg. usbpre ) ;
116+ w. sw ( ) . bits ( if cfg. pllmul . is_some ( ) {
117+ // PLL
118+ 0b10
119+ } else if cfg. hse . is_some ( ) {
120+ // HSE
121+ 0b1
122+ } else {
123+ // HSI
124+ 0b0
125+ } )
126+ } ) ;
127+
128+ #[ cfg( feature = "stm32f103" ) ]
129+ rcc. cfgr ( ) . modify ( |_, w| unsafe {
130+ w. adcpre ( ) . variant ( cfg. adcpre ) ;
131+ w. ppre2 ( ) . bits ( cfg. ppre2 as u8 ) ;
132+ w. ppre1 ( ) . bits ( cfg. ppre1 as u8 ) ;
133+ w. hpre ( ) . bits ( cfg. hpre as u8 ) ;
134+ w. usbpre ( ) . variant ( cfg. usbpre ) ;
135+ w. sw ( ) . bits ( if cfg. pllmul . is_some ( ) {
136+ // PLL
137+ 0b10
138+ } else {
139+ // HSE or HSI
140+ u8:: from ( cfg. hse . is_some ( ) )
141+ } )
142+ } ) ;
143+
144+ #[ cfg( any( feature = "stm32f100" , feature = "stm32f101" ) ) ]
145+ rcc. cfgr ( ) . modify ( |_, w| unsafe {
146+ w. adcpre ( ) . variant ( cfg. adcpre ) ;
147+ w. ppre2 ( ) . bits ( cfg. ppre2 as u8 ) ;
148+ w. ppre1 ( ) . bits ( cfg. ppre1 as u8 ) ;
149+ w. hpre ( ) . bits ( cfg. hpre as u8 ) ;
150+ w. sw ( ) . bits ( if cfg. pllmul . is_some ( ) {
151+ // PLL
152+ 0b10
153+ } else if cfg. hse . is_some ( ) {
154+ // HSE
155+ 0b1
156+ } else {
157+ // HSI
158+ 0b0
159+ } )
160+ } ) ;
161+
162+ Self {
163+ rb : self . rb ,
164+ clocks,
165+ }
166+ }
167+
168+ pub fn enable < T : Enable > ( & mut self , _periph : & T ) {
169+ T :: enable ( self ) ;
170+ }
171+
172+ pub fn reset < T : Reset > ( & mut self , _periph : & T ) {
173+ T :: reset ( self ) ;
174+ }
175+
176+ pub fn get_clock < T : BusClock > ( & self , _periph : & T ) -> Hertz {
177+ T :: clock ( & self . clocks )
178+ }
179+
180+ pub fn get_timer_clock < T : BusTimerClock > ( & self , _periph : & T ) -> Hertz {
181+ T :: timer_clock ( & self . clocks )
182+ }
183+ }
184+
48185impl Deref for Rcc {
49186 type Target = RCC ;
50187 fn deref ( & self ) -> & Self :: Target {
@@ -186,127 +323,6 @@ impl Config {
186323 }
187324}
188325
189- impl Rcc {
190- /// Applies the clock configuration and returns a `Clocks` struct that signifies that the
191- /// clocks are frozen, and contains the frequencies used. After this function is called,
192- /// the clocks can not change
193- ///
194- /// Usage:
195- ///
196- /// ```rust
197- /// let dp = pac::Peripherals::take().unwrap();
198- /// let mut flash = dp.FLASH.constrain();
199- /// let cfg = rcc::Config::hse(8.MHz()).sysclk(72.MHz());
200- /// let mut rcc = dp.RCC.constrain().freeze(cfg, &mut flash.acr);
201- /// ```
202- #[ allow( unused_variables) ]
203- #[ inline( always) ]
204- pub fn freeze ( self , cfg : impl Into < RawConfig > , acr : & mut ACR ) -> Self {
205- let cfg = cfg. into ( ) ;
206- let clocks = cfg. get_clocks ( ) ;
207- // adjust flash wait states
208- #[ cfg( any( feature = "stm32f103" , feature = "connectivity" ) ) ]
209- unsafe {
210- acr. acr ( ) . write ( |w| {
211- w. latency ( ) . bits ( if clocks. sysclk <= MHz ( 24 ) {
212- 0b000
213- } else if clocks. sysclk <= MHz ( 48 ) {
214- 0b001
215- } else {
216- 0b010
217- } )
218- } ) ;
219- }
220-
221- let rcc = unsafe { & * RCC :: ptr ( ) } ;
222-
223- if cfg. hse . is_some ( ) {
224- // enable HSE and wait for it to be ready
225-
226- rcc. cr ( ) . modify ( |_, w| {
227- if cfg. hse_bypass {
228- w. hsebyp ( ) . bypassed ( ) ;
229- }
230- w. hseon ( ) . set_bit ( )
231- } ) ;
232-
233- while rcc. cr ( ) . read ( ) . hserdy ( ) . bit_is_clear ( ) { }
234- }
235-
236- if let Some ( pllmul_bits) = cfg. pllmul {
237- // enable PLL and wait for it to be ready
238-
239- #[ allow( unused_unsafe) ]
240- rcc. cfgr ( ) . modify ( |_, w| unsafe {
241- w. pllmul ( ) . bits ( pllmul_bits) . pllsrc ( ) . bit ( cfg. hse . is_some ( ) )
242- } ) ;
243-
244- rcc. cr ( ) . modify ( |_, w| w. pllon ( ) . set_bit ( ) ) ;
245-
246- while rcc. cr ( ) . read ( ) . pllrdy ( ) . bit_is_clear ( ) { }
247- }
248-
249- // set prescalers and clock source
250- #[ cfg( feature = "connectivity" ) ]
251- rcc. cfgr ( ) . modify ( |_, w| unsafe {
252- w. adcpre ( ) . variant ( cfg. adcpre ) ;
253- w. ppre2 ( ) . bits ( cfg. ppre2 as u8 ) ;
254- w. ppre1 ( ) . bits ( cfg. ppre1 as u8 ) ;
255- w. hpre ( ) . bits ( cfg. hpre as u8 ) ;
256- w. otgfspre ( ) . variant ( cfg. usbpre ) ;
257- w. sw ( ) . bits ( if cfg. pllmul . is_some ( ) {
258- // PLL
259- 0b10
260- } else if cfg. hse . is_some ( ) {
261- // HSE
262- 0b1
263- } else {
264- // HSI
265- 0b0
266- } )
267- } ) ;
268-
269- #[ cfg( feature = "stm32f103" ) ]
270- rcc. cfgr ( ) . modify ( |_, w| unsafe {
271- w. adcpre ( ) . variant ( cfg. adcpre ) ;
272- w. ppre2 ( ) . bits ( cfg. ppre2 as u8 ) ;
273- w. ppre1 ( ) . bits ( cfg. ppre1 as u8 ) ;
274- w. hpre ( ) . bits ( cfg. hpre as u8 ) ;
275- w. usbpre ( ) . variant ( cfg. usbpre ) ;
276- w. sw ( ) . bits ( if cfg. pllmul . is_some ( ) {
277- // PLL
278- 0b10
279- } else {
280- // HSE or HSI
281- u8:: from ( cfg. hse . is_some ( ) )
282- } )
283- } ) ;
284-
285- #[ cfg( any( feature = "stm32f100" , feature = "stm32f101" ) ) ]
286- rcc. cfgr ( ) . modify ( |_, w| unsafe {
287- w. adcpre ( ) . variant ( cfg. adcpre ) ;
288- w. ppre2 ( ) . bits ( cfg. ppre2 as u8 ) ;
289- w. ppre1 ( ) . bits ( cfg. ppre1 as u8 ) ;
290- w. hpre ( ) . bits ( cfg. hpre as u8 ) ;
291- w. sw ( ) . bits ( if cfg. pllmul . is_some ( ) {
292- // PLL
293- 0b10
294- } else if cfg. hse . is_some ( ) {
295- // HSE
296- 0b1
297- } else {
298- // HSI
299- 0b0
300- } )
301- } ) ;
302-
303- Self {
304- rb : self . rb ,
305- clocks,
306- }
307- }
308- }
309-
310326pub trait BkpExt {
311327 /// Enables write access to the registers in the backup domain
312328 fn constrain ( self , pwr : & mut PWR , rcc : & mut RCC ) -> BackupDomain ;
0 commit comments