@@ -64,7 +64,7 @@ impl ED for Disabled {}
64
64
65
65
macro_rules! impl_dac {
66
66
( $DACxCHy: ident) => {
67
- pub struct $DACxCHy<ED > {
67
+ pub struct $DACxCHy<const MODE_BITS : u8 , ED > {
68
68
_enabled: PhantomData <ED >,
69
69
}
70
70
} ;
@@ -83,21 +83,63 @@ pub trait Pins<DAC> {
83
83
type Output ;
84
84
}
85
85
86
+ const M_EXT_PIN : u8 = 0b000 ;
87
+ const M_MIX_SIG : u8 = 0b001 ;
88
+ const M_INT_SIG : u8 = 0b011 ;
89
+
90
+ pub struct Dac1IntSig1 ;
91
+ pub struct Dac1IntSig2 ;
92
+ pub struct Dac2IntSig1 ;
93
+ pub struct Dac3IntSig1 ;
94
+ pub struct Dac3IntSig2 ;
95
+ pub struct Dac4IntSig1 ;
96
+ pub struct Dac4IntSig2 ;
97
+
86
98
macro_rules! impl_pin_for_dac {
87
99
( $DAC: ident: $pin: ty, $output: ty) => {
100
+ #[ allow( unused_parens) ]
88
101
impl Pins <$DAC> for $pin {
102
+ #[ allow( unused_parens) ]
89
103
type Output = $output;
90
104
}
91
105
} ;
92
106
}
93
107
94
- impl_pin_for_dac ! ( DAC1 : PA4 <DefaultMode >, Dac1Ch1 <Disabled >) ;
95
- impl_pin_for_dac ! ( DAC1 : PA5 <DefaultMode >, Dac1Ch2 <Disabled >) ;
96
- impl_pin_for_dac ! (
97
- DAC1 : ( PA4 <DefaultMode >, PA5 <DefaultMode >) ,
98
- ( Dac1Ch1 <Disabled >, Dac1Ch2 <Disabled >)
99
- ) ;
100
- impl_pin_for_dac ! ( DAC2 : PA6 <DefaultMode >, Dac2Ch1 <Disabled >) ;
108
+ // Implement all combinations of ch2 for the specified ch1 on DAC1
109
+ macro_rules! impl_dac1_ch2_combos {
110
+ ( $( $pin_ch1: ty, $output_ch1: ty) * ) => {
111
+ $( impl_pin_for_dac!( DAC1 : $pin_ch1, // ch2: Not used
112
+ $output_ch1
113
+ ) ; ) *
114
+ impl_pin_for_dac!( DAC1 : ( $( $pin_ch1, ) * PA5 <DefaultMode >) , // ch2: Ext pin
115
+ ( $( $output_ch1, ) * Dac1Ch2 <M_EXT_PIN , Disabled >)
116
+ ) ;
117
+ impl_pin_for_dac!( DAC1 : ( $( $pin_ch1, ) * Dac1IntSig2 ) , // ch2: Internal
118
+ ( $( $output_ch1, ) * Dac1Ch2 <M_INT_SIG , Disabled >)
119
+ ) ;
120
+ impl_pin_for_dac!( DAC1 : ( $( $pin_ch1, ) * ( PA5 <DefaultMode >, Dac1IntSig2 ) ) , // ch2: Mixed
121
+ ( $( $output_ch1, ) * Dac1Ch2 <M_MIX_SIG , Disabled >)
122
+ ) ;
123
+ } ;
124
+ }
125
+
126
+ impl_dac1_ch2_combos ! ( ) ; // ch1: Not used
127
+ impl_dac1_ch2_combos ! ( PA4 <DefaultMode >, Dac1Ch1 <M_EXT_PIN , Disabled >) ; // ch1: Ext pin
128
+ impl_dac1_ch2_combos ! ( Dac1IntSig1 , Dac1Ch1 <M_INT_SIG , Disabled >) ; // ch1: Internal
129
+ impl_dac1_ch2_combos ! ( ( PA4 <DefaultMode >, Dac1IntSig1 ) , Dac1Ch1 <M_MIX_SIG , Disabled >) ; // ch1: Mixed
130
+
131
+ // DAC2
132
+ impl_pin_for_dac ! ( DAC2 : PA6 <DefaultMode >, Dac2Ch1 <M_EXT_PIN , Disabled >) ; // ch1: Ext pin
133
+ impl_pin_for_dac ! ( DAC2 : Dac2IntSig1 , Dac2Ch1 <M_INT_SIG , Disabled >) ; // ch1: Internal
134
+ impl_pin_for_dac ! ( DAC2 : ( PA6 <DefaultMode >, Dac2IntSig1 ) , Dac2Ch1 <M_MIX_SIG , Disabled >) ; // ch1: Mixed
135
+
136
+ // DAC3 int
137
+ impl_pin_for_dac ! ( DAC3 : Dac3IntSig1 , Dac3Ch1 <M_INT_SIG , Disabled >) ;
138
+ impl_pin_for_dac ! ( DAC3 : Dac3IntSig2 , Dac3Ch2 <M_INT_SIG , Disabled >) ;
139
+
140
+ // DAC4 int
141
+ impl_pin_for_dac ! ( DAC4 : Dac4IntSig1 , Dac4Ch1 <M_INT_SIG , Disabled >) ;
142
+ impl_pin_for_dac ! ( DAC4 : Dac4IntSig2 , Dac4Ch2 <M_INT_SIG , Disabled >) ;
101
143
102
144
pub fn dac < DAC , PINS > ( _dac : DAC , _pins : PINS , _rcc : & mut Rcc ) -> PINS :: Output
103
145
where
@@ -132,19 +174,19 @@ macro_rules! dac_helper {
132
174
$swtrig: ident
133
175
) , ) +) => {
134
176
$(
135
- impl $CX<Disabled > {
136
- pub fn enable( self ) -> $CX<Enabled > {
177
+ impl < const MODE_BITS : u8 > $CX<MODE_BITS , Disabled > {
178
+ pub fn enable( self ) -> $CX<MODE_BITS , Enabled > {
137
179
let dac = unsafe { & ( * <$DAC>:: ptr( ) ) } ;
138
180
139
- dac. dac_mcr. modify( |_, w| unsafe { w. $mode( ) . bits( 1 ) } ) ;
181
+ dac. dac_mcr. modify( |_, w| unsafe { w. $mode( ) . bits( MODE_BITS ) } ) ;
140
182
dac. dac_cr. modify( |_, w| w. $en( ) . set_bit( ) ) ;
141
183
142
184
$CX {
143
185
_enabled: PhantomData ,
144
186
}
145
187
}
146
188
147
- pub fn enable_unbuffered( self ) -> $CX<EnabledUnbuffered > {
189
+ /* pub fn enable_unbuffered(self) -> $CX<MODE_BITS, EnabledUnbuffered> {
148
190
let dac = unsafe { &(*<$DAC>::ptr()) };
149
191
150
192
dac.dac_mcr.modify(|_, w| unsafe { w.$mode().bits(2) });
@@ -153,12 +195,12 @@ macro_rules! dac_helper {
153
195
$CX {
154
196
_enabled: PhantomData,
155
197
}
156
- }
198
+ }*/
157
199
158
- pub fn enable_generator( self , config: GeneratorConfig ) -> $CX<WaveGenerator > {
200
+ pub fn enable_generator( self , config: GeneratorConfig ) -> $CX<MODE_BITS , WaveGenerator > {
159
201
let dac = unsafe { & ( * <$DAC>:: ptr( ) ) } ;
160
202
161
- dac. dac_mcr. modify( |_, w| unsafe { w. $mode( ) . bits( 1 ) } ) ;
203
+ dac. dac_mcr. modify( |_, w| unsafe { w. $mode( ) . bits( MODE_BITS ) } ) ;
162
204
dac. dac_cr. modify( |_, w| unsafe {
163
205
w. $wave( ) . bits( config. mode) ;
164
206
w. $ten( ) . set_bit( ) ;
@@ -172,7 +214,7 @@ macro_rules! dac_helper {
172
214
}
173
215
}
174
216
175
- impl <ED > $CX<ED > {
217
+ impl <const MODE_BITS : u8 , ED > $CX<MODE_BITS , ED > {
176
218
/// Calibrate the DAC output buffer by performing a "User
177
219
/// trimming" operation. It is useful when the VDDA/VREF+
178
220
/// voltage or temperature differ from the factory trimming
@@ -184,7 +226,7 @@ macro_rules! dac_helper {
184
226
///
185
227
/// After the calibration operation, the DAC channel is
186
228
/// disabled.
187
- pub fn calibrate_buffer<T >( self , delay: & mut T ) -> $CX<Disabled >
229
+ pub fn calibrate_buffer<T >( self , delay: & mut T ) -> $CX<MODE_BITS , Disabled >
188
230
where
189
231
T : DelayUs <u32 >,
190
232
{
@@ -209,7 +251,7 @@ macro_rules! dac_helper {
209
251
}
210
252
211
253
/// Disable the DAC channel
212
- pub fn disable( self ) -> $CX<Disabled > {
254
+ pub fn disable( self ) -> $CX<MODE_BITS , Disabled > {
213
255
let dac = unsafe { & ( * <$DAC>:: ptr( ) ) } ;
214
256
dac. dac_cr. modify( |_, w| unsafe {
215
257
w. $en( ) . clear_bit( ) . $wave( ) . bits( 0 ) . $ten( ) . clear_bit( )
@@ -223,7 +265,7 @@ macro_rules! dac_helper {
223
265
224
266
/// DacOut implementation available in any Enabled/Disabled
225
267
/// state
226
- impl <ED > DacOut <u16 > for $CX<ED > {
268
+ impl <const MODE_BITS : u8 , ED > DacOut <u16 > for $CX<MODE_BITS , ED > {
227
269
fn set_value( & mut self , val: u16 ) {
228
270
let dac = unsafe { & ( * <$DAC>:: ptr( ) ) } ;
229
271
dac. $dhrx. write( |w| unsafe { w. bits( val as u32 ) } ) ;
@@ -236,7 +278,7 @@ macro_rules! dac_helper {
236
278
}
237
279
238
280
/// Wave generator state implementation
239
- impl $CX<WaveGenerator > {
281
+ impl < const MODE_BITS : u8 > $CX<MODE_BITS , WaveGenerator > {
240
282
pub fn trigger( & mut self ) {
241
283
let dac = unsafe { & ( * <$DAC>:: ptr( ) ) } ;
242
284
dac. dac_swtrgr. write( |w| { w. $swtrig( ) . set_bit( ) } ) ;
0 commit comments