Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
crc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Brill Power Ltd.
3 * Copyright (c) 2025 Renesas Electronics Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13
14#ifndef ZEPHYR_INCLUDE_DRIVERS_CRC_H
15#define ZEPHYR_INCLUDE_DRIVERS_CRC_H
16
17#include <zephyr/device.h>
18#include <zephyr/kernel.h>
19#include <zephyr/sys/crc.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
33
40
44#define CRC_FLAG_REVERSE_INPUT BIT(0)
45
49#define CRC_FLAG_REVERSE_OUTPUT BIT(1)
50
52
58
60#define CRC4_INIT_VAL 0x0
61
63#define CRC4_TI_INIT_VAL 0x0
64
66#define CRC7_BE_INIT_VAL 0x0
67
69#define CRC8_INIT_VAL 0x0
70
72#define CRC8_CCITT_INIT_VAL 0xFF
73
75#define CRC8_ROHC_INIT_VAL 0xFF
76
78#define CRC16_INIT_VAL 0x0
79
81#define CRC16_ANSI_INIT_VAL 0x0
82
84#define CRC16_CCITT_INIT_VAL 0x0000
85
87#define CRC16_ITU_T_INIT_VAL 0x0000
88
90#define CRC24_PGP_INIT_VALUE 0x00B704CEU
91
93#define CRC32_C_INIT_VAL 0xFFFFFFFFU
94
96#define CRC32_IEEE_INIT_VAL 0xFFFFFFFFU
97
99#define CRC32_K_4_2_INIT_VAL 0xFFFFFFFFU
100
102#define CRC32_MPEG2_INIT_VAL 0xFFFFFFFFU
103
105
109
116
121
127
132
140
155
161
167typedef int (*crc_api_begin)(const struct device *dev, struct crc_ctx *ctx);
168
174typedef int (*crc_api_update)(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
175 size_t bufsize);
176
182typedef int (*crc_api_finish)(const struct device *dev, struct crc_ctx *ctx);
183
195
197
208__syscall int crc_begin(const struct device *dev, struct crc_ctx *ctx);
209
210static inline int z_impl_crc_begin(const struct device *dev, struct crc_ctx *ctx)
211{
212 const struct crc_driver_api *api = DEVICE_API_GET(crc, dev);
213
214 if (api->begin == NULL) {
215 return -ENOSYS;
216 }
217
218 return api->begin(dev, ctx);
219}
220
233__syscall int crc_update(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
234 size_t bufsize);
235
236static inline int z_impl_crc_update(const struct device *dev, struct crc_ctx *ctx,
237 const void *buffer, size_t bufsize)
238{
239 const struct crc_driver_api *api = DEVICE_API_GET(crc, dev);
240
241 if (api->update == NULL) {
242 return -ENOSYS;
243 }
244
245 return api->update(dev, ctx, buffer, bufsize);
246}
247
258__syscall int crc_finish(const struct device *dev, struct crc_ctx *ctx);
259
260static inline int z_impl_crc_finish(const struct device *dev, struct crc_ctx *ctx)
261{
262 const struct crc_driver_api *api = DEVICE_API_GET(crc, dev);
263
264 if (api->finish == NULL) {
265 return -ENOSYS;
266 }
267
268 return api->finish(dev, ctx);
269}
270
281static inline int crc_verify(struct crc_ctx *ctx, crc_result_t expected)
282{
283 if (ctx == NULL) {
284 return -EINVAL;
285 }
286
287 if (ctx->state == CRC_STATE_IN_PROGRESS) {
288 return -EBUSY;
289 }
290
291 if (expected != ctx->result) {
292 return -EPERM;
293 }
294
295 return 0;
296}
297
301
302#ifdef __cplusplus
303}
304#endif
305
306#include <zephyr/syscalls/crc.h>
307
308#endif /* ZEPHYR_INCLUDE_DRIVERS_CRC_H */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1449
uint32_t crc_init_val_t
Provides a type to hold CRC initial seed value.
Definition crc.h:120
uint32_t crc_result_t
Provides a type to hold CRC result value.
Definition crc.h:131
int(* crc_api_update)(const struct device *dev, struct crc_ctx *ctx, const void *buffer, size_t bufsize)
Callback API to feed data into an in-progress CRC calculation.
Definition crc.h:174
int(* crc_api_begin)(const struct device *dev, struct crc_ctx *ctx)
@def_driverbackendgroup{CRC,crc_interface}
Definition crc.h:167
uint32_t crc_poly_t
Provides a type to hold CRC polynomial value.
Definition crc.h:126
int crc_finish(const struct device *dev, struct crc_ctx *ctx)
Finalize CRC calculation.
crc_state
CRC state enumeration.
Definition crc.h:110
int(* crc_api_finish)(const struct device *dev, struct crc_ctx *ctx)
Callback API to finalize CRC calculation.
Definition crc.h:182
int crc_begin(const struct device *dev, struct crc_ctx *ctx)
Configure CRC unit for calculation.
static int crc_verify(struct crc_ctx *ctx, crc_result_t expected)
Verify CRC result.
Definition crc.h:281
int crc_update(const struct device *dev, struct crc_ctx *ctx, const void *buffer, size_t bufsize)
Perform CRC calculation on the provided data buffer and retrieve result.
@ CRC_STATE_IN_PROGRESS
CRC calculation is in-progress.
Definition crc.h:114
@ CRC_STATE_IDLE
CRC device is in IDLE state.
Definition crc.h:112
crc_type
CRC algorithm enumeration.
Definition crc.h:106
#define EINVAL
Invalid argument.
Definition errno.h:61
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define EBUSY
Mount device busy.
Definition errno.h:55
#define EPERM
Not owner.
Definition errno.h:40
#define NULL
Definition iar_missing_defs.h:20
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
CRC context structure.
Definition crc.h:141
crc_poly_t polynomial
CRC polynomial.
Definition crc.h:149
enum crc_state state
Current CRC device state.
Definition crc.h:145
crc_init_val_t seed
CRC initial seed value.
Definition crc.h:151
uint32_t reversed
CRC input/output reverse flags.
Definition crc.h:147
crc_result_t result
CRC result.
Definition crc.h:153
enum crc_type type
CRC calculation type.
Definition crc.h:143
@driver_ops{CRC}
Definition crc.h:187
crc_api_finish finish
@driver_ops_optional Finalize CRC calculation.
Definition crc.h:193
crc_api_update update
@driver_ops_optional Perform CRC calculation on the provided data buffer and retrieve result.
Definition crc.h:191
crc_api_begin begin
@driver_ops_optional Configure CRC unit for calculation.
Definition crc.h:189
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
CRC computation function.