@@ -4,9 +4,8 @@ use crate::gpio::gpioa;
4
4
use crate :: gpio:: AF9 ;
5
5
use crate :: rcc:: APB1 ;
6
6
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 } ;
10
9
11
10
use core:: sync:: atomic:: { AtomicU8 , Ordering } ;
12
11
@@ -22,7 +21,7 @@ pub enum CanId {
22
21
#[ derive( Debug , Clone , Eq , PartialEq ) ]
23
22
pub struct CanFrame {
24
23
pub id : CanId ,
25
- pub data : ArrayVec < [ u8 ; 8 ] > ,
24
+ pub data : Vec < u8 , U8 > ,
26
25
}
27
26
28
27
pub enum FilterMode {
@@ -111,7 +110,7 @@ impl embedded_hal_can::Frame for CanFrame {
111
110
#[ inline( always) ]
112
111
fn data ( & self ) -> Option < & [ u8 ] > {
113
112
if self . data . len ( ) > 0 {
114
- Some ( & self . data . as_slice ( ) )
113
+ Some ( & self . data )
115
114
} else {
116
115
None
117
116
}
@@ -294,7 +293,7 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
294
293
& mut self ,
295
294
frame : & Self :: Frame ,
296
295
) -> Result < Option < Self :: Frame > , nb:: Error < Self :: Error > > {
297
- let can = can_register ( ) ;
296
+ let can = unsafe { & * stm32 :: CAN :: ptr ( ) } ;
298
297
299
298
for tx_idx in 0 ..3 {
300
299
let free = match tx_idx {
@@ -319,9 +318,11 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
319
318
}
320
319
321
320
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 {
325
326
match j {
326
327
0 => tx. tdlr . modify ( |_, w| w. data0 ( ) . bits ( * val) ) ,
327
328
1 => tx. tdlr . modify ( |_, w| w. data1 ( ) . bits ( * val) ) ,
@@ -334,10 +335,12 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
334
335
_ => unreachable ! ( ) ,
335
336
}
336
337
}
337
-
338
- tx. tdtr . modify ( |_, w| w. dlc ( ) . bits ( frame. data . len ( ) as u8 ) ) ;
339
338
}
340
339
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
+
341
344
tx. tir . modify ( |_, w| w. rtr ( ) . clear_bit ( ) ) ;
342
345
} else {
343
346
tx. tir . modify ( |_, w| w. rtr ( ) . set_bit ( ) ) ;
@@ -355,11 +358,11 @@ impl embedded_hal_can::Transmitter for CanTransmitter {
355
358
356
359
impl Receiver for CanFifo {
357
360
fn receive ( & mut self ) -> Result < Self :: Frame , Error < Self :: Error > > {
358
- let can = can_register ( ) ;
361
+ let can = unsafe { & * stm32 :: CAN :: ptr ( ) } ;
359
362
360
363
let rx = & can. rx [ self . idx ] ;
361
364
if can. rfr [ self . idx ] . read ( ) . fmp ( ) . bits ( ) > 0 {
362
- let mut data = ArrayVec :: < [ u8 ; 8 ] > :: new ( ) ;
365
+ let mut data = Vec :: < _ , U8 > :: new ( ) ;
363
366
364
367
let len = rx. rdtr . read ( ) . dlc ( ) . bits ( ) as usize ;
365
368
@@ -368,14 +371,14 @@ impl Receiver for CanFifo {
368
371
369
372
for i in 0 ..len {
370
373
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 ( ) ,
379
382
_ => unreachable ! ( ) ,
380
383
}
381
384
}
@@ -402,7 +405,7 @@ impl Receiver for CanFifo {
402
405
403
406
fn set_filter ( & mut self , filter : Self :: Filter ) {
404
407
cortex_m:: interrupt:: free ( |_cs| {
405
- let can = can_register ( ) ;
408
+ let can = unsafe { & * stm32 :: CAN :: ptr ( ) } ;
406
409
407
410
// Filter init mode
408
411
can. fmr . modify ( |_, w| w. finit ( ) . set_bit ( ) ) ;
@@ -472,10 +475,10 @@ impl CanFrame {
472
475
pub fn new_with_len ( id : CanId , src : & [ u8 ] , length : usize ) -> CanFrame {
473
476
assert ! ( length <= 8 , "CAN Frames can have at most 8 data bytes" ) ;
474
477
475
- let mut data = ArrayVec :: < [ u8 ; 8 ] > :: new ( ) ;
478
+ let mut data = Vec :: < u8 , U8 > :: new ( ) ;
476
479
477
480
// 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 ( ) ;
479
482
480
483
CanFrame { id, data }
481
484
}
@@ -487,12 +490,7 @@ impl CanFrame {
487
490
pub fn remote_frame ( id : CanId ) -> CanFrame {
488
491
CanFrame {
489
492
id,
490
- data : ArrayVec :: < _ > :: new ( ) ,
493
+ data : Vec :: < _ , _ > :: new ( ) ,
491
494
}
492
495
}
493
496
}
494
-
495
- #[ inline]
496
- fn can_register ( ) -> & ' static stm32:: can:: RegisterBlock {
497
- return unsafe { & * stm32:: CAN :: ptr ( ) } ;
498
- }
0 commit comments