Skip to content

Commit bb6e710

Browse files
authored
Removing unnecessary bitfield from I2C driver (esp-rs#1900)
* Getting rid of bitfield usage fmt * use `variant` methods for opcode * update pacs dependency
1 parent 6ac56a2 commit bb6e710

File tree

2 files changed

+47
-94
lines changed

2 files changed

+47
-94
lines changed

esp-hal/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ xtensa-lx = { version = "0.9.0", optional = true }
5353
# IMPORTANT:
5454
# Each supported device MUST have its PAC included below along with a
5555
# corresponding feature.
56-
esp32 = { version = "0.32.0", features = ["critical-section", "rt"], optional = true }
57-
esp32c2 = { version = "0.21.0", features = ["critical-section", "rt"], optional = true }
58-
esp32c3 = { version = "0.24.0", features = ["critical-section", "rt"], optional = true }
59-
esp32c6 = { version = "0.15.0", features = ["critical-section", "rt"], optional = true }
60-
esp32h2 = { version = "0.11.0", features = ["critical-section", "rt"], optional = true }
61-
esp32s2 = { version = "0.23.0", features = ["critical-section", "rt"], optional = true }
62-
esp32s3 = { version = "0.27.0", features = ["critical-section", "rt"], optional = true }
56+
esp32 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
57+
esp32c2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
58+
esp32c3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
59+
esp32c6 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
60+
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
61+
esp32s2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
62+
esp32s3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "27dd7e5", features = ["critical-section", "rt"], optional = true }
6363

6464
[target.'cfg(target_arch = "riscv32")'.dependencies]
6565
esp-riscv-rt = { version = "0.9.0", path = "../esp-riscv-rt" }

esp-hal/src/i2c.rs

Lines changed: 40 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -131,73 +131,6 @@ impl embedded_hal::i2c::Error for Error {
131131
}
132132
}
133133

134-
// This should really be defined in the PAC, but the PAC only
135-
// defines the "command" field as a 16-bit field :-(
136-
bitfield::bitfield! {
137-
struct CommandReg(u32);
138-
cmd_done, _: 31;
139-
from into Opcode, opcode, set_opcode: 13, 11;
140-
from into Ack, ack_value, set_ack_value: 10, 10;
141-
from into Ack, ack_exp, set_ack_exp: 9, 9;
142-
ack_check_en, set_ack_check_en: 8;
143-
length, set_length: 7, 0;
144-
}
145-
146-
impl CommandReg {
147-
fn bits(&self) -> u32 {
148-
self.0
149-
}
150-
151-
fn new_start() -> Self {
152-
let mut cmd = Self(0);
153-
cmd.set_opcode(Opcode::RStart);
154-
cmd
155-
}
156-
157-
fn new_end() -> Self {
158-
let mut cmd = Self(0);
159-
cmd.set_opcode(Opcode::End);
160-
cmd
161-
}
162-
163-
fn new_stop() -> Self {
164-
let mut cmd = Self(0);
165-
cmd.set_opcode(Opcode::Stop);
166-
cmd
167-
}
168-
169-
fn new_write(ack_exp: Ack, ack_check_en: bool, length: u8) -> Self {
170-
let mut cmd = Self(0);
171-
cmd.set_opcode(Opcode::Write);
172-
cmd.set_ack_exp(ack_exp);
173-
cmd.set_ack_check_en(ack_check_en);
174-
cmd.set_length(length as u32);
175-
cmd
176-
}
177-
178-
fn new_read(ack_value: Ack, length: u8) -> Self {
179-
let mut cmd = Self(0);
180-
cmd.set_opcode(Opcode::Read);
181-
cmd.set_ack_value(ack_value);
182-
cmd.set_length(length as u32);
183-
cmd
184-
}
185-
}
186-
187-
#[cfg(feature = "debug")]
188-
impl core::fmt::Debug for CommandReg {
189-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
190-
f.debug_struct("CommandReg")
191-
.field("cmd_done", &self.cmd_done())
192-
.field("opcode", &self.opcode())
193-
.field("ack_value", &self.ack_value())
194-
.field("ack_exp", &self.ack_exp())
195-
.field("ack_check_en", &self.ack_check_en())
196-
.field("length", &self.length())
197-
.finish()
198-
}
199-
}
200-
201134
/// A generic I2C Command
202135
#[cfg_attr(feature = "debug", derive(Debug))]
203136
enum Command {
@@ -223,22 +156,6 @@ enum Command {
223156
},
224157
}
225158

226-
impl From<Command> for CommandReg {
227-
fn from(c: Command) -> Self {
228-
match c {
229-
Command::Start => CommandReg::new_start(),
230-
Command::End => CommandReg::new_end(),
231-
Command::Stop => CommandReg::new_stop(),
232-
Command::Write {
233-
ack_exp,
234-
ack_check_en,
235-
length,
236-
} => CommandReg::new_write(ack_exp, ack_check_en, length),
237-
Command::Read { ack_value, length } => CommandReg::new_read(ack_value, length),
238-
}
239-
}
240-
}
241-
242159
enum OperationType {
243160
Write = 0,
244161
Read = 1,
@@ -1739,8 +1656,12 @@ pub trait Instance: crate::private::Sealed {
17391656
// but does not seem to clear the done bit! So we don't check the done
17401657
// status of an end command
17411658
for cmd_reg in self.register_block().comd_iter() {
1742-
let cmd = CommandReg(cmd_reg.read().bits());
1743-
if cmd.bits() != 0x0 && cmd.opcode() != Opcode::End && !cmd.cmd_done() {
1659+
let cmd = cmd_reg.read();
1660+
1661+
if cmd.bits() != 0x0
1662+
&& cmd.opcode().bits() != (OPCODE_END as u8)
1663+
&& !cmd.command_done().bit_is_set()
1664+
{
17441665
return Err(Error::ExecIncomplete);
17451666
}
17461667
}
@@ -2098,8 +2019,40 @@ where
20982019
I: Iterator<Item = &'a COMD>,
20992020
{
21002021
let cmd = cmd_iterator.next().ok_or(Error::CommandNrExceeded)?;
2101-
let cmd_reg: CommandReg = command.into();
2102-
cmd.write(|w| unsafe { w.bits(cmd_reg.bits()) });
2022+
unsafe {
2023+
match command {
2024+
Command::Start => {
2025+
cmd.write(|w| w.opcode().rstart());
2026+
}
2027+
Command::Stop => {
2028+
cmd.write(|w| w.opcode().stop());
2029+
}
2030+
Command::End => {
2031+
cmd.write(|w| w.opcode().end());
2032+
}
2033+
Command::Write {
2034+
ack_exp,
2035+
ack_check_en,
2036+
length,
2037+
} => {
2038+
cmd.write(|w| {
2039+
w.opcode().write();
2040+
w.ack_exp().bit(ack_exp == Ack::Nack);
2041+
w.ack_check_en().bit(ack_check_en);
2042+
w.byte_num().bits(length);
2043+
w
2044+
});
2045+
}
2046+
Command::Read { ack_value, length } => {
2047+
cmd.write(|w| {
2048+
w.opcode().read();
2049+
w.ack_value().bit(ack_value == Ack::Nack);
2050+
w.byte_num().bits(length);
2051+
w
2052+
});
2053+
}
2054+
}
2055+
}
21032056
Ok(())
21042057
}
21052058

0 commit comments

Comments
 (0)