File tree Expand file tree Collapse file tree 4 files changed +69
-0
lines changed Expand file tree Collapse file tree 4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ //! CRC calculation
2
+
3
+ #![ deny( unsafe_code) ]
4
+ #![ no_main]
5
+ #![ no_std]
6
+
7
+ use panic_halt as _;
8
+
9
+ use cortex_m_rt:: entry;
10
+ use cortex_m_semihosting:: hprintln;
11
+ use stm32f1xx_hal:: { pac, prelude:: * } ;
12
+
13
+ #[ entry]
14
+ fn main ( ) -> ! {
15
+ let p = pac:: Peripherals :: take ( ) . unwrap ( ) ;
16
+
17
+ let mut rcc = p. RCC . constrain ( ) ;
18
+ let mut crc = p. CRC . new ( & mut rcc. ahb ) ;
19
+
20
+ crc. reset ( ) ;
21
+ crc. write ( 0x12345678 ) ;
22
+
23
+ let val = crc. read ( ) ;
24
+ hprintln ! ( "found={:08x}, expected={:08x}" , val, 0xdf8a8a2bu32 ) . ok ( ) ;
25
+
26
+ loop { }
27
+ }
Original file line number Diff line number Diff line change
1
+ //! CRC
2
+
3
+ use crate :: pac:: CRC ;
4
+ use crate :: rcc:: { Enable , AHB } ;
5
+
6
+ /// Extension trait to constrain the CRC peripheral
7
+ pub trait CrcExt {
8
+ /// Constrains the CRC peripheral to play nicely with the other abstractions
9
+ fn new ( self , ahb : & mut AHB ) -> Crc ;
10
+ }
11
+
12
+ impl CrcExt for CRC {
13
+ fn new ( self , ahb : & mut AHB ) -> Crc {
14
+ CRC :: enable ( ahb) ;
15
+ Crc { crc : self }
16
+ }
17
+ }
18
+
19
+ /// Constrained CRC peripheral
20
+ pub struct Crc {
21
+ crc : CRC ,
22
+ }
23
+
24
+ impl Crc {
25
+ pub fn read ( & self ) -> u32 {
26
+ self . crc . dr . read ( ) . bits ( )
27
+ }
28
+
29
+ pub fn write ( & mut self , val : u32 ) {
30
+ self . crc . dr . write ( |w| w. dr ( ) . bits ( val) )
31
+ }
32
+
33
+ pub fn reset ( & self ) {
34
+ self . crc . cr . write ( |w| w. reset ( ) . set_bit ( ) ) ;
35
+ // calling CRC::dr::write() just after CRC::cr::reset() will not work as expected, and
36
+ // inserting single nop() seems to solve the problem.
37
+ cortex_m:: asm:: nop ( ) ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change @@ -128,6 +128,8 @@ pub mod backup_domain;
128
128
#[ cfg( feature = "device-selected" ) ]
129
129
pub mod bb;
130
130
#[ cfg( feature = "device-selected" ) ]
131
+ pub mod crc;
132
+ #[ cfg( feature = "device-selected" ) ]
131
133
pub mod delay;
132
134
#[ cfg( feature = "device-selected" ) ]
133
135
pub mod dma;
Original file line number Diff line number Diff line change 1
1
pub use crate :: adc:: ChannelTimeSequence as _stm32_hal_adc_ChannelTimeSequence;
2
2
pub use crate :: afio:: AfioExt as _stm32_hal_afio_AfioExt;
3
+ pub use crate :: crc:: CrcExt as _stm32_hal_crc_CrcExt;
3
4
pub use crate :: dma:: CircReadDma as _stm32_hal_dma_CircReadDma;
4
5
pub use crate :: dma:: DmaExt as _stm32_hal_dma_DmaExt;
5
6
pub use crate :: dma:: ReadDma as _stm32_hal_dma_ReadDma;
You can’t perform that action at this time.
0 commit comments