This commit is contained in:
yanorei32 2023-10-11 01:37:34 +09:00
parent d88a5aeb14
commit 56c68af52e
3 changed files with 31 additions and 123 deletions

19
Cargo.lock generated
View File

@ -70,7 +70,6 @@ checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9"
dependencies = [
"bare-metal 0.2.5",
"bitfield",
"critical-section",
"embedded-hal",
"volatile-register",
]
@ -138,16 +137,6 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "embedded-dma"
version = "0.2.0"
@ -225,11 +214,9 @@ name = "hello-can-stm32"
version = "0.1.0"
dependencies = [
"bxcan",
"cortex-m",
"cortex-m-rt",
"cortex-m-rtic",
"cortex-m-semihosting",
"embedded-alloc",
"embedded-hal",
"heapless",
"nb 1.1.0",
@ -248,12 +235,6 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "linked_list_allocator"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
[[package]]
name = "lock_api"
version = "0.4.10"

View File

@ -6,18 +6,16 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[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"
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"
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]
default = []

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(_)) => {}
}
}
}