1 /*!
2     \file    gd32l23x_crc.h
3     \brief   definitions for the CRC
4 
5     \version 2021-08-04, V1.0.0, firmware for GD32L23x
6 */
7 
8 /*
9     Copyright (c) 2021, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef GD32L23X_CRC_H
36 #define GD32L23X_CRC_H
37 
38 #include "gd32l23x.h"
39 
40 /* CRC definitions */
41 #define CRC                            CRC_BASE                        /*!< CRC bsae address */
42 
43 /* registers definitions */
44 #define CRC_DATA                       REG32((CRC) + 0x00000000U)      /*!< CRC data register */
45 #define CRC_FDATA                      REG32((CRC) + 0x00000004U)      /*!< CRC free data register */
46 #define CRC_CTL                        REG32((CRC) + 0x00000008U)      /*!< CRC control register */
47 #define CRC_IDATA                      REG32((CRC) + 0x00000010U)      /*!< CRC initialization data register */
48 #define CRC_POLY                       REG32((CRC) + 0x00000014U)      /*!< CRC polynomial register */
49 
50 /* bits definitions */
51 /* CRC_DATA */
52 #define CRC_DATA_DATA                  BITS(0, 31)                     /*!< CRC data bits */
53 
54 /* CRC_FDATA */
55 #define CRC_FDATA_FDATA                BITS(0, 7)                      /*!< CRC free data bits */
56 
57 /* CRC_CTL */
58 #define CRC_CTL_RST                    BIT(0)                          /*!< CRC reset bit */
59 #define CRC_CTL_PS                     BITS(3, 4)                      /*!< size of polynomial function bits */
60 #define CRC_CTL_REV_I                  BITS(5, 6)                      /*!< input data reverse function bits */
61 #define CRC_CTL_REV_O                  BIT(7)                          /*!< output data reverse function bit */
62 
63 /* CRC_INIT */
64 #define CRC_IDATA_IDATA                BITS(0, 31)                     /*!< CRC initialization data bits */
65 
66 /* CRC_POLY */
67 #define CRC_POLY_POLY                  BITS(0, 31)                     /*!< CRC polynomial value bits */
68 
69 /* constants definitions */
70 /* size of polynomial function */
71 #define CTL_PS(regval)                 (BITS(3, 4) & ((regval) << 3))
72 #define CRC_CTL_PS_32                  CTL_PS(0)                       /*!< 32-bit polynomial for CRC calculation */
73 #define CRC_CTL_PS_16                  CTL_PS(1)                       /*!< 16-bit polynomial for CRC calculation */
74 #define CRC_CTL_PS_8                   CTL_PS(2)                       /*!< 8-bit polynomial for CRC calculation */
75 #define CRC_CTL_PS_7                   CTL_PS(3)                       /*!< 7-bit polynomial for CRC calculation */
76 
77 /* input data reverse function */
78 #define CTL_REV_I(regval)              (BITS(5, 6) & ((regval) << 5))
79 #define CRC_INPUT_DATA_NOT             CTL_REV_I(0)                    /*!< input data not reverse */
80 #define CRC_INPUT_DATA_BYTE            CTL_REV_I(1)                    /*!< input data reversed by byte type */
81 #define CRC_INPUT_DATA_HALFWORD        CTL_REV_I(2)                    /*!< input data reversed by half-word type */
82 #define CRC_INPUT_DATA_WORD            CTL_REV_I(3)                    /*!< input data reversed by word type */
83 
84 /* input data format */
85 #define INPUT_FORMAT_WORD              0U                              /*!< input data in word format */
86 #define INPUT_FORMAT_HALFWORD          1U                              /*!< input data in half-word format */
87 #define INPUT_FORMAT_BYTE              2U                              /*!< input data in byte format */
88 
89 /* function declarations */
90 /* deinit CRC calculation unit */
91 void crc_deinit(void);
92 /* enable the reverse operation of output data */
93 void crc_reverse_output_data_enable(void);
94 /* disable the reverse operation of output data */
95 void crc_reverse_output_data_disable(void);
96 
97 /* reset data register to the value of initialization data register */
98 void crc_data_register_reset(void);
99 /* read the data register */
100 uint32_t crc_data_register_read(void);
101 
102 /* read the free data register */
103 uint8_t crc_free_data_register_read(void);
104 /* write the free data register */
105 void crc_free_data_register_write(uint8_t free_data);
106 
107 /* write the initial value register */
108 void crc_init_data_register_write(uint32_t init_data);
109 /* configure the CRC input data function */
110 void crc_input_data_reverse_config(uint32_t data_reverse);
111 
112 /* configure the CRC size of polynomial function */
113 void crc_polynomial_size_set(uint32_t poly_size);
114 /* configure the CRC polynomial value function */
115 void crc_polynomial_set(uint32_t poly);
116 
117 /* CRC calculate single data */
118 uint32_t crc_single_data_calculate(uint32_t sdata, uint8_t data_format);
119 /* CRC calculate a data array */
120 uint32_t crc_block_data_calculate(void *array, uint32_t size, uint8_t data_format);
121 
122 #endif /* GD32L23X_CRC_H */
123