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;
128128#[ cfg( feature = "device-selected" ) ]
129129pub mod bb;
130130#[ cfg( feature = "device-selected" ) ]
131+ pub mod crc;
132+ #[ cfg( feature = "device-selected" ) ]
131133pub mod delay;
132134#[ cfg( feature = "device-selected" ) ]
133135pub mod dma;
Original file line number Diff line number Diff line change 11pub use crate :: adc:: ChannelTimeSequence as _stm32_hal_adc_ChannelTimeSequence;
22pub use crate :: afio:: AfioExt as _stm32_hal_afio_AfioExt;
3+ pub use crate :: crc:: CrcExt as _stm32_hal_crc_CrcExt;
34pub use crate :: dma:: CircReadDma as _stm32_hal_dma_CircReadDma;
45pub use crate :: dma:: DmaExt as _stm32_hal_dma_DmaExt;
56pub use crate :: dma:: ReadDma as _stm32_hal_dma_ReadDma;
You can’t perform that action at this time.
0 commit comments