@@ -55,6 +55,8 @@ macro_rules! opamps {
55
55
$opamp: ident
56
56
:
57
57
{
58
+ vinm0: $vinm0: ty,
59
+
58
60
inverting
59
61
:
60
62
{
@@ -95,6 +97,13 @@ macro_rules! opamps {
95
97
$(
96
98
/// States for opampX.
97
99
pub mod $opamp {
100
+ use crate :: stm32:: opamp:: [ <$opamp _csr>] :: PGA_GAIN_A ;
101
+
102
+ #[ allow( unused_imports) ]
103
+ use crate :: gpio:: gpioa:: * ;
104
+
105
+ #[ allow( unused_imports) ]
106
+ use crate :: gpio:: gpiob:: * ;
98
107
99
108
/// State type for disabled opamp.
100
109
pub struct Disabled ;
@@ -115,26 +124,69 @@ macro_rules! opamps {
115
124
/// State type for opamp running in programmable-gain mode.
116
125
pub struct Pga <NonInverting , MODE > {
117
126
non_inverting: NonInverting ,
118
- config: Config < MODE > ,
127
+ config: MODE ,
119
128
output: Option <$output>,
120
129
}
121
130
131
+ trait PgaMode {
132
+ fn pga_gain_bits( & self ) -> PGA_GAIN_A ;
133
+ }
134
+
135
+ struct PgaModeInternal ( NonInvertingGain ) ;
136
+ impl PgaMode for PgaModeInternal {
137
+ fn pga_gain_bits( & self ) -> PGA_GAIN_A {
138
+ match self . 0 {
139
+ NonInvertingGain :: Gain2 => PGA_GAIN_A :: Gain2 ,
140
+ NonInvertingGain :: Gain4 => PGA_GAIN_A :: Gain4 ,
141
+ NonInvertingGain :: Gain8 => PGA_GAIN_A :: Gain8 ,
142
+ NonInvertingGain :: Gain16 => PGA_GAIN_A :: Gain16 ,
143
+ NonInvertingGain :: Gain32 => PGA_GAIN_A :: Gain32 ,
144
+ NonInvertingGain :: Gain64 => PGA_GAIN_A :: Gain64 ,
145
+ }
146
+ }
147
+ }
148
+
149
+ /// Same as PgaModeInternal but the inverted signal is routed to pin to allow external filter
150
+ struct PgaModeInvertedInputFiltered <PIN > {
151
+ gain: NonInvertingGain ,
152
+ pin: core:: marker:: PhantomData <PIN >
153
+ }
154
+ impl <PIN > PgaMode for PgaModeInvertedInputFiltered <PIN > {
155
+ fn pga_gain_bits( & self ) -> PGA_GAIN_A {
156
+ match self . gain {
157
+ NonInvertingGain :: Gain2 => PGA_GAIN_A :: Gain2FilteringVinm0 ,
158
+ NonInvertingGain :: Gain4 => PGA_GAIN_A :: Gain4FilteringVinm0 ,
159
+ NonInvertingGain :: Gain8 => PGA_GAIN_A :: Gain8FilteringVinm0 ,
160
+ NonInvertingGain :: Gain16 => PGA_GAIN_A :: Gain16FilteringVinm0 ,
161
+ NonInvertingGain :: Gain32 => PGA_GAIN_A :: Gain32FilteringVinm0 ,
162
+ NonInvertingGain :: Gain64 => PGA_GAIN_A :: Gain64FilteringVinm0 ,
163
+ }
164
+ }
165
+ }
166
+
167
+ /// PGA Gain for non inverted modes
122
168
pub enum NonInvertingGain {
169
+ /// 2x Gain
123
170
Gain2 = 0 ,
171
+
172
+ /// 4x Gain
124
173
Gain4 = 1 ,
174
+
175
+ /// 8x Gain
125
176
Gain8 = 2 ,
177
+
178
+ /// 16x Gain
126
179
Gain16 = 3 ,
180
+
181
+ /// 32x Gain
127
182
Gain32 = 4 ,
183
+
184
+ /// 64x Gain
128
185
Gain64 = 5
129
186
}
130
187
131
188
// TODO: Inverting gain
132
189
133
- pub struct Config <MODE > {
134
- mode: MODE ,
135
- pga_gain: crate :: stm32:: opamp:: [ <$opamp _csr>] :: PGA_GAIN_A ,
136
- }
137
-
138
190
/// Trait for opamps that can be run in follower mode.
139
191
pub trait IntoFollower <IntoInput , IntoOutput , Input >
140
192
where
@@ -160,7 +212,7 @@ macro_rules! opamps {
160
212
IntoOutput : Into <$output>,
161
213
{
162
214
/// Configures the opamp for programmable gain operation.
163
- fn pga( self , non_inverting: IntoNonInverting , config: Config < MODE > , output: Option <IntoOutput >)
215
+ fn pga( self , non_inverting: IntoNonInverting , config: MODE , output: Option <IntoOutput >)
164
216
-> Pga <NonInverting , MODE >;
165
217
}
166
218
@@ -225,7 +277,7 @@ macro_rules! opamps {
225
277
impl <NonInverting , MODE > Pga <NonInverting , MODE > {
226
278
227
279
/// Disables the opamp and returns the resources it held.
228
- pub fn disable( self ) -> ( Disabled , NonInverting , Config < MODE > , Option <$output>) {
280
+ pub fn disable( self ) -> ( Disabled , NonInverting , MODE , Option <$output>) {
229
281
unsafe { ( * crate :: stm32:: OPAMP :: ptr( ) ) . [ <$opamp _csr>] . reset( ) }
230
282
( Disabled , self . non_inverting, self . config, self . output)
231
283
}
@@ -253,7 +305,7 @@ macro_rules! opamps {
253
305
254
306
opamps!{ @follower $opamp, $output, $( $non_inverting_mask, $non_inverting) ,* }
255
307
opamps!{ @open_loop_tt $opamp, $output, $( $non_inverting_mask, $non_inverting) ,* : ( $( $inverting_mask, $inverting) ,* ) }
256
- opamps!{ @pga_tt $opamp, $output, $( $non_inverting_mask, $non_inverting) ,* : ( $ ( $inverting ) , * ) }
308
+ opamps!{ @pga_tt $opamp, $output, $( $non_inverting_mask, $non_inverting) ,* : $vinm0 }
257
309
}
258
310
) *
259
311
@@ -411,10 +463,13 @@ macro_rules! opamps {
411
463
,
412
464
$( $non_inverting_mask: tt, $non_inverting: ty) ,*
413
465
:
414
- $invertings : tt
466
+ $vinm0 : ident
415
467
} => {
416
468
$(
417
- opamps!{ @pga $opamp, $output, $non_inverting_mask, $non_inverting, $invertings }
469
+ opamps!{ @pga $opamp, $output, $non_inverting_mask, $non_inverting, PgaModeInternal }
470
+ opamps!{ @pga $opamp, $output, $non_inverting_mask, $non_inverting, PgaModeInvertedInputFiltered <$vinm0<crate :: gpio:: Analog >> }
471
+ // TODO: Add `PGA mode, non-inverting gain setting (x2/x4/x8/x16/x32/x64) or inverting gain setting (x-1/x-3/x-7/x-15/x-31/x-63)`
472
+ // TODO: Add `PGA mode, non-inverting gain setting (x2/x4/x8/x16/x32/x64) or inverting gain setting (x-1/x-3/x-7/x-15/x-31/x-63) with filtering`
418
473
) *
419
474
} ;
420
475
@@ -428,25 +483,26 @@ macro_rules! opamps {
428
483
,
429
484
$non_inverting: ty
430
485
,
431
- ( $ ( $invertings : ty) , * )
486
+ $mode : ty
432
487
} => {
433
488
paste:: paste!{
434
- $ ( impl <IntoNonInverting , $mode , IntoOutput > IntoPga
435
- <IntoNonInverting , Config < $mode> , IntoOutput , $non_inverting> for Disabled
489
+ impl <IntoNonInverting , IntoOutput > IntoPga
490
+ <IntoNonInverting , $mode, IntoOutput , $non_inverting> for Disabled
436
491
where
437
492
IntoNonInverting : Into <$non_inverting>,
438
493
IntoOutput : Into <$output>,
439
494
{
440
495
fn pga(
441
496
self ,
442
497
non_inverting: IntoNonInverting ,
443
- config: Config < $mode> ,
498
+ config: $mode,
444
499
output: Option <IntoOutput >,
445
- ) -> Pga <$non_inverting, $config > {
500
+ ) -> Pga <$non_inverting, $mode > {
446
501
let non_inverting = non_inverting. into( ) ;
447
502
let output = output. map( |output| output. into( ) ) ;
448
503
unsafe {
449
504
use crate :: stm32:: opamp:: [ <$opamp _csr>] :: OPAINTOEN_A ;
505
+
450
506
( * crate :: stm32:: OPAMP :: ptr( ) )
451
507
. [ <$opamp _csr>]
452
508
. write( |csr_w|
@@ -455,7 +511,7 @@ macro_rules! opamps {
455
511
. vm_sel( )
456
512
. pga( )
457
513
. pga_gain( )
458
- . variant( config. pga_gain )
514
+ . variant( config. pga_gain_bits ( ) )
459
515
. opaintoen( )
460
516
. variant( match output {
461
517
Some ( _) => OPAINTOEN_A :: OutputPin ,
@@ -467,14 +523,15 @@ macro_rules! opamps {
467
523
}
468
524
Pga { non_inverting, config, output}
469
525
}
470
- } ) *
526
+ }
471
527
}
472
528
} ;
473
529
}
474
530
475
531
#[ cfg( any( feature = "stm32g431" , feature = "stm32g441" , feature = "stm32g471" , ) ) ]
476
532
opamps ! {
477
533
opamp1: {
534
+ vinm0: PA3 ,
478
535
inverting: {
479
536
crate :: gpio:: gpioa:: PA3 <crate :: gpio:: Analog >: vinm0,
480
537
crate :: gpio:: gpioc:: PC5 <crate :: gpio:: Analog >: vinm1,
@@ -487,6 +544,7 @@ opamps! {
487
544
output: crate :: gpio:: gpioa:: PA2 <crate :: gpio:: Analog >,
488
545
} ,
489
546
opamp2: {
547
+ vinm0: PA5 ,
490
548
inverting: {
491
549
crate :: gpio:: gpioa:: PA5 <crate :: gpio:: Analog >: vinm0,
492
550
crate :: gpio:: gpioc:: PC5 <crate :: gpio:: Analog >: vinm1,
@@ -500,6 +558,7 @@ opamps! {
500
558
output: crate :: gpio:: gpioa:: PA6 <crate :: gpio:: Analog >,
501
559
} ,
502
560
opamp3: {
561
+ vinm0: PB2 ,
503
562
inverting: {
504
563
crate :: gpio:: gpiob:: PB2 <crate :: gpio:: Analog >: vinm0,
505
564
crate :: gpio:: gpiob:: PB10 <crate :: gpio:: Analog >: vinm1,
@@ -521,6 +580,7 @@ opamps! {
521
580
) ) ]
522
581
opamps ! {
523
582
opamp1: {
583
+ vinm0: PA3 ,
524
584
inverting: {
525
585
crate :: gpio:: gpioa:: PA3 <crate :: gpio:: Analog >: vinm0,
526
586
crate :: gpio:: gpioc:: PC5 <crate :: gpio:: Analog >: vinm1,
@@ -533,6 +593,7 @@ opamps! {
533
593
output: crate :: gpio:: gpioa:: PA2 <crate :: gpio:: Analog >,
534
594
} ,
535
595
opamp2: {
596
+ vinm0: PA5 ,
536
597
inverting: {
537
598
crate :: gpio:: gpioa:: PA5 <crate :: gpio:: Analog >: vinm0,
538
599
crate :: gpio:: gpioc:: PC5 <crate :: gpio:: Analog >: vinm1,
@@ -546,6 +607,7 @@ opamps! {
546
607
output: crate :: gpio:: gpioa:: PA6 <crate :: gpio:: Analog >,
547
608
} ,
548
609
opamp3: {
610
+ vinm0: PB2 ,
549
611
inverting: {
550
612
crate :: gpio:: gpiob:: PB2 <crate :: gpio:: Analog >: vinm0,
551
613
crate :: gpio:: gpiob:: PB10 <crate :: gpio:: Analog >: vinm1,
@@ -558,6 +620,7 @@ opamps! {
558
620
output: crate :: gpio:: gpiob:: PB1 <crate :: gpio:: Analog >,
559
621
} ,
560
622
opamp4: {
623
+ vinm0: PB10 ,
561
624
inverting: {
562
625
crate :: gpio:: gpiob:: PB10 <crate :: gpio:: Analog >: vinm0,
563
626
crate :: gpio:: gpiod:: PD8 <crate :: gpio:: Analog >: vinm1,
@@ -570,6 +633,7 @@ opamps! {
570
633
output: crate :: gpio:: gpiob:: PB12 <crate :: gpio:: Analog >,
571
634
} ,
572
635
opamp5: {
636
+ vinm0: PB15 ,
573
637
inverting: {
574
638
crate :: gpio:: gpiob:: PB15 <crate :: gpio:: Analog >: vinm0,
575
639
crate :: gpio:: gpioa:: PA3 <crate :: gpio:: Analog >: vinm1,
@@ -582,6 +646,7 @@ opamps! {
582
646
output: crate :: gpio:: gpioa:: PA8 <crate :: gpio:: Analog >,
583
647
} ,
584
648
opamp6: {
649
+ vinm0: PA1 ,
585
650
inverting: {
586
651
crate :: gpio:: gpioa:: PA1 <crate :: gpio:: Analog >: vinm0,
587
652
crate :: gpio:: gpiob:: PB1 <crate :: gpio:: Analog >: vinm1,
0 commit comments