-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//! CRC calculation | ||
|
||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
use panic_halt as _; | ||
|
||
use cortex_m_rt::entry; | ||
use cortex_m_semihosting::hprintln; | ||
use stm32f1xx_hal::{pac, prelude::*}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let p = pac::Peripherals::take().unwrap(); | ||
|
||
let mut rcc = p.RCC.constrain(); | ||
let mut crc = p.CRC.new(&mut rcc.ahb); | ||
|
||
crc.reset(); | ||
crc.write(0x12345678); | ||
|
||
let val = crc.read(); | ||
hprintln!("found={:08x}, expected={:08x}", val, 0xdf8a8a2bu32).ok(); | ||
|
||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! CRC | ||
|
||
use crate::pac::CRC; | ||
use crate::rcc::{Enable, AHB}; | ||
|
||
/// Extension trait to constrain the CRC peripheral | ||
pub trait CrcExt { | ||
/// Constrains the CRC peripheral to play nicely with the other abstractions | ||
fn new(self, ahb: &mut AHB) -> Crc; | ||
} | ||
|
||
impl CrcExt for CRC { | ||
fn new(self, ahb: &mut AHB) -> Crc { | ||
CRC::enable(ahb); | ||
Crc { crc: self } | ||
} | ||
} | ||
|
||
/// Constrained CRC peripheral | ||
pub struct Crc { | ||
crc: CRC, | ||
} | ||
|
||
impl Crc { | ||
pub fn read(&self) -> u32 { | ||
self.crc.dr.read().bits() | ||
} | ||
|
||
pub fn write(&mut self, val: u32) { | ||
self.crc.dr.write(|w| w.dr().bits(val)) | ||
} | ||
|
||
pub fn reset(&self) { | ||
self.crc.cr.write(|w| w.reset().set_bit()); | ||
// calling CRC::dr::write() just after CRC::cr::reset() will not work as expected, and | ||
// inserting single nop() seems to solve the problem. | ||
cortex_m::asm::nop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters