stm32g0xx_hal/i2c/
mod.rs

1#[cfg(feature = "i2c-blocking")]
2pub mod blocking;
3
4#[cfg(feature = "i2c-nonblocking")]
5pub mod nonblocking;
6
7#[cfg(feature = "i2c-nonblocking")]
8pub use nonblocking::*;
9
10pub mod config;
11
12use crate::rcc::*;
13pub use config::Config;
14
15#[derive(Debug, Clone, Copy)]
16pub enum SlaveAddressMask {
17    MaskNone = 0,
18    MaskOneBit,
19    MaskTwoBits,
20    MaskThreeBits,
21    MaskFourBits,
22    MaskFiveBits,
23    MaskSixBits,
24    MaskAllBits,
25}
26
27#[derive(Debug, Clone, Copy)]
28pub 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
33#[derive(Debug, Clone, Copy)]
34pub enum I2cDirection {
35    MasterReadSlaveWrite = 0,
36    MasterWriteSlaveRead = 1,
37}
38
39#[derive(Debug, Clone, Copy)]
40pub enum Event {
41    AddressMatch,
42    Rxne,
43}
44
45/// I2C error
46#[derive(Debug, Clone, Copy)]
47pub enum Error {
48    Overrun,
49    Nack,
50    PECError,
51    BusError,
52    ArbitrationLost,
53    IncorrectFrameSize(usize),
54}
55
56/// I2C SDA pin
57pub trait SDAPin<I2C> {
58    fn setup(&self);
59    fn release(self) -> Self;
60}
61
62/// I2C SCL pin
63pub trait SCLPin<I2C> {
64    fn setup(&self);
65    fn release(self) -> Self;
66}
67
68pub trait I2cExt<I2C> {
69    fn i2c<SDA, SCL>(
70        self,
71        sda: SDA,
72        scl: SCL,
73        config: impl Into<Config>,
74        rcc: &mut Rcc,
75    ) -> I2c<I2C, SDA, SCL>
76    where
77        SDA: SDAPin<I2C>,
78        SCL: SCLPin<I2C>;
79}
80
81/// I2C abstraction
82#[cfg(feature = "i2c-blocking")]
83pub struct I2c<I2C, SDA, SCL> {
84    i2c: I2C,
85    sda: SDA,
86    scl: SCL,
87}
88
89#[cfg(feature = "i2c-nonblocking")]
90pub struct I2c<I2C, SDA, SCL> {
91    i2c: I2C,
92    sda: SDA,
93    scl: SCL,
94    address: u16,
95    watchdog: u16, // on each start set to 10, on each stop set to 0
96    index: usize,
97    length: usize,
98    errors: usize,            // global error counter, reset on read
99    length_write_read: usize, // for a master write_read operation this remembers the size of the read operation
100    // for a slave device this must be 0
101    data: [u8; 255], // during transfer the driver will be the owner of the buffer
102}