1 /********************************** (C) COPYRIGHT  *******************************
2  * File Name          : ch32v10x_crc.c
3  * Author             : WCH
4  * Version            : V1.0.0
5  * Date               : 2020/04/30
6  * Description        : This file provides all the CRC firmware functions.
7  * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
8  * SPDX-License-Identifier: Apache-2.0
9  *******************************************************************************/
10 #include "ch32v10x_crc.h"
11 
12 /*********************************************************************
13  * @fn      CRC_ResetDR
14  *
15  * @brief   Resets the CRC Data register (DR).
16  *
17  * @return  none
18  */
CRC_ResetDR(void)19 void CRC_ResetDR(void)
20 {
21     CRC->CTLR = CRC_CTLR_RESET;
22 }
23 
24 /*********************************************************************
25  * @fn      CRC_CalcCRC
26  *
27  * @brief   Computes the 32-bit CRC of a given data word(32-bit).
28  *
29  * @param   Data - data word(32-bit) to compute its CRC.
30  *
31  * @return  32-bit CRC.
32  */
CRC_CalcCRC(uint32_t Data)33 uint32_t CRC_CalcCRC(uint32_t Data)
34 {
35     CRC->DATAR = Data;
36 
37     return (CRC->DATAR);
38 }
39 
40 /*********************************************************************
41  * @fn      CRC_CalcBlockCRC
42  *
43  * @brief   Computes the 32-bit CRC of a given buffer of data word(32-bit).
44  *
45  * @param   pBuffer - pointer to the buffer containing the data to be computed.
46  *          BufferLength - length of the buffer to be computed.
47  *
48  * @return  32-bit CRC.
49  */
CRC_CalcBlockCRC(uint32_t pBuffer[],uint32_t BufferLength)50 uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
51 {
52     uint32_t index = 0;
53 
54     for(index = 0; index < BufferLength; index++)
55     {
56         CRC->DATAR = pBuffer[index];
57     }
58 
59     return (CRC->DATAR);
60 }
61 
62 /*********************************************************************
63  * @fn      CRC_GetCRC
64  *
65  * @brief   Returns the current CRC value.
66  *
67  * @return  32-bit CRC.
68  */
CRC_GetCRC(void)69 uint32_t CRC_GetCRC(void)
70 {
71     return (CRC->IDATAR);
72 }
73 
74 /*********************************************************************
75  * @fn      CRC_SetIDRegister
76  *
77  * @brief   Stores a 8-bit data in the Independent Data(ID) register.
78  *
79  * @param   IDValue - 8-bit value to be stored in the ID register.
80  *
81  * @return  none
82  */
CRC_SetIDRegister(uint8_t IDValue)83 void CRC_SetIDRegister(uint8_t IDValue)
84 {
85     CRC->IDATAR = IDValue;
86 }
87 
88 /*********************************************************************
89  * @fn      CRC_GetIDRegister
90  *
91  * @brief   Returns the 8-bit data stored in the Independent Data(ID) register.
92  *
93  * @return  8-bit value of the ID register.
94  */
CRC_GetIDRegister(void)95 uint8_t CRC_GetIDRegister(void)
96 {
97     return (CRC->IDATAR);
98 }
99 
100 
101 
102 
103 
104