Skip to content

Commit e91feef

Browse files
authored
Feature/i2c nonblocking (#98)
* Implemented non-blocking i2c, with watchdog and slave address change possebility * Added features to cargo.toml i2c-blocking and i2c-nonblocking * Added default feature i2c-blocking to solve compilation problem * Added address and direction to result, to be able to fully parse the result in RTIC. * Formatting cargo fmt * Simplified frame handling, to make use more easy. See comments on traits * Fixed small bug
1 parent 760102e commit e91feef

File tree

3 files changed

+665
-0
lines changed

3 files changed

+665
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ st7735-lcd = "0.6.1"
4949
ws2812-spi = { version = "0.3.0", features = [] }
5050

5151
[features]
52+
default = ["i2c-blocking"]
5253
device-selected = []
5354
rt = ["stm32g0/rt"]
5455
stm32g030 = ["stm32g0/stm32g030", "stm32g0x0", "device-selected"]
@@ -61,6 +62,9 @@ stm32g081 = ["stm32g0/stm32g081", "stm32g0x1", "device-selected"]
6162
stm32g0x0 = []
6263
stm32g0x1 = []
6364

65+
i2c-blocking = []
66+
i2c-nonblocking = []
67+
6468
[profile.dev]
6569
incremental = false
6670

src/i2c/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
#[cfg(feature = "i2c-blocking")]
12
pub mod blocking;
3+
4+
#[cfg(feature = "i2c-nonblocking")]
5+
pub mod nonblocking;
6+
7+
#[cfg(feature = "i2c-nonblocking")]
8+
pub use nonblocking::*;
9+
210
pub mod config;
311

412
use crate::rcc::*;
@@ -16,6 +24,12 @@ pub enum SlaveAddressMask {
1624
MaskAllBits,
1725
}
1826

27+
#[derive(Debug, Clone, Copy)]
28+
pub enum I2cResult<'a> {
29+
Data(u16, I2cDirection, &'a [u8]), // contains address, direction and data slice reference
30+
Addressed(u16, I2cDirection), // a slave is addressed by a master
31+
}
32+
1933
#[derive(Debug, Clone, Copy)]
2034
pub enum I2cDirection {
2135
MasterReadSlaveWrite = 0,
@@ -59,8 +73,24 @@ pub trait I2cExt<I2C> {
5973
}
6074

6175
/// I2C abstraction
76+
#[cfg(feature = "i2c-blocking")]
77+
pub struct I2c<I2C, SDA, SCL> {
78+
i2c: I2C,
79+
sda: SDA,
80+
scl: SCL,
81+
}
82+
83+
#[cfg(feature = "i2c-nonblocking")]
6284
pub struct I2c<I2C, SDA, SCL> {
6385
i2c: I2C,
6486
sda: SDA,
6587
scl: SCL,
88+
address: u16,
89+
watchdog: u16, // on each start set to 10, on each stop set to 0
90+
index: usize,
91+
length: usize,
92+
errors: usize, // global error counter, reset on read
93+
length_write_read: usize, // for a master write_read operation this remembers the size of the read operation
94+
// for a slave device this must be 0
95+
data: [u8; 255], // during transfer the driver will be the owner of the buffer
6696
}

0 commit comments

Comments
 (0)