Skip to content

Commit

Permalink
Implement PortDriverPolarity for Mcp23x17
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-k authored and Rahix committed Apr 7, 2024
1 parent 23ae3dd commit c41baac
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/dev/mcp23x17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,32 @@ impl<B: Mcp23x17Bus> crate::PortDriverPullUp for Driver<B> {
}
}

impl<B: Mcp23x17Bus> crate::PortDriverPolarity for Driver<B> {
fn set_polarity(&mut self, mask: u32, inverted: bool) -> Result<(), Self::Error> {
let (mask_set, mask_clear) = match inverted {
true => (mask as u16, 0),
false => (0, mask as u16),
};
if mask & 0x00FF != 0 {
self.bus.update_reg(
self.addr,
Regs::IPOLA,
(mask_set & 0xFF) as u8,
(mask_clear & 0xFF) as u8,
)?;
}
if mask & 0xFF00 != 0 {
self.bus.update_reg(
self.addr,
Regs::IPOLB,
(mask_set >> 8) as u8,
(mask_clear >> 8) as u8,
)?;
}
Ok(())
}
}

// We need these newtype wrappers since we can't implement `Mcp23x17Bus` for both `I2cBus` and `SpiBus`
// at the same time
pub struct Mcp23017Bus<I2C>(I2C);
Expand Down

0 comments on commit c41baac

Please sign in to comment.