aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-07-06 14:59:29 +0000
committerGitHub <[email protected]>2022-07-06 14:59:29 +0000
commit6baddaf53982b75149cb7e91280c571f7fe2e7bc (patch)
tree24ab8956f759f1eb148c7d88673a93a6f342ad62 /embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
parent5fef527764f1b694a9213050f4a187339dcc049b (diff)
parent455374b7f9da0ebd55849e47d7800e313197cb33 (diff)
Merge #845
845: Add blocking shared bus for i2c and SPI r=Dirbaio a=kalkyl Blocking versions of the shared buses Co-authored-by: Henrik Alsér <[email protected]> Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking/i2c.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/i2c.rs84
1 files changed, 84 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..2c762fe14
--- /dev/null
+++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
@@ -0,0 +1,84 @@
1//! Blocking shared I2C bus
2use core::cell::RefCell;
3
4use embassy::blocking_mutex::raw::RawMutex;
5use embassy::blocking_mutex::Mutex;
6use embedded_hal_1::i2c::blocking::{I2c, Operation};
7use embedded_hal_1::i2c::ErrorType;
8
9use crate::shared_bus::i2c::I2cBusDeviceError;
10
11pub struct I2cBusDevice<'a, M: RawMutex, BUS> {
12 bus: &'a Mutex<M, RefCell<BUS>>,
13}
14
15impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> {
16 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>) -> Self {
17 Self { bus }
18 }
19}
20
21impl<'a, M: RawMutex, BUS> ErrorType for I2cBusDevice<'a, M, BUS>
22where
23 BUS: ErrorType,
24{
25 type Error = I2cBusDeviceError<BUS::Error>;
26}
27
28impl<M, BUS> I2c for I2cBusDevice<'_, M, BUS>
29where
30 M: RawMutex,
31 BUS: I2c,
32{
33 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
34 self.bus
35 .lock(|bus| bus.borrow_mut().read(address, buffer).map_err(I2cBusDeviceError::I2c))
36 }
37
38 fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
39 self.bus
40 .lock(|bus| bus.borrow_mut().write(address, bytes).map_err(I2cBusDeviceError::I2c))
41 }
42
43 fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> {
44 self.bus.lock(|bus| {
45 bus.borrow_mut()
46 .write_read(address, wr_buffer, rd_buffer)
47 .map_err(I2cBusDeviceError::I2c)
48 })
49 }
50
51 fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> {
52 let _ = address;
53 let _ = operations;
54 todo!()
55 }
56
57 fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> {
58 let _ = addr;
59 let _ = bytes;
60 todo!()
61 }
62
63 fn write_iter_read<B: IntoIterator<Item = u8>>(
64 &mut self,
65 addr: u8,
66 bytes: B,
67 buffer: &mut [u8],
68 ) -> Result<(), Self::Error> {
69 let _ = addr;
70 let _ = bytes;
71 let _ = buffer;
72 todo!()
73 }
74
75 fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>(
76 &mut self,
77 address: u8,
78 operations: O,
79 ) -> Result<(), Self::Error> {
80 let _ = address;
81 let _ = operations;
82 todo!()
83 }
84}