aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-07-18 20:01:39 +0200
committerDario Nieuwenhuis <[email protected]>2022-07-18 20:02:05 +0200
commita3a40bad6c6cb5a3de67234d0206475e9042e8e7 (patch)
tree2b92b1187bdb1b732b9058878417dbf03affc3e6 /embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
parent4dc800710d2269d5baebd62ae5a62205570db365 (diff)
Rename XXBusDevice to XXDevice.
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking/i2c.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/i2c.rs56
1 files changed, 28 insertions, 28 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
index ac361a786..1ec480c0c 100644
--- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
+++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
@@ -3,7 +3,7 @@
3//! # Example (nrf52) 3//! # Example (nrf52)
4//! 4//!
5//! ```rust 5//! ```rust
6//! use embassy_embedded_hal::shared_bus::blocking::i2c::I2cBusDevice; 6//! use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
7//! use embassy::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; 7//! use embassy::blocking_mutex::{NoopMutex, raw::NoopRawMutex};
8//! 8//!
9//! static I2C_BUS: Forever<NoopMutex<RefCell<Twim<TWISPI0>>>> = Forever::new(); 9//! static I2C_BUS: Forever<NoopMutex<RefCell<Twim<TWISPI0>>>> = Forever::new();
@@ -12,7 +12,7 @@
12//! let i2c_bus = NoopMutex::new(RefCell::new(i2c)); 12//! let i2c_bus = NoopMutex::new(RefCell::new(i2c));
13//! let i2c_bus = I2C_BUS.put(i2c_bus); 13//! let i2c_bus = I2C_BUS.put(i2c_bus);
14//! 14//!
15//! let i2c_dev1 = I2cBusDevice::new(i2c_bus); 15//! let i2c_dev1 = I2cDevice::new(i2c_bus);
16//! let mpu = Mpu6050::new(i2c_dev1); 16//! let mpu = Mpu6050::new(i2c_dev1);
17//! ``` 17//! ```
18 18
@@ -23,46 +23,46 @@ use embassy::blocking_mutex::Mutex;
23use embedded_hal_1::i2c::blocking::{I2c, Operation}; 23use embedded_hal_1::i2c::blocking::{I2c, Operation};
24use embedded_hal_1::i2c::ErrorType; 24use embedded_hal_1::i2c::ErrorType;
25 25
26use crate::shared_bus::I2cBusDeviceError; 26use crate::shared_bus::I2cDeviceError;
27use crate::SetConfig; 27use crate::SetConfig;
28 28
29pub struct I2cBusDevice<'a, M: RawMutex, BUS> { 29pub struct I2cDevice<'a, M: RawMutex, BUS> {
30 bus: &'a Mutex<M, RefCell<BUS>>, 30 bus: &'a Mutex<M, RefCell<BUS>>,
31} 31}
32 32
33impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { 33impl<'a, M: RawMutex, BUS> I2cDevice<'a, M, BUS> {
34 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>) -> Self { 34 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>) -> Self {
35 Self { bus } 35 Self { bus }
36 } 36 }
37} 37}
38 38
39impl<'a, M: RawMutex, BUS> ErrorType for I2cBusDevice<'a, M, BUS> 39impl<'a, M: RawMutex, BUS> ErrorType for I2cDevice<'a, M, BUS>
40where 40where
41 BUS: ErrorType, 41 BUS: ErrorType,
42{ 42{
43 type Error = I2cBusDeviceError<BUS::Error>; 43 type Error = I2cDeviceError<BUS::Error>;
44} 44}
45 45
46impl<M, BUS> I2c for I2cBusDevice<'_, M, BUS> 46impl<M, BUS> I2c for I2cDevice<'_, M, BUS>
47where 47where
48 M: RawMutex, 48 M: RawMutex,
49 BUS: I2c, 49 BUS: I2c,
50{ 50{
51 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { 51 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
52 self.bus 52 self.bus
53 .lock(|bus| bus.borrow_mut().read(address, buffer).map_err(I2cBusDeviceError::I2c)) 53 .lock(|bus| bus.borrow_mut().read(address, buffer).map_err(I2cDeviceError::I2c))
54 } 54 }
55 55
56 fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { 56 fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
57 self.bus 57 self.bus
58 .lock(|bus| bus.borrow_mut().write(address, bytes).map_err(I2cBusDeviceError::I2c)) 58 .lock(|bus| bus.borrow_mut().write(address, bytes).map_err(I2cDeviceError::I2c))
59 } 59 }
60 60
61 fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { 61 fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> {
62 self.bus.lock(|bus| { 62 self.bus.lock(|bus| {
63 bus.borrow_mut() 63 bus.borrow_mut()
64 .write_read(address, wr_buffer, rd_buffer) 64 .write_read(address, wr_buffer, rd_buffer)
65 .map_err(I2cBusDeviceError::I2c) 65 .map_err(I2cDeviceError::I2c)
66 }) 66 })
67 } 67 }
68 68
@@ -101,68 +101,68 @@ where
101 } 101 }
102} 102}
103 103
104impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS> 104impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cDevice<'_, M, BUS>
105where 105where
106 M: RawMutex, 106 M: RawMutex,
107 BUS: embedded_hal_02::blocking::i2c::Write<Error = E>, 107 BUS: embedded_hal_02::blocking::i2c::Write<Error = E>,
108{ 108{
109 type Error = I2cBusDeviceError<E>; 109 type Error = I2cDeviceError<E>;
110 110
111 fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> { 111 fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> {
112 self.bus 112 self.bus
113 .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cBusDeviceError::I2c)) 113 .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cDeviceError::I2c))
114 } 114 }
115} 115}
116 116
117impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cBusDevice<'_, M, BUS> 117impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cDevice<'_, M, BUS>
118where 118where
119 M: RawMutex, 119 M: RawMutex,
120 BUS: embedded_hal_02::blocking::i2c::Read<Error = E>, 120 BUS: embedded_hal_02::blocking::i2c::Read<Error = E>,
121{ 121{
122 type Error = I2cBusDeviceError<E>; 122 type Error = I2cDeviceError<E>;
123 123
124 fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> { 124 fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> {
125 self.bus 125 self.bus
126 .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cBusDeviceError::I2c)) 126 .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cDeviceError::I2c))
127 } 127 }
128} 128}
129 129
130impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cBusDevice<'_, M, BUS> 130impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cDevice<'_, M, BUS>
131where 131where
132 M: RawMutex, 132 M: RawMutex,
133 BUS: embedded_hal_02::blocking::i2c::WriteRead<Error = E>, 133 BUS: embedded_hal_02::blocking::i2c::WriteRead<Error = E>,
134{ 134{
135 type Error = I2cBusDeviceError<E>; 135 type Error = I2cDeviceError<E>;
136 136
137 fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> { 137 fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> {
138 self.bus.lock(|bus| { 138 self.bus.lock(|bus| {
139 bus.borrow_mut() 139 bus.borrow_mut()
140 .write_read(addr, bytes, buffer) 140 .write_read(addr, bytes, buffer)
141 .map_err(I2cBusDeviceError::I2c) 141 .map_err(I2cDeviceError::I2c)
142 }) 142 })
143 } 143 }
144} 144}
145 145
146pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { 146pub struct I2cDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> {
147 bus: &'a Mutex<M, RefCell<BUS>>, 147 bus: &'a Mutex<M, RefCell<BUS>>,
148 config: BUS::Config, 148 config: BUS::Config,
149} 149}
150 150
151impl<'a, M: RawMutex, BUS: SetConfig> I2cBusDeviceWithConfig<'a, M, BUS> { 151impl<'a, M: RawMutex, BUS: SetConfig> I2cDeviceWithConfig<'a, M, BUS> {
152 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: BUS::Config) -> Self { 152 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: BUS::Config) -> Self {
153 Self { bus, config } 153 Self { bus, config }
154 } 154 }
155} 155}
156 156
157impl<'a, M, BUS> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS> 157impl<'a, M, BUS> ErrorType for I2cDeviceWithConfig<'a, M, BUS>
158where 158where
159 M: RawMutex, 159 M: RawMutex,
160 BUS: ErrorType + SetConfig, 160 BUS: ErrorType + SetConfig,
161{ 161{
162 type Error = I2cBusDeviceError<BUS::Error>; 162 type Error = I2cDeviceError<BUS::Error>;
163} 163}
164 164
165impl<M, BUS> I2c for I2cBusDeviceWithConfig<'_, M, BUS> 165impl<M, BUS> I2c for I2cDeviceWithConfig<'_, M, BUS>
166where 166where
167 M: RawMutex, 167 M: RawMutex,
168 BUS: I2c + SetConfig, 168 BUS: I2c + SetConfig,
@@ -171,7 +171,7 @@ where
171 self.bus.lock(|bus| { 171 self.bus.lock(|bus| {
172 let mut bus = bus.borrow_mut(); 172 let mut bus = bus.borrow_mut();
173 bus.set_config(&self.config); 173 bus.set_config(&self.config);
174 bus.read(address, buffer).map_err(I2cBusDeviceError::I2c) 174 bus.read(address, buffer).map_err(I2cDeviceError::I2c)
175 }) 175 })
176 } 176 }
177 177
@@ -179,7 +179,7 @@ where
179 self.bus.lock(|bus| { 179 self.bus.lock(|bus| {
180 let mut bus = bus.borrow_mut(); 180 let mut bus = bus.borrow_mut();
181 bus.set_config(&self.config); 181 bus.set_config(&self.config);
182 bus.write(address, bytes).map_err(I2cBusDeviceError::I2c) 182 bus.write(address, bytes).map_err(I2cDeviceError::I2c)
183 }) 183 })
184 } 184 }
185 185
@@ -188,7 +188,7 @@ where
188 let mut bus = bus.borrow_mut(); 188 let mut bus = bus.borrow_mut();
189 bus.set_config(&self.config); 189 bus.set_config(&self.config);
190 bus.write_read(address, wr_buffer, rd_buffer) 190 bus.write_read(address, wr_buffer, rd_buffer)
191 .map_err(I2cBusDeviceError::I2c) 191 .map_err(I2cDeviceError::I2c)
192 }) 192 })
193 } 193 }
194 194