1 /*!
2  * @file        apm32f0xx_crc.c
3  *
4  * @brief       This file provides all the CRC firmware functions
5  *
6  * @version     V1.0.3
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* include */
27 #include "apm32f0xx_crc.h"
28 
29 /** @addtogroup APM32F0xx_StdPeriphDriver
30   @{
31 */
32 
33 /** @addtogroup CRC_Driver
34   @{
35 */
36 
37 /** @defgroup CRC_Macros Macros
38   @{
39 */
40 
41 /**@} end of group CRC_Macros */
42 
43 /** @defgroup CRC_Enumerations Enumerations
44   @{
45 */
46 
47 /**@} end of group CRC_Enumerations */
48 
49 /** @defgroup CRC_Structures Structures
50   @{
51 */
52 
53 /**@} end of group CRC_Structures */
54 
55 /** @defgroup CRC_Variables Variables
56   @{
57 */
58 
59 /**@} end of group CRC_Variables */
60 
61 /** @defgroup CRC_Functions Functions
62   @{
63 */
64 
65 /*!
66  * @brief     Resets the CRC peripheral registers to their default reset values.
67  *
68  * @param     None
69  *
70  * @retval    None
71  */
CRC_Reset(void)72 void CRC_Reset(void)
73 {
74     CRC->DATA = 0xFFFFFFFF;
75     CRC->INDATA = 0x00;
76     CRC->INITVAL = 0xFFFFFFFF;
77     CRC->CTRL = 0x00000000;
78     CRC->POL = 0x04C11DB7;
79 }
80 
81 /*!
82  * @brief     Reset CRC data register (DATA)
83  *
84  * @param     None
85  *
86  * @retval    None
87  */
CRC_ResetDATA(void)88 void CRC_ResetDATA(void)
89 {
90     CRC->CTRL_B.RST = BIT_SET;
91 }
92 
93 /*!
94  * @brief     Set the CRC polynomial size.
95  *
96  * @param     polynomialSize: CRC polynomial size
97  *                         The parameter can be one of following values:
98  *                         @arg CRC_POLYNOMIAL_SIZE_7:   7-bit polynomial for CRC calculation
99  *                         @arg CRC_POLYNOMIAL_SIZE_8:   8-bit polynomial for CRC calculation
100  *                         @arg CRC_POLYNOMIAL_SIZE_16:  16-bit polynomial for CRC calculation
101  *                         @arg CRC_POLYNOMIAL_SIZE_32:  32-bit polynomial for CRC calculation
102  *
103  * @retval    None
104  *
105  * @note      It's only for APM32F072 and APM32F091 devices
106  */
CRC_SetPolynomialSize(CRC_POLYNOMIAL_SIZE_T polynomialSize)107 void CRC_SetPolynomialSize(CRC_POLYNOMIAL_SIZE_T polynomialSize)
108 {
109     CRC->CTRL_B.POLSIZE = polynomialSize;
110 }
111 
112 /*!
113  * @brief     Set the CRC polynomail coefficients.
114  *
115  * @param     polynomialValue: Polynomial to be used for CRC calculation
116  *
117  * @retval    None
118  *
119  * @note      It's only for APM32F072 and APM32F091 devices
120  */
CRC_SetPolynomialValue(uint32_t polynomialValue)121 void CRC_SetPolynomialValue(uint32_t polynomialValue)
122 {
123     CRC->POL = polynomialValue;
124 }
125 
126 /*!
127  * @brief     Selects the reverse operation to be performed on input data
128  *
129  * @param     revInData:   Reverse input data
130  *                         The parameter can be one of following values:
131  *                         @arg CRC_REVERSE_INPUT_DATA_NO:   Bit order not affected
132  *                         @arg CRC_REVERSE_INPUT_DATA_8B:   Bit reversal done by byte
133  *                         @arg CRC_REVERSE_INPUT_DATA_16B:  Bit reversal done by half-word
134  *                         @arg CRC_REVERSE_INPUT_DATA_32B:  Bit reversal done by word
135  *
136  * @retval    None
137  */
CRC_SelectReverseInputData(CRC_REVERSE_INPUT_DATA_T revInData)138 void CRC_SelectReverseInputData(CRC_REVERSE_INPUT_DATA_T revInData)
139 {
140     CRC->CTRL_B.REVI = revInData;
141 }
142 
143 /*!
144  * @brief     Enable the reverse operation on output data
145  *
146  * @param     None
147  *
148  * @retval    None
149  */
CRC_EnableReverseOutputData(void)150 void CRC_EnableReverseOutputData(void)
151 {
152     CRC->CTRL_B.REVO = BIT_SET;
153 }
154 
155 /*!
156  * @brief     Disable the reverse operation on output data
157  *
158  * @param     None
159  *
160  * @retval    None
161  */
CRC_DisableReverseOutputData(void)162 void CRC_DisableReverseOutputData(void)
163 {
164     CRC->CTRL_B.REVO = BIT_RESET;
165 }
166 
167 /*!
168  * @brief     Initializes the INITVAL register.
169  *
170  * @param     initValue: Programmable initial CRC value
171  *
172  * @retval    None
173  */
CRC_WriteInitRegister(uint32_t initValue)174 void CRC_WriteInitRegister(uint32_t initValue)
175 {
176     CRC->INITVAL = initValue;
177 }
178 
179 /*!
180  * @brief     Calculate a 32-bit CRC for a given data word (32 bits)
181  *
182  * @param     data: data word(32-bit) to compute its CRC
183  *
184  * @retval    32-bit CRC
185  */
CRC_CalculateCRC(uint32_t data)186 uint32_t CRC_CalculateCRC(uint32_t data)
187 {
188     CRC->DATA = data;
189 
190     return (CRC->DATA);
191 }
192 
193 /*!
194  * @brief     Calculate a 32-bit CRC for a given data word (16 bits)
195  *
196  * @param     data: data word(16-bit) to compute its CRC
197  *
198  * @retval    16-bit CRC
199  *
200  * @note      It's only for APM32F072 and APM32F091 devices
201  */
CRC_CalculateCRC16bits(uint16_t data)202 uint32_t CRC_CalculateCRC16bits(uint16_t data)
203 {
204     *(uint16_t*)(CRC_BASE) = (uint16_t) data;
205 
206     return (CRC->DATA);
207 }
208 
209 /*!
210  * @brief     Calculate a 32-bit CRC for a given data word (8 bits)
211  *
212  * @param     data: data word(8-bit) to compute its CRC
213  *
214  * @retval    8-bit CRC
215  *
216  * @note      It's only for APM32F072 and APM32F091 devices
217  */
CRC_CalculateCRC8bits(uint8_t data)218 uint32_t CRC_CalculateCRC8bits(uint8_t data)
219 {
220     *(uint8_t*)(CRC_BASE) = (uint8_t) data;
221 
222     return (CRC->DATA);
223 }
224 
225 /*!
226  * @brief     Computes the 32-bit CRC of a given buffer of data word(32-bit)
227  *
228  * @param     pBuffer: Pointer to the buffer containing the data to be computed
229  *
230  * @param     bufferLength: buffer length
231  *
232  * @retval    32-bit CRC
233  */
CRC_CalculateBlockCRC(uint32_t pBuffer[],uint32_t bufferLength)234 uint32_t CRC_CalculateBlockCRC(uint32_t pBuffer[], uint32_t bufferLength)
235 {
236     uint32_t index = 0;
237 
238     for (index = 0; index < bufferLength; index++)
239     {
240         CRC->DATA = pBuffer[index];
241     }
242 
243     return (CRC->DATA);
244 }
245 
246 /*!
247  * @brief     Returns the current CRC value
248  *
249  * @param     None
250  *
251  * @retval    32-bit CRC
252  */
CRC_ReadCRC(void)253 uint32_t CRC_ReadCRC(void)
254 {
255     return (CRC->DATA);
256 }
257 
258 /*!
259  * @brief     Stores a 8-bit data in the Independent Data(INDATA) register
260  *
261  * @param     IDValue: 8-bit value to be stored in the INDATA register
262  *
263  * @retval    None
264  */
CRC_WriteIDRegister(uint8_t IDValue)265 void CRC_WriteIDRegister(uint8_t IDValue)
266 {
267     CRC->INDATA = IDValue;
268 }
269 
270 /*!
271  * @brief      Returns a 8-bit data stored in the Independent Data(INDATA) register
272  *
273  * @param      None
274  *
275  * @retval     8-bit value of the INDATA register
276  */
CRC_ReadIDRegister(void)277 uint8_t CRC_ReadIDRegister(void)
278 {
279     return (CRC->INDATA);
280 }
281 
282 /**@} end of group CRC_Functions */
283 /**@} end of group CRC_Driver */
284 /**@} end of group APM32F0xx_StdPeriphDriver */
285