Skip to content

Commit 7630578

Browse files
committed
i2c
1 parent 031c887 commit 7630578

File tree

4 files changed

+102
-17
lines changed

4 files changed

+102
-17
lines changed

src/i2c.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub mod blocking;
1515
pub use blocking::BlockingI2c;
1616

1717
mod hal_02;
18+
mod hal_1;
19+
20+
pub use embedded_hal::i2c::NoAcknowledgeSource;
1821

1922
/// I2C error
2023
#[derive(Debug, Eq, PartialEq)]
@@ -23,9 +26,9 @@ pub enum Error {
2326
/// Bus error
2427
Bus,
2528
/// Arbitration loss
26-
Arbitration,
29+
ArbitrationLoss,
2730
/// No ack received
28-
Acknowledge,
31+
NoAcknowledge(NoAcknowledgeSource),
2932
/// Overrun/underrun
3033
Overrun,
3134
// Pec, // SMBUS mode only

src/i2c/blocking.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ impl<I2C, PINS> I2c<I2C, PINS> {
113113
}
114114

115115
macro_rules! wait_for_flag {
116-
($i2c:expr, $flag:ident) => {{
116+
($i2c:expr, $flag:ident, $nack:ident) => {{
117117
let sr1 = $i2c.sr1.read();
118118

119119
if sr1.berr().bit_is_set() {
120120
$i2c.sr1.write(|w| w.berr().clear_bit());
121121
Err(Error::Bus.into())
122122
} else if sr1.arlo().bit_is_set() {
123123
$i2c.sr1.write(|w| w.arlo().clear_bit());
124-
Err(Error::Arbitration.into())
124+
Err(Error::ArbitrationLoss.into())
125125
} else if sr1.af().bit_is_set() {
126126
$i2c.sr1.write(|w| w.af().clear_bit());
127-
Err(Error::Acknowledge.into())
127+
Err(Error::NoAcknowledge(NoAcknowledgeSource::$nack).into())
128128
} else if sr1.ovr().bit_is_set() {
129129
$i2c.sr1.write(|w| w.ovr().clear_bit());
130130
Err(Error::Overrun.into())
@@ -190,7 +190,7 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
190190
/// method returns `WouldBlock` so the program can act accordingly
191191
/// (busy wait, async, ...)
192192
fn wait_after_sent_start(&mut self) -> nb::Result<(), Error> {
193-
wait_for_flag!(self.nb.i2c, sb)
193+
wait_for_flag!(self.nb.i2c, sb, Unknown)
194194
}
195195

196196
/// Check if STOP condition is generated. If the condition is not generated, this
@@ -225,8 +225,11 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
225225
fn send_addr_and_wait(&mut self, addr: u8, read: bool) -> Result<(), Error> {
226226
self.nb.i2c.sr1.read();
227227
self.nb.send_addr(addr, read);
228-
let ret = busy_wait_cycles!(wait_for_flag!(self.nb.i2c, addr), self.timeouts.addr);
229-
if ret == Err(Error::Acknowledge) {
228+
let ret = busy_wait_cycles!(
229+
wait_for_flag!(self.nb.i2c, addr, Address),
230+
self.timeouts.addr
231+
);
232+
if let Err(Error::NoAcknowledge(_)) = ret {
230233
self.nb.send_stop();
231234
}
232235
ret
@@ -239,10 +242,10 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
239242
self.nb.i2c.dr.write(|w| w.dr().bits(bytes[0]));
240243

241244
for byte in &bytes[1..] {
242-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, tx_e), self.timeouts.data)?;
245+
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, tx_e, Data), self.timeouts.data)?;
243246
self.nb.i2c.dr.write(|w| w.dr().bits(*byte));
244247
}
245-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, btf), self.timeouts.data)?;
248+
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, btf, Data), self.timeouts.data)?;
246249

247250
Ok(())
248251
}
@@ -252,7 +255,7 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
252255
self.send_addr_and_wait(addr, false)?;
253256

254257
let ret = self.write_bytes_and_wait(bytes);
255-
if ret == Err(Error::Acknowledge) {
258+
if let Err(Error::NoAcknowledge(_)) = ret {
256259
self.nb.send_stop();
257260
}
258261
ret
@@ -277,7 +280,7 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
277280
self.nb.i2c.sr2.read();
278281
self.nb.send_stop();
279282

280-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, rx_ne), self.timeouts.data)?;
283+
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, rx_ne, Data), self.timeouts.data)?;
281284
buffer[0] = self.nb.i2c.dr.read().dr().bits();
282285

283286
busy_wait_cycles!(self.wait_for_stop(), self.timeouts.data)?;
@@ -292,7 +295,7 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
292295
self.nb.i2c.sr2.read();
293296
self.nb.i2c.cr1.modify(|_, w| w.ack().clear_bit());
294297

295-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, btf), self.timeouts.data)?;
298+
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, btf, Data), self.timeouts.data)?;
296299
self.nb.send_stop();
297300
buffer[0] = self.nb.i2c.dr.read().dr().bits();
298301
buffer[1] = self.nb.i2c.dr.read().dr().bits();
@@ -311,16 +314,19 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
311314

312315
let (first_bytes, last_two_bytes) = buffer.split_at_mut(buffer_len - 3);
313316
for byte in first_bytes {
314-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, rx_ne), self.timeouts.data)?;
317+
busy_wait_cycles!(
318+
wait_for_flag!(self.nb.i2c, rx_ne, Data),
319+
self.timeouts.data
320+
)?;
315321
*byte = self.nb.i2c.dr.read().dr().bits();
316322
}
317323

318-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, btf), self.timeouts.data)?;
324+
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, btf, Data), self.timeouts.data)?;
319325
self.nb.i2c.cr1.modify(|_, w| w.ack().clear_bit());
320326
last_two_bytes[0] = self.nb.i2c.dr.read().dr().bits();
321327
self.nb.send_stop();
322328
last_two_bytes[1] = self.nb.i2c.dr.read().dr().bits();
323-
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, rx_ne), self.timeouts.data)?;
329+
busy_wait_cycles!(wait_for_flag!(self.nb.i2c, rx_ne, Data), self.timeouts.data)?;
324330
last_two_bytes[2] = self.nb.i2c.dr.read().dr().bits();
325331

326332
busy_wait_cycles!(self.wait_for_stop(), self.timeouts.data)?;
@@ -345,4 +351,20 @@ impl<I2C: Instance, PINS> BlockingI2c<I2C, PINS> {
345351

346352
Ok(())
347353
}
354+
355+
pub fn transaction_slice(
356+
&mut self,
357+
_addr: u8,
358+
_ops_slice: &mut [embedded_hal::i2c::Operation<'_>],
359+
) -> Result<(), Error> {
360+
todo!();
361+
}
362+
363+
pub(crate) fn transaction_slice_hal_02(
364+
&mut self,
365+
_addr: u8,
366+
_ops_slice: &mut [embedded_hal_02::blocking::i2c::Operation<'_>],
367+
) -> Result<(), Error> {
368+
todo!();
369+
}
348370
}

src/i2c/hal_02.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use embedded_hal_02::blocking::i2c::{Read, Write, WriteRead};
2+
use embedded_hal_02::blocking::i2c::{Operation, Read, Transactional, Write, WriteRead};
33

44
impl<I2C: Instance, PINS> Write for BlockingI2c<I2C, PINS> {
55
type Error = Error;
@@ -24,3 +24,14 @@ impl<I2C: Instance, PINS> WriteRead for BlockingI2c<I2C, PINS> {
2424
self.write_read(addr, bytes, buffer)
2525
}
2626
}
27+
28+
impl<I2C, PINS> Transactional for BlockingI2c<I2C, PINS>
29+
where
30+
I2C: Instance,
31+
{
32+
type Error = Error;
33+
34+
fn exec(&mut self, address: u8, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
35+
self.transaction_slice_hal_02(address, operations)
36+
}
37+
}

src/i2c/hal_1.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use embedded_hal::i2c::{Error, ErrorKind, ErrorType};
2+
3+
impl Error for super::Error {
4+
fn kind(&self) -> ErrorKind {
5+
match *self {
6+
Self::Overrun => ErrorKind::Overrun,
7+
Self::Bus => ErrorKind::Bus,
8+
Self::ArbitrationLoss => ErrorKind::ArbitrationLoss,
9+
Self::NoAcknowledge(nack) => ErrorKind::NoAcknowledge(nack),
10+
Self::Timeout => ErrorKind::Other,
11+
}
12+
}
13+
}
14+
15+
impl<I2C: super::Instance, PINS> ErrorType for super::BlockingI2c<I2C, PINS> {
16+
type Error = super::Error;
17+
}
18+
19+
mod blocking {
20+
use super::super::{BlockingI2c, Instance};
21+
use embedded_hal::i2c::Operation;
22+
23+
impl<I2C: Instance, PINS> embedded_hal::i2c::I2c for BlockingI2c<I2C, PINS> {
24+
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
25+
self.read(addr, buffer)
26+
}
27+
28+
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
29+
self.write(addr, bytes)
30+
}
31+
32+
fn write_read(
33+
&mut self,
34+
addr: u8,
35+
bytes: &[u8],
36+
buffer: &mut [u8],
37+
) -> Result<(), Self::Error> {
38+
self.write_read(addr, bytes, buffer)
39+
}
40+
41+
fn transaction(
42+
&mut self,
43+
addr: u8,
44+
operations: &mut [Operation<'_>],
45+
) -> Result<(), Self::Error> {
46+
self.transaction_slice(addr, operations)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)