Skip to content

Commit aee685c

Browse files
committed
fix CRC
1 parent 009cfc3 commit aee685c

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

examples/crc.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate cortex_m_rt as rt;
88
extern crate panic_halt;
99
extern crate stm32g0xx_hal as hal;
1010

11-
use cortex_m_semihosting::hprintln;
11+
use defmt_rtt as _;
1212
use hal::crc::*;
1313
use hal::prelude::*;
1414
use hal::stm32;
@@ -25,24 +25,24 @@ fn main() -> ! {
2525
.output_bit_reversal(true)
2626
.freeze();
2727

28-
loop {
29-
crc.reset();
30-
crc.feed(b"123456789");
31-
32-
let hash_sum = crc.result();
33-
hprintln!(
34-
"crc32: 0x{:x}, crc32b: 0x{:x}",
35-
hash_sum,
36-
hash_sum ^ 0xffff_ffff
37-
);
38-
39-
crc.reset();
40-
crc.feed(b"The quick brown fox jumps over the lazy dog");
41-
let hash_sum = crc.result();
42-
hprintln!(
43-
"crc32: 0x{:x}, crc32b: 0x{:x}",
44-
hash_sum,
45-
hash_sum ^ 0xffff_ffff
46-
);
47-
}
28+
crc.reset();
29+
crc.feed(b"123456789");
30+
31+
let hash_sum = crc.result();
32+
defmt::info!(
33+
"target: 0xcbf43926, crc32: 0x{:x}, crc32b: 0x{:x}",
34+
hash_sum,
35+
hash_sum ^ 0xffff_ffff
36+
);
37+
38+
crc.reset();
39+
crc.feed(b"The quick brown fox jumps over the lazy dog");
40+
let hash_sum = crc.result();
41+
defmt::info!(
42+
"target: 0x414fa339, crc32: 0x{:x}, crc32b: 0x{:x}",
43+
hash_sum,
44+
hash_sum ^ 0xffff_ffff
45+
);
46+
47+
loop {}
4848
}

src/crc.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ impl Crc {
173173
pub fn feed(&mut self, data: &[u8]) {
174174
let crc = unsafe { &(*CRC::ptr()) };
175175
for byte in data {
176-
crc.dr().write(|w| unsafe { w.dr().bits((*byte).into()) });
176+
unsafe {
177+
core::ptr::write_volatile(
178+
core::cell::UnsafeCell::<u8>::raw_get(crc.dr().as_ptr() as _),
179+
*byte,
180+
)
181+
}
177182
}
178183
}
179184

src/spi.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::rcc::*;
33
use crate::stm32::{SPI1, SPI2};
44
use crate::time::Hertz;
55
use core::convert::Infallible;
6-
use core::ptr;
76
use embedded_hal::delay::DelayNs;
87
use hal::digital;
98
use hal::digital::OutputPin;
@@ -310,7 +309,7 @@ macro_rules! spi {
310309
} else if sr.crcerr().bit_is_set() {
311310
nb::Error::Other(Error::Crc)
312311
} else if sr.rxne().bit_is_set() {
313-
return Ok(self.spi.dr().read().bits() as u8);
312+
return Ok(self.spi.dr8().read().bits() as u8);
314313
} else {
315314
nb::Error::WouldBlock
316315
})
@@ -325,9 +324,7 @@ macro_rules! spi {
325324
} else if sr.crcerr().bit_is_set() {
326325
nb::Error::Other(Error::Crc)
327326
} else if sr.txe().bit_is_set() {
328-
unsafe {
329-
ptr::write_volatile(core::cell::UnsafeCell::raw_get(self.spi.dr() as *const _ as _), byte)
330-
}
327+
self.spi.dr8().write(|w| unsafe { w.dr().bits(byte as _) });
331328
return Ok(());
332329
} else {
333330
nb::Error::WouldBlock

0 commit comments

Comments
 (0)