@@ -12,6 +12,7 @@ use core::ops::Deref;
1212use crate :: gpio:: { DefaultMode , PA4 , PA5 , PA6 } ;
1313use crate :: pac;
1414use crate :: rcc:: { self , * } ;
15+ use crate :: stm32:: dac1:: mcr:: HFSEL ;
1516use crate :: stm32:: RCC ;
1617use embedded_hal:: delay:: DelayNs ;
1718
@@ -260,15 +261,23 @@ impl_pin_for_dac!(
260261 ( Dac4Ch1 <M_INT_SIG , Disabled >, Dac4Ch2 <M_INT_SIG , Disabled >)
261262) ;
262263
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+
263272impl < DAC : Instance , const CH : u8 , const MODE_BITS : u8 > DacCh < DAC , CH , MODE_BITS , Disabled > {
264273 /// TODO: The DAC does not seem to work unless `calibrate_buffer` has been callen
265274 /// 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 > {
267276 // We require rcc here just to ensure exclusive access to registers common to ch1 and ch2
268277 let dac = unsafe { & ( * DAC :: ptr ( ) ) } ;
269278
270279 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 ) } ) ;
272281 dac. cr ( ) . modify ( |_, w| w. en ( CH ) . set_bit ( ) ) ;
273282
274283 DacCh :: new ( )
@@ -277,13 +286,13 @@ impl<DAC: Instance, const CH: u8, const MODE_BITS: u8> DacCh<DAC, CH, MODE_BITS,
277286 pub fn enable_generator (
278287 self ,
279288 config : GeneratorConfig ,
280- _rcc : & mut Rcc ,
289+ rcc : & mut Rcc ,
281290 ) -> DacCh < DAC , CH , MODE_BITS , WaveGenerator > {
282291 // We require rcc here just to ensure exclusive access to registers common to ch1 and ch2
283292 let dac = unsafe { & ( * DAC :: ptr ( ) ) } ;
284293
285294 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 ) } ) ;
287296 dac. cr ( ) . modify ( |_, w| unsafe {
288297 w. wave ( CH ) . bits ( config. mode ) ;
289298 w. ten ( CH ) . set_bit ( ) ;
@@ -297,13 +306,13 @@ impl<DAC: Instance, const CH: u8, const MODE_BITS: u8> DacCh<DAC, CH, MODE_BITS,
297306 pub fn enable_sawtooth_generator (
298307 self ,
299308 config : SawtoothConfig ,
300- _rcc : & mut Rcc ,
309+ rcc : & mut Rcc ,
301310 ) -> DacCh < DAC , CH , MODE_BITS , SawtoothGenerator > {
302311 // TODO: We require rcc here just to ensure exclusive access to registers common to ch1 and ch2
303312 let dac = unsafe { & ( * DAC :: ptr ( ) ) } ;
304313
305314 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 ) } ) ;
307316
308317 unsafe {
309318 dac. stmodr ( ) . modify ( |_, w| {
0 commit comments