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