aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/i2c.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/i2c.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/i2c.rs120
1 files changed, 120 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs
new file mode 100644
index 000000000..5a180e897
--- /dev/null
+++ b/embassy-embedded-hal/src/shared_bus/i2c.rs
@@ -0,0 +1,120 @@
1//! Asynchronous shared I2C bus
2//!
3//! # Example (nrf52)
4//!
5//! ```rust
6//! use embassy_embedded_hal::shared_bus::i2c::I2cBusDevice;
7//! use embassy::mutex::Mutex;
8//! use embassy::blocking_mutex::raw::ThreadModeRawMutex;
9//!
10//! static I2C_BUS: Forever<Mutex::<ThreadModeRawMutex, Twim<TWISPI0>>> = Forever::new();
11//! let config = twim::Config::default();
12//! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);
13//! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config);
14//! let i2c_bus = Mutex::<ThreadModeRawMutex, _>::new(i2c);
15//! let i2c_bus = I2C_BUS.put(i2c_bus);
16//!
17//! // Device 1, using embedded-hal-async compatible driver for QMC5883L compass
18//! let i2c_dev1 = I2cBusDevice::new(i2c_bus);
19//! let compass = QMC5883L::new(i2c_dev1).await.unwrap();
20//!
21//! // Device 2, using embedded-hal-async compatible driver for Mpu6050 accelerometer
22//! let i2c_dev2 = I2cBusDevice::new(i2c_bus);
23//! let mpu = Mpu6050::new(i2c_dev2);
24//! ```
25use core::{fmt::Debug, future::Future};
26use embassy::blocking_mutex::raw::RawMutex;
27use embassy::mutex::Mutex;
28use embedded_hal_async::i2c;
29
30#[derive(Copy, Clone, Eq, PartialEq, Debug)]
31pub enum I2cBusDeviceError<BUS> {
32 I2c(BUS),
33}
34
35impl<BUS> i2c::Error for I2cBusDeviceError<BUS>
36where
37 BUS: i2c::Error + Debug,
38{
39 fn kind(&self) -> i2c::ErrorKind {
40 match self {
41 Self::I2c(e) => e.kind(),
42 }
43 }
44}
45
46pub struct I2cBusDevice<'a, M: RawMutex, BUS> {
47 bus: &'a Mutex<M, BUS>,
48}
49
50impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> {
51 pub fn new(bus: &'a Mutex<M, BUS>) -> Self {
52 Self { bus }
53 }
54}
55
56impl<'a, M: RawMutex, BUS> i2c::ErrorType for I2cBusDevice<'a, M, BUS>
57where
58 BUS: i2c::ErrorType,
59{
60 type Error = I2cBusDeviceError<BUS::Error>;
61}
62
63impl<M, BUS> i2c::I2c for I2cBusDevice<'_, M, BUS>
64where
65 M: RawMutex + 'static,
66 BUS: i2c::I2c + 'static,
67{
68 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
69
70 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
71 async move {
72 let mut bus = self.bus.lock().await;
73 bus.read(address, buffer)
74 .await
75 .map_err(I2cBusDeviceError::I2c)?;
76 Ok(())
77 }
78 }
79
80 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
81
82 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
83 async move {
84 let mut bus = self.bus.lock().await;
85 bus.write(address, bytes)
86 .await
87 .map_err(I2cBusDeviceError::I2c)?;
88 Ok(())
89 }
90 }
91
92 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
93
94 fn write_read<'a>(
95 &'a mut self,
96 address: u8,
97 wr_buffer: &'a [u8],
98 rd_buffer: &'a mut [u8],
99 ) -> Self::WriteReadFuture<'a> {
100 async move {
101 let mut bus = self.bus.lock().await;
102 bus.write_read(address, wr_buffer, rd_buffer)
103 .await
104 .map_err(I2cBusDeviceError::I2c)?;
105 Ok(())
106 }
107 }
108
109 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
110
111 fn transaction<'a, 'b>(
112 &'a mut self,
113 address: u8,
114 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
115 ) -> Self::TransactionFuture<'a, 'b> {
116 let _ = address;
117 let _ = operations;
118 async move { todo!() }
119 }
120}