1 /**************************************************************************//**
2  * @file     crc.c
3  * @version  V3.00
4  * @brief    Cyclic Redundancy Check(CRC) driver source file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #include "NuMicro.h"
10 
11 
12 /** @addtogroup Standard_Driver Standard Driver
13   @{
14 */
15 
16 /** @addtogroup CRC_Driver CRC Driver
17   @{
18 */
19 
20 /** @addtogroup CRC_EXPORTED_FUNCTIONS CRC Exported Functions
21   @{
22 */
23 
24 /**
25   * @brief      CRC Open
26   *
27   * @param[in]  u32Mode         CRC operation polynomial mode. Valid values are:
28   *                             - \ref CRC_CCITT
29   *                             - \ref CRC_8
30   *                             - \ref CRC_16
31   *                             - \ref CRC_32
32   * @param[in]  u32Attribute    CRC operation data attribute. Valid values are combined with:
33   *                             - \ref CRC_CHECKSUM_COM
34   *                             - \ref CRC_CHECKSUM_RVS
35   *                             - \ref CRC_WDATA_COM
36   *                             - \ref CRC_WDATA_RVS
37   * @param[in]  u32Seed         Seed value.
38   * @param[in]  u32DataLen      CPU Write Data Length. Valid values are:
39   *                             - \ref CRC_CPU_WDATA_8
40   *                             - \ref CRC_CPU_WDATA_16
41   *                             - \ref CRC_CPU_WDATA_32
42   *
43   * @return     None
44   *
45   * @details    This function will enable the CRC controller by specify CRC operation mode, attribute, initial seed and write data length. \n
46   *             After that, user can start to perform CRC calculate by calling CRC_WRITE_DATA macro or CRC_DAT register directly.
47   */
CRC_Open(uint32_t u32Mode,uint32_t u32Attribute,uint32_t u32Seed,uint32_t u32DataLen)48 void CRC_Open(uint32_t u32Mode, uint32_t u32Attribute, uint32_t u32Seed, uint32_t u32DataLen)
49 {
50     CRC->SEED = u32Seed;
51     CRC->CTL = u32Mode | u32Attribute | u32DataLen | CRC_CTL_CRCEN_Msk;
52 
53     /* Setting CHKSINIT bit will reload the initial seed value(CRC_SEED register) to CRC controller */
54     CRC->CTL |= CRC_CTL_CHKSINIT_Msk;
55 }
56 
57 /**
58   * @brief      Get CRC Checksum
59   *
60   * @param[in]  None
61   *
62   * @return     Checksum Result
63   *
64   * @details    This function gets the CRC checksum result by current CRC polynomial mode.
65   */
CRC_GetChecksum(void)66 uint32_t CRC_GetChecksum(void)
67 {
68     uint32_t u32Checksum = 0UL;
69 
70     switch(CRC->CTL & CRC_CTL_CRCMODE_Msk)
71     {
72         case CRC_CCITT:
73         case CRC_16:
74             u32Checksum = (CRC->CHECKSUM & 0xFFFFUL);
75             break;
76 
77         case CRC_32:
78             u32Checksum = CRC->CHECKSUM;
79             break;
80 
81         case CRC_8:
82             u32Checksum = (CRC->CHECKSUM & 0xFFUL);
83             break;
84 
85         default:
86             break;
87     }
88 
89     return u32Checksum;
90 }
91 
92 /**@}*/ /* end of group CRC_EXPORTED_FUNCTIONS */
93 
94 /**@}*/ /* end of group CRC_Driver */
95 
96 /**@}*/ /* end of group Standard_Driver */
97