Skip to content

Commit 4618c51

Browse files
committed
switch to heapless, cleanup from comments
1 parent be1a6fe commit 4618c51

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ stm32f3 = "0.10"
2929
version = "0.1.0"
3030
optional = true
3131

32-
[dependencies.arrayvec]
33-
version = "0.5.1"
34-
default-features = false
32+
[dependencies.heapless]
33+
version = "0.5.5"
3534
optional = true
3635

3736
[dependencies.bare-metal]
@@ -60,7 +59,7 @@ default = ["unproven"]
6059
device-selected = []
6160
direct-call-deprecated = []
6261
rt = ["stm32f3/rt"]
63-
can = ["embedded-hal-can", "arrayvec"]
62+
can = ["embedded-hal-can", "heapless"]
6463
# Any Changes here should be mirrored in README.md and src/lib.rs
6564
stm32f301 = ["stm32f3/stm32f301", "direct-call-deprecated"]
6665
stm32f301xb = ["stm32f301", "device-selected"]
@@ -105,7 +104,7 @@ required-features = ["stm32f303xc"]
105104

106105
[[example]]
107106
name = "toggle"
108-
required-features = ["rt", "stm32f302xc"]
107+
required-features = ["rt", "stm32f303xc"]
109108

110109
[[example]]
111110
name = "usb_serial"

examples/can.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() -> ! {
3737
.pclk2(16.mhz())
3838
.freeze(&mut flash.acr);
3939

40-
// Configure CAN RX and TX pins (AF7)
40+
// Configure CAN RX and TX pins (AF9)
4141
let can_rx = gpioa.pa11.into_af9(&mut gpioa.moder, &mut gpioa.afrh);
4242
let can_tx = gpioa.pa12.into_af9(&mut gpioa.moder, &mut gpioa.afrh);
4343

src/can.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use crate::gpio::gpioa;
44
use crate::gpio::AF9;
55
use crate::rcc::APB1;
66
use crate::stm32;
7-
use arrayvec::ArrayVec;
8-
use nb;
9-
use nb::Error;
7+
use heapless::{consts::U8, Vec};
8+
use nb::{self, Error};
109

1110
use core::sync::atomic::{AtomicU8, Ordering};
1211

@@ -22,7 +21,7 @@ pub enum CanId {
2221
#[derive(Debug, Clone, Eq, PartialEq)]
2322
pub struct CanFrame {
2423
pub id: CanId,
25-
pub data: ArrayVec<[u8; 8]>,
24+
pub data: Vec<u8, U8>,
2625
}
2726

2827
pub enum FilterMode {
@@ -111,7 +110,7 @@ impl embedded_hal_can::Frame for CanFrame {
111110
#[inline(always)]
112111
fn data(&self) -> Option<&[u8]> {
113112
if self.data.len() > 0 {
114-
Some(&self.data.as_slice())
113+
Some(&self.data)
115114
} else {
116115
None
117116
}
@@ -294,7 +293,7 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
294293
&mut self,
295294
frame: &Self::Frame,
296295
) -> Result<Option<Self::Frame>, nb::Error<Self::Error>> {
297-
let can = can_register();
296+
let can = unsafe { &*stm32::CAN::ptr() };
298297

299298
for tx_idx in 0..3 {
300299
let free = match tx_idx {
@@ -319,9 +318,11 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
319318
}
320319

321320
if let Some(_) = frame.data() {
322-
unsafe {
323-
for j in 0..frame.data.len() {
324-
let val = &frame.data[j];
321+
for j in 0..frame.data.len() {
322+
let val = &frame.data[j];
323+
324+
// NOTE(unsafe): full 8bit write is unsafe via the svd2rust api
325+
unsafe {
325326
match j {
326327
0 => tx.tdlr.modify(|_, w| w.data0().bits(*val)),
327328
1 => tx.tdlr.modify(|_, w| w.data1().bits(*val)),
@@ -334,10 +335,12 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
334335
_ => unreachable!(),
335336
}
336337
}
337-
338-
tx.tdtr.modify(|_, w| w.dlc().bits(frame.data.len() as u8));
339338
}
340339

340+
// NOTE(unsafe): full 8bit write is unsafe via the svd2rust api
341+
tx.tdtr
342+
.modify(|_, w| unsafe { w.dlc().bits(frame.data.len() as u8) });
343+
341344
tx.tir.modify(|_, w| w.rtr().clear_bit());
342345
} else {
343346
tx.tir.modify(|_, w| w.rtr().set_bit());
@@ -355,11 +358,11 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
355358

356359
impl Receiver for CanFifo {
357360
fn receive(&mut self) -> Result<Self::Frame, Error<Self::Error>> {
358-
let can = can_register();
361+
let can = unsafe { &*stm32::CAN::ptr() };
359362

360363
let rx = &can.rx[self.idx];
361364
if can.rfr[self.idx].read().fmp().bits() > 0 {
362-
let mut data = ArrayVec::<[u8; 8]>::new();
365+
let mut data = Vec::<_, U8>::new();
363366

364367
let len = rx.rdtr.read().dlc().bits() as usize;
365368

@@ -368,14 +371,14 @@ impl Receiver for CanFifo {
368371

369372
for i in 0..len {
370373
match i {
371-
0 => data.push(data_low.data0().bits()),
372-
1 => data.push(data_low.data1().bits()),
373-
2 => data.push(data_low.data2().bits()),
374-
3 => data.push(data_low.data3().bits()),
375-
4 => data.push(data_high.data4().bits()),
376-
5 => data.push(data_high.data5().bits()),
377-
6 => data.push(data_high.data6().bits()),
378-
7 => data.push(data_high.data7().bits()),
374+
0 => data.push(data_low.data0().bits()).unwrap(),
375+
1 => data.push(data_low.data1().bits()).unwrap(),
376+
2 => data.push(data_low.data2().bits()).unwrap(),
377+
3 => data.push(data_low.data3().bits()).unwrap(),
378+
4 => data.push(data_high.data4().bits()).unwrap(),
379+
5 => data.push(data_high.data5().bits()).unwrap(),
380+
6 => data.push(data_high.data6().bits()).unwrap(),
381+
7 => data.push(data_high.data7().bits()).unwrap(),
379382
_ => unreachable!(),
380383
}
381384
}
@@ -402,7 +405,7 @@ impl Receiver for CanFifo {
402405

403406
fn set_filter(&mut self, filter: Self::Filter) {
404407
cortex_m::interrupt::free(|_cs| {
405-
let can = can_register();
408+
let can = unsafe { &*stm32::CAN::ptr() };
406409

407410
// Filter init mode
408411
can.fmr.modify(|_, w| w.finit().set_bit());
@@ -472,10 +475,10 @@ impl CanFrame {
472475
pub fn new_with_len(id: CanId, src: &[u8], length: usize) -> CanFrame {
473476
assert!(length <= 8, "CAN Frames can have at most 8 data bytes");
474477

475-
let mut data = ArrayVec::<[u8; 8]>::new();
478+
let mut data = Vec::<u8, U8>::new();
476479

477480
// The vector is always empty and the data size has alreay been checked, this will always succeed
478-
data.try_extend_from_slice(src).unwrap();
481+
data.extend_from_slice(src).unwrap();
479482

480483
CanFrame { id, data }
481484
}
@@ -487,12 +490,7 @@ impl CanFrame {
487490
pub fn remote_frame(id: CanId) -> CanFrame {
488491
CanFrame {
489492
id,
490-
data: ArrayVec::<_>::new(),
493+
data: Vec::<_, _>::new(),
491494
}
492495
}
493496
}
494-
495-
#[inline]
496-
fn can_register() -> &'static stm32::can::RegisterBlock {
497-
return unsafe { &*stm32::CAN::ptr() };
498-
}

0 commit comments

Comments
 (0)