From 37fb18c9ef0dda81c3fe82b16a1b6256eb3b2d5b Mon Sep 17 00:00:00 2001 From: Jihyun Yu Date: Sun, 19 Apr 2020 21:12:55 +0900 Subject: [PATCH] initial CRC support --- examples/crc.rs | 27 +++++++++++++++++++++++++++ src/crc.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/prelude.rs | 7 ++++--- 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 examples/crc.rs create mode 100644 src/crc.rs diff --git a/examples/crc.rs b/examples/crc.rs new file mode 100644 index 00000000..da58af53 --- /dev/null +++ b/examples/crc.rs @@ -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 {} +} diff --git a/src/crc.rs b/src/crc.rs new file mode 100644 index 00000000..6d99b99b --- /dev/null +++ b/src/crc.rs @@ -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) { + unsafe { self.crc.dr.write(|w| w.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(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 49319bd2..543f1556 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,6 +106,8 @@ pub mod backup_domain; #[cfg(feature = "device-selected")] pub mod bb; #[cfg(feature = "device-selected")] +pub mod crc; +#[cfg(feature = "device-selected")] pub mod delay; #[cfg(feature = "device-selected")] pub mod dma; diff --git a/src/prelude.rs b/src/prelude.rs index a6691ab1..ff349473 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,6 +1,10 @@ pub use crate::adc::ChannelTimeSequence as _stm32_hal_adc_ChannelTimeSequence; pub use crate::afio::AfioExt as _stm32_hal_afio_AfioExt; +pub use crate::crc::CrcExt as _stm32_hal_crc_CrcExt; +pub use crate::dma::CircReadDma as _stm32_hal_dma_CircReadDma; pub use crate::dma::DmaExt as _stm32_hal_dma_DmaExt; +pub use crate::dma::ReadDma as _stm32_hal_dma_ReadDma; +pub use crate::dma::WriteDma as _stm32_hal_dma_WriteDma; pub use crate::flash::FlashExt as _stm32_hal_flash_FlashExt; pub use crate::gpio::GpioExt as _stm32_hal_gpio_GpioExt; pub use crate::hal::adc::OneShot as _embedded_hal_adc_OneShot; @@ -8,7 +12,4 @@ pub use crate::hal::digital::v2::StatefulOutputPin as _embedded_hal_digital_Stat pub use crate::hal::digital::v2::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPin; pub use crate::hal::prelude::*; pub use crate::rcc::RccExt as _stm32_hal_rcc_RccExt; -pub use crate::dma::CircReadDma as _stm32_hal_dma_CircReadDma; -pub use crate::dma::ReadDma as _stm32_hal_dma_ReadDma; -pub use crate::dma::WriteDma as _stm32_hal_dma_WriteDma; pub use crate::time::U32Ext as _stm32_hal_time_U32Ext;