1 /*
2  * @brief LPC15xx Cyclic Redundancy Check (CRC) Engine driver
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2013
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products.  This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licenser disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights.  NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers.  This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #ifndef __CRC_15XX_H_
33 #define __CRC_15XX_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /** @defgroup CRC_15XX CHIP: LPC15xx Cyclic Redundancy Check Engine driver
40  * @ingroup CHIP_15XX_Drivers
41  * @{
42  */
43 
44 /**
45  * @brief CRC register block structure
46  */
47 typedef struct {						/*!< CRC Structure */
48 	__IO    uint32_t    MODE;			/*!< CRC Mode Register */
49 	__IO    uint32_t    SEED;			/*!< CRC SEED Register */
50 	union {
51 		__I     uint32_t    SUM;		/*!< CRC Checksum Register. */
52 		__O     uint32_t    WRDATA32;	/*!< CRC Data Register: write size 32-bit*/
53 		__O     uint16_t    WRDATA16;	/*!< CRC Data Register: write size 16-bit*/
54 		__O     uint8_t     WRDATA8;	/*!< CRC Data Register: write size 8-bit*/
55 	};
56 
57 } LPC_CRC_T;
58 
59 /*
60  * @brief CRC MODE register description
61  */
62 #define CRC_MODE_POLY_BITMASK   ((0x03))	/** CRC polynomial Bit mask */
63 #define CRC_MODE_POLY_CCITT     (0x00)		/** Select CRC-CCITT polynomial */
64 #define CRC_MODE_POLY_CRC16     (0x01)		/** Select CRC-16 polynomial */
65 #define CRC_MODE_POLY_CRC32     (0x02)		/** Select CRC-32 polynomial */
66 #define CRC_MODE_WRDATA_BITMASK (0x03 << 2)	/** CRC WR_Data Config Bit mask */
67 #define CRC_MODE_WRDATA_BIT_RVS (1 << 2)	/** Select Bit order reverse for WR_DATA (per byte) */
68 #define CRC_MODE_WRDATA_CMPL    (1 << 3)	/** Select One's complement for WR_DATA */
69 #define CRC_MODE_SUM_BITMASK    (0x03 << 4)	/** CRC Sum Config Bit mask */
70 #define CRC_MODE_SUM_BIT_RVS    (1 << 4)	/** Select Bit order reverse for CRC_SUM */
71 #define CRC_MODE_SUM_CMPL       (1 << 5)	/** Select One's complement for CRC_SUM */
72 
73 #define MODE_CFG_CCITT          (0x00)	/** Pre-defined mode word for default CCITT setup */
74 #define MODE_CFG_CRC16          (0x15)	/** Pre-defined mode word for default CRC16 setup */
75 #define MODE_CFG_CRC32          (0x36)	/** Pre-defined mode word for default CRC32 setup */
76 
77 #define CRC_SEED_CCITT          (0x0000FFFF)/** Initial seed value for CCITT mode */
78 #define CRC_SEED_CRC16          (0x00000000)/** Initial seed value for CRC16 mode */
79 #define CRC_SEED_CRC32          (0xFFFFFFFF)/** Initial seed value for CRC32 mode */
80 
81 /**
82  * @brief CRC polynomial
83  */
84 typedef enum IP_CRC_001_POLY {
85 	CRC_POLY_CCITT = CRC_MODE_POLY_CCITT,	/**< CRC-CCIT polynomial */
86 	CRC_POLY_CRC16 = CRC_MODE_POLY_CRC16,	/**< CRC-16 polynomial */
87 	CRC_POLY_CRC32 = CRC_MODE_POLY_CRC32,	/**< CRC-32 polynomial */
88 	CRC_POLY_LAST,
89 } CRC_POLY_T;
90 
91 /**
92  * @brief	Initializes the CRC Engine
93  * @return	Nothing
94  */
95 void Chip_CRC_Init(void);
96 
97 /**
98  * @brief	Deinitializes the CRC Engine
99  * @return	Nothing
100  */
101 void Chip_CRC_Deinit(void);
102 
103 /**
104  * @brief	Set the polynomial used for the CRC calculation
105  * @param	poly	: The enumerated polynomial to be used
106  * @param	flags	: An Or'ed value of flags that setup the mode
107  * @return	Nothing
108  * @note	Flags for setting up the mode word include CRC_MODE_WRDATA_BIT_RVS,
109  * CRC_MODE_WRDATA_CMPL, CRC_MODE_SUM_BIT_RVS, and CRC_MODE_SUM_CMPL.
110  */
Chip_CRC_SetPoly(CRC_POLY_T poly,uint32_t flags)111 STATIC INLINE void Chip_CRC_SetPoly(CRC_POLY_T poly, uint32_t flags)
112 {
113 	LPC_CRC->MODE = (uint32_t) poly | flags;
114 }
115 
116 /**
117  * @brief	Sets up the CRC engine for CRC16 mode
118  * @return	Nothing
119  */
Chip_CRC_UseCRC16(void)120 STATIC INLINE void Chip_CRC_UseCRC16(void)
121 {
122 	LPC_CRC->MODE = MODE_CFG_CRC16;
123 	LPC_CRC->SEED = CRC_SEED_CRC16;
124 }
125 
126 /**
127  * @brief	Sets up the CRC engine for CRC32 mode
128  * @return	Nothing
129  */
Chip_CRC_UseCRC32(void)130 STATIC INLINE void Chip_CRC_UseCRC32(void)
131 {
132 	LPC_CRC->MODE = MODE_CFG_CRC32;
133 	LPC_CRC->SEED = CRC_SEED_CRC32;
134 }
135 
136 /**
137  * @brief	Sets up the CRC engine for CCITT mode
138  * @return	Nothing
139  */
Chip_CRC_UseCCITT(void)140 STATIC INLINE void Chip_CRC_UseCCITT(void)
141 {
142 	LPC_CRC->MODE = MODE_CFG_CCITT;
143 	LPC_CRC->SEED = CRC_SEED_CCITT;
144 }
145 
146 /**
147  * @brief	Engage the CRC engine with defaults based on the polynomial to be used
148  * @param	poly	: The enumerated polynomial to be used
149  * @return	Nothing
150  */
151 void Chip_CRC_UseDefaultConfig(CRC_POLY_T poly);
152 
153 /**
154  * @brief	Set the CRC Mode bits
155  * @param	mode	: Mode value
156  * @return	Nothing
157  */
Chip_CRC_SetMode(uint32_t mode)158 STATIC INLINE void Chip_CRC_SetMode(uint32_t mode)
159 {
160 	LPC_CRC->MODE = mode;
161 }
162 
163 /**
164  * @brief	Get the CRC Mode bits
165  * @return	The current value of the CRC Mode bits
166  */
Chip_CRC_GetMode(void)167 STATIC INLINE uint32_t Chip_CRC_GetMode(void)
168 {
169 	return LPC_CRC->MODE;
170 }
171 
172 /**
173  * @brief	Set the seed bits used by the CRC_SUM register
174  * @param	seed	: Seed value
175  * @return	Nothing
176  */
Chip_CRC_SetSeed(uint32_t seed)177 STATIC INLINE void Chip_CRC_SetSeed(uint32_t seed)
178 {
179 	LPC_CRC->SEED = seed;
180 }
181 
182 /**
183  * @brief	Get the CRC seed value
184  * @return	Seed value
185  */
Chip_CRC_GetSeed(void)186 STATIC INLINE uint32_t Chip_CRC_GetSeed(void)
187 {
188 	return LPC_CRC->SEED;
189 }
190 
191 /**
192  * @brief	Convenience function for writing 8-bit data to the CRC engine
193  * @param	data	: 8-bit data to write
194  * @return	Nothing
195  */
Chip_CRC_Write8(uint8_t data)196 STATIC INLINE void Chip_CRC_Write8(uint8_t data)
197 {
198 	LPC_CRC->WRDATA8 = data;
199 }
200 
201 /**
202  * @brief	Convenience function for writing 16-bit data to the CRC engine
203  * @param	data	: 16-bit data to write
204  * @return	Nothing
205  */
Chip_CRC_Write16(uint16_t data)206 STATIC INLINE void Chip_CRC_Write16(uint16_t data)
207 {
208 	LPC_CRC->WRDATA16 = data;
209 }
210 
211 /**
212  * @brief	Convenience function for writing 32-bit data to the CRC engine
213  * @param	data	: 32-bit data to write
214  * @return	Nothing
215  */
Chip_CRC_Write32(uint32_t data)216 STATIC INLINE void Chip_CRC_Write32(uint32_t data)
217 {
218 	LPC_CRC->WRDATA32 = data;
219 }
220 
221 /**
222  * @brief	Gets the CRC Sum based on the Mode and Seed as previously configured
223  * @return	CRC Checksum value
224  */
Chip_CRC_Sum(void)225 STATIC INLINE uint32_t Chip_CRC_Sum(void)
226 {
227 	return LPC_CRC->SUM;
228 }
229 
230 /**
231  * @brief	Convenience function for computing a standard CCITT checksum from an 8-bit data block
232  * @param	data	: Pointer to the block of 8-bit data
233  * @param   bytes	: The number of bytes pointed to by data
234  * @return	Check sum value
235  */
236 uint32_t Chip_CRC_CRC8(const uint8_t *data, uint32_t bytes);
237 
238 /**
239  * @brief	Convenience function for computing a standard CRC16 checksum from 16-bit data block
240  * @param	data	: Pointer to the block of 16-bit data
241  * @param   hwords	: The number of 16 byte entries pointed to by data
242  * @return	Check sum value
243  */
244 uint32_t Chip_CRC_CRC16(const uint16_t *data, uint32_t hwords);
245 
246 /**
247  * @brief	Convenience function for computing a standard CRC32 checksum from 32-bit data block
248  * @param	data	: Pointer to the block of 32-bit data
249  * @param   words	: The number of 32-bit entries pointed to by data
250  * @return	Check sum value
251  */
252 uint32_t Chip_CRC_CRC32(const uint32_t *data, uint32_t words);
253 
254 /**
255  * @}
256  */
257 
258 #ifdef __cplusplus
259 }
260 #endif
261 
262 #endif /* __CRC_15XX_H_ */
263