1 /*!
2     \file    gd32f4xx_crc.c
3     \brief   CRC driver
4 
5     \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6     \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7     \version 2020-09-30, V2.1.0, firmware for GD32F4xx
8     \version 2022-03-09, V3.0.0, firmware for GD32F4xx
9 */
10 
11 /*
12     Copyright (c) 2022, GigaDevice Semiconductor Inc.
13 
14     Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16 
17     1. Redistributions of source code must retain the above copyright notice, this
18        list of conditions and the following disclaimer.
19     2. Redistributions in binary form must reproduce the above copyright notice,
20        this list of conditions and the following disclaimer in the documentation
21        and/or other materials provided with the distribution.
22     3. Neither the name of the copyright holder nor the names of its contributors
23        may be used to endorse or promote products derived from this software without
24        specific prior written permission.
25 
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37 
38 #include "gd32f4xx_crc.h"
39 
40 #define CRC_DATA_RESET_VALUE      ((uint32_t)0xFFFFFFFFU)
41 #define CRC_FDATA_RESET_VALUE     ((uint32_t)0x00000000U)
42 
43 /*!
44     \brief      deinit CRC calculation unit
45     \param[in]  none
46     \param[out] none
47     \retval     none
48 */
crc_deinit(void)49 void crc_deinit(void)
50 {
51     CRC_DATA  = CRC_DATA_RESET_VALUE;
52     CRC_FDATA = CRC_FDATA_RESET_VALUE;
53     CRC_CTL   = (uint32_t)CRC_CTL_RST;
54 }
55 
56 /*!
57     \brief      reset data register(CRC_DATA) to the value of 0xFFFFFFFF
58     \param[in]  none
59     \param[out] none
60     \retval     none
61 */
crc_data_register_reset(void)62 void crc_data_register_reset(void)
63 {
64     CRC_CTL |= (uint32_t)CRC_CTL_RST;
65 }
66 
67 /*!
68     \brief      read the value of the data register
69     \param[in]  none
70     \param[out] none
71     \retval     32-bit value of the data register
72 */
crc_data_register_read(void)73 uint32_t crc_data_register_read(void)
74 {
75     uint32_t data;
76     data = CRC_DATA;
77     return (data);
78 }
79 
80 /*!
81     \brief      read the value of the free data register
82     \param[in]  none
83     \param[out] none
84     \retval     8-bit value of the free data register
85 */
crc_free_data_register_read(void)86 uint8_t crc_free_data_register_read(void)
87 {
88     uint8_t fdata;
89     fdata = (uint8_t)CRC_FDATA;
90     return (fdata);
91 }
92 
93 /*!
94     \brief      write data to the free data register
95     \param[in]  free_data: specified 8-bit data
96     \param[out] none
97     \retval     none
98 */
crc_free_data_register_write(uint8_t free_data)99 void crc_free_data_register_write(uint8_t free_data)
100 {
101     CRC_FDATA = (uint32_t)free_data;
102 }
103 
104 /*!
105     \brief      calculate the CRC value of a 32-bit data
106     \param[in]  sdata: specified 32-bit data
107     \param[out] none
108     \retval     32-bit value calculated by CRC
109 */
crc_single_data_calculate(uint32_t sdata)110 uint32_t crc_single_data_calculate(uint32_t sdata)
111 {
112     CRC_DATA = sdata;
113     return (CRC_DATA);
114 }
115 
116 /*!
117     \brief      calculate the CRC value of an array of 32-bit values
118     \param[in]  array: pointer to an array of 32-bit values
119     \param[in]  size: size of the array
120     \param[out] none
121     \retval     32-bit value calculated by CRC
122 */
crc_block_data_calculate(uint32_t array[],uint32_t size)123 uint32_t crc_block_data_calculate(uint32_t array[], uint32_t size)
124 {
125     uint32_t index;
126     for(index = 0U; index < size; index++) {
127         CRC_DATA = array[index];
128     }
129     return (CRC_DATA);
130 }
131