aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Balbi <[email protected]>2025-12-05 08:41:43 -0800
committerFelipe Balbi <[email protected]>2025-12-05 09:03:59 -0800
commitb943d71365ef05b23db7066727f8bb6158b1a624 (patch)
tree9da854c46e3fdc47ffc2d29e87cedb987e92b1b2
parentae285b84ed551aa5c81443fcacb73e788a6d36c2 (diff)
mcxa/crc: document the use of align_to
-rw-r--r--embassy-mcxa/src/crc.rs20
-rw-r--r--examples/mcxa/src/bin/crc.rs10
2 files changed, 23 insertions, 7 deletions
diff --git a/embassy-mcxa/src/crc.rs b/embassy-mcxa/src/crc.rs
index 83b5eca8d..f01a671c4 100644
--- a/embassy-mcxa/src/crc.rs
+++ b/embassy-mcxa/src/crc.rs
@@ -222,7 +222,15 @@ impl<'d> Crc<'d, Crc16> {
222 } 222 }
223 223
224 /// Feeds a slice of bytes into the CRC peripheral. Returns the computed checksum. 224 /// Feeds a slice of bytes into the CRC peripheral. Returns the computed checksum.
225 pub fn feed_bytes(&mut self, bytes: &[u8]) -> u16 { 225 ///
226 /// The input is split using [`align_to::<u32>`] into:
227 /// - `prefix`: unaligned leading bytes,
228 /// - `data`: aligned `u32` words,
229 /// - `suffix`: trailing bytes.
230 ///
231 /// This allows efficient 32‑bit writes where possible, falling back to byte writes
232 /// for the remainder.
233 pub fn feed(&mut self, bytes: &[u8]) -> u16 {
226 let (prefix, data, suffix) = unsafe { bytes.align_to::<u32>() }; 234 let (prefix, data, suffix) = unsafe { bytes.align_to::<u32>() };
227 235
228 for b in prefix { 236 for b in prefix {
@@ -325,7 +333,15 @@ impl<'d> Crc<'d, Crc32> {
325 } 333 }
326 334
327 /// Feeds a slice of bytes into the CRC peripheral. Returns the computed checksum. 335 /// Feeds a slice of bytes into the CRC peripheral. Returns the computed checksum.
328 pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 { 336 ///
337 /// The input is split using [`align_to::<u32>`] into:
338 /// - `prefix`: unaligned leading bytes,
339 /// - `data`: aligned `u32` words,
340 /// - `suffix`: trailing bytes.
341 ///
342 /// This allows efficient 32‑bit writes where possible, falling back to byte writes
343 /// for the remainder.
344 pub fn feed(&mut self, bytes: &[u8]) -> u32 {
329 let (prefix, data, suffix) = unsafe { bytes.align_to::<u32>() }; 345 let (prefix, data, suffix) = unsafe { bytes.align_to::<u32>() };
330 346
331 for b in prefix { 347 for b in prefix {
diff --git a/examples/mcxa/src/bin/crc.rs b/examples/mcxa/src/bin/crc.rs
index 417b4f865..f963620cc 100644
--- a/examples/mcxa/src/bin/crc.rs
+++ b/examples/mcxa/src/bin/crc.rs
@@ -16,23 +16,23 @@ async fn main(_spawner: Spawner) {
16 let buf = b"123456789"; 16 let buf = b"123456789";
17 17
18 let mut crc = Crc::new_ccitt_false(p.CRC0.reborrow()); 18 let mut crc = Crc::new_ccitt_false(p.CRC0.reborrow());
19 let sum = crc.feed_bytes(buf); 19 let sum = crc.feed(buf);
20 assert_eq!(sum, 0x29b1); 20 assert_eq!(sum, 0x29b1);
21 21
22 let mut crc = Crc::new_maxim(p.CRC0.reborrow()); 22 let mut crc = Crc::new_maxim(p.CRC0.reborrow());
23 let sum = crc.feed_bytes(buf); 23 let sum = crc.feed(buf);
24 assert_eq!(sum, 0x44c2); 24 assert_eq!(sum, 0x44c2);
25 25
26 let mut crc = Crc::new_kermit(p.CRC0.reborrow()); 26 let mut crc = Crc::new_kermit(p.CRC0.reborrow());
27 let sum = crc.feed_bytes(buf); 27 let sum = crc.feed(buf);
28 assert_eq!(sum, 0x2189); 28 assert_eq!(sum, 0x2189);
29 29
30 let mut crc = Crc::new_iso_hdlc(p.CRC0.reborrow()); 30 let mut crc = Crc::new_iso_hdlc(p.CRC0.reborrow());
31 let sum = crc.feed_bytes(buf); 31 let sum = crc.feed(buf);
32 assert_eq!(sum, 0xcbf4_3926); 32 assert_eq!(sum, 0xcbf4_3926);
33 33
34 let mut crc = Crc::new_posix(p.CRC0.reborrow()); 34 let mut crc = Crc::new_posix(p.CRC0.reborrow());
35 let sum = crc.feed_bytes(buf); 35 let sum = crc.feed(buf);
36 assert_eq!(sum, 0x765e_7680); 36 assert_eq!(sum, 0x765e_7680);
37 37
38 defmt::info!("CRC successful"); 38 defmt::info!("CRC successful");