aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-07-24 15:24:18 +0000
committerGitHub <[email protected]>2023-07-24 15:24:18 +0000
commit9f898c460fa926d201d3011f97261dcad399029c (patch)
tree46ea9800142046f4f3845ac932c4a64e880b97c5
parent8d50f8a3d3e5a50cd266ca45e221565e5982e744 (diff)
parent2a0fe7304595497b3458b350574ed788a5a07876 (diff)
Merge pull request #1685 from chemicstry/bxcan_async_enable
stm32/can: bxcan async enable
-rw-r--r--embassy-stm32/src/can/bxcan.rs12
-rw-r--r--examples/stm32f4/src/bin/can.rs7
2 files changed, 17 insertions, 2 deletions
diff --git a/embassy-stm32/src/can/bxcan.rs b/embassy-stm32/src/can/bxcan.rs
index 5a0153464..8b8244d4f 100644
--- a/embassy-stm32/src/can/bxcan.rs
+++ b/embassy-stm32/src/can/bxcan.rs
@@ -161,6 +161,18 @@ impl<'d, T: Instance> Can<'d, T> {
161 .leave_disabled(); 161 .leave_disabled();
162 } 162 }
163 163
164 /// Enables the peripheral and synchronizes with the bus.
165 ///
166 /// This will wait for 11 consecutive recessive bits (bus idle state).
167 /// Contrary to enable method from bxcan library, this will not freeze the executor while waiting.
168 pub async fn enable(&mut self) {
169 while self.borrow_mut().enable_non_blocking().is_err() {
170 // SCE interrupt is only generated for entering sleep mode, but not leaving.
171 // Yield to allow other tasks to execute while can bus is initializing.
172 embassy_futures::yield_now().await;
173 }
174 }
175
164 /// Queues the message to be sent but exerts backpressure 176 /// Queues the message to be sent but exerts backpressure
165 pub async fn write(&mut self, frame: &Frame) -> bxcan::TransmitStatus { 177 pub async fn write(&mut self, frame: &Frame) -> bxcan::TransmitStatus {
166 poll_fn(|cx| { 178 poll_fn(|cx| {
diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs
index 08bed88db..f84f74d30 100644
--- a/examples/stm32f4/src/bin/can.rs
+++ b/examples/stm32f4/src/bin/can.rs
@@ -40,10 +40,13 @@ async fn main(_spawner: Spawner) {
40 40
41 can.as_mut() 41 can.as_mut()
42 .modify_config() 42 .modify_config()
43 .set_bit_timing(0x001c0003) // http://www.bittiming.can-wiki.info/
44 .set_loopback(true) // Receive own frames 43 .set_loopback(true) // Receive own frames
45 .set_silent(true) 44 .set_silent(true)
46 .enable(); 45 .leave_disabled();
46
47 can.set_bitrate(1_000_000);
48
49 can.enable().await;
47 50
48 let mut i: u8 = 0; 51 let mut i: u8 = 0;
49 loop { 52 loop {