This commit is contained in:
2023-10-11 01:37:34 +09:00
parent d88a5aeb14
commit 56c68af52e
3 changed files with 31 additions and 123 deletions
+23 -94
View File
@@ -1,24 +1,16 @@
#![no_std]
#![no_main]
use embedded_alloc::Heap;
use panic_halt as _;
#[global_allocator]
static HEAP: Heap = Heap::empty();
use core::cmp::Ordering;
use bxcan::Frame;
use core::cmp::Ordering;
use heapless::binary_heap::{BinaryHeap, Max};
use panic_halt as _;
use stm32f1xx_hal::pac::Interrupt;
extern crate alloc;
#[derive(Debug)]
pub struct PriorityFrame(Frame);
/// Ordering is based on the Identifier and frame type (data vs. remote) and can be used to sort
/// frames by priority.
impl Ord for PriorityFrame {
fn cmp(&self, other: &Self) -> Ordering {
self.0.priority().cmp(&other.0.priority())
@@ -46,7 +38,6 @@ fn enqueue_frame(queue: &mut BinaryHeap<PriorityFrame, Max, 16>, frame: Frame) {
#[rtic::app(device = stm32f1xx_hal::pac)]
mod app {
use cortex_m_semihosting::hprintln;
use heapless::binary_heap::{BinaryHeap, Max};
use super::{enqueue_frame, PriorityFrame};
@@ -72,7 +63,6 @@ mod app {
can_rx: Rx0<Can<CAN1>>,
led: Pin<'C', 13, Output>,
delay: Delay<TIM2, 1000000>,
counter: usize,
}
#[init]
@@ -80,8 +70,6 @@ mod app {
let mut flash = ctx.device.FLASH.constrain();
let rcc = ctx.device.RCC.constrain();
// let clocks = rcc.cfgr.freeze(&mut flash.acr);
let clocks = rcc
.cfgr
.use_hse(8.MHz())
@@ -107,7 +95,6 @@ mod app {
can.modify_filters()
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
// Sync to the bus and start normal operation.
can.enable_interrupts(
Interrupts::TRANSMIT_MAILBOX_EMPTY | Interrupts::FIFO0_MESSAGE_PENDING,
);
@@ -133,7 +120,6 @@ mod app {
can_rx,
led,
delay,
counter: 0,
},
init::Monotonics(),
)
@@ -145,85 +131,31 @@ mod app {
}
#[idle(shared = [can_tx_queue, tx_count], local = [delay])]
fn idle(mut cx: idle::Context) -> ! {
fn idle(cx: idle::Context) -> ! {
let mut tx_queue = cx.shared.can_tx_queue;
loop {
for speed in 0..=30 {
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(StandardId::new(0x1FF).unwrap(), speed2array(speed * 1000)),
);
});
for sign in [1, -1] {
for speed in (0..=30)
.chain((0..10).map(|_| 30))
.chain((0..=30).map(|i| 30 - i))
{
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(
StandardId::new(0x1FF).unwrap(),
speed2array(speed * 1000 * sign),
),
);
});
cx.local.delay.delay(100.millis());
}
for _ in 0..10 {
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(StandardId::new(0x1FF).unwrap(), speed2array(30000)),
);
});
cx.local.delay.delay(100.millis());
}
for speed in 0..=30 {
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(
StandardId::new(0x1FF).unwrap(),
speed2array(30000 - (speed * 1000)),
),
);
});
cx.local.delay.delay(100.millis());
}
for speed in 0..=30 {
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(StandardId::new(0x1FF).unwrap(), speed2array(speed * 1000 * -1)),
);
});
cx.local.delay.delay(100.millis());
}
for _ in 0..10 {
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(StandardId::new(0x1FF).unwrap(), speed2array(30000 * -1)),
);
});
cx.local.delay.delay(100.millis());
}
for speed in 0..=30 {
tx_queue.lock(|mut tx_queue| {
enqueue_frame(
&mut tx_queue,
Frame::new_data(
StandardId::new(0x1FF).unwrap(),
speed2array(( 30000 - (speed * 1000) ) * -1),
),
);
});
cx.local.delay.delay(100.millis());
cx.local.delay.delay(100.millis());
}
}
}
}
// This ISR is triggered by each finished frame transmission.
#[task(binds = USB_HP_CAN_TX, local = [can_tx, led], shared = [can_tx_queue, tx_count])]
fn can_tx(cx: can_tx::Context) {
let tx = cx.local.can_tx;
@@ -232,8 +164,6 @@ mod app {
tx.clear_interrupt_flags();
// There is now a free mailbox. Try to transmit pending frames until either
// the queue is empty or transmission would block the execution of this ISR.
(&mut tx_queue, &mut tx_count).lock(|tx_queue, tx_count| {
while let Some(frame) = tx_queue.peek() {
match tx.transmit(&frame.0) {
@@ -256,15 +186,14 @@ mod app {
}
#[task(binds = USB_LP_CAN_RX0, local = [can_rx], shared = [can_tx_queue])]
fn can_rx0(mut cx: can_rx0::Context) {
// Echo back received packages with correct priority ordering.
fn can_rx0(cx: can_rx0::Context) {
loop {
match cx.local.can_rx.receive() {
Ok(frame) => {
// let data = frame.data().unwrap();
Ok(_frame) => {
// ここがデータきたときのあれ。
}
Err(nb::Error::WouldBlock) => break,
Err(nb::Error::Other(_)) => {} // Ignore overrun errors.
Err(nb::Error::Other(_)) => {}
}
}
}