aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/hal/inc/fsl_crc_hal.h
blob: 3bd864453f7da492206040bf31946572437b3172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * Copyright (c) 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#if !defined(__FSL_CRC_HAL_H__)
#define __FSL_CRC_HAL_H__

#include <stdint.h>
#include <stdbool.h>
#include "fsl_device_registers.h"
#if FSL_FEATURE_SOC_CRC_COUNT

/*! @addtogroup crc_hal*/
/*! @{*/

/*! @file*/

/*!*****************************************************************************
 * Definitions
 ******************************************************************************/

/*!
 * @brief CRC status return codes.
 */
typedef enum _crc_status
{
    kStatus_CRC_Success         = 0U, /*!< Success. */
    kStatus_CRC_InvalidArgument = 1U, /*!< Invalid argument existed. */
    kStatus_CRC_Failed          = 2U  /*!< Execution failed. */
} crc_status_t;

/*!
 * @brief Define type of enumerating transpose modes for CRC peripheral.
 */
typedef enum _crc_transpose
{
    kCrcNoTranspose    = 0U, /*!< No transposition. @internal gui name="No Transpose" */
    kCrcTransposeBits  = 1U, /*!< Bits in bytes are transposed; bytes are not transposed. @internal gui name="Transpose Bits" */
    kCrcTransposeBoth  = 2U, /*!< Both bits in bytes and bytes are transposed. @internal gui name="Transpose Bits in Bytes and Bytes" */
    kCrcTransposeBytes = 3U  /*!< Only bytes are transposed; no bits in a byte are transposed. @internal gui name="Transpose Bytes" */
}crc_transpose_t;

/*!
 * @brief Define type of enumerating CRC protocol widths for CRC peripheral.
 */
typedef enum _crc_prot_width
{
    kCrc16Bits = 0U, /*!< 16-bit CRC protocol. @internal gui name="16 bits" */
    kCrc32Bits = 1U, /*!< 32-bit CRC protocol. @internal gui name="32 bits" */
}crc_prot_width_t;

/*!*****************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus*/

/*! @name CRC-related feature APIs*/

/*!
 * @brief This function initializes the module to a known state.
 *
 * @param base The CRC peripheral base address.
 */
void CRC_HAL_Init(CRC_Type * base);

/*!
 * @brief Returns the current CRC result from the data register.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 32-bit value.
 */
static inline uint32_t CRC_HAL_GetDataReg(CRC_Type * base)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    return CRC_RD_CRC(base);
#else
    return CRC_RD_DATA(base);
#endif
}

/*!
 * @brief Returns the upper 16 bits of the current CRC result from the data register.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 16-bit value.
 */
static inline uint16_t CRC_HAL_GetDataHReg(CRC_Type * base)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    return CRC_RD_CRCH(base);
#else
    return CRC_RD_DATAH(base);
#endif
}

/*!
 * @brief Returns the lower 16 bits of the current CRC result from the data register.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 16-bit value.
 */
static inline uint16_t CRC_HAL_GetDataLReg(CRC_Type * base)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    return CRC_RD_CRCL(base);
#else
    return CRC_RD_DATAL(base);
#endif
}

/*!
 * @brief Sets the CRC data register (4 bytes).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 32-bit value.
 */
static inline void CRC_HAL_SetDataReg(CRC_Type * base, uint32_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRC(base, value);
#else
    CRC_WR_DATA(base, value);
#endif
}

/*!
 * @brief Sets the CRC data register (upper 2 bytes).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 16-bit value.
 */
static inline void CRC_HAL_SetDataHReg(CRC_Type * base, uint16_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRCH(base, value);
#else
    CRC_WR_DATAH(base, value);
#endif
}

/*!
 * @brief Sets the CRC data register (lower 2 bytes).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 16-bit value.
 */
static inline void CRC_HAL_SetDataLReg(CRC_Type * base, uint16_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRCL(base, value);
#else
    CRC_WR_DATAL(base, value);
#endif
}

/*!
 * @brief Sets the CRC data register (HL byte).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 8-bit value.
 */
static inline void CRC_HAL_SetDataHLReg(CRC_Type * base, uint8_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRCHL(base, value);
#else
    CRC_WR_DATAHL(base, value);
#endif
}

/*!
 * @brief Sets the CRC data register (HU byte).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 8-bit value.
 */
static inline void CRC_HAL_SetDataHUReg(CRC_Type * base, uint8_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRCHU(base, value);
#else
    CRC_WR_DATAHU(base, value);
#endif
}

/*!
 * @brief Sets the CRC data register (LL byte).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 8-bit value.
 */
static inline void CRC_HAL_SetDataLLReg(CRC_Type * base, uint8_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRCLL(base, value);
#else
    CRC_WR_DATALL(base, value);
#endif
}

/*!
 * @brief Sets the CRC data register (LU byte).
 *
 * @param base The CRC peripheral base address.
 * @param value New data for CRC computation. This parameter is a 8-bit value.
 */
static inline void CRC_HAL_SetDataLUReg(CRC_Type * base, uint8_t value)
{
#if FSL_FEATURE_CRC_HAS_CRC_REG
    CRC_WR_CRCLU(base, value);
#else
    CRC_WR_DATALU(base, value);
#endif
}

/*!
 * @brief Returns the polynomial register value.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 32-bit value.
 */
static inline uint32_t CRC_HAL_GetPolyReg(CRC_Type * base)
{
    return CRC_RD_GPOLY(base);
}

/*!
 * @brief Returns the upper 16 bits of polynomial register.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 16-bit value.
 */
static inline uint16_t CRC_HAL_GetPolyHReg(CRC_Type * base)
{
    return CRC_RD_GPOLYH(base);
}

/*!
 * @brief Returns the lower 16 bits of polynomial register.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 16-bit value.
 */
static inline uint16_t CRC_HAL_GetPolyLReg(CRC_Type * base)
{
    return CRC_RD_GPOLYL(base);
}

/*!
 * @brief Sets the polynomial register.
 *
 * @param base The CRC peripheral base address.
 * @param value Polynomial value. This parameter is a 32-bit value.
 */
static inline void CRC_HAL_SetPolyReg(CRC_Type * base, uint32_t value)
{
    CRC_WR_GPOLY(base, value);
}

/*!
 * @brief Sets the upper 16 bits of polynomial register.
 *
 * @param base The CRC peripheral base address.
 * @param value Polynomial value. This parameter is a 16-bit value.
 */
static inline void CRC_HAL_SetPolyHReg(CRC_Type * base, uint16_t value)
{
    CRC_WR_GPOLYH(base, value);
}

/*!
 * @brief Sets the lower 16 bits of polynomial register.
 *
 * @param base The CRC peripheral base address.
 * @param value Polynomial value. This parameter is a 16-bit value.
 */
static inline void CRC_HAL_SetPolyLReg(CRC_Type * base, uint16_t value)
{
    CRC_WR_GPOLYL(base, value);
}

/*!
 * @brief Returns the CRC control register.
 *
 * @param base The CRC peripheral base address.
 * @return Returns a 32-bit value.
 */
static inline uint32_t CRC_HAL_GetCtrlReg(CRC_Type * base)
{
    return CRC_RD_CTRL(base);
}

/*!
 * @brief Sets the CRC control register.
 *
 * @param base The CRC peripheral base address.
 * @param value Control register value. This parameter is a 32-bit value.
 */
static inline void CRC_HAL_SetCtrlReg(CRC_Type * base, uint32_t value)
{
    CRC_WR_CTRL(base, value);
}

/*!
 * @brief Gets the CRC seed mode.
 *
 * @param base The CRC peripheral base address.
 * @return CRC seed mode
 *         -true: Seed mode is enabled
 *         -false: Data mode is enabled
 */
static inline bool CRC_HAL_GetSeedOrDataMode(CRC_Type * base)
{
    return (bool)CRC_BRD_CTRL_WAS(base);
}

/*!
 * @brief Sets the CRC seed mode.
 *
 * @param base The CRC peripheral base address.
 * @param enable Enable or disable seed mode.
          -true: use CRC data register for seed values
          -false: use CRC data register for data values
 */
static inline void CRC_HAL_SetSeedOrDataMode(CRC_Type * base, bool enable)
{
    CRC_BWR_CTRL_WAS(base, enable);
}

/*!
 * @brief Gets the CRC transpose type for writes.
 *
 * @param base The CRC peripheral base address.
 * @return CRC input transpose type for writes.
 */
static inline crc_transpose_t CRC_HAL_GetWriteTranspose(CRC_Type * base)
{
    return (crc_transpose_t)CRC_BRD_CTRL_TOT(base);
}

/*!
 * @brief Sets the CRC transpose type for writes.
 *
 * @param base The CRC peripheral base address.
 * @param transp The CRC input transpose type.
 */
static inline void CRC_HAL_SetWriteTranspose(CRC_Type * base, crc_transpose_t transp)
{
    CRC_BWR_CTRL_TOT(base, transp);
}

/*!
 * @brief Gets the CRC transpose type for reads.
 *
 * @param base The CRC peripheral base address.
 * @return CRC output transpose type.
 */
static inline crc_transpose_t CRC_HAL_GetReadTranspose(CRC_Type * base)
{
    return (crc_transpose_t)CRC_BRD_CTRL_TOTR(base);
}

/*!
 * @brief Sets the CRC transpose type for reads.
 *
 * @param base The CRC peripheral base address.
 * @param transp The CRC output transpose type.
 */
static inline void CRC_HAL_SetReadTranspose(CRC_Type * base, crc_transpose_t transp)
{
    CRC_BWR_CTRL_TOTR(base, transp);
}

/*!
 * @brief Gets the CRC XOR mode.
 *
 * Some CRC protocols require the final checksum to be XORed with 0xFFFFFFFF
 * or 0xFFFF. XOR mode enables "on the fly" complementing of read data.
 *
 * @param base The CRC peripheral base address.
 * @return CRC XOR mode
 *         -true: XOR mode is enabled
 *         -false: XOR mode is disabled
 */
static inline bool CRC_HAL_GetXorMode(CRC_Type * base)
{
    return (bool)CRC_BRD_CTRL_FXOR(base);
}

/*!
 * @brief Sets the CRC XOR mode.
 *
 * Some CRC protocols require the final checksum to be XORed with 0xFFFFFFFF
 * or 0xFFFF. XOR mode enables "on the fly" complementing of read data.
 *
 * @param base The CRC peripheral base address.
 * @param enable Enable or disable XOR mode.
 */
static inline void CRC_HAL_SetXorMode(CRC_Type * base, bool enable)
{
    CRC_BWR_CTRL_FXOR(base, enable);
}

/*!
 * @brief Gets the CRC protocol width.
 *
 * @param base The CRC peripheral base address.
 * @return CRC protocol width
 *         -kCrc16Bits: 16-bit CRC protocol
 *         -kCrc32Bits: 32-bit CRC protocol
 */
static inline crc_prot_width_t CRC_HAL_GetProtocolWidth(CRC_Type * base)
{
    return (crc_prot_width_t)CRC_BRD_CTRL_TCRC(base);
}

/*!
 * @brief Sets the CRC protocol width.
 *
 * @param base The CRC peripheral base address.
 * @param width The CRC protocol width
 *         -kCrc16Bits: 16-bit CRC protocol
 *         -kCrc32Bits: 32-bit CRC protocol
 */
static inline void CRC_HAL_SetProtocolWidth(CRC_Type * base, crc_prot_width_t width)
{
    CRC_BWR_CTRL_TCRC(base, width);
}

/*!
 * @brief CRC_HAL_GetCrc32
 *
 * This method appends 32-bit data to the current CRC calculation
 * and returns new result. If the newSeed is true, seed set and
 * result are calculated from the seed new value (new CRC calculation).
 *
 * @param base The CRC peripheral base address.
 * @param data input data for CRC calculation
 * @param newSeed Sets new CRC calculation.
 *        - true: New seed set and used for new calculation.
 *        - false: seed argument ignored, continues old calculation.
 * @param seed New seed if newSeed is true, else ignored
 * @return new CRC result.
 */
uint32_t CRC_HAL_GetCrc32(CRC_Type * base, uint32_t data, bool newSeed, uint32_t seed);

/*!
 * @brief CRC_HAL_GetCrc16
 *
 * This method appends the 16-bit data to the current CRC calculation
 * and returns a new result. If the newSeed is true, seed set and
 * result are calculated from the seed new value (new CRC calculation).
 *
 * @param base The CRC peripheral base address.
 * @param data input data for CRC calculation
 * @param newSeed Sets new CRC calculation.
 *        - true: New seed set and used for new calculation.
 *        - false: seed argument ignored, continues old calculation.
 * @param seed New seed if newSeed is true, else ignored
 * @return new CRC result.
 */
uint32_t CRC_HAL_GetCrc16(CRC_Type * base, uint16_t data, bool newSeed, uint32_t seed);

/*!
 * @brief CRC_HAL_GetCrc8
 *
 * This method appends the 8-bit data to the current CRC calculation
 * and returns the new result. If the newSeed is true, seed set and
 * result are calculated from the seed new value (new CRC calculation).
 *
 * @param base The CRC peripheral base address.
 * @param data input data for CRC calculation
 * @param newSeed Sets new CRC calculation.
 *        - true: New seed set and used for new calculation.
 *        - false: seed argument ignored, continues old calculation.
 * @param seed New seed if newSeed is true, else ignored
 * @return new CRC result.
 */
uint32_t CRC_HAL_GetCrc8(CRC_Type * base, uint8_t data, bool newSeed, uint32_t seed);

/*!
 * @brief CRC_HAL_GetCrcResult
 *
 * This method returns the current result of the CRC calculation.
 * The result is the ReadTranspose dependent.
 *
 * @param base The CRC peripheral base address.
 * @return result of CRC calculation.
 */
uint32_t CRC_HAL_GetCrcResult(CRC_Type * base);

/*@}*/

#if defined(__cplusplus)
}
#endif /* __cplusplus*/

/*! @}*/

#endif
#endif /* __FSL_CRC_HAL_H__*/