1 /*!
2     \file    gd32f3x0_crc.c
3     \brief   CRC driver
4 
5     \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6     \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7     \version 2020-09-30, V2.1.0, firmware for GD32F3x0
8 */
9 
10 /*
11     Copyright (c) 2020, 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 "gd32f3x0_crc.h"
38 
39 /*!
40     \brief      deinit 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      enable the reverse operation of output data
56     \param[in]  none
57     \param[out] none
58     \retval     none
59 */
crc_reverse_output_data_enable(void)60 void crc_reverse_output_data_enable(void)
61 {
62     CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
63     CRC_CTL |= (uint32_t)CRC_CTL_REV_O;
64 }
65 
66 /*!
67     \brief      disable the reverse operation of output data
68     \param[in]  none
69     \param[out] none
70     \retval     none
71 */
crc_reverse_output_data_disable(void)72 void crc_reverse_output_data_disable(void)
73 {
74     CRC_CTL &= (uint32_t)(~ CRC_CTL_REV_O);
75 }
76 
77 /*!
78     \brief      reset data register to the value of initializaiton data register
79     \param[in]  none
80     \param[out] none
81     \retval     none
82 */
crc_data_register_reset(void)83 void crc_data_register_reset(void)
84 {
85     CRC_CTL |= (uint32_t)CRC_CTL_RST;
86 }
87 
88 /*!
89     \brief      read the data register
90     \param[in]  none
91     \param[out] none
92     \retval     32-bit value of the data register
93 */
crc_data_register_read(void)94 uint32_t crc_data_register_read(void)
95 {
96     uint32_t data;
97     data = CRC_DATA;
98     return (data);
99 }
100 
101 /*!
102     \brief      read the free data register
103     \param[in]  none
104     \param[out] none
105     \retval     8-bit value of the free data register
106 */
crc_free_data_register_read(void)107 uint8_t crc_free_data_register_read(void)
108 {
109     uint8_t fdata;
110     fdata = (uint8_t)CRC_FDATA;
111     return (fdata);
112 }
113 
114 /*!
115     \brief      write the free data register
116     \param[in]  free_data: specify 8-bit data
117     \param[out] none
118     \retval     none
119 */
crc_free_data_register_write(uint8_t free_data)120 void crc_free_data_register_write(uint8_t free_data)
121 {
122     CRC_FDATA = (uint32_t)free_data;
123 }
124 
125 /*!
126     \brief      write the initializaiton data register
127     \param[in]  init_data:specify 32-bit data
128     \param[out] none
129     \retval     none
130 */
crc_init_data_register_write(uint32_t init_data)131 void crc_init_data_register_write(uint32_t init_data)
132 {
133     CRC_IDATA = (uint32_t)init_data;
134 }
135 
136 /*!
137     \brief      configure the CRC input data function
138     \param[in]  data_reverse: specify input data reverse function
139                 only one parameter can be selected which is shown as below:
140       \arg        CRC_INPUT_DATA_NOT: input data is not reversed
141       \arg        CRC_INPUT_DATA_BYTE: input data is reversed on 8 bits
142       \arg        CRC_INPUT_DATA_HALFWORD: input data is reversed on 16 bits
143       \arg        CRC_INPUT_DATA_WORD: input data is reversed on 32 bits
144     \param[out] none
145     \retval     none
146 */
crc_input_data_reverse_config(uint32_t data_reverse)147 void crc_input_data_reverse_config(uint32_t data_reverse)
148 {
149     CRC_CTL &= (uint32_t)(~CRC_CTL_REV_I);
150     CRC_CTL |= (uint32_t)data_reverse;
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