1 /******************************************************************************
2 *Copyright(C)2017, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 
43 /** \file crc.c
44  **
45  ** Common API of crc.
46  ** @link crcGroup Some description @endlink
47  **
48  **   - 2017-05-16
49  **
50  ******************************************************************************/
51 
52 /*******************************************************************************
53  * Include files
54  ******************************************************************************/
55 #include "crc.h"
56 /**
57  *******************************************************************************
58  ** \addtogroup CrcGroup
59  ******************************************************************************/
60 //@{
61 
62 /*******************************************************************************
63  * Local pre-processor symbols/macros ('#define')
64  ******************************************************************************/
65 
66 /*******************************************************************************
67  * Global variable definitions (declared in header file with 'extern')
68  ******************************************************************************/
69 
70 /*******************************************************************************
71  * Local type definitions ('typedef')
72  ******************************************************************************/
73 
74 /*******************************************************************************
75  * Local variable definitions ('static')
76  ******************************************************************************/
77 
78 /*******************************************************************************
79  * Local function prototypes ('static')
80  ******************************************************************************/
81 
82 
83 /*******************************************************************************
84  * Function implementation - global ('extern') and local ('static')
85  ******************************************************************************/
86 
87 /**
88  *****************************************************************************
89  ** \brief CRC16 编码(字节填充方式)
90  **
91  ** 该函数主要用于生成CRC16编码.
92  **
93  ** \param [in]  pu8Data          待编码数据指针(字节方式输入)
94  ** \param [in]  u32Len           待编码数据长度(字节数)
95  **
96  ** \retval CRC16                 CRC16编码值.
97  *****************************************************************************/
CRC16_Get8(uint8_t * pu8Data,uint32_t u32Len)98 uint16_t CRC16_Get8(uint8_t* pu8Data, uint32_t u32Len)
99 {
100     uint32_t u32Index = 0;
101 
102     M0P_CRC->CR_f.CR = 0;
103     M0P_CRC->RESULT = 0xFFFF;
104     for(u32Index = 0;u32Index<u32Len;u32Index++)
105     {
106             *((volatile uint8_t*)(&(M0P_CRC->DATA)))  = pu8Data[u32Index];
107     }
108 
109     return (M0P_CRC->RESULT_f.RESULT);
110 }
111 
112 /**
113  *****************************************************************************
114  ** \brief CRC16 编码(半字填充方式)
115  **
116  ** 该函数主要用于生成CRC16编码.
117  **
118  ** \param [in]  pu16Data          待编码数据指针(半字方式输入)
119  ** \param [in]  u32Len           待编码数据长度(半字数)
120  **
121  ** \retval CRC16                 CRC16编码值.
122  *****************************************************************************/
CRC16_Get16(uint16_t * pu16Data,uint32_t u32Len)123 uint16_t CRC16_Get16(uint16_t* pu16Data, uint32_t u32Len)
124 {
125     uint32_t u32Index = 0;
126 
127     M0P_CRC->CR_f.CR = 0;
128     M0P_CRC->RESULT_f.RESULT = 0xFFFF;
129     for (u32Index=0; u32Index<u32Len; u32Index++)
130     {
131         *((volatile uint16_t*)(&(M0P_CRC->DATA))) = pu16Data[u32Index];
132     }
133 
134     return (M0P_CRC->RESULT_f.RESULT);
135 }
136 
137 /**
138  *****************************************************************************
139  ** \brief CRC16 编码(字填充方式)
140  **
141  ** 该函数主要用于生成CRC16编码.
142  **
143  ** \param [in]  pu32Data          待编码数据指针(字方式输入)
144  ** \param [in]  u32Len            待编码数据长度(字数)
145  **
146  ** \retval CRC16                  CRC16编码值.
147  *****************************************************************************/
CRC16_Get32(uint32_t * pu32Data,uint32_t u32Len)148 uint16_t CRC16_Get32(uint32_t* pu32Data, uint32_t u32Len)
149 {
150     uint32_t u32Index = 0;
151 
152 	M0P_CRC->CR_f.CR = 0;
153     M0P_CRC->RESULT_f.RESULT = 0xFFFF;
154     for (u32Index=0; u32Index<u32Len; u32Index++)
155     {
156         M0P_CRC->DATA_f.DATA = pu32Data[u32Index];
157     }
158 
159     return (M0P_CRC->RESULT_f.RESULT);
160 }
161 
162 /**
163  *****************************************************************************
164  ** \brief CRC16 校验(字节填充方式)
165  **
166  ** 该函数主要用于对数据及CRC16值进行校验.
167  **
168  ** \param [in]  pu8Data          待校验数据指针(字节方式输入)
169  ** \param [in]  u32Len           待校验数据长度(字节数)
170  ** \param [in]  u16CRC           待校验CRC16值
171  **
172  ** \retval Ok                    CRC校验正确
173  ** \retval Error                 CRC校验错误
174  *****************************************************************************/
CRC16_Check8(uint8_t * pu8Data,uint32_t u32Len,uint16_t u16CRC)175 en_result_t CRC16_Check8(uint8_t* pu8Data, uint32_t u32Len, uint16_t u16CRC)
176 {
177     en_result_t enResult = Ok;
178     uint32_t    u32Index = 0;
179 
180     M0P_CRC->CR_f.CR = 0;
181     M0P_CRC->RESULT_f.RESULT = 0xFFFF;
182     for (u32Index=0; u32Index<u32Len; u32Index++)
183     {
184         *((volatile uint8_t*)(&(M0P_CRC->DATA))) = pu8Data[u32Index];
185     }
186 
187      *((volatile uint8_t*)(&(M0P_CRC->DATA))) = (uint8_t)((((uint32_t)u16CRC)>>0)&0xFF);
188      *((volatile uint8_t*)(&(M0P_CRC->DATA))) = (uint8_t)(((uint32_t)u16CRC>>8)&0xFF);
189 
190     enResult = M0P_CRC->CR_f.FLAG ? Ok : Error;
191 
192     return (enResult);
193 }
194 
195 /**
196  *****************************************************************************
197  ** \brief CRC16 校验(半字填充方式)
198  **
199  ** 该函数主要用于对数据及CRC16值进行校验.
200  **
201  ** \param [in]  pu16Data         待校验数据指针(半字方式输入)
202  ** \param [in]  u32Len           待校验数据长度(半字数)
203  ** \param [in]  u16CRC           待校验CRC16值
204  **
205  ** \retval Ok                    CRC校验正确
206  ** \retval Error                 CRC校验错误
207  *****************************************************************************/
CRC16_Check16(uint16_t * pu16Data,uint32_t u32Len,uint16_t u16CRC)208 en_result_t CRC16_Check16(uint16_t* pu16Data, uint32_t u32Len, uint16_t u16CRC)
209 {
210     en_result_t enResult = Ok;
211     uint32_t    u32Index = 0;
212 
213     M0P_CRC->CR_f.CR = 0;
214     M0P_CRC->RESULT_f.RESULT = 0xFFFF;
215     for (u32Index=0; u32Index<u32Len; u32Index++)
216     {
217         *((volatile uint16_t*)(&(M0P_CRC->DATA))) = pu16Data[u32Index];
218     }
219 
220      *((volatile uint16_t*)(&(M0P_CRC->DATA))) = u16CRC;
221 
222     enResult = M0P_CRC->CR_f.FLAG ? Ok : Error;
223 
224     return (enResult);
225 }
226 
227 /**
228  *****************************************************************************
229  ** \brief CRC16 校验(字填充方式)
230  **
231  ** 该函数主要用于对数据及CRC16值进行校验.
232  **
233  ** \param [in]  pu32Data         待校验数据指针(字方式输入)
234  ** \param [in]  u32Len           待校验数据长度(字数)
235  ** \param [in]  u16CRC           待校验CRC16值
236  **
237  ** \retval Ok                    CRC校验正确
238  ** \retval Error                 CRC校验错误
239  *****************************************************************************/
CRC16_Check32(uint32_t * pu32Data,uint32_t u32Len,uint16_t u16CRC)240 en_result_t CRC16_Check32(uint32_t* pu32Data, uint32_t u32Len, uint16_t u16CRC)
241 {
242     en_result_t enResult = Ok;
243     uint32_t    u32Index = 0;
244 
245     M0P_CRC->CR_f.CR = 0;
246     M0P_CRC->RESULT_f.RESULT = 0xFFFFFFFFu;
247     for (u32Index=0; u32Index<u32Len; u32Index++)
248     {
249         *((volatile uint32_t*)(&(M0P_CRC->DATA))) = pu32Data[u32Index];
250     }
251 
252      *((volatile uint16_t*)(&(M0P_CRC->DATA))) = ((uint16_t)u16CRC);
253 
254     enResult = M0P_CRC->CR_f.FLAG ? Ok : Error;
255 
256     return (enResult);
257 }
258 
259 /**
260  *****************************************************************************
261  ** \brief CRC16 编码(字节填充方式)
262  **
263  ** 该函数主要用于生成CRC16编码.
264  **
265  ** \param [in]  pu8Data          待编码数据指针(字节方式输入)
266  ** \param [in]  u32Len           待编码数据长度(字节数)
267  **
268  ** \retval CRC16                 CRC16编码值.
269  *****************************************************************************/
CRC32_Get8(uint8_t * pu8Data,uint32_t u32Len)270 uint32_t CRC32_Get8(uint8_t* pu8Data, uint32_t u32Len)
271 {
272     uint32_t u32Index = 0;
273 
274     M0P_CRC->CR_f.CR = 1;
275     M0P_CRC->RESULT = 0xFFFFFFFFu;
276     for(u32Index = 0;u32Index<u32Len;u32Index++)
277     {
278             *((volatile uint8_t*)(&(M0P_CRC->DATA)))  = pu8Data[u32Index];
279     }
280 
281     return (M0P_CRC->RESULT_f.RESULT);
282 }
283 
284 /**
285  *****************************************************************************
286  ** \brief CRC16 编码(半字填充方式)
287  **
288  ** 该函数主要用于生成CRC16编码.
289  **
290  ** \param [in]  pu16Data          待编码数据指针(半字方式输入)
291  ** \param [in]  u32Len           待编码数据长度(半字数)
292  **
293  ** \retval CRC16                 CRC16编码值.
294  *****************************************************************************/
CRC32_Get16(uint16_t * pu16Data,uint32_t u32Len)295 uint32_t CRC32_Get16(uint16_t* pu16Data, uint32_t u32Len)
296 {
297     uint32_t u32Index = 0;
298 
299     M0P_CRC->CR_f.CR = 1;
300     M0P_CRC->RESULT_f.RESULT = 0xFFFFFFFFu;
301     for (u32Index=0; u32Index<u32Len; u32Index++)
302     {
303         *((volatile uint16_t*)(&(M0P_CRC->DATA))) = pu16Data[u32Index];
304     }
305 
306     return (M0P_CRC->RESULT_f.RESULT);
307 }
308 
309 /**
310  *****************************************************************************
311  ** \brief CRC16 编码(字填充方式)
312  **
313  ** 该函数主要用于生成CRC16编码.
314  **
315  ** \param [in]  pu32Data          待编码数据指针(字方式输入)
316  ** \param [in]  u32Len            待编码数据长度(字数)
317  **
318  ** \retval CRC16                  CRC16编码值.
319  *****************************************************************************/
CRC32_Get32(uint32_t * pu32Data,uint32_t u32Len)320 uint32_t CRC32_Get32(uint32_t* pu32Data, uint32_t u32Len)
321 {
322     uint32_t u32Index = 0;
323 
324     M0P_CRC->CR_f.CR = 1;
325     M0P_CRC->RESULT_f.RESULT = 0xFFFFFFFFu;
326     for (u32Index=0; u32Index<u32Len; u32Index++)
327     {
328         M0P_CRC->DATA_f.DATA = pu32Data[u32Index];
329     }
330 
331     return (M0P_CRC->RESULT_f.RESULT);
332 }
333 
334 /**
335  *****************************************************************************
336  ** \brief CRC16 校验(字节填充方式)
337  **
338  ** 该函数主要用于对数据及CRC16值进行校验.
339  **
340  ** \param [in]  pu8Data          待校验数据指针(字节方式输入)
341  ** \param [in]  u32Len           待校验数据长度(字节数)
342  ** \param [in]  u16CRC           待校验CRC16值
343  **
344  ** \retval Ok                    CRC校验正确
345  ** \retval Error                 CRC校验错误
346  *****************************************************************************/
CRC32_Check8(uint8_t * pu8Data,uint32_t u32Len,uint32_t u32CRC)347 en_result_t CRC32_Check8(uint8_t* pu8Data, uint32_t u32Len, uint32_t u32CRC)
348 {
349     en_result_t enResult = Ok;
350     uint32_t    u32Index = 0;
351 
352     M0P_CRC->CR_f.CR = 1;
353     M0P_CRC->RESULT_f.RESULT = 0xFFFFFFFFu;
354     for (u32Index=0; u32Index<u32Len; u32Index++)
355     {
356         *((volatile uint8_t*)(&(M0P_CRC->DATA))) = pu8Data[u32Index];
357     }
358 
359      *((volatile uint8_t*)(&(M0P_CRC->DATA))) = (uint8_t)((u32CRC>>0)&0xFF);
360      *((volatile uint8_t*)(&(M0P_CRC->DATA))) = (uint8_t)((u32CRC>>8)&0xFF);
361      *((volatile uint8_t*)(&(M0P_CRC->DATA))) = (uint8_t)((u32CRC>>16)&0xFF);
362      *((volatile uint8_t*)(&(M0P_CRC->DATA))) = (uint8_t)((u32CRC>>24)&0xFF);
363 
364     enResult = M0P_CRC->CR_f.FLAG ? Ok : Error;
365 
366     return (enResult);
367 }
368 
369 /**
370  *****************************************************************************
371  ** \brief CRC16 校验(半字填充方式)
372  **
373  ** 该函数主要用于对数据及CRC16值进行校验.
374  **
375  ** \param [in]  pu16Data         待校验数据指针(半字方式输入)
376  ** \param [in]  u32Len           待校验数据长度(半字数)
377  ** \param [in]  u16CRC           待校验CRC16值
378  **
379  ** \retval Ok                    CRC校验正确
380  ** \retval Error                 CRC校验错误
381  *****************************************************************************/
CRC32_Check16(uint16_t * pu16Data,uint32_t u32Len,uint32_t u32CRC)382 en_result_t CRC32_Check16(uint16_t* pu16Data, uint32_t u32Len, uint32_t u32CRC)
383 {
384     en_result_t enResult = Ok;
385     uint32_t    u32Index = 0;
386 
387     M0P_CRC->CR_f.CR = 1;
388     M0P_CRC->RESULT_f.RESULT = 0xFFFFFFFFu;
389     for (u32Index=0; u32Index<u32Len; u32Index++)
390     {
391         *((volatile uint16_t*)(&(M0P_CRC->DATA))) = pu16Data[u32Index];
392     }
393 
394      *((volatile uint16_t*)(&(M0P_CRC->DATA))) = (uint16_t)((u32CRC>>0)&0xFFFF);
395      *((volatile uint16_t*)(&(M0P_CRC->DATA))) = (uint16_t)((u32CRC>>16)&0xFFFF);
396 
397 
398     enResult = M0P_CRC->CR_f.FLAG ? Ok : Error;
399 
400     return (enResult);
401 }
402 
403 /**
404  *****************************************************************************
405  ** \brief CRC16 校验(字填充方式)
406  **
407  ** 该函数主要用于对数据及CRC16值进行校验.
408  **
409  ** \param [in]  pu32Data         待校验数据指针(字方式输入)
410  ** \param [in]  u32Len           待校验数据长度(字数)
411  ** \param [in]  u16CRC           待校验CRC16值
412  **
413  ** \retval Ok                    CRC校验正确
414  ** \retval Error                 CRC校验错误
415  *****************************************************************************/
CRC32_Check32(uint32_t * pu32Data,uint32_t u32Len,uint32_t u32CRC)416 en_result_t CRC32_Check32(uint32_t* pu32Data, uint32_t u32Len, uint32_t u32CRC)
417 {
418     en_result_t enResult = Ok;
419     uint32_t    u32Index = 0;
420 
421     M0P_CRC->CR_f.CR = 1;
422     M0P_CRC->RESULT_f.RESULT = 0xFFFFFFFFu;
423     for (u32Index=0; u32Index<u32Len; u32Index++)
424     {
425         *((volatile uint32_t*)(&(M0P_CRC->DATA))) = pu32Data[u32Index];
426     }
427 
428      *((volatile uint32_t*)(&(M0P_CRC->DATA))) = u32CRC;
429 
430     enResult = M0P_CRC->CR_f.FLAG ? Ok : Error;
431 
432     return (enResult);
433 }
434 //@} // CrcGroup
435 
436 /*******************************************************************************
437  * EOF (not truncated)
438  ******************************************************************************/
439