Skip to content

Commit e9917ef

Browse files
committed
common
1 parent 28bfb2c commit e9917ef

File tree

3 files changed

+76
-68
lines changed

3 files changed

+76
-68
lines changed

src/fmpi2c.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
use core::ops::Deref;
22

33
use crate::gpio;
4-
use crate::i2c::{Address, Error, Hal02Operation, Hal1Operation, NoAcknowledgeSource};
4+
55
use crate::pac::fmpi2c1 as i2c1;
66
use crate::pac::{self, rcc, RCC};
77
use crate::rcc::{BusClock, Clocks, Enable, Reset};
88
use fugit::{HertzU32 as Hertz, RateExtU32};
99
use micromath::F32Ext;
1010

11+
#[path = "i2c/common.rs"]
12+
mod common;
13+
pub use common::{Address, Error, NoAcknowledgeSource};
14+
use common::{Hal02Operation, Hal1Operation};
15+
1116
#[path = "i2c/hal_02.rs"]
1217
mod hal_02;
1318
#[path = "i2c/hal_1.rs"]

src/i2c.rs

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,14 @@ use crate::gpio;
88
use crate::rcc::Clocks;
99
use fugit::{HertzU32 as Hertz, RateExtU32};
1010

11+
mod common;
1112
mod hal_02;
1213
mod hal_1;
1314

14-
pub mod dma;
15-
16-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
17-
pub enum Address {
18-
Seven(u8),
19-
Ten(u16),
20-
}
15+
pub use common::{Address, Error, NoAcknowledgeSource};
16+
use common::{Hal02Operation, Hal1Operation};
2117

22-
impl From<u8> for Address {
23-
fn from(value: u8) -> Self {
24-
Self::Seven(value)
25-
}
26-
}
27-
28-
impl From<u16> for Address {
29-
fn from(value: u16) -> Self {
30-
Self::Ten(value)
31-
}
32-
}
18+
pub mod dma;
3319

3420
#[derive(Debug, Eq, PartialEq)]
3521
pub enum DutyCycle {
@@ -88,52 +74,6 @@ pub struct I2c<I2C: Instance> {
8874
pins: (I2C::Scl, I2C::Sda),
8975
}
9076

91-
pub use embedded_hal::i2c::NoAcknowledgeSource;
92-
93-
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
94-
#[non_exhaustive]
95-
pub enum Error {
96-
Overrun,
97-
NoAcknowledge(NoAcknowledgeSource),
98-
Timeout,
99-
// Note: The Bus error type is not currently returned, but is maintained for compatibility.
100-
Bus,
101-
Crc,
102-
ArbitrationLoss,
103-
}
104-
105-
impl Error {
106-
pub(crate) fn nack_addr(self) -> Self {
107-
match self {
108-
Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
109-
Error::NoAcknowledge(NoAcknowledgeSource::Address)
110-
}
111-
e => e,
112-
}
113-
}
114-
pub(crate) fn nack_data(self) -> Self {
115-
match self {
116-
Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
117-
Error::NoAcknowledge(NoAcknowledgeSource::Data)
118-
}
119-
e => e,
120-
}
121-
}
122-
}
123-
124-
use embedded_hal::i2c::ErrorKind;
125-
impl embedded_hal::i2c::Error for Error {
126-
fn kind(&self) -> ErrorKind {
127-
match *self {
128-
Self::Overrun => ErrorKind::Overrun,
129-
Self::Bus => ErrorKind::Bus,
130-
Self::ArbitrationLoss => ErrorKind::ArbitrationLoss,
131-
Self::NoAcknowledge(nack) => ErrorKind::NoAcknowledge(nack),
132-
Self::Crc | Self::Timeout => ErrorKind::Other,
133-
}
134-
}
135-
}
136-
13777
pub trait Instance:
13878
crate::Sealed + Deref<Target = i2c1::RegisterBlock> + Enable + Reset + gpio::alt::I2cCommon
13979
{
@@ -727,9 +667,6 @@ macro_rules! transaction_impl {
727667
}
728668
use transaction_impl;
729669

730-
pub(crate) type Hal1Operation<'a> = embedded_hal::i2c::Operation<'a>;
731-
pub(crate) type Hal02Operation<'a> = embedded_hal_02::blocking::i2c::Operation<'a>;
732-
733670
impl<I2C: Instance> embedded_hal_02::blocking::i2c::WriteIter for I2c<I2C> {
734671
type Error = Error;
735672

src/i2c/common.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2+
pub enum Address {
3+
Seven(u8),
4+
Ten(u16),
5+
}
6+
7+
impl From<u8> for Address {
8+
fn from(value: u8) -> Self {
9+
Self::Seven(value)
10+
}
11+
}
12+
13+
impl From<u16> for Address {
14+
fn from(value: u16) -> Self {
15+
Self::Ten(value)
16+
}
17+
}
18+
19+
pub use embedded_hal::i2c::NoAcknowledgeSource;
20+
21+
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
22+
#[non_exhaustive]
23+
pub enum Error {
24+
Overrun,
25+
NoAcknowledge(NoAcknowledgeSource),
26+
Timeout,
27+
// Note: The Bus error type is not currently returned, but is maintained for compatibility.
28+
Bus,
29+
Crc,
30+
ArbitrationLoss,
31+
}
32+
33+
impl Error {
34+
pub(crate) fn nack_addr(self) -> Self {
35+
match self {
36+
Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
37+
Error::NoAcknowledge(NoAcknowledgeSource::Address)
38+
}
39+
e => e,
40+
}
41+
}
42+
pub(crate) fn nack_data(self) -> Self {
43+
match self {
44+
Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
45+
Error::NoAcknowledge(NoAcknowledgeSource::Data)
46+
}
47+
e => e,
48+
}
49+
}
50+
}
51+
52+
use embedded_hal::i2c::ErrorKind;
53+
impl embedded_hal::i2c::Error for Error {
54+
fn kind(&self) -> ErrorKind {
55+
match *self {
56+
Self::Overrun => ErrorKind::Overrun,
57+
Self::Bus => ErrorKind::Bus,
58+
Self::ArbitrationLoss => ErrorKind::ArbitrationLoss,
59+
Self::NoAcknowledge(nack) => ErrorKind::NoAcknowledge(nack),
60+
Self::Crc | Self::Timeout => ErrorKind::Other,
61+
}
62+
}
63+
}
64+
65+
pub(crate) type Hal1Operation<'a> = embedded_hal::i2c::Operation<'a>;
66+
pub(crate) type Hal02Operation<'a> = embedded_hal_02::blocking::i2c::Operation<'a>;

0 commit comments

Comments
 (0)