aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorin Cooper-Bennun <[email protected]>2024-02-28 10:10:15 +0000
committerCorey Schuhen <[email protected]>2024-03-02 10:08:20 +1000
commitbefbb2845aebde5aa879bbe6dd0bdb08c277e492 (patch)
tree9b716225dc44d04c1945e2263f737092f9b96048
parent30606f9782247b7f0e485b39042100ec3286aaa2 (diff)
stm32: can: fd: write: if in TX FIFO mode & bufs full, then abort
-rw-r--r--embassy-stm32/src/can/fd/config.rs11
-rw-r--r--embassy-stm32/src/can/fd/peripheral.rs21
2 files changed, 24 insertions, 8 deletions
diff --git a/embassy-stm32/src/can/fd/config.rs b/embassy-stm32/src/can/fd/config.rs
index e959913e4..338e4979d 100644
--- a/embassy-stm32/src/can/fd/config.rs
+++ b/embassy-stm32/src/can/fd/config.rs
@@ -288,7 +288,7 @@ impl Default for GlobalFilter {
288} 288}
289 289
290/// TX buffer operation mode 290/// TX buffer operation mode
291#[derive(Clone, Copy, Debug)] 291#[derive(Clone, Copy, PartialEq, Eq, Debug)]
292pub enum TxBufferMode { 292pub enum TxBufferMode {
293 /// TX FIFO operation 293 /// TX FIFO operation
294 Fifo, 294 Fifo,
@@ -305,6 +305,15 @@ impl From<TxBufferMode> for crate::pac::can::vals::Tfqm {
305 } 305 }
306} 306}
307 307
308impl From<crate::pac::can::vals::Tfqm> for TxBufferMode {
309 fn from(value: crate::pac::can::vals::Tfqm) -> Self {
310 match value {
311 crate::pac::can::vals::Tfqm::QUEUE => Self::Queue,
312 crate::pac::can::vals::Tfqm::FIFO => Self::Fifo,
313 }
314 }
315}
316
308/// FdCan Config Struct 317/// FdCan Config Struct
309#[derive(Clone, Copy, Debug)] 318#[derive(Clone, Copy, Debug)]
310pub struct FdCanConfig { 319pub struct FdCanConfig {
diff --git a/embassy-stm32/src/can/fd/peripheral.rs b/embassy-stm32/src/can/fd/peripheral.rs
index 0ebbdb3b7..2a183d2e8 100644
--- a/embassy-stm32/src/can/fd/peripheral.rs
+++ b/embassy-stm32/src/can/fd/peripheral.rs
@@ -114,6 +114,12 @@ impl Registers {
114 self.regs.txfqs().read().tfqf() 114 self.regs.txfqs().read().tfqf()
115 } 115 }
116 116
117 /// Returns the current TX buffer operation mode (queue or FIFO)
118 #[inline]
119 pub fn tx_queue_mode(&self) -> TxBufferMode {
120 self.regs.txbc().read().tfqm().into()
121 }
122
117 #[inline] 123 #[inline]
118 pub fn has_pending_frame(&self, idx: usize) -> bool { 124 pub fn has_pending_frame(&self, idx: usize) -> bool {
119 self.regs.txbrp().read().trp(idx) 125 self.regs.txbrp().read().trp(idx)
@@ -197,13 +203,14 @@ impl Registers {
197 } 203 }
198 204
199 pub fn write<F: embedded_can::Frame + CanHeader>(&self, frame: &F) -> nb::Result<Option<F>, Infallible> { 205 pub fn write<F: embedded_can::Frame + CanHeader>(&self, frame: &F) -> nb::Result<Option<F>, Infallible> {
200 let queue_is_full = self.tx_queue_is_full(); 206 let (idx, pending_frame) = if self.tx_queue_is_full() {
201 207 if self.tx_queue_mode() == TxBufferMode::Fifo {
202 let id = frame.header().id(); 208 // Does not make sense to cancel a pending frame when using FIFO
203 209 return Err(nb::Error::WouldBlock);
204 // If the queue is full, 210 }
205 // Discard the first slot with a lower priority message 211 // If the queue is full,
206 let (idx, pending_frame) = if queue_is_full { 212 // Discard the first slot with a lower priority message
213 let id = frame.header().id();
207 if self.is_available(0, id) { 214 if self.is_available(0, id) {
208 (0, self.abort_pending_mailbox_generic(0)) 215 (0, self.abort_pending_mailbox_generic(0))
209 } else if self.is_available(1, id) { 216 } else if self.is_available(1, id) {