aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/can/bx/filter.rs22
-rw-r--r--embassy-stm32/src/can/bx/frame.rs10
-rw-r--r--embassy-stm32/src/can/bx/mod.rs90
3 files changed, 32 insertions, 90 deletions
diff --git a/embassy-stm32/src/can/bx/filter.rs b/embassy-stm32/src/can/bx/filter.rs
index 0213e0f44..c149d37ea 100644
--- a/embassy-stm32/src/can/bx/filter.rs
+++ b/embassy-stm32/src/can/bx/filter.rs
@@ -286,12 +286,7 @@ impl<I: FilterOwner> MasterFilters<'_, I> {
286 /// - `index`: the filter index. 286 /// - `index`: the filter index.
287 /// - `fifo`: the receive FIFO the filter should pass accepted messages to. 287 /// - `fifo`: the receive FIFO the filter should pass accepted messages to.
288 /// - `config`: the filter configuration. 288 /// - `config`: the filter configuration.
289 pub fn enable_bank( 289 pub fn enable_bank(&mut self, index: u8, fifo: Fifo, config: impl Into<BankConfig>) -> &mut Self {
290 &mut self,
291 index: u8,
292 fifo: Fifo,
293 config: impl Into<BankConfig>,
294 ) -> &mut Self {
295 self.banks_imm().enable(index, fifo, config.into()); 290 self.banks_imm().enable(index, fifo, config.into());
296 self 291 self
297 } 292 }
@@ -380,12 +375,7 @@ impl<I: Instance> SlaveFilters<'_, I> {
380 /// - `index`: the filter index. 375 /// - `index`: the filter index.
381 /// - `fifo`: the receive FIFO the filter should pass accepted messages to. 376 /// - `fifo`: the receive FIFO the filter should pass accepted messages to.
382 /// - `config`: the filter configuration. 377 /// - `config`: the filter configuration.
383 pub fn enable_bank( 378 pub fn enable_bank(&mut self, index: u8, fifo: Fifo, config: impl Into<BankConfig>) -> &mut Self {
384 &mut self,
385 index: u8,
386 fifo: Fifo,
387 config: impl Into<BankConfig>,
388 ) -> &mut Self {
389 self.banks_imm().enable(index, fifo, config.into()); 379 self.banks_imm().enable(index, fifo, config.into());
390 self 380 self
391 } 381 }
@@ -415,9 +405,7 @@ impl FilterBanks<'_> {
415 fn disable(&mut self, index: u8) { 405 fn disable(&mut self, index: u8) {
416 self.assert_bank_index(index); 406 self.assert_bank_index(index);
417 407
418 self.can 408 self.can.fa1r.modify(|r, w| unsafe { w.bits(r.bits() & !(1 << index)) })
419 .fa1r
420 .modify(|r, w| unsafe { w.bits(r.bits() & !(1 << index)) })
421 } 409 }
422 410
423 fn enable(&mut self, index: u8, fifo: Fifo, config: BankConfig) { 411 fn enable(&mut self, index: u8, fifo: Fifo, config: BankConfig) {
@@ -482,9 +470,7 @@ impl FilterBanks<'_> {
482 }); 470 });
483 471
484 // Set active. 472 // Set active.
485 self.can 473 self.can.fa1r.modify(|r, w| unsafe { w.bits(r.bits() | (1 << index)) })
486 .fa1r
487 .modify(|r, w| unsafe { w.bits(r.bits() | (1 << index)) })
488 } 474 }
489} 475}
490 476
diff --git a/embassy-stm32/src/can/bx/frame.rs b/embassy-stm32/src/can/bx/frame.rs
index a7e8d5b20..2a51d1b15 100644
--- a/embassy-stm32/src/can/bx/frame.rs
+++ b/embassy-stm32/src/can/bx/frame.rs
@@ -22,10 +22,7 @@ impl Frame {
22 Id::Extended(id) => IdReg::new_extended(id), 22 Id::Extended(id) => IdReg::new_extended(id),
23 }; 23 };
24 24
25 Self { 25 Self { id, data: data.into() }
26 id,
27 data: data.into(),
28 }
29 } 26 }
30 27
31 /// Creates a new remote frame with configurable data length code (DLC). 28 /// Creates a new remote frame with configurable data length code (DLC).
@@ -182,10 +179,7 @@ impl Data {
182 /// Creates an empty data payload containing 0 bytes. 179 /// Creates an empty data payload containing 0 bytes.
183 #[inline] 180 #[inline]
184 pub const fn empty() -> Self { 181 pub const fn empty() -> Self {
185 Self { 182 Self { len: 0, bytes: [0; 8] }
186 len: 0,
187 bytes: [0; 8],
188 }
189 } 183 }
190} 184}
191 185
diff --git a/embassy-stm32/src/can/bx/mod.rs b/embassy-stm32/src/can/bx/mod.rs
index 2789d1ff9..d6e023217 100644
--- a/embassy-stm32/src/can/bx/mod.rs
+++ b/embassy-stm32/src/can/bx/mod.rs
@@ -19,21 +19,8 @@
19//! 19//!
20//! - Support for querying error states and handling error interrupts is incomplete. 20//! - Support for querying error states and handling error interrupts is incomplete.
21//! 21//!
22//! # Cargo Features
23//!
24//! | Feature | Description |
25//! |---------|-------------|
26//! | `defmt` | Implements [`defmt`]'s `Format` trait for the types in this crate.[^1] |
27//!
28//! [^1]: The specific version of defmt is unspecified and may be updated in a patch release.
29//!
30//! [`defmt`]: https://docs.rs/defmt
31//! [`embedded-hal`]: https://docs.rs/embedded-hal
32 22
33#![doc(html_root_url = "https://docs.rs/bxcan/0.7.0")]
34// Deny a few warnings in doctests, since rustdoc `allow`s many warnings by default 23// Deny a few warnings in doctests, since rustdoc `allow`s many warnings by default
35#![doc(test(attr(deny(unused_imports, unused_must_use))))]
36#![no_std]
37#![allow(clippy::unnecessary_operation)] // lint is bugged 24#![allow(clippy::unnecessary_operation)] // lint is bugged
38 25
39//mod embedded_hal; 26//mod embedded_hal;
@@ -45,20 +32,19 @@ mod interrupt;
45#[allow(clippy::all)] // generated code 32#[allow(clippy::all)] // generated code
46mod pac; 33mod pac;
47 34
48pub use id::{ExtendedId, Id, StandardId};
49
50pub use crate::can::bx::frame::{Data, Frame, FramePriority};
51pub use crate::can::bx::interrupt::{Interrupt, Interrupts};
52pub use crate::can::bx::pac::can::RegisterBlock;
53
54use crate::can::bx::filter::MasterFilters;
55use core::cmp::{Ord, Ordering}; 35use core::cmp::{Ord, Ordering};
56use core::convert::{Infallible, TryInto}; 36use core::convert::{Infallible, TryInto};
57use core::marker::PhantomData; 37use core::marker::PhantomData;
58use core::mem; 38use core::mem;
59use core::ptr::NonNull; 39use core::ptr::NonNull;
60 40
61use self::pac::generic::*; // To make the PAC extraction build 41pub use id::{ExtendedId, Id, StandardId};
42
43use self::pac::generic::*;
44use crate::can::bx::filter::MasterFilters;
45pub use crate::can::bx::frame::{Data, Frame, FramePriority};
46pub use crate::can::bx::interrupt::{Interrupt, Interrupts};
47pub use crate::can::bx::pac::can::RegisterBlock; // To make the PAC extraction build
62 48
63/// A bxCAN peripheral instance. 49/// A bxCAN peripheral instance.
64/// 50///
@@ -186,9 +172,7 @@ impl IdReg {
186 if self.is_extended() { 172 if self.is_extended() {
187 Id::Extended(unsafe { ExtendedId::new_unchecked(self.0 >> Self::EXTENDED_SHIFT) }) 173 Id::Extended(unsafe { ExtendedId::new_unchecked(self.0 >> Self::EXTENDED_SHIFT) })
188 } else { 174 } else {
189 Id::Standard(unsafe { 175 Id::Standard(unsafe { StandardId::new_unchecked((self.0 >> Self::STANDARD_SHIFT) as u16) })
190 StandardId::new_unchecked((self.0 >> Self::STANDARD_SHIFT) as u16)
191 })
192 } 176 }
193 } 177 }
194 178
@@ -229,12 +213,9 @@ impl Ord for IdReg {
229 .reverse() 213 .reverse()
230 .then(Ordering::Greater) 214 .then(Ordering::Greater)
231 } 215 }
232 (Id::Extended(a), Id::Standard(b)) => a 216 (Id::Extended(a), Id::Standard(b)) => {
233 .standard_id() 217 a.standard_id().as_raw().cmp(&b.as_raw()).reverse().then(Ordering::Less)
234 .as_raw() 218 }
235 .cmp(&b.as_raw())
236 .reverse()
237 .then(Ordering::Less),
238 } 219 }
239 } 220 }
240} 221}
@@ -326,8 +307,7 @@ impl<I: Instance> CanConfig<'_, I> {
326 /// Leaves initialization mode, enters sleep mode. 307 /// Leaves initialization mode, enters sleep mode.
327 fn leave_init_mode(&mut self) { 308 fn leave_init_mode(&mut self) {
328 let can = self.can.registers(); 309 let can = self.can.registers();
329 can.mcr 310 can.mcr.modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
330 .modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
331 loop { 311 loop {
332 let msr = can.msr.read(); 312 let msr = can.msr.read();
333 if msr.slak().bit_is_set() && msr.inak().bit_is_clear() { 313 if msr.slak().bit_is_set() && msr.inak().bit_is_clear() {
@@ -426,8 +406,7 @@ impl<I: Instance> CanBuilder<I> {
426 /// Leaves initialization mode, enters sleep mode. 406 /// Leaves initialization mode, enters sleep mode.
427 fn leave_init_mode(&mut self) { 407 fn leave_init_mode(&mut self) {
428 let can = self.can.registers(); 408 let can = self.can.registers();
429 can.mcr 409 can.mcr.modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
430 .modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
431 loop { 410 loop {
432 let msr = can.msr.read(); 411 let msr = can.msr.read();
433 if msr.slak().bit_is_set() && msr.inak().bit_is_clear() { 412 if msr.slak().bit_is_set() && msr.inak().bit_is_clear() {
@@ -448,15 +427,11 @@ where
448{ 427{
449 /// Creates a [`CanBuilder`] for constructing a CAN interface. 428 /// Creates a [`CanBuilder`] for constructing a CAN interface.
450 pub fn builder(instance: I) -> CanBuilder<I> { 429 pub fn builder(instance: I) -> CanBuilder<I> {
451 let can_builder = CanBuilder { 430 let can_builder = CanBuilder { can: Can { instance } };
452 can: Can { instance },
453 };
454 431
455 let can_reg = can_builder.can.registers(); 432 let can_reg = can_builder.can.registers();
456 // Enter init mode. 433 // Enter init mode.
457 can_reg 434 can_reg.mcr.modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
458 .mcr
459 .modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
460 loop { 435 loop {
461 let msr = can_reg.msr.read(); 436 let msr = can_reg.msr.read();
462 if msr.slak().bit_is_clear() && msr.inak().bit_is_set() { 437 if msr.slak().bit_is_clear() && msr.inak().bit_is_set() {
@@ -505,8 +480,7 @@ where
505 let can = self.registers(); 480 let can = self.registers();
506 481
507 // Enter init mode. 482 // Enter init mode.
508 can.mcr 483 can.mcr.modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
509 .modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
510 loop { 484 loop {
511 let msr = can.msr.read(); 485 let msr = can.msr.read();
512 if msr.slak().bit_is_clear() && msr.inak().bit_is_set() { 486 if msr.slak().bit_is_clear() && msr.inak().bit_is_set() {
@@ -541,8 +515,7 @@ where
541 let can = self.registers(); 515 let can = self.registers();
542 let msr = can.msr.read(); 516 let msr = can.msr.read();
543 if msr.slak().bit_is_set() { 517 if msr.slak().bit_is_set() {
544 can.mcr 518 can.mcr.modify(|_, w| w.abom().set_bit().sleep().clear_bit());
545 .modify(|_, w| w.abom().set_bit().sleep().clear_bit());
546 Err(nb::Error::WouldBlock) 519 Err(nb::Error::WouldBlock)
547 } else { 520 } else {
548 Ok(()) 521 Ok(())
@@ -554,8 +527,7 @@ where
554 /// While in sleep mode, an incoming CAN frame will trigger [`Interrupt::Wakeup`] if enabled. 527 /// While in sleep mode, an incoming CAN frame will trigger [`Interrupt::Wakeup`] if enabled.
555 pub fn sleep(&mut self) { 528 pub fn sleep(&mut self) {
556 let can = self.registers(); 529 let can = self.registers();
557 can.mcr 530 can.mcr.modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
558 .modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
559 loop { 531 loop {
560 let msr = can.msr.read(); 532 let msr = can.msr.read();
561 if msr.slak().bit_is_set() && msr.inak().bit_is_clear() { 533 if msr.slak().bit_is_set() && msr.inak().bit_is_clear() {
@@ -570,8 +542,7 @@ where
570 /// frame will cause that interrupt. 542 /// frame will cause that interrupt.
571 pub fn wakeup(&mut self) { 543 pub fn wakeup(&mut self) {
572 let can = self.registers(); 544 let can = self.registers();
573 can.mcr 545 can.mcr.modify(|_, w| w.sleep().clear_bit().inrq().clear_bit());
574 .modify(|_, w| w.sleep().clear_bit().inrq().clear_bit());
575 loop { 546 loop {
576 let msr = can.msr.read(); 547 let msr = can.msr.read();
577 if msr.slak().bit_is_clear() && msr.inak().bit_is_clear() { 548 if msr.slak().bit_is_clear() && msr.inak().bit_is_clear() {
@@ -791,8 +762,7 @@ where
791 let tsr = can.tsr.read(); 762 let tsr = can.tsr.read();
792 let idx = tsr.code().bits() as usize; 763 let idx = tsr.code().bits() as usize;
793 764
794 let frame_is_pending = 765 let frame_is_pending = tsr.tme0().bit_is_clear() || tsr.tme1().bit_is_clear() || tsr.tme2().bit_is_clear();
795 tsr.tme0().bit_is_clear() || tsr.tme1().bit_is_clear() || tsr.tme2().bit_is_clear();
796 let pending_frame = if frame_is_pending { 766 let pending_frame = if frame_is_pending {
797 // High priority frames are transmitted first by the mailbox system. 767 // High priority frames are transmitted first by the mailbox system.
798 // Frames with identical identifier shall be transmitted in FIFO order. 768 // Frames with identical identifier shall be transmitted in FIFO order.
@@ -860,20 +830,12 @@ where
860 debug_assert!(idx < 3); 830 debug_assert!(idx < 3);
861 let mb = unsafe { &can.tx.get_unchecked(idx) }; 831 let mb = unsafe { &can.tx.get_unchecked(idx) };
862 832
863 mb.tdtr 833 mb.tdtr.write(|w| unsafe { w.dlc().bits(frame.dlc() as u8) });
864 .write(|w| unsafe { w.dlc().bits(frame.dlc() as u8) }); 834 mb.tdlr
865 mb.tdlr.write(|w| unsafe { 835 .write(|w| unsafe { w.bits(u32::from_ne_bytes(frame.data.bytes[0..4].try_into().unwrap())) });
866 w.bits(u32::from_ne_bytes( 836 mb.tdhr
867 frame.data.bytes[0..4].try_into().unwrap(), 837 .write(|w| unsafe { w.bits(u32::from_ne_bytes(frame.data.bytes[4..8].try_into().unwrap())) });
868 )) 838 mb.tir.write(|w| unsafe { w.bits(frame.id.0).txrq().set_bit() });
869 });
870 mb.tdhr.write(|w| unsafe {
871 w.bits(u32::from_ne_bytes(
872 frame.data.bytes[4..8].try_into().unwrap(),
873 ))
874 });
875 mb.tir
876 .write(|w| unsafe { w.bits(frame.id.0).txrq().set_bit() });
877 } 839 }
878 840
879 fn read_pending_mailbox(&mut self, idx: usize) -> Option<Frame> { 841 fn read_pending_mailbox(&mut self, idx: usize) -> Option<Frame> {