aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Salzedo <[email protected]>2021-09-26 19:14:08 -0700
committerJoshua Salzedo <[email protected]>2021-09-26 19:14:08 -0700
commit642b0825a62a7d40c3496c32ac6abd8dce3e55bd (patch)
treee898cd3032b086452657f46f690f49733df7bb74
parentf9ff5336d4c78e571c755e63ef360393e29a1f51 (diff)
V3 is just an extension of V2, merge modules.
-rw-r--r--embassy-stm32/src/crc/v2.rs144
-rw-r--r--embassy-stm32/src/crc/v3.rs1
2 files changed, 106 insertions, 39 deletions
diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs
index b21e6c633..3336d3f75 100644
--- a/embassy-stm32/src/crc/v2.rs
+++ b/embassy-stm32/src/crc/v2.rs
@@ -1,5 +1,5 @@
1use crate::pac::CRC as PAC_CRC;
2use crate::pac::crc::vals; 1use crate::pac::crc::vals;
2use crate::pac::CRC as PAC_CRC;
3use crate::peripherals::CRC; 3use crate::peripherals::CRC;
4use crate::rcc::sealed::RccPeripheral; 4use crate::rcc::sealed::RccPeripheral;
5 5
@@ -9,14 +9,16 @@ pub struct Crc {
9} 9}
10 10
11pub enum CrcConfigError { 11pub enum CrcConfigError {
12 InvalidPolynomial 12 InvalidPolynomial,
13} 13}
14 14
15pub struct CrcConfig { 15pub struct CrcConfig {
16 reverse_in: CrcInputReverseConfig, 16 reverse_in: CrcInputReverseConfig,
17 reverse_out: bool, 17 reverse_out: bool,
18 #[cfg(crc_v3)]
18 poly_size: PolySize, 19 poly_size: PolySize,
19 crc_init_value: u32, 20 crc_init_value: u32,
21 #[cfg(crc_v3)]
20 crc_poly: u32, 22 crc_poly: u32,
21} 23}
22 24
@@ -28,16 +30,33 @@ pub enum CrcInputReverseConfig {
28} 30}
29 31
30impl CrcConfig { 32impl CrcConfig {
31 pub fn new(reverse_in: CrcInputReverseConfig, reverse_out: bool, poly_size: PolySize, crc_init_value: u32, crc_poly: u32) -> Result<Self, CrcConfigError> { 33 pub fn new(
34 reverse_in: CrcInputReverseConfig,
35 reverse_out: bool,
36 #[cfg(crc_v3)]
37 poly_size: PolySize,
38 crc_init_value: u32,
39 #[cfg(crc_v3)]
40 crc_poly: u32,
41 ) -> Result<Self, CrcConfigError> {
32 // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. 42 // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported.
43 #[cfg(crc_v3)]
33 if crc_poly % 2 == 0 { 44 if crc_poly % 2 == 0 {
34 Err(CrcConfigError::InvalidPolynomial) 45 return Err(CrcConfigError::InvalidPolynomial);
35 } else {
36 Ok(CrcConfig { reverse_in, reverse_out, poly_size, crc_init_value, crc_poly })
37 } 46 }
47 Ok(CrcConfig {
48 reverse_in,
49 reverse_out,
50 #[cfg(crc_v3)]
51 poly_size,
52 crc_init_value,
53 #[cfg(crc_v3)]
54 crc_poly,
55 })
38 } 56 }
39} 57}
40 58
59#[cfg(crc_v3)]
41pub enum PolySize { 60pub enum PolySize {
42 Width7, 61 Width7,
43 Width8, 62 Width8,
@@ -47,7 +66,7 @@ pub enum PolySize {
47 66
48impl Crc { 67impl Crc {
49 /// Instantiates the CRC32 peripheral and initializes it to default values. 68 /// Instantiates the CRC32 peripheral and initializes it to default values.
50 pub fn new(peripheral: CRC, init_value: u32, config: CrcConfig) -> Self { 69 pub fn new(peripheral: CRC, config: CrcConfig) -> Self {
51 // Note: enable and reset come from RccPeripheral. 70 // Note: enable and reset come from RccPeripheral.
52 // enable CRC clock in RCC. 71 // enable CRC clock in RCC.
53 CRC::enable(); 72 CRC::enable();
@@ -57,53 +76,102 @@ impl Crc {
57 _peripheral: peripheral, 76 _peripheral: peripheral,
58 _config: config, 77 _config: config,
59 }; 78 };
60 unimplemented!(); 79 CRC::reset();
61 // instance.init(); 80 instance.reconfigure();
62 // instance 81 instance.reset();
82 instance
63 } 83 }
64 84
65
66
67 pub fn reset(&mut self) { 85 pub fn reset(&mut self) {
68 unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)); } 86 unsafe {
87 PAC_CRC.cr().modify(|w| w.set_reset(true));
88 }
69 } 89 }
70 90
71 91 /// Reconfigures the CRC peripheral. Doesn't reset.
72 fn reconfigure(&mut self) { 92 fn reconfigure(&mut self) {
73 unsafe { 93 unsafe {
74 // Init CRC value 94 // Init CRC value
75 PAC_CRC.init().write_value(self._config.crc_init_value); 95 PAC_CRC.init().write_value(self._config.crc_init_value);
96 #[cfg(crc_v3)]
97 PAC_CRC.pol().write_value(self._config.crc_poly);
76 98
99 // configure CR components
100 // (reverse I/O, polysize, poly)
77 PAC_CRC.cr().modify(|w| { 101 PAC_CRC.cr().modify(|w| {
78 // configure reverse output 102 // configure reverse output
79 w.set_rev_out( 103 w.set_rev_out(match self._config.reverse_out {
80 match self._config.reverse_out { 104 true => vals::RevOut::REVERSED,
81 true => { vals::RevOut::REVERSED } 105 false => vals::RevOut::NORMAL,
82 false => { vals::RevOut::NORMAL } 106 });
83 }
84 );
85 // configure reverse input 107 // configure reverse input
86 w.set_rev_in( 108 w.set_rev_in(match self._config.reverse_in {
87 match self._config.reverse_in { 109 CrcInputReverseConfig::None => vals::RevIn::NORMAL,
88 CrcInputReverseConfig::None => { vals::RevIn::NORMAL } 110 CrcInputReverseConfig::Byte => vals::RevIn::BYTE,
89 CrcInputReverseConfig::Byte => { vals::RevIn::BYTE } 111 CrcInputReverseConfig::Halfword => vals::RevIn::HALFWORD,
90 CrcInputReverseConfig::Halfword => { vals::RevIn::HALFWORD } 112 CrcInputReverseConfig::Word => vals::RevIn::WORD,
91 CrcInputReverseConfig::Word => { vals::RevIn::WORD } 113 });
92 }
93 );
94 // configure the polynomial. 114 // configure the polynomial.
95 w.set_polysize( 115 #[cfg(crc_v3)]
96 match self._config.poly_size { 116 w.set_polysize(match self._config.poly_size {
97 PolySize::Width7 => { vals::Polysize::POLYSIZE7 } 117 PolySize::Width7 => vals::Polysize::POLYSIZE7,
98 PolySize::Width8 => { vals::Polysize::POLYSIZE8 } 118 PolySize::Width8 => vals::Polysize::POLYSIZE8,
99 PolySize::Width16 => { vals::Polysize::POLYSIZE16 } 119 PolySize::Width16 => vals::Polysize::POLYSIZE16,
100 PolySize::Width32 => { vals::Polysize::POLYSIZE32 } 120 PolySize::Width32 => vals::Polysize::POLYSIZE32,
101 } 121 });
102 )
103
104 }) 122 })
105 } 123 }
106 124
107 self.reset(); 125 self.reset();
108 } 126 }
109} \ No newline at end of file 127
128 /// Feeds a byte into the CRC peripheral. Returns the computed checksum.
129 pub fn feed_byte(&mut self, byte: u8) -> u32 {
130 unsafe {
131 PAC_CRC.dr8().write_value(byte as u32);
132 PAC_CRC.dr().read()
133 }
134 }
135
136 /// Feeds an slice of bytes into the CRC peripheral. Returns the computed checksum.
137 pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 {
138 for byte in bytes {
139 unsafe { PAC_CRC.dr8().write_value(*byte as u32); }
140 }
141 unsafe {
142 PAC_CRC.dr().read()
143 }
144 }
145 /// Feeds a halfword into the CRC peripheral. Returns the computed checksum.
146 pub fn feed_halfword(&mut self, byte: u16) -> u32 {
147 unsafe {
148 PAC_CRC.dr16().write_value(byte as u32);
149 PAC_CRC.dr().read()
150 }
151 }
152 /// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum.
153 pub fn feed_halfwords(&mut self, bytes: &[u16]) -> u32 {
154 for byte in bytes {
155 unsafe { PAC_CRC.dr16().write_value(*byte as u32); }
156 }
157 unsafe {
158 PAC_CRC.dr().read()
159 }
160 }
161 /// Feeds a halfword into the CRC peripheral. Returns the computed checksum.
162 pub fn feed_word(&mut self, byte: u32) -> u32 {
163 unsafe {
164 PAC_CRC.dr().write_value(byte as u32);
165 PAC_CRC.dr().read()
166 }
167 }
168 /// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum.
169 pub fn feed_words(&mut self, bytes: &[u32]) -> u32 {
170 for byte in bytes {
171 unsafe { PAC_CRC.dr().write_value(*byte as u32); }
172 }
173 unsafe {
174 PAC_CRC.dr().read()
175 }
176 }
177}
diff --git a/embassy-stm32/src/crc/v3.rs b/embassy-stm32/src/crc/v3.rs
deleted file mode 100644
index 8b1378917..000000000
--- a/embassy-stm32/src/crc/v3.rs
+++ /dev/null
@@ -1 +0,0 @@
1