@@ -12,6 +12,7 @@ use core::ops::Deref;
12
12
use crate :: gpio:: { DefaultMode , PA4 , PA5 , PA6 } ;
13
13
use crate :: pac;
14
14
use crate :: rcc:: { self , * } ;
15
+ use crate :: stm32:: dac1:: mcr:: HFSEL ;
15
16
use crate :: stm32:: RCC ;
16
17
use embedded_hal:: delay:: DelayNs ;
17
18
@@ -260,15 +261,23 @@ impl_pin_for_dac!(
260
261
( Dac4Ch1 <M_INT_SIG , Disabled >, Dac4Ch2 <M_INT_SIG , Disabled >)
261
262
) ;
262
263
264
+ pub fn hfsel ( rcc : & Rcc ) -> HFSEL {
265
+ match rcc. clocks . ahb_clk . to_MHz ( ) {
266
+ 0 ..80 => pac:: dac1:: mcr:: HFSEL :: Disabled ,
267
+ 80 ..160 => pac:: dac1:: mcr:: HFSEL :: More80mhz ,
268
+ 160 .. => pac:: dac1:: mcr:: HFSEL :: More160mhz ,
269
+ }
270
+ }
271
+
263
272
impl < DAC : Instance , const CH : u8 , const MODE_BITS : u8 > DacCh < DAC , CH , MODE_BITS , Disabled > {
264
273
/// TODO: The DAC does not seem to work unless `calibrate_buffer` has been callen
265
274
/// even when only using dac output internally
266
- pub fn enable ( self , _rcc : & mut Rcc ) -> DacCh < DAC , CH , MODE_BITS , Enabled > {
275
+ pub fn enable ( self , rcc : & mut Rcc ) -> DacCh < DAC , CH , MODE_BITS , Enabled > {
267
276
// We require rcc here just to ensure exclusive access to registers common to ch1 and ch2
268
277
let dac = unsafe { & ( * DAC :: ptr ( ) ) } ;
269
278
270
279
dac. mcr ( )
271
- . modify ( |_, w| unsafe { w. mode ( CH ) . bits ( MODE_BITS ) } ) ;
280
+ . modify ( |_, w| unsafe { w. hfsel ( ) . variant ( hfsel ( rcc ) ) . mode ( CH ) . bits ( MODE_BITS ) } ) ;
272
281
dac. cr ( ) . modify ( |_, w| w. en ( CH ) . set_bit ( ) ) ;
273
282
274
283
DacCh :: new ( )
@@ -277,13 +286,13 @@ impl<DAC: Instance, const CH: u8, const MODE_BITS: u8> DacCh<DAC, CH, MODE_BITS,
277
286
pub fn enable_generator (
278
287
self ,
279
288
config : GeneratorConfig ,
280
- _rcc : & mut Rcc ,
289
+ rcc : & mut Rcc ,
281
290
) -> DacCh < DAC , CH , MODE_BITS , WaveGenerator > {
282
291
// We require rcc here just to ensure exclusive access to registers common to ch1 and ch2
283
292
let dac = unsafe { & ( * DAC :: ptr ( ) ) } ;
284
293
285
294
dac. mcr ( )
286
- . modify ( |_, w| unsafe { w. mode ( CH ) . bits ( MODE_BITS ) } ) ;
295
+ . modify ( |_, w| unsafe { w. hfsel ( ) . variant ( hfsel ( rcc ) ) . mode ( CH ) . bits ( MODE_BITS ) } ) ;
287
296
dac. cr ( ) . modify ( |_, w| unsafe {
288
297
w. wave ( CH ) . bits ( config. mode ) ;
289
298
w. ten ( CH ) . set_bit ( ) ;
@@ -297,13 +306,13 @@ impl<DAC: Instance, const CH: u8, const MODE_BITS: u8> DacCh<DAC, CH, MODE_BITS,
297
306
pub fn enable_sawtooth_generator (
298
307
self ,
299
308
config : SawtoothConfig ,
300
- _rcc : & mut Rcc ,
309
+ rcc : & mut Rcc ,
301
310
) -> DacCh < DAC , CH , MODE_BITS , SawtoothGenerator > {
302
311
// TODO: We require rcc here just to ensure exclusive access to registers common to ch1 and ch2
303
312
let dac = unsafe { & ( * DAC :: ptr ( ) ) } ;
304
313
305
314
dac. mcr ( )
306
- . modify ( |_, w| unsafe { w. mode ( CH ) . bits ( MODE_BITS ) } ) ;
315
+ . modify ( |_, w| unsafe { w. hfsel ( ) . variant ( hfsel ( rcc ) ) . mode ( CH ) . bits ( MODE_BITS ) } ) ;
307
316
308
317
unsafe {
309
318
dac. stmodr ( ) . modify ( |_, w| {
0 commit comments