1 /*!
2     \file    gd32f403_crc.c
3     \brief   CRC driver
4 
5     \version 2017-02-10, V1.0.0, firmware for GD32F403
6     \version 2018-12-25, V2.0.0, firmware for GD32F403
7     \version 2020-09-30, V2.1.0, firmware for GD32F403
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 "gd32f403_crc.h"
38 
39 #define CRC_DATA_RESET_VALUE      ((uint32_t)0xFFFFFFFFU)
40 #define CRC_FDATA_RESET_VALUE     ((uint32_t)0x00000000U)
41 
42 /*!
43     \brief      deinit CRC calculation unit
44     \param[in]  none
45     \param[out] none
46     \retval     none
47 */
crc_deinit(void)48 void crc_deinit(void)
49 {
50     CRC_DATA  = CRC_DATA_RESET_VALUE;
51     CRC_FDATA = CRC_FDATA_RESET_VALUE;
52     CRC_CTL   = (uint32_t)CRC_CTL_RST;
53 }
54 
55 /*!
56     \brief      reset data register(CRC_DATA) to the value of 0xFFFFFFFF
57     \param[in]  none
58     \param[out] none
59     \retval     none
60 */
crc_data_register_reset(void)61 void crc_data_register_reset(void)
62 {
63     CRC_CTL |= (uint32_t)CRC_CTL_RST;
64 }
65 
66 /*!
67     \brief      read the value of the data register
68     \param[in]  none
69     \param[out] none
70     \retval     32-bit value of the data register
71 */
crc_data_register_read(void)72 uint32_t crc_data_register_read(void)
73 {
74     uint32_t data;
75     data = CRC_DATA;
76     return (data);
77 }
78 
79 /*!
80     \brief      read the value of the free data register
81     \param[in]  none
82     \param[out] none
83     \retval     8-bit value of the free data register
84 */
crc_free_data_register_read(void)85 uint8_t crc_free_data_register_read(void)
86 {
87     uint8_t fdata;
88     fdata = (uint8_t)CRC_FDATA;
89     return (fdata);
90 }
91 
92 /*!
93     \brief      write data to the free data register
94     \param[in]  free_data: specified 8-bit data
95     \param[out] none
96     \retval     none
97 */
crc_free_data_register_write(uint8_t free_data)98 void crc_free_data_register_write(uint8_t free_data)
99 {
100     CRC_FDATA = (uint32_t)free_data;
101 }
102 
103 /*!
104     \brief      calculate the CRC value of a 32-bit data
105     \param[in]  sdata: specified 32-bit data
106     \param[out] none
107     \retval     32-bit value calculated by CRC
108 */
crc_single_data_calculate(uint32_t sdata)109 uint32_t crc_single_data_calculate(uint32_t sdata)
110 {
111     CRC_DATA = sdata;
112     return (CRC_DATA);
113 }
114 
115 /*!
116     \brief      calculate the CRC value of an array of 32-bit values
117     \param[in]  array: pointer to an array of 32-bit values
118     \param[in]  size: size of the array
119     \param[out] none
120     \retval     32-bit value calculated by CRC
121 */
crc_block_data_calculate(uint32_t array[],uint32_t size)122 uint32_t crc_block_data_calculate(uint32_t array[], uint32_t size)
123 {
124     uint32_t index;
125     for(index = 0U; index < size; index++){
126         CRC_DATA = array[index];
127     }
128     return (CRC_DATA);
129 }
130