aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
diff options
context:
space:
mode:
authorHenrik Alsér <[email protected]>2022-07-06 02:35:46 +0200
committerHenrik Alsér <[email protected]>2022-07-06 02:35:46 +0200
commit264b32d71baf471d0d36a34cc48aa10d429bed04 (patch)
tree80545647cfbbc6be6b97be2ac83ebb85a254e8d3 /embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
parent5fef527764f1b694a9213050f4a187339dcc049b (diff)
Add blocking shared bus for i2c and SPI
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking/i2c.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/i2c.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
new file mode 100644
index 000000000..2a6ea6dc8
--- /dev/null
+++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
@@ -0,0 +1,69 @@
1//! Blocking shared I2C bus
2use core::cell::RefCell;
3use core::fmt::Debug;
4use core::future::Future;
5
6use embedded_hal_1::i2c;
7
8#[derive(Copy, Clone, Eq, PartialEq, Debug)]
9pub enum I2cBusDeviceError<BUS> {
10 I2c(BUS),
11}
12
13impl<BUS> i2c::Error for I2cBusDeviceError<BUS>
14where
15 BUS: i2c::Error + Debug,
16{
17 fn kind(&self) -> i2c::ErrorKind {
18 match self {
19 Self::I2c(e) => e.kind(),
20 }
21 }
22}
23
24pub struct I2cBusDevice<'a, BUS> {
25 bus: &'a RefCell<BUS>,
26}
27
28impl<'a, BUS> I2cBusDevice<'a, BUS> {
29 pub fn new(bus: &'a RefCell<BUS>) -> Self {
30 Self { bus }
31 }
32}
33
34impl<'a, BUS> i2c::ErrorType for I2cBusDevice<'a, BUS>
35where
36 BUS: i2c::ErrorType,
37{
38 type Error = I2cBusDeviceError<BUS::Error>;
39}
40
41impl<M, BUS> i2c::I2c for I2cBusDevice<'_, BUS>
42where
43 BUS: i2c::I2c,
44{
45 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
46 let mut bus = self.bus.borrow_mut();
47 bus.read(address, buffer).map_err(I2cBusDeviceError::I2c)?;
48 Ok(())
49 }
50
51 fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
52 let mut bus = self.bus.borrow_mut();
53 bus.write(address, bytes).map_err(I2cBusDeviceError::I2c)?;
54 Ok(())
55 }
56
57 fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> {
58 let mut bus = self.bus.borrow_mut();
59 bus.write_read(address, wr_buffer, rd_buffer)
60 .map_err(I2cBusDeviceError::I2c)?;
61 Ok(())
62 }
63
64 fn transaction<'a>(&mut self, address: u8, operations: &mut [i2c::Operation<'a>]) -> Result<(), Self::Error> {
65 let _ = address;
66 let _ = operations;
67 todo!()
68 }
69}