Skip to content

Commit

Permalink
Opamp - Pga does not need to own non-inverting input (#94)
Browse files Browse the repository at this point in the history
* Opamp - Pga does not need to own non-inverting input

* Update example
  • Loading branch information
usbalbin authored Dec 18, 2023
1 parent c34b89b commit 53783b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/opamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ fn main() -> ! {

#[allow(unreachable_code)]
{
let (_opamp1, _pa1, _mode, _some_pa2) = _opamp1.disable();
let (_opamp2, _pa7, _mode, _none) = opamp2.disable();
let (_opamp1, _pa1, _mode) = _opamp1.disable();
let (_opamp2, _pa7, _mode) = opamp2.disable();

loop {
delay.delay_ms(100);
Expand Down
23 changes: 11 additions & 12 deletions src/opamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ macro_rules! opamps {

/// States for opampX.
pub mod $opamp {
use core::{borrow::Borrow, marker::PhantomData};

#[allow(unused_imports)]
use crate::gpio::gpioa::*;

Expand Down Expand Up @@ -219,18 +221,18 @@ macro_rules! opamps {

/// State type for opamp running in programmable-gain mode.
pub struct Pga<NonInverting, MODE> {
non_inverting: NonInverting,
non_inverting: PhantomData<NonInverting>,
config: MODE,
output: Option<$output>,
}

/// Trait for opamps that can be run in programmable gain mode.
pub trait IntoPga <IntoNonInverting, MODE, IntoOutput, NonInverting>
pub trait IntoPga <MODE, IntoOutput, NonInverting>
where
IntoOutput: Into<$output>,
{
/// Configures the opamp for programmable gain operation.
fn pga(self, non_inverting: IntoNonInverting, config: MODE, output: Option<IntoOutput>)
fn pga<B: Borrow<NonInverting>>(self, non_inverting: B, config: MODE, output: Option<IntoOutput>)
-> Pga<NonInverting, MODE>;
}

Expand Down Expand Up @@ -295,9 +297,9 @@ macro_rules! opamps {
impl<NonInverting, MODE> Pga<NonInverting, MODE> {

/// Disables the opamp and returns the resources it held.
pub fn disable(self) -> (Disabled, NonInverting, MODE, Option<$output>) {
pub fn disable(self) -> (Disabled, MODE, Option<$output>) {
unsafe { (*crate::stm32::OPAMP::ptr()).[<$opamp _csr>].reset() }
(Disabled, self.non_inverting, self.config, self.output)
(Disabled, self.config, self.output)
}

/// Enables the external output pin.
Expand Down Expand Up @@ -504,19 +506,16 @@ macro_rules! opamps {
$mode:ty
} => {
paste::paste!{
impl <IntoNonInverting, IntoOutput> IntoPga
<IntoNonInverting, $mode, IntoOutput, $non_inverting> for Disabled
impl<IntoOutput> IntoPga<$mode, IntoOutput, $non_inverting> for Disabled
where
IntoNonInverting: Into<$non_inverting>,
IntoOutput: Into<$output>,
{
fn pga(
fn pga<B: Borrow<$non_inverting>>(
self,
non_inverting: IntoNonInverting,
_non_inverting: B,
config: $mode,
output: Option<IntoOutput>,
) -> Pga<$non_inverting, $mode> {
let non_inverting = non_inverting.into();
let output = output.map(|output| output.into());
unsafe {
use crate::stm32::opamp::[<$opamp _csr>]::OPAINTOEN_A;
Expand All @@ -539,7 +538,7 @@ macro_rules! opamps {
.enabled()
);
}
Pga {non_inverting, config, output}
Pga {non_inverting: PhantomData, config, output}
}
}
}
Expand Down

0 comments on commit 53783b3

Please sign in to comment.