1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _CRC_H_
10 #define _CRC_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup CRC_Adapter
16  * @{
17  */
18 
19 /************************************************************************************
20 *************************************************************************************
21 * Include
22 *************************************************************************************
23 ***********************************************************************************/
24 
25 /************************************************************************************
26 *************************************************************************************
27 * Public types
28 *************************************************************************************
29 ************************************************************************************/
30 /*! @brief crcRefIn definitions. */
31 typedef enum _hal_crc_cfg_refin
32 {
33     KHAL_CrcInputNoRef = 0U, /*!< Do not manipulate input data stream. */
34     KHAL_CrcRefInput   = 1U  /*!< Reflect each byte in the input stream bitwise. */
35 } hal_crc_cfg_refin_t;
36 
37 /*! @brief crcRefOut definitions. */
38 typedef enum _hal_crc_cfg_refout
39 {
40     KHAL_CrcOutputNoRef = 0U, /*!< Do not manipulate CRC result. */
41     KHAL_CrcRefOutput   = 1U  /*!< CRC result is to be reflected bitwise (operated on entire word). */
42 } hal_crc_cfg_refout_t;
43 
44 /*! @brief crcByteOrder definitions. */
45 typedef enum _hal_crc_cfg_byteord
46 {
47     KHAL_CrcLSByteFirst = 0U, /*!< Byte order of the CRC LS Byte first. */
48     KHAL_CrcMSByteFirst = 1U  /*!< Bit order of the CRC  MS Byte first. */
49 } hal_crc_cfg_byteord_t;
50 
51 /*! @brief CRC polynomials to use. */
52 typedef enum _hal_crc_polynomial
53 {
54     KHAL_CrcPolynomial_CRC_8_CCITT = 0x103,      /*!< x^8+x^2+x^1+1 */
55     KHAL_CrcPolynomial_CRC_16      = 0x1021,     /*!< x^16+x^12+x^5+1 */
56     KHAL_CrcPolynomial_CRC_32      = 0x4C11DB7U, /*!< x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1 */
57 } hal_crc_polynomial_t;
58 
59 /*! @brief CRC configuration structure. */
60 typedef struct _hal_crc_config
61 {
62     hal_crc_cfg_refin_t crcRefIn;       /*!< CRC reflect input. See "hal_crc_cfg_refin_t". */
63     hal_crc_cfg_refout_t crcRefOut;     /*!< CRC reflect output. See "hal_crc_cfg_refout_t". */
64     hal_crc_cfg_byteord_t crcByteOrder; /*!< CRC byte order. See "hal_crc_cfg_byteord_t". */
65     uint32_t crcSeed;                   /*!< CRC Seed value. Initial value for CRC LFSR. */
66     uint32_t crcPoly;                   /*!< CRC Polynomial value. */
67     uint32_t crcXorOut;                 /*!< XOR mask for CRC result (for no mask, should be 0). */
68     uint8_t complementChecksum;         /*!< wether output the complement checksum. */
69     uint8_t crcSize;                    /*!< Number of CRC octets, 2 mean use CRC16, 4 mean use CRC32. */
70     uint8_t crcStartByte; /*!< Start CRC with this byte position. Byte #0 is the first byte of Sync Address. */
71 } hal_crc_config_t;
72 
73 /************************************************************************************
74 *************************************************************************************
75 * Public prototypes
76 *************************************************************************************
77 ************************************************************************************/
78 
79 #if defined(__cplusplus)
80 extern "C" {
81 #endif
82 
83 /*!
84  * @name CRC
85  * @{
86  */
87 
88 /*!
89  * @brief Compute CRC function.
90  *
91  * The function computes the CRC.
92  *
93  *  @code
94  * config = (hal_crc_config_t) {
95  *     .crcSize       = 4,
96  *     .crcStartByte = 0,
97  *     .crcRefIn       = KHAL_CrcInputNoRef,
98  *     .crcRefOut    = KHAL_CrcOutputNoRef,
99  *     .crcByteOrder = KHAL_CrcMSByteFirst,
100  *     .complementChecksum = true,
101  *     .crcSeed       = 0xFFFFFFFF,
102  *     .crcPoly       = KHAL_CrcPolynomial_CRC_32,
103  *     .crcXorOut    = 0xFFFFFFFF,
104  * };
105  *
106  * res = HAL_CrcCompute(&config, (uint8_t *) pattern, strlen(pattern));
107  *  @endcode
108  *
109  * @note The settings for compute CRC are taken from the passed CRC_config_t structure.
110  *
111  * @param crcConfig    configuration structure.
112  * @param dataIn input data buffer.
113  * @param length input data buffer size.
114  *
115  * @retval Computed CRC value.
116  */
117 uint32_t HAL_CrcCompute(hal_crc_config_t *crcConfig, uint8_t *dataIn, uint32_t length);
118 
119 /*! @} */
120 
121 #if defined(__cplusplus)
122 }
123 #endif
124 
125 /*! @} */
126 
127 #endif /* _CRC_H_ */
128