Implement I2C pullup configuration

This commit is contained in:
chemicstry
2022-08-09 22:13:35 +03:00
parent b7b4c84067
commit 6498324b58
7 changed files with 86 additions and 12 deletions

View File

@ -4,11 +4,24 @@ use embassy_embedded_hal::SetConfig;
use embassy_hal_common::into_ref;
use crate::gpio::sealed::AFType;
use crate::gpio::Pull;
use crate::i2c::{Error, Instance, SclPin, SdaPin};
use crate::pac::i2c;
use crate::time::Hertz;
use crate::Peripheral;
#[non_exhaustive]
#[derive(Copy, Clone)]
pub struct Config {
pullup_enable: bool,
}
impl Default for Config {
fn default() -> Self {
Self { pullup_enable: true }
}
}
pub struct State {}
impl State {
@ -27,15 +40,21 @@ impl<'d, T: Instance> I2c<'d, T> {
scl: impl Peripheral<P = impl SclPin<T>> + 'd,
sda: impl Peripheral<P = impl SdaPin<T>> + 'd,
freq: Hertz,
config: Config,
) -> Self {
into_ref!(scl, sda);
T::enable();
T::reset();
let pull = match config.pullup_enable {
true => Pull::Up,
false => Pull::None,
};
unsafe {
scl.set_as_af(scl.af_num(), AFType::OutputOpenDrain);
sda.set_as_af(sda.af_num(), AFType::OutputOpenDrain);
scl.set_as_af_pull(scl.af_num(), AFType::OutputOpenDrain, pull);
sda.set_as_af_pull(sda.af_num(), AFType::OutputOpenDrain, pull);
}
unsafe {