1 /*!
2     \file    gd32e50x_crc.c
3     \brief   CRC driver
4 
5     \version 2020-03-10, V1.0.0, firmware for GD32E50x
6     \version 2020-08-26, V1.1.0, firmware for GD32E50x
7     \version 2021-03-23, V1.2.0, firmware for GD32E50x
8 */
9 
10 /*
11     Copyright (c) 2021, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #include "gd32e50x_crc.h"
38 
39 /*!
40     \brief      deinitialize CRC calculation unit
41     \param[in]  none
42     \param[out] none
43     \retval     none
44 */
crc_deinit(void)45 void crc_deinit(void)
46 {
47     CRC_IDATA = (uint32_t)0xFFFFFFFFU;
48     CRC_DATA  = (uint32_t)0xFFFFFFFFU;
49     CRC_FDATA = (uint32_t)0x00000000U;
50     CRC_POLY  = (uint32_t)0x04C11DB7U;
51     CRC_CTL   = CRC_CTL_RST;
52 }
53 
54 /*!
55     \brief      reset data register to the value of initializaiton data register
56     \param[in]  none
57     \param[out] none
58     \retval     none
59 */
crc_data_register_reset(void)60 void crc_data_register_reset(void)
61 {
62     CRC_CTL |= (uint32_t)CRC_CTL_RST;
63 }
64 
65 /*!
66     \brief      enable the reverse operation of output data
67     \param[in]  none
68     \param[out] none
69     \retval     none
70 */
crc_reverse_output_data_enable(void)71 void crc_reverse_output_data_enable(void)
72 {
73     CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
74     CRC_CTL |= (uint32_t)CRC_CTL_REV_O;
75 }
76 
77 /*!
78     \brief      disable the reverse operation of output data
79     \param[in]  none
80     \param[out] none
81     \retval     none
82 */
crc_reverse_output_data_disable(void)83 void crc_reverse_output_data_disable(void)
84 {
85     CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
86 }
87 
88 /*!
89     \brief      configure the CRC input data function
90     \param[in]  data_reverse: specify input data reverse function
91                 only one parameter can be selected which is shown as below:
92       \arg        CRC_INPUT_DATA_NOT: input data is not reversed
93       \arg        CRC_INPUT_DATA_BYTE: input data is reversed on 8 bits
94       \arg        CRC_INPUT_DATA_HALFWORD: input data is reversed on 16 bits
95       \arg        CRC_INPUT_DATA_WORD: input data is reversed on 32 bits
96     \param[out] none
97     \retval     none
98 */
crc_input_data_reverse_config(uint32_t data_reverse)99 void crc_input_data_reverse_config(uint32_t data_reverse)
100 {
101     CRC_CTL &= (uint32_t)(~CRC_CTL_REV_I);
102     CRC_CTL |= (uint32_t)data_reverse;
103 }
104 
105 /*!
106     \brief      read the data register
107     \param[in]  none
108     \param[out] none
109     \retval     32-bit value of the data register
110 */
crc_data_register_read(void)111 uint32_t crc_data_register_read(void)
112 {
113     uint32_t data;
114     data = CRC_DATA;
115     return (data);
116 }
117 
118 /*!
119     \brief      read the free data register
120     \param[in]  none
121     \param[out] none
122     \retval     8-bit value of the free data register
123 */
crc_free_data_register_read(void)124 uint8_t crc_free_data_register_read(void)
125 {
126     uint8_t fdata;
127     fdata = (uint8_t)CRC_FDATA;
128     return (fdata);
129 }
130 
131 /*!
132     \brief      write the free data register
133     \param[in]  free_data: specify 8-bit data
134     \param[out] none
135     \retval     none
136 */
crc_free_data_register_write(uint8_t free_data)137 void crc_free_data_register_write(uint8_t free_data)
138 {
139     CRC_FDATA = (uint32_t)free_data;
140 }
141 
142 /*!
143     \brief      write the initializaiton data register
144     \param[in]  init_data: specify 32-bit data
145     \param[out] none
146     \retval     none
147 */
crc_init_data_register_write(uint32_t init_data)148 void crc_init_data_register_write(uint32_t init_data)
149 {
150     CRC_IDATA = (uint32_t)init_data;
151 }
152 
153 /*!
154     \brief      configure the CRC size of polynomial function
155     \param[in]  poly_size: size of polynomial
156                 only one parameter can be selected which is shown as below:
157       \arg        CRC_CTL_PS_32: 32-bit polynomial for CRC calculation
158       \arg        CRC_CTL_PS_16: 16-bit polynomial for CRC calculation
159       \arg        CRC_CTL_PS_8: 8-bit polynomial for CRC calculation
160       \arg        CRC_CTL_PS_7: 7-bit polynomial for CRC calculation
161     \param[out] none
162     \retval     none
163 */
crc_polynomial_size_set(uint32_t poly_size)164 void crc_polynomial_size_set(uint32_t poly_size)
165 {
166     CRC_CTL &= (uint32_t)(~(CRC_CTL_PS));
167     CRC_CTL |= (uint32_t)poly_size;
168 }
169 
170 /*!
171     \brief      configure the CRC polynomial value function
172     \param[in]  poly: configurable polynomial value
173     \param[out] none
174     \retval     none
175 */
crc_polynomial_set(uint32_t poly)176 void crc_polynomial_set(uint32_t poly)
177 {
178     CRC_POLY &= (uint32_t)(~CRC_POLY_POLY);
179     CRC_POLY = poly;
180 }
181 
182 /*!
183     \brief      CRC calculate single data
184     \param[in]  sdata: specify input data data
185     \param[in]  data_format: input data format
186                 only one parameter can be selected which is shown as below:
187       \arg        INPUT_FORMAT_WORD: input data in word format
188       \arg        INPUT_FORMAT_HALFWORD: input data in half-word format
189       \arg        INPUT_FORMAT_BYTE: input data in byte format
190     \param[out] none
191     \retval     CRC calculate value
192 */
crc_single_data_calculate(uint32_t sdata,uint8_t data_format)193 uint32_t crc_single_data_calculate(uint32_t sdata, uint8_t data_format)
194 {
195     if(INPUT_FORMAT_WORD == data_format){
196         REG32(CRC) = sdata;
197     }else if(INPUT_FORMAT_HALFWORD == data_format){
198         REG16(CRC) = (uint16_t)sdata;
199     }else{
200         REG8(CRC) = (uint8_t)sdata;
201     }
202 
203     return(CRC_DATA);
204 }
205 
206 /*!
207     \brief      CRC calculate a data array
208     \param[in]  array: pointer to the input data array
209     \param[in]  size: size of the array
210     \param[in]  data_format: input data format
211                 only one parameter can be selected which is shown as below:
212       \arg        INPUT_FORMAT_WORD: input data in word format
213       \arg        INPUT_FORMAT_HALFWORD: input data in half-word format
214       \arg        INPUT_FORMAT_BYTE: input data in byte format
215     \param[out] none
216     \retval     CRC calculate value
217 */
crc_block_data_calculate(void * array,uint32_t size,uint8_t data_format)218 uint32_t crc_block_data_calculate(void *array, uint32_t size, uint8_t data_format)
219 {
220     uint8_t *data8;
221     uint16_t *data16;
222     uint32_t *data32;
223     uint32_t index;
224 
225     if(INPUT_FORMAT_WORD == data_format){
226         data32 = (uint32_t *)array;
227         for(index = 0U; index < size; index++){
228             REG32(CRC) = data32[index];
229         }
230     }else if(INPUT_FORMAT_HALFWORD == data_format){
231         data16 = (uint16_t *)array;
232         for(index = 0U; index < size; index++){
233             REG16(CRC) = data16[index];
234         }
235     }else{
236         data8 = (uint8_t *)array;
237         for(index = 0U; index < size; index++){
238             REG8(CRC) =  data8[index];
239         }
240     }
241 
242     return (CRC_DATA);
243 }
244