aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Salzedo <[email protected]>2021-09-27 10:38:55 -0700
committerJoshua Salzedo <[email protected]>2021-09-27 10:38:55 -0700
commit43ad28b9f9b25f2b7369be33dfd1f7db5d34e330 (patch)
tree84c0b8ab5c0854ce70ccf2482746e43ba86c4dc4
parent7392e33ad563642453bde20c9d33120af6b7ccbb (diff)
Use unborrow for CRC constructor
sort feature gates fix repetition in CRC config names
-rw-r--r--embassy-stm32/src/crc/mod.rs4
-rw-r--r--embassy-stm32/src/crc/v1.rs7
-rw-r--r--embassy-stm32/src/crc/v2v3.rs (renamed from embassy-stm32/src/crc/v2.rs)33
3 files changed, 26 insertions, 18 deletions
diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs
index 70c87ab53..2ebebc935 100644
--- a/embassy-stm32/src/crc/mod.rs
+++ b/embassy-stm32/src/crc/mod.rs
@@ -1,6 +1,6 @@
1#[cfg_attr(crc_v2, path = "v2.rs")]
2#[cfg_attr(crc_v1, path = "v1.rs")] 1#[cfg_attr(crc_v1, path = "v1.rs")]
3#[cfg_attr(crc_v3, path = "v2.rs")] 2#[cfg_attr(crc_v2, path = "v2v3.rs")]
3#[cfg_attr(crc_v3, path = "v2v3.rs")]
4mod _version; 4mod _version;
5 5
6pub use _version::Crc; 6pub use _version::Crc;
diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs
index 09c8eecb6..8431c07f6 100644
--- a/embassy-stm32/src/crc/v1.rs
+++ b/embassy-stm32/src/crc/v1.rs
@@ -1,6 +1,9 @@
1use crate::pac::CRC as PAC_CRC; 1use crate::pac::CRC as PAC_CRC;
2use crate::peripherals::CRC; 2use crate::peripherals::CRC;
3use crate::rcc::sealed::RccPeripheral; 3use crate::rcc::sealed::RccPeripheral;
4use embassy_hal_common::unborrow;
5use embassy::util::Unborrow;
6
4 7
5pub struct Crc { 8pub struct Crc {
6 _peripheral: CRC, 9 _peripheral: CRC,
@@ -8,12 +11,14 @@ pub struct Crc {
8 11
9impl Crc { 12impl Crc {
10 /// Instantiates the CRC32 peripheral and initializes it to default values. 13 /// Instantiates the CRC32 peripheral and initializes it to default values.
11 pub fn new(peripheral: CRC) -> Self { 14 pub fn new(peripheral: impl Unborrow<Target= CRC>) -> Self {
12 // Note: enable and reset come from RccPeripheral. 15 // Note: enable and reset come from RccPeripheral.
13 // enable CRC clock in RCC. 16 // enable CRC clock in RCC.
14 CRC::enable(); 17 CRC::enable();
15 // Reset CRC to default values. 18 // Reset CRC to default values.
16 CRC::reset(); 19 CRC::reset();
20 // Unborrow the peripheral
21 unborrow!(peripheral);
17 let mut instance = Self { 22 let mut instance = Self {
18 _peripheral: peripheral, 23 _peripheral: peripheral,
19 }; 24 };
diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2v3.rs
index 7705b4285..91c24215c 100644
--- a/embassy-stm32/src/crc/v2.rs
+++ b/embassy-stm32/src/crc/v2v3.rs
@@ -2,18 +2,20 @@ use crate::pac::crc::vals;
2use crate::pac::CRC as PAC_CRC; 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;
5use embassy_hal_common::unborrow;
6use embassy::util::Unborrow;
5 7
6pub struct Crc { 8pub struct Crc {
7 _peripheral: CRC, 9 _peripheral: CRC,
8 _config: CrcConfig, 10 _config: Config,
9} 11}
10 12
11pub enum CrcConfigError { 13pub enum ConfigError {
12 InvalidPolynomial, 14 InvalidPolynomial,
13} 15}
14 16
15pub struct CrcConfig { 17pub struct Config {
16 reverse_in: CrcInputReverseConfig, 18 reverse_in: InputReverseConfig,
17 reverse_out: bool, 19 reverse_out: bool,
18 #[cfg(crc_v3)] 20 #[cfg(crc_v3)]
19 poly_size: PolySize, 21 poly_size: PolySize,
@@ -22,27 +24,27 @@ pub struct CrcConfig {
22 crc_poly: u32, 24 crc_poly: u32,
23} 25}
24 26
25pub enum CrcInputReverseConfig { 27pub enum InputReverseConfig {
26 None, 28 None,
27 Byte, 29 Byte,
28 Halfword, 30 Halfword,
29 Word, 31 Word,
30} 32}
31 33
32impl CrcConfig { 34impl Config {
33 pub fn new( 35 pub fn new(
34 reverse_in: CrcInputReverseConfig, 36 reverse_in: InputReverseConfig,
35 reverse_out: bool, 37 reverse_out: bool,
36 #[cfg(crc_v3)] poly_size: PolySize, 38 #[cfg(crc_v3)] poly_size: PolySize,
37 crc_init_value: u32, 39 crc_init_value: u32,
38 #[cfg(crc_v3)] crc_poly: u32, 40 #[cfg(crc_v3)] crc_poly: u32,
39 ) -> Result<Self, CrcConfigError> { 41 ) -> Result<Self, ConfigError> {
40 // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. 42 // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported.
41 #[cfg(crc_v3)] 43 #[cfg(crc_v3)]
42 if crc_poly % 2 == 0 { 44 if crc_poly % 2 == 0 {
43 return Err(CrcConfigError::InvalidPolynomial); 45 return Err(ConfigError::InvalidPolynomial);
44 } 46 }
45 Ok(CrcConfig { 47 Ok(Config {
46 reverse_in, 48 reverse_in,
47 reverse_out, 49 reverse_out,
48 #[cfg(crc_v3)] 50 #[cfg(crc_v3)]
@@ -64,12 +66,13 @@ pub enum PolySize {
64 66
65impl Crc { 67impl Crc {
66 /// Instantiates the CRC32 peripheral and initializes it to default values. 68 /// Instantiates the CRC32 peripheral and initializes it to default values.
67 pub fn new(peripheral: CRC, config: CrcConfig) -> Self { 69 pub fn new(peripheral: impl Unborrow<Target= CRC>, config: Config) -> Self {
68 // Note: enable and reset come from RccPeripheral. 70 // Note: enable and reset come from RccPeripheral.
69 // enable CRC clock in RCC. 71 // enable CRC clock in RCC.
70 CRC::enable(); 72 CRC::enable();
71 // Reset CRC to default values. 73 // Reset CRC to default values.
72 CRC::reset(); 74 CRC::reset();
75 unborrow!(peripheral);
73 let mut instance = Self { 76 let mut instance = Self {
74 _peripheral: peripheral, 77 _peripheral: peripheral,
75 _config: config, 78 _config: config,
@@ -104,10 +107,10 @@ impl Crc {
104 }); 107 });
105 // configure reverse input 108 // configure reverse input
106 w.set_rev_in(match self._config.reverse_in { 109 w.set_rev_in(match self._config.reverse_in {
107 CrcInputReverseConfig::None => vals::RevIn::NORMAL, 110 InputReverseConfig::None => vals::RevIn::NORMAL,
108 CrcInputReverseConfig::Byte => vals::RevIn::BYTE, 111 InputReverseConfig::Byte => vals::RevIn::BYTE,
109 CrcInputReverseConfig::Halfword => vals::RevIn::HALFWORD, 112 InputReverseConfig::Halfword => vals::RevIn::HALFWORD,
110 CrcInputReverseConfig::Word => vals::RevIn::WORD, 113 InputReverseConfig::Word => vals::RevIn::WORD,
111 }); 114 });
112 // configure the polynomial. 115 // configure the polynomial.
113 #[cfg(crc_v3)] 116 #[cfg(crc_v3)]