summaryrefslogtreecommitdiff
path: root/embassy-stm32/src/crc/v2v3.rs
blob: 63f24e4e1b22b9b3d3201ea6d310ab075a1e19b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use core::marker::PhantomData;

use embassy_hal_common::unborrow;

use crate::pac::crc::vals;
use crate::pac::CRC as PAC_CRC;
use crate::peripherals::CRC;
use crate::rcc::sealed::RccPeripheral;
use crate::Unborrow;

pub struct Crc<'d> {
    _peripheral: CRC,
    _phantom: PhantomData<&'d mut CRC>,
    _config: Config,
}

pub enum ConfigError {
    InvalidPolynomial,
}

pub struct Config {
    reverse_in: InputReverseConfig,
    reverse_out: bool,
    #[cfg(crc_v3)]
    poly_size: PolySize,
    crc_init_value: u32,
    #[cfg(crc_v3)]
    crc_poly: u32,
}

pub enum InputReverseConfig {
    None,
    Byte,
    Halfword,
    Word,
}

impl Config {
    pub fn new(
        reverse_in: InputReverseConfig,
        reverse_out: bool,
        #[cfg(crc_v3)] poly_size: PolySize,
        crc_init_value: u32,
        #[cfg(crc_v3)] crc_poly: u32,
    ) -> Result<Self, ConfigError> {
        // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported.
        #[cfg(crc_v3)]
        if crc_poly % 2 == 0 {
            return Err(ConfigError::InvalidPolynomial);
        }
        Ok(Config {
            reverse_in,
            reverse_out,
            #[cfg(crc_v3)]
            poly_size,
            crc_init_value,
            #[cfg(crc_v3)]
            crc_poly,
        })
    }
}

#[cfg(crc_v3)]
pub enum PolySize {
    Width7,
    Width8,
    Width16,
    Width32,
}

impl<'d> Crc<'d> {
    /// Instantiates the CRC32 peripheral and initializes it to default values.
    pub fn new(peripheral: impl Unborrow<Target = CRC> + 'd, config: Config) -> Self {
        // Note: enable and reset come from RccPeripheral.
        // enable CRC clock in RCC.
        CRC::enable();
        // Reset CRC to default values.
        CRC::reset();
        unborrow!(peripheral);
        let mut instance = Self {
            _peripheral: peripheral,
            _phantom: PhantomData,
            _config: config,
        };
        CRC::reset();
        instance.reconfigure();
        instance.reset();
        instance
    }

    pub fn reset(&mut self) {
        unsafe {
            PAC_CRC.cr().modify(|w| w.set_reset(true));
        }
    }

    /// Reconfigures the CRC peripheral. Doesn't reset.
    fn reconfigure(&mut self) {
        unsafe {
            // Init CRC value
            PAC_CRC.init().write_value(self._config.crc_init_value);
            #[cfg(crc_v3)]
            PAC_CRC.pol().write_value(self._config.crc_poly);

            // configure CR components
            // (reverse I/O, polysize, poly)
            PAC_CRC.cr().write(|w| {
                // configure reverse output
                w.set_rev_out(match self._config.reverse_out {
                    true => vals::RevOut::REVERSED,
                    false => vals::RevOut::NORMAL,
                });
                // configure reverse input
                w.set_rev_in(match self._config.reverse_in {
                    InputReverseConfig::None => vals::RevIn::NORMAL,
                    InputReverseConfig::Byte => vals::RevIn::BYTE,
                    InputReverseConfig::Halfword => vals::RevIn::HALFWORD,
                    InputReverseConfig::Word => vals::RevIn::WORD,
                });
                // configure the polynomial.
                #[cfg(crc_v3)]
                w.set_polysize(match self._config.poly_size {
                    PolySize::Width7 => vals::Polysize::POLYSIZE7,
                    PolySize::Width8 => vals::Polysize::POLYSIZE8,
                    PolySize::Width16 => vals::Polysize::POLYSIZE16,
                    PolySize::Width32 => vals::Polysize::POLYSIZE32,
                });
            })
        }

        self.reset();
    }

    /// Feeds a byte into the CRC peripheral. Returns the computed checksum.
    pub fn feed_byte(&mut self, byte: u8) -> u32 {
        unsafe {
            PAC_CRC.dr8().write_value(byte);
            PAC_CRC.dr().read()
        }
    }

    /// Feeds an slice of bytes into the CRC peripheral. Returns the computed checksum.
    pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 {
        for byte in bytes {
            unsafe {
                PAC_CRC.dr8().write_value(*byte);
            }
        }
        unsafe { PAC_CRC.dr().read() }
    }
    /// Feeds a halfword into the CRC peripheral. Returns the computed checksum.
    pub fn feed_halfword(&mut self, halfword: u16) -> u32 {
        unsafe {
            PAC_CRC.dr16().write_value(halfword);
            PAC_CRC.dr().read()
        }
    }
    /// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum.
    pub fn feed_halfwords(&mut self, halfwords: &[u16]) -> u32 {
        for halfword in halfwords {
            unsafe {
                PAC_CRC.dr16().write_value(*halfword);
            }
        }
        unsafe { PAC_CRC.dr().read() }
    }
    /// Feeds a words into the CRC peripheral. Returns the computed checksum.
    pub fn feed_word(&mut self, word: u32) -> u32 {
        unsafe {
            PAC_CRC.dr().write_value(word as u32);
            PAC_CRC.dr().read()
        }
    }
    /// Feeds an slice of words into the CRC peripheral. Returns the computed checksum.
    pub fn feed_words(&mut self, words: &[u32]) -> u32 {
        for word in words {
            unsafe {
                PAC_CRC.dr().write_value(*word as u32);
            }
        }
        unsafe { PAC_CRC.dr().read() }
    }
}