aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Alsér <[email protected]>2022-07-08 15:47:47 +0200
committerHenrik Alsér <[email protected]>2022-07-08 15:47:47 +0200
commitf30ff9cadcf575100a4e08c972d2f161172a3fc9 (patch)
tree3c19664e1e72a483a1d2d85b176cf760694642f4
parent6baddaf53982b75149cb7e91280c571f7fe2e7bc (diff)
Shared buses with SetConfig
-rw-r--r--embassy-embedded-hal/src/lib.rs4
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/i2c.rs84
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/spi.rs50
-rw-r--r--embassy-embedded-hal/src/shared_bus/i2c.rs78
-rw-r--r--embassy-embedded-hal/src/shared_bus/spi.rs61
-rw-r--r--embassy-nrf/Cargo.toml1
-rw-r--r--embassy-nrf/src/spim.rs43
-rw-r--r--embassy-nrf/src/twim.rs10
-rw-r--r--examples/nrf/src/bin/shared_bus.rs21
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
5pub mod adapter; 5pub mod adapter;
6pub mod shared_bus; 6pub mod shared_bus;
7
8pub 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};
7use embedded_hal_1::i2c::ErrorType; 7use embedded_hal_1::i2c::ErrorType;
8 8
9use crate::shared_bus::i2c::I2cBusDeviceError; 9use crate::shared_bus::i2c::I2cBusDeviceError;
10use crate::SetConfig;
10 11
11pub struct I2cBusDevice<'a, M: RawMutex, BUS> { 12pub 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
87pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> {
88 bus: &'a Mutex<M, RefCell<BUS>>,
89 config: C,
90}
91
92impl<'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
98impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C>
99where
100 BUS: ErrorType,
101{
102 type Error = I2cBusDeviceError<BUS::Error>;
103}
104
105impl<M, BUS, C> I2c for I2cBusDeviceWithConfig<'_, M, BUS, C>
106where
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;
8use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; 8use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice};
9 9
10use crate::shared_bus::spi::SpiBusDeviceError; 10use crate::shared_bus::spi::SpiBusDeviceError;
11use crate::SetConfig;
11 12
12pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { 13pub 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
60pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> {
61 bus: &'a Mutex<M, RefCell<BUS>>,
62 cs: CS,
63 config: C,
64}
65
66impl<'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
72impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C>
73where
74 BUS: spi::ErrorType,
75 CS: OutputPin,
76{
77 type Error = SpiBusDeviceError<BUS::Error, CS::Error>;
78}
79
80impl<BUS, M, CS, C> SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C>
81where
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;
29use embassy::mutex::Mutex; 29use embassy::mutex::Mutex;
30use embedded_hal_async::i2c; 30use embedded_hal_async::i2c;
31 31
32use crate::SetConfig;
33
32#[derive(Copy, Clone, Eq, PartialEq, Debug)] 34#[derive(Copy, Clone, Eq, PartialEq, Debug)]
33pub enum I2cBusDeviceError<BUS> { 35pub 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
122pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> {
123 bus: &'a Mutex<M, BUS>,
124 config: C,
125}
126
127impl<'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
133impl<'a, M: RawMutex, BUS, C> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C>
134where
135 BUS: i2c::ErrorType,
136{
137 type Error = I2cBusDeviceError<BUS::Error>;
138}
139
140impl<M, BUS, C> i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS, C>
141where
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;
34use embedded_hal_1::spi::ErrorType; 34use embedded_hal_1::spi::ErrorType;
35use embedded_hal_async::spi; 35use embedded_hal_async::spi;
36 36
37use crate::SetConfig;
38
37#[derive(Copy, Clone, Eq, PartialEq, Debug)] 39#[derive(Copy, Clone, Eq, PartialEq, Debug)]
38pub enum SpiBusDeviceError<BUS, CS> { 40pub enum SpiBusDeviceError<BUS, CS> {
39 Spi(BUS), 41 Spi(BUS),
@@ -109,3 +111,62 @@ where
109 } 111 }
110 } 112 }
111} 113}
114
115pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> {
116 bus: &'a Mutex<M, BUS>,
117 cs: CS,
118 config: C,
119}
120
121impl<'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
127impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C>
128where
129 BUS: spi::ErrorType,
130 CS: OutputPin,
131{
132 type Error = SpiBusDeviceError<BUS::Error, CS::Error>;
133}
134
135impl<M, BUS, CS, C> spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C>
136where
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" }
68embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]} 68embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]}
69embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["nrf"]} 69embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["nrf"]}
70embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } 70embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" }
71embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" }
71embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional=true } 72embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional=true }
72 73
73embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } 74embedded-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;
4use core::sync::atomic::{compiler_fence, Ordering}; 4use core::sync::atomic::{compiler_fence, Ordering};
5use core::task::Poll; 5use core::task::Poll;
6 6
7use embassy_embedded_hal::SetConfig;
7use embassy_hal_common::unborrow; 8use embassy_hal_common::unborrow;
8pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; 9pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
9use futures::future::poll_fn; 10use futures::future::poll_fn;
@@ -521,3 +522,45 @@ cfg_if::cfg_if! {
521 } 522 }
522 } 523 }
523} 524}
525
526impl<'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")]
16use embassy::time::{Duration, Instant}; 16use embassy::time::{Duration, Instant};
17use embassy::waitqueue::AtomicWaker; 17use embassy::waitqueue::AtomicWaker;
18use embassy_embedded_hal::SetConfig;
18use embassy_hal_common::unborrow; 19use embassy_hal_common::unborrow;
19use futures::future::poll_fn; 20use futures::future::poll_fn;
20 21
@@ -24,6 +25,7 @@ use crate::interrupt::{Interrupt, InterruptExt};
24use crate::util::{slice_in_ram, slice_in_ram_or}; 25use crate::util::{slice_in_ram, slice_in_ram_or};
25use crate::{gpio, pac, Unborrow}; 26use crate::{gpio, pac, Unborrow};
26 27
28#[derive(Clone, Copy)]
27pub enum Frequency { 29pub 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
883impl<'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
5use embassy::executor::Spawner;
6use embassy::time::{Duration, Timer};
7use embassy_nrf::gpio::{Level, Output, OutputDrive};
8use embassy_nrf::Peripherals;
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy::main]
12async 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}