Refactor
This commit is contained in:
parent
d88a5aeb14
commit
56c68af52e
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -70,7 +70,6 @@ checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bare-metal 0.2.5",
|
"bare-metal 0.2.5",
|
||||||
"bitfield",
|
"bitfield",
|
||||||
"critical-section",
|
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
"volatile-register",
|
"volatile-register",
|
||||||
]
|
]
|
||||||
|
@ -138,16 +137,6 @@ version = "1.1.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216"
|
checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "embedded-alloc"
|
|
||||||
version = "0.5.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8931e47e33c5d3194fbcf9cc82df0919193bd2fa40008f388eb1d28fd9c9ea6b"
|
|
||||||
dependencies = [
|
|
||||||
"critical-section",
|
|
||||||
"linked_list_allocator",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "embedded-dma"
|
name = "embedded-dma"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
@ -225,11 +214,9 @@ name = "hello-can-stm32"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bxcan",
|
"bxcan",
|
||||||
"cortex-m",
|
|
||||||
"cortex-m-rt",
|
"cortex-m-rt",
|
||||||
"cortex-m-rtic",
|
"cortex-m-rtic",
|
||||||
"cortex-m-semihosting",
|
"cortex-m-semihosting",
|
||||||
"embedded-alloc",
|
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
"heapless",
|
"heapless",
|
||||||
"nb 1.1.0",
|
"nb 1.1.0",
|
||||||
|
@ -248,12 +235,6 @@ dependencies = [
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "linked_list_allocator"
|
|
||||||
version = "0.10.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
version = "0.4.10"
|
version = "0.4.10"
|
||||||
|
|
18
Cargo.toml
18
Cargo.toml
|
@ -6,18 +6,16 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
embedded-hal = "0.2.7"
|
|
||||||
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
|
|
||||||
cortex-m-rtic = "1.1.3"
|
|
||||||
panic-halt = "0.2.0"
|
|
||||||
cortex-m-semihosting = { version = "0.5.0" }
|
|
||||||
stm32f1xx-hal = { version = "0.10.0", features = ["rt", "stm32f103"] }
|
|
||||||
shared-bus-rtic = "0.2.2"
|
|
||||||
embedded-alloc = "0.5.0"
|
|
||||||
nb = "1.1.0"
|
|
||||||
cortex-m-rt = "0.7.3"
|
|
||||||
bxcan = "0.7.0"
|
bxcan = "0.7.0"
|
||||||
|
cortex-m-rt = "0.7.3"
|
||||||
|
cortex-m-rtic = "1.1.3"
|
||||||
|
cortex-m-semihosting = { version = "0.5.0" }
|
||||||
|
embedded-hal = "0.2.7"
|
||||||
heapless = "0.7.16"
|
heapless = "0.7.16"
|
||||||
|
nb = "1.1.0"
|
||||||
|
panic-halt = "0.2.0"
|
||||||
|
shared-bus-rtic = "0.2.2"
|
||||||
|
stm32f1xx-hal = { version = "0.10.0", features = ["rt", "stm32f103"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
99
src/main.rs
99
src/main.rs
|
@ -1,24 +1,16 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use embedded_alloc::Heap;
|
use core::cmp::Ordering;
|
||||||
use panic_halt as _;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static HEAP: Heap = Heap::empty();
|
|
||||||
|
|
||||||
use bxcan::Frame;
|
use bxcan::Frame;
|
||||||
use core::cmp::Ordering;
|
|
||||||
use heapless::binary_heap::{BinaryHeap, Max};
|
use heapless::binary_heap::{BinaryHeap, Max};
|
||||||
|
use panic_halt as _;
|
||||||
use stm32f1xx_hal::pac::Interrupt;
|
use stm32f1xx_hal::pac::Interrupt;
|
||||||
extern crate alloc;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PriorityFrame(Frame);
|
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 {
|
impl Ord for PriorityFrame {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
self.0.priority().cmp(&other.0.priority())
|
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)]
|
#[rtic::app(device = stm32f1xx_hal::pac)]
|
||||||
mod app {
|
mod app {
|
||||||
use cortex_m_semihosting::hprintln;
|
|
||||||
use heapless::binary_heap::{BinaryHeap, Max};
|
use heapless::binary_heap::{BinaryHeap, Max};
|
||||||
|
|
||||||
use super::{enqueue_frame, PriorityFrame};
|
use super::{enqueue_frame, PriorityFrame};
|
||||||
|
@ -72,7 +63,6 @@ mod app {
|
||||||
can_rx: Rx0<Can<CAN1>>,
|
can_rx: Rx0<Can<CAN1>>,
|
||||||
led: Pin<'C', 13, Output>,
|
led: Pin<'C', 13, Output>,
|
||||||
delay: Delay<TIM2, 1000000>,
|
delay: Delay<TIM2, 1000000>,
|
||||||
counter: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[init]
|
#[init]
|
||||||
|
@ -80,8 +70,6 @@ mod app {
|
||||||
let mut flash = ctx.device.FLASH.constrain();
|
let mut flash = ctx.device.FLASH.constrain();
|
||||||
let rcc = ctx.device.RCC.constrain();
|
let rcc = ctx.device.RCC.constrain();
|
||||||
|
|
||||||
// let clocks = rcc.cfgr.freeze(&mut flash.acr);
|
|
||||||
|
|
||||||
let clocks = rcc
|
let clocks = rcc
|
||||||
.cfgr
|
.cfgr
|
||||||
.use_hse(8.MHz())
|
.use_hse(8.MHz())
|
||||||
|
@ -107,7 +95,6 @@ mod app {
|
||||||
can.modify_filters()
|
can.modify_filters()
|
||||||
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
|
.enable_bank(0, Fifo::Fifo0, Mask32::accept_all());
|
||||||
|
|
||||||
// Sync to the bus and start normal operation.
|
|
||||||
can.enable_interrupts(
|
can.enable_interrupts(
|
||||||
Interrupts::TRANSMIT_MAILBOX_EMPTY | Interrupts::FIFO0_MESSAGE_PENDING,
|
Interrupts::TRANSMIT_MAILBOX_EMPTY | Interrupts::FIFO0_MESSAGE_PENDING,
|
||||||
);
|
);
|
||||||
|
@ -133,7 +120,6 @@ mod app {
|
||||||
can_rx,
|
can_rx,
|
||||||
led,
|
led,
|
||||||
delay,
|
delay,
|
||||||
counter: 0,
|
|
||||||
},
|
},
|
||||||
init::Monotonics(),
|
init::Monotonics(),
|
||||||
)
|
)
|
||||||
|
@ -145,75 +131,21 @@ mod app {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle(shared = [can_tx_queue, tx_count], local = [delay])]
|
#[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;
|
let mut tx_queue = cx.shared.can_tx_queue;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for speed in 0..=30 {
|
for sign in [1, -1] {
|
||||||
tx_queue.lock(|mut tx_queue| {
|
for speed in (0..=30)
|
||||||
enqueue_frame(
|
.chain((0..10).map(|_| 30))
|
||||||
&mut tx_queue,
|
.chain((0..=30).map(|i| 30 - i))
|
||||||
Frame::new_data(StandardId::new(0x1FF).unwrap(), speed2array(speed * 1000)),
|
{
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
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| {
|
tx_queue.lock(|mut tx_queue| {
|
||||||
enqueue_frame(
|
enqueue_frame(
|
||||||
&mut tx_queue,
|
&mut tx_queue,
|
||||||
Frame::new_data(
|
Frame::new_data(
|
||||||
StandardId::new(0x1FF).unwrap(),
|
StandardId::new(0x1FF).unwrap(),
|
||||||
speed2array(30000 - (speed * 1000)),
|
speed2array(speed * 1000 * sign),
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
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),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -222,8 +154,8 @@ mod app {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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])]
|
#[task(binds = USB_HP_CAN_TX, local = [can_tx, led], shared = [can_tx_queue, tx_count])]
|
||||||
fn can_tx(cx: can_tx::Context) {
|
fn can_tx(cx: can_tx::Context) {
|
||||||
let tx = cx.local.can_tx;
|
let tx = cx.local.can_tx;
|
||||||
|
@ -232,8 +164,6 @@ mod app {
|
||||||
|
|
||||||
tx.clear_interrupt_flags();
|
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| {
|
(&mut tx_queue, &mut tx_count).lock(|tx_queue, tx_count| {
|
||||||
while let Some(frame) = tx_queue.peek() {
|
while let Some(frame) = tx_queue.peek() {
|
||||||
match tx.transmit(&frame.0) {
|
match tx.transmit(&frame.0) {
|
||||||
|
@ -256,15 +186,14 @@ mod app {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(binds = USB_LP_CAN_RX0, local = [can_rx], shared = [can_tx_queue])]
|
#[task(binds = USB_LP_CAN_RX0, local = [can_rx], shared = [can_tx_queue])]
|
||||||
fn can_rx0(mut cx: can_rx0::Context) {
|
fn can_rx0(cx: can_rx0::Context) {
|
||||||
// Echo back received packages with correct priority ordering.
|
|
||||||
loop {
|
loop {
|
||||||
match cx.local.can_rx.receive() {
|
match cx.local.can_rx.receive() {
|
||||||
Ok(frame) => {
|
Ok(_frame) => {
|
||||||
// let data = frame.data().unwrap();
|
// ここがデータきたときのあれ。
|
||||||
}
|
}
|
||||||
Err(nb::Error::WouldBlock) => break,
|
Err(nb::Error::WouldBlock) => break,
|
||||||
Err(nb::Error::Other(_)) => {} // Ignore overrun errors.
|
Err(nb::Error::Other(_)) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user