diff options
| author | Henrik Alsér <[email protected]> | 2022-07-08 15:47:47 +0200 |
|---|---|---|
| committer | Henrik Alsér <[email protected]> | 2022-07-08 15:47:47 +0200 |
| commit | f30ff9cadcf575100a4e08c972d2f161172a3fc9 (patch) | |
| tree | 3c19664e1e72a483a1d2d85b176cf760694642f4 | |
| parent | 6baddaf53982b75149cb7e91280c571f7fe2e7bc (diff) | |
Shared buses with SetConfig
| -rw-r--r-- | embassy-embedded-hal/src/lib.rs | 4 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 84 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 50 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/i2c.rs | 78 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/spi.rs | 61 | ||||
| -rw-r--r-- | embassy-nrf/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-nrf/src/spim.rs | 43 | ||||
| -rw-r--r-- | embassy-nrf/src/twim.rs | 10 | ||||
| -rw-r--r-- | examples/nrf/src/bin/shared_bus.rs | 21 |
9 files changed, 352 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 27ffa7421..aae719925 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs | |||
| @@ -4,3 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | pub mod adapter; | 5 | pub mod adapter; |
| 6 | pub mod shared_bus; | 6 | pub mod shared_bus; |
| 7 | |||
| 8 | pub trait SetConfig<C> { | ||
| 9 | fn set_config(&mut self, config: &C); | ||
| 10 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 2c762fe14..6f5f07051 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -7,6 +7,7 @@ use embedded_hal_1::i2c::blocking::{I2c, Operation}; | |||
| 7 | use embedded_hal_1::i2c::ErrorType; | 7 | use embedded_hal_1::i2c::ErrorType; |
| 8 | 8 | ||
| 9 | use crate::shared_bus::i2c::I2cBusDeviceError; | 9 | use crate::shared_bus::i2c::I2cBusDeviceError; |
| 10 | use crate::SetConfig; | ||
| 10 | 11 | ||
| 11 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { | 12 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { |
| 12 | bus: &'a Mutex<M, RefCell<BUS>>, | 13 | bus: &'a Mutex<M, RefCell<BUS>>, |
| @@ -82,3 +83,86 @@ where | |||
| 82 | todo!() | 83 | todo!() |
| 83 | } | 84 | } |
| 84 | } | 85 | } |
| 86 | |||
| 87 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { | ||
| 88 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 89 | config: C, | ||
| 90 | } | ||
| 91 | |||
| 92 | impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { | ||
| 93 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: C) -> Self { | ||
| 94 | Self { bus, config } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> | ||
| 99 | where | ||
| 100 | BUS: ErrorType, | ||
| 101 | { | ||
| 102 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 103 | } | ||
| 104 | |||
| 105 | impl<M, BUS, C> I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> | ||
| 106 | where | ||
| 107 | M: RawMutex, | ||
| 108 | BUS: I2c + SetConfig<C>, | ||
| 109 | { | ||
| 110 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 111 | self.bus.lock(|bus| { | ||
| 112 | let mut bus = bus.borrow_mut(); | ||
| 113 | bus.set_config(&self.config); | ||
| 114 | bus.read(address, buffer).map_err(I2cBusDeviceError::I2c) | ||
| 115 | }) | ||
| 116 | } | ||
| 117 | |||
| 118 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 119 | self.bus.lock(|bus| { | ||
| 120 | let mut bus = bus.borrow_mut(); | ||
| 121 | bus.set_config(&self.config); | ||
| 122 | bus.write(address, bytes).map_err(I2cBusDeviceError::I2c) | ||
| 123 | }) | ||
| 124 | } | ||
| 125 | |||
| 126 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 127 | self.bus.lock(|bus| { | ||
| 128 | let mut bus = bus.borrow_mut(); | ||
| 129 | bus.set_config(&self.config); | ||
| 130 | bus.write_read(address, wr_buffer, rd_buffer) | ||
| 131 | .map_err(I2cBusDeviceError::I2c) | ||
| 132 | }) | ||
| 133 | } | ||
| 134 | |||
| 135 | fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> { | ||
| 136 | let _ = address; | ||
| 137 | let _ = operations; | ||
| 138 | todo!() | ||
| 139 | } | ||
| 140 | |||
| 141 | fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> { | ||
| 142 | let _ = addr; | ||
| 143 | let _ = bytes; | ||
| 144 | todo!() | ||
| 145 | } | ||
| 146 | |||
| 147 | fn write_iter_read<B: IntoIterator<Item = u8>>( | ||
| 148 | &mut self, | ||
| 149 | addr: u8, | ||
| 150 | bytes: B, | ||
| 151 | buffer: &mut [u8], | ||
| 152 | ) -> Result<(), Self::Error> { | ||
| 153 | let _ = addr; | ||
| 154 | let _ = bytes; | ||
| 155 | let _ = buffer; | ||
| 156 | todo!() | ||
| 157 | } | ||
| 158 | |||
| 159 | fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>( | ||
| 160 | &mut self, | ||
| 161 | address: u8, | ||
| 162 | operations: O, | ||
| 163 | ) -> Result<(), Self::Error> { | ||
| 164 | let _ = address; | ||
| 165 | let _ = operations; | ||
| 166 | todo!() | ||
| 167 | } | ||
| 168 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index c08bcbf62..d54ca6bfa 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -8,6 +8,7 @@ use embedded_hal_1::spi; | |||
| 8 | use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; | 8 | use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; |
| 9 | 9 | ||
| 10 | use crate::shared_bus::spi::SpiBusDeviceError; | 10 | use crate::shared_bus::spi::SpiBusDeviceError; |
| 11 | use crate::SetConfig; | ||
| 11 | 12 | ||
| 12 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { | 13 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { |
| 13 | bus: &'a Mutex<M, RefCell<BUS>>, | 14 | bus: &'a Mutex<M, RefCell<BUS>>, |
| @@ -55,3 +56,52 @@ where | |||
| 55 | }) | 56 | }) |
| 56 | } | 57 | } |
| 57 | } | 58 | } |
| 59 | |||
| 60 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { | ||
| 61 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 62 | cs: CS, | ||
| 63 | config: C, | ||
| 64 | } | ||
| 65 | |||
| 66 | impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { | ||
| 67 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS, config: C) -> Self { | ||
| 68 | Self { bus, cs, config } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> | ||
| 73 | where | ||
| 74 | BUS: spi::ErrorType, | ||
| 75 | CS: OutputPin, | ||
| 76 | { | ||
| 77 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 78 | } | ||
| 79 | |||
| 80 | impl<BUS, M, CS, C> SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> | ||
| 81 | where | ||
| 82 | M: RawMutex, | ||
| 83 | BUS: SpiBusFlush + SetConfig<C>, | ||
| 84 | CS: OutputPin, | ||
| 85 | { | ||
| 86 | type Bus = BUS; | ||
| 87 | |||
| 88 | fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> { | ||
| 89 | self.bus.lock(|bus| { | ||
| 90 | let mut bus = bus.borrow_mut(); | ||
| 91 | bus.set_config(&self.config); | ||
| 92 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 93 | |||
| 94 | let f_res = f(&mut bus); | ||
| 95 | |||
| 96 | // On failure, it's important to still flush and deassert CS. | ||
| 97 | let flush_res = bus.flush(); | ||
| 98 | let cs_res = self.cs.set_high(); | ||
| 99 | |||
| 100 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 101 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 102 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 103 | |||
| 104 | Ok(f_res) | ||
| 105 | }) | ||
| 106 | } | ||
| 107 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs index e8131288a..0e964773c 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs | |||
| @@ -29,6 +29,8 @@ use embassy::blocking_mutex::raw::RawMutex; | |||
| 29 | use embassy::mutex::Mutex; | 29 | use embassy::mutex::Mutex; |
| 30 | use embedded_hal_async::i2c; | 30 | use embedded_hal_async::i2c; |
| 31 | 31 | ||
| 32 | use crate::SetConfig; | ||
| 33 | |||
| 32 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 34 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 33 | pub enum I2cBusDeviceError<BUS> { | 35 | pub enum I2cBusDeviceError<BUS> { |
| 34 | I2c(BUS), | 36 | I2c(BUS), |
| @@ -116,3 +118,79 @@ where | |||
| 116 | async move { todo!() } | 118 | async move { todo!() } |
| 117 | } | 119 | } |
| 118 | } | 120 | } |
| 121 | |||
| 122 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { | ||
| 123 | bus: &'a Mutex<M, BUS>, | ||
| 124 | config: C, | ||
| 125 | } | ||
| 126 | |||
| 127 | impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { | ||
| 128 | pub fn new(bus: &'a Mutex<M, BUS>, config: C) -> Self { | ||
| 129 | Self { bus, config } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | impl<'a, M: RawMutex, BUS, C> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> | ||
| 134 | where | ||
| 135 | BUS: i2c::ErrorType, | ||
| 136 | { | ||
| 137 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 138 | } | ||
| 139 | |||
| 140 | impl<M, BUS, C> i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> | ||
| 141 | where | ||
| 142 | M: RawMutex + 'static, | ||
| 143 | BUS: i2c::I2c + SetConfig<C> + 'static, | ||
| 144 | { | ||
| 145 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 146 | |||
| 147 | fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 148 | async move { | ||
| 149 | let mut bus = self.bus.lock().await; | ||
| 150 | bus.set_config(&self.config); | ||
| 151 | bus.read(address, buffer).await.map_err(I2cBusDeviceError::I2c)?; | ||
| 152 | Ok(()) | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 157 | |||
| 158 | fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 159 | async move { | ||
| 160 | let mut bus = self.bus.lock().await; | ||
| 161 | bus.set_config(&self.config); | ||
| 162 | bus.write(address, bytes).await.map_err(I2cBusDeviceError::I2c)?; | ||
| 163 | Ok(()) | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 168 | |||
| 169 | fn write_read<'a>( | ||
| 170 | &'a mut self, | ||
| 171 | address: u8, | ||
| 172 | wr_buffer: &'a [u8], | ||
| 173 | rd_buffer: &'a mut [u8], | ||
| 174 | ) -> Self::WriteReadFuture<'a> { | ||
| 175 | async move { | ||
| 176 | let mut bus = self.bus.lock().await; | ||
| 177 | bus.set_config(&self.config); | ||
| 178 | bus.write_read(address, wr_buffer, rd_buffer) | ||
| 179 | .await | ||
| 180 | .map_err(I2cBusDeviceError::I2c)?; | ||
| 181 | Ok(()) | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; | ||
| 186 | |||
| 187 | fn transaction<'a, 'b>( | ||
| 188 | &'a mut self, | ||
| 189 | address: u8, | ||
| 190 | operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], | ||
| 191 | ) -> Self::TransactionFuture<'a, 'b> { | ||
| 192 | let _ = address; | ||
| 193 | let _ = operations; | ||
| 194 | async move { todo!() } | ||
| 195 | } | ||
| 196 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs index fd4b6d565..04378c330 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/spi.rs | |||
| @@ -34,6 +34,8 @@ use embedded_hal_1::digital::blocking::OutputPin; | |||
| 34 | use embedded_hal_1::spi::ErrorType; | 34 | use embedded_hal_1::spi::ErrorType; |
| 35 | use embedded_hal_async::spi; | 35 | use embedded_hal_async::spi; |
| 36 | 36 | ||
| 37 | use crate::SetConfig; | ||
| 38 | |||
| 37 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 39 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 38 | pub enum SpiBusDeviceError<BUS, CS> { | 40 | pub enum SpiBusDeviceError<BUS, CS> { |
| 39 | Spi(BUS), | 41 | Spi(BUS), |
| @@ -109,3 +111,62 @@ where | |||
| 109 | } | 111 | } |
| 110 | } | 112 | } |
| 111 | } | 113 | } |
| 114 | |||
| 115 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { | ||
| 116 | bus: &'a Mutex<M, BUS>, | ||
| 117 | cs: CS, | ||
| 118 | config: C, | ||
| 119 | } | ||
| 120 | |||
| 121 | impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { | ||
| 122 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: C) -> Self { | ||
| 123 | Self { bus, cs, config } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> | ||
| 128 | where | ||
| 129 | BUS: spi::ErrorType, | ||
| 130 | CS: OutputPin, | ||
| 131 | { | ||
| 132 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 133 | } | ||
| 134 | |||
| 135 | impl<M, BUS, CS, C> spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> | ||
| 136 | where | ||
| 137 | M: RawMutex + 'static, | ||
| 138 | BUS: spi::SpiBusFlush + SetConfig<C> + 'static, | ||
| 139 | CS: OutputPin, | ||
| 140 | { | ||
| 141 | type Bus = BUS; | ||
| 142 | |||
| 143 | type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a | ||
| 144 | where | ||
| 145 | Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 146 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a; | ||
| 147 | |||
| 148 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> | ||
| 149 | where | ||
| 150 | R: 'a, | ||
| 151 | F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 152 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a, | ||
| 153 | { | ||
| 154 | async move { | ||
| 155 | let mut bus = self.bus.lock().await; | ||
| 156 | bus.set_config(&self.config); | ||
| 157 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 158 | |||
| 159 | let f_res = f(&mut *bus).await; | ||
| 160 | |||
| 161 | // On failure, it's important to still flush and deassert CS. | ||
| 162 | let flush_res = bus.flush().await; | ||
| 163 | let cs_res = self.cs.set_high(); | ||
| 164 | |||
| 165 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 166 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 167 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 168 | |||
| 169 | Ok(f_res) | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 887ea1bd2..3f9dabbb5 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml | |||
| @@ -68,6 +68,7 @@ embassy = { version = "0.1.0", path = "../embassy" } | |||
| 68 | embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]} | 68 | embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]} |
| 69 | embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["nrf"]} | 69 | embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["nrf"]} |
| 70 | embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } | 70 | embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } |
| 71 | embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" } | ||
| 71 | embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional=true } | 72 | embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional=true } |
| 72 | 73 | ||
| 73 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } | 74 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } |
diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index efccfeca3..882bf4b47 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs | |||
| @@ -4,6 +4,7 @@ use core::marker::PhantomData; | |||
| 4 | use core::sync::atomic::{compiler_fence, Ordering}; | 4 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 5 | use core::task::Poll; | 5 | use core::task::Poll; |
| 6 | 6 | ||
| 7 | use embassy_embedded_hal::SetConfig; | ||
| 7 | use embassy_hal_common::unborrow; | 8 | use embassy_hal_common::unborrow; |
| 8 | pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; | 9 | pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; |
| 9 | use futures::future::poll_fn; | 10 | use futures::future::poll_fn; |
| @@ -521,3 +522,45 @@ cfg_if::cfg_if! { | |||
| 521 | } | 522 | } |
| 522 | } | 523 | } |
| 523 | } | 524 | } |
| 525 | |||
| 526 | impl<'d, T: Instance> SetConfig<Config> for Spim<'d, T> { | ||
| 527 | fn set_config(&mut self, config: &Config) { | ||
| 528 | let r = T::regs(); | ||
| 529 | // Configure mode. | ||
| 530 | let mode = config.mode; | ||
| 531 | r.config.write(|w| { | ||
| 532 | match mode { | ||
| 533 | MODE_0 => { | ||
| 534 | w.order().msb_first(); | ||
| 535 | w.cpol().active_high(); | ||
| 536 | w.cpha().leading(); | ||
| 537 | } | ||
| 538 | MODE_1 => { | ||
| 539 | w.order().msb_first(); | ||
| 540 | w.cpol().active_high(); | ||
| 541 | w.cpha().trailing(); | ||
| 542 | } | ||
| 543 | MODE_2 => { | ||
| 544 | w.order().msb_first(); | ||
| 545 | w.cpol().active_low(); | ||
| 546 | w.cpha().leading(); | ||
| 547 | } | ||
| 548 | MODE_3 => { | ||
| 549 | w.order().msb_first(); | ||
| 550 | w.cpol().active_low(); | ||
| 551 | w.cpha().trailing(); | ||
| 552 | } | ||
| 553 | } | ||
| 554 | |||
| 555 | w | ||
| 556 | }); | ||
| 557 | |||
| 558 | // Configure frequency. | ||
| 559 | let frequency = config.frequency; | ||
| 560 | r.frequency.write(|w| w.frequency().variant(frequency)); | ||
| 561 | |||
| 562 | // Set over-read character | ||
| 563 | let orc = config.orc; | ||
| 564 | r.orc.write(|w| unsafe { w.orc().bits(orc) }); | ||
| 565 | } | ||
| 566 | } | ||
diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs index c3921104e..8189d3e71 100644 --- a/embassy-nrf/src/twim.rs +++ b/embassy-nrf/src/twim.rs | |||
| @@ -15,6 +15,7 @@ use core::task::Poll; | |||
| 15 | #[cfg(feature = "time")] | 15 | #[cfg(feature = "time")] |
| 16 | use embassy::time::{Duration, Instant}; | 16 | use embassy::time::{Duration, Instant}; |
| 17 | use embassy::waitqueue::AtomicWaker; | 17 | use embassy::waitqueue::AtomicWaker; |
| 18 | use embassy_embedded_hal::SetConfig; | ||
| 18 | use embassy_hal_common::unborrow; | 19 | use embassy_hal_common::unborrow; |
| 19 | use futures::future::poll_fn; | 20 | use futures::future::poll_fn; |
| 20 | 21 | ||
| @@ -24,6 +25,7 @@ use crate::interrupt::{Interrupt, InterruptExt}; | |||
| 24 | use crate::util::{slice_in_ram, slice_in_ram_or}; | 25 | use crate::util::{slice_in_ram, slice_in_ram_or}; |
| 25 | use crate::{gpio, pac, Unborrow}; | 26 | use crate::{gpio, pac, Unborrow}; |
| 26 | 27 | ||
| 28 | #[derive(Clone, Copy)] | ||
| 27 | pub enum Frequency { | 29 | pub enum Frequency { |
| 28 | #[doc = "26738688: 100 kbps"] | 30 | #[doc = "26738688: 100 kbps"] |
| 29 | K100 = 26738688, | 31 | K100 = 26738688, |
| @@ -877,3 +879,11 @@ cfg_if::cfg_if! { | |||
| 877 | } | 879 | } |
| 878 | } | 880 | } |
| 879 | } | 881 | } |
| 882 | |||
| 883 | impl<'d, T: Instance> SetConfig<Config> for Twim<'d, T> { | ||
| 884 | fn set_config(&mut self, config: &Config) { | ||
| 885 | let r = T::regs(); | ||
| 886 | r.frequency | ||
| 887 | .write(|w| unsafe { w.frequency().bits(config.frequency as u32) }); | ||
| 888 | } | ||
| 889 | } | ||
diff --git a/examples/nrf/src/bin/shared_bus.rs b/examples/nrf/src/bin/shared_bus.rs new file mode 100644 index 000000000..23d16f796 --- /dev/null +++ b/examples/nrf/src/bin/shared_bus.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::executor::Spawner; | ||
| 6 | use embassy::time::{Duration, Timer}; | ||
| 7 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 8 | use embassy_nrf::Peripherals; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | #[embassy::main] | ||
| 12 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 13 | let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); | ||
| 14 | |||
| 15 | loop { | ||
| 16 | led.set_high(); | ||
| 17 | Timer::after(Duration::from_millis(300)).await; | ||
| 18 | led.set_low(); | ||
| 19 | Timer::after(Duration::from_millis(300)).await; | ||
| 20 | } | ||
| 21 | } | ||
