diff --git a/src/main.rs b/src/main.rs index 221b5f8..9dc2584 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,52 +1,20 @@ #![no_std] #![no_main] -use core::cmp::Ordering; +mod model; -use bxcan::Frame; -use heapless::binary_heap::{BinaryHeap, Max}; use panic_halt as _; -use stm32f1xx_hal::pac::Interrupt; - -#[derive(Debug)] -pub struct PriorityFrame(Frame); - -impl Ord for PriorityFrame { - fn cmp(&self, other: &Self) -> Ordering { - self.0.priority().cmp(&other.0.priority()) - } -} - -impl PartialOrd for PriorityFrame { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl PartialEq for PriorityFrame { - fn eq(&self, other: &Self) -> bool { - self.cmp(other) == Ordering::Equal - } -} - -impl Eq for PriorityFrame {} - -fn enqueue_frame(queue: &mut BinaryHeap, frame: Frame) { - queue.push(PriorityFrame(frame)).unwrap(); - rtic::pend(Interrupt::USB_HP_CAN_TX); -} #[rtic::app(device = stm32f1xx_hal::pac)] mod app { - use heapless::binary_heap::{BinaryHeap, Max}; - - use super::{enqueue_frame, PriorityFrame}; + use crate::model::PriorityFrame; use bxcan::{filter::Mask32, Fifo, Frame, Interrupts, Rx0, StandardId, Tx}; + use heapless::binary_heap::{BinaryHeap, Max}; use stm32f1xx_hal::{ can::Can, device::TIM2, gpio::{Output, Pin}, - pac::CAN1, + pac::{CAN1, Interrupt}, prelude::*, timer::Delay, }; @@ -130,6 +98,11 @@ mod app { [b[0], b[1], b[0], b[1], b[0], b[1], b[0], b[1]] } + fn enqueue_frame(queue: &mut BinaryHeap, frame: Frame) { + queue.push(PriorityFrame(frame)).unwrap(); + rtic::pend(Interrupt::USB_HP_CAN_TX); + } + #[idle(shared = [can_tx_queue, tx_count], local = [delay])] fn idle(cx: idle::Context) -> ! { let mut tx_queue = cx.shared.can_tx_queue; diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..74604c1 --- /dev/null +++ b/src/model.rs @@ -0,0 +1,26 @@ +use core::cmp::Ordering; + +use bxcan::Frame; + +#[derive(Debug)] +pub struct PriorityFrame(pub Frame); + +impl Ord for PriorityFrame { + fn cmp(&self, other: &Self) -> Ordering { + self.0.priority().cmp(&other.0.priority()) + } +} + +impl PartialOrd for PriorityFrame { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for PriorityFrame { + fn eq(&self, other: &Self) -> bool { + self.cmp(other) == Ordering::Equal + } +} + +impl Eq for PriorityFrame {}