Skip to content

Commit d1ad359

Browse files
yjh0502TheZoq2
authored andcommitted
initial CRC support
1 parent 3801f39 commit d1ad359

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

examples/crc.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

src/crc.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub mod backup_domain;
128128
#[cfg(feature = "device-selected")]
129129
pub mod bb;
130130
#[cfg(feature = "device-selected")]
131+
pub mod crc;
132+
#[cfg(feature = "device-selected")]
131133
pub mod delay;
132134
#[cfg(feature = "device-selected")]
133135
pub mod dma;

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub use crate::adc::ChannelTimeSequence as _stm32_hal_adc_ChannelTimeSequence;
22
pub use crate::afio::AfioExt as _stm32_hal_afio_AfioExt;
3+
pub use crate::crc::CrcExt as _stm32_hal_crc_CrcExt;
34
pub use crate::dma::CircReadDma as _stm32_hal_dma_CircReadDma;
45
pub use crate::dma::DmaExt as _stm32_hal_dma_DmaExt;
56
pub use crate::dma::ReadDma as _stm32_hal_dma_ReadDma;

0 commit comments

Comments
 (0)