Skip to content

Commit

Permalink
Fix i2c + get rid of unused constants/enums (#1940)
Browse files Browse the repository at this point in the history
  • Loading branch information
playfulFence authored Aug 13, 2024
1 parent 352879a commit c53ba38
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 185 deletions.
14 changes: 7 additions & 7 deletions esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ xtensa-lx = { version = "0.9.0", optional = true }
# IMPORTANT:
# Each supported device MUST have its PAC included below along with a
# corresponding feature.
esp32 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32c2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32c3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32c6 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32s2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32s3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
esp32 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }
esp32c2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }
esp32c3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }
esp32c6 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }
esp32s2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }
esp32s3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "5270c86", features = ["critical-section", "rt"], optional = true }

[target.'cfg(target_arch = "riscv32")'.dependencies]
esp-riscv-rt = { version = "0.9.0", path = "../esp-riscv-rt" }
Expand Down
179 changes: 1 addition & 178 deletions esp-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,55 +184,6 @@ impl From<Ack> for u32 {
}
}

cfg_if::cfg_if! {
if #[cfg(any(esp32, esp32s2))] {
const OPCODE_RSTART: u32 = 0;
const OPCODE_WRITE: u32 = 1;
const OPCODE_READ: u32 = 2;
const OPCODE_STOP: u32 = 3;
const OPCODE_END: u32 = 4;
} else if #[cfg(any(esp32c2, esp32c3, esp32c6, esp32h2, esp32s3))] {
const OPCODE_RSTART: u32 = 6;
const OPCODE_WRITE: u32 = 1;
const OPCODE_READ: u32 = 3;
const OPCODE_STOP: u32 = 2;
const OPCODE_END: u32 = 4;
}
}
#[cfg_attr(feature = "debug", derive(Debug))]
#[derive(PartialEq)]
enum Opcode {
RStart,
Write,
Read,
Stop,
End,
}

impl From<Opcode> for u32 {
fn from(opcode: Opcode) -> u32 {
match opcode {
Opcode::RStart => OPCODE_RSTART,
Opcode::Write => OPCODE_WRITE,
Opcode::Read => OPCODE_READ,
Opcode::Stop => OPCODE_STOP,
Opcode::End => OPCODE_END,
}
}
}
impl From<u32> for Opcode {
fn from(opcode: u32) -> Self {
match opcode {
OPCODE_RSTART => Opcode::RStart,
OPCODE_WRITE => Opcode::Write,
OPCODE_READ => Opcode::Read,
OPCODE_STOP => Opcode::Stop,
OPCODE_END => Opcode::End,
_ => unreachable!(),
}
}
}

/// I2C peripheral container (I2C)
pub struct I2C<'d, T, DM: crate::Mode> {
peripheral: PeripheralRef<'d, T>,
Expand Down Expand Up @@ -1660,10 +1611,7 @@ pub trait Instance: crate::private::Sealed {
for cmd_reg in self.register_block().comd_iter() {
let cmd = cmd_reg.read();

if cmd.bits() != 0x0
&& cmd.opcode().bits() != (OPCODE_END as u8)
&& !cmd.command_done().bit_is_set()
{
if cmd.bits() != 0x0 && !cmd.opcode().is_end() && !cmd.command_done().bit_is_set() {
return Err(Error::ExecIncomplete);
}
}
Expand Down Expand Up @@ -2213,15 +2161,6 @@ pub mod lp_i2c {
Nack,
}

#[allow(unused)]
enum Opcode {
RStart = 6,
Write = 1,
Read = 3,
Stop = 2,
End = 4,
}

#[derive(PartialEq)]
#[allow(unused)]
enum Command {
Expand All @@ -2248,122 +2187,6 @@ pub mod lp_i2c {
},
}

impl From<Command> for u16 {
fn from(c: Command) -> u16 {
let opcode = match c {
Command::Start => Opcode::RStart,
Command::Stop => Opcode::Stop,
Command::End => Opcode::End,
Command::Write { .. } => Opcode::Write,
Command::Read { .. } => Opcode::Read,
};

let length = match c {
Command::Start | Command::Stop | Command::End => 0,
Command::Write { length: l, .. } | Command::Read { length: l, .. } => l,
};

let ack_exp = match c {
Command::Start | Command::Stop | Command::End | Command::Read { .. } => Ack::Nack,
Command::Write { ack_exp: exp, .. } => exp,
};

let ack_check_en = match c {
Command::Start | Command::Stop | Command::End | Command::Read { .. } => false,
Command::Write {
ack_check_en: en, ..
} => en,
};

let ack_value = match c {
Command::Start | Command::Stop | Command::End | Command::Write { .. } => Ack::Nack,
Command::Read { ack_value: ack, .. } => ack,
};

let mut cmd: u16 = length.into();

if ack_check_en {
cmd |= 1 << 8;
} else {
cmd &= !(1 << 8);
}

if ack_exp == Ack::Nack {
cmd |= 1 << 9;
} else {
cmd &= !(1 << 9);
}

if ack_value == Ack::Nack {
cmd |= 1 << 10;
} else {
cmd &= !(1 << 10);
}

cmd |= (opcode as u16) << 11;

cmd
}
}

impl From<Command> for u32 {
fn from(c: Command) -> u32 {
let opcode = match c {
Command::Start => Opcode::RStart,
Command::Stop => Opcode::Stop,
Command::End => Opcode::End,
Command::Write { .. } => Opcode::Write,
Command::Read { .. } => Opcode::Read,
};

let length = match c {
Command::Start | Command::Stop | Command::End => 0,
Command::Write { length: l, .. } | Command::Read { length: l, .. } => l,
};

let ack_exp = match c {
Command::Start | Command::Stop | Command::End | Command::Read { .. } => Ack::Nack,
Command::Write { ack_exp: exp, .. } => exp,
};

let ack_check_en = match c {
Command::Start | Command::Stop | Command::End | Command::Read { .. } => false,
Command::Write {
ack_check_en: en, ..
} => en,
};

let ack_value = match c {
Command::Start | Command::Stop | Command::End | Command::Write { .. } => Ack::Nack,
Command::Read { ack_value: ack, .. } => ack,
};

let mut cmd: u32 = length.into();

if ack_check_en {
cmd |= 1 << 8;
} else {
cmd &= !(1 << 8);
}

if ack_exp == Ack::Nack {
cmd |= 1 << 9;
} else {
cmd &= !(1 << 9);
}

if ack_value == Ack::Nack {
cmd |= 1 << 10;
} else {
cmd &= !(1 << 10);
}

cmd |= (opcode as u32) << 11;

cmd
}
}

// https://github.com/espressif/esp-idf/blob/master/components/ulp/lp_core/lp_core_i2c.c#L122
// TX/RX RAM size is 16*8 bit
// TX RX FIFO has 16 bit depth
Expand Down

0 comments on commit c53ba38

Please sign in to comment.