Zephyr Project API 4.3.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
31
38
42#define CRC_FLAG_REVERSE_INPUT BIT(0)
43
47#define CRC_FLAG_REVERSE_OUTPUT BIT(1)
48
50
56
58#define CRC4_INIT_VAL 0x0
59
61#define CRC4_TI_INIT_VAL 0x0
62
64#define CRC7_BE_INIT_VAL 0x0
65
67#define CRC8_INIT_VAL 0x0
68
70#define CRC8_CCITT_INIT_VAL 0xFF
71
73#define CRC8_ROHC_INIT_VAL 0xFF
74
76#define CRC16_INIT_VAL 0x0
77
79#define CRC16_ANSI_INIT_VAL 0x0
80
82#define CRC16_CCITT_INIT_VAL 0x0000
83
85#define CRC16_ITU_T_INIT_VAL 0x0000
86
88#define CRC24_PGP_INIT_VALUE 0x00B704CEU
89
91#define CRC32_C_INIT_VAL 0xFFFFFFFFU
92
94#define CRC32_IEEE_INIT_VAL 0xFFFFFFFFU
95
97#define CRC32_K_4_2_INIT_VAL 0xFFFFFFFFU
98
100
104
111
116
122
127
135
150
155typedef int (*crc_api_begin)(const struct device *dev, struct crc_ctx *ctx);
156
161typedef int (*crc_api_update)(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
162 size_t bufsize);
163
168typedef int (*crc_api_finish)(const struct device *dev, struct crc_ctx *ctx);
169
175
186__syscall int crc_begin(const struct device *dev, struct crc_ctx *ctx);
187
188static inline int z_impl_crc_begin(const struct device *dev, struct crc_ctx *ctx)
189{
190 const struct crc_driver_api *api = (const struct crc_driver_api *)dev->api;
191
192 if (api->begin == NULL) {
193 return -ENOSYS;
194 }
195
196 return api->begin(dev, ctx);
197}
198
211__syscall int crc_update(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
212 size_t bufsize);
213
214static inline int z_impl_crc_update(const struct device *dev, struct crc_ctx *ctx,
215 const void *buffer, size_t bufsize)
216{
217 const struct crc_driver_api *api = (const struct crc_driver_api *)dev->api;
218
219 if (api->update == NULL) {
220 return -ENOSYS;
221 }
222
223 return api->update(dev, ctx, buffer, bufsize);
224}
225
236__syscall int crc_finish(const struct device *dev, struct crc_ctx *ctx);
237
238static inline int z_impl_crc_finish(const struct device *dev, struct crc_ctx *ctx)
239{
240 const struct crc_driver_api *api = (const struct crc_driver_api *)dev->api;
241
242 if (api->finish == NULL) {
243 return -ENOSYS;
244 }
245
246 return api->finish(dev, ctx);
247}
248
259static inline int crc_verify(struct crc_ctx *ctx, crc_result_t expected)
260{
261 if (ctx == NULL) {
262 return -EINVAL;
263 }
264
265 if (ctx->state == CRC_STATE_IN_PROGRESS) {
266 return -EBUSY;
267 }
268
269 if (expected != ctx->result) {
270 return -EPERM;
271 }
272
273 return 0;
274}
275
279
280#ifdef __cplusplus
281}
282#endif
283
284#include <zephyr/syscalls/crc.h>
285
286#endif /* ZEPHYR_INCLUDE_DRIVERS_CRC_H */
uint32_t crc_init_val_t
Provides a type to hold CRC initial seed value.
Definition crc.h:115
uint32_t crc_result_t
Provides a type to hold CRC result value.
Definition crc.h:126
int(* crc_api_update)(const struct device *dev, struct crc_ctx *ctx, const void *buffer, size_t bufsize)
Callback API upon CRC calculation stream update See crc_update() for argument description.
Definition crc.h:161
int(* crc_api_begin)(const struct device *dev, struct crc_ctx *ctx)
Callback API upon CRC calculation begin See crc_begin() for argument description.
Definition crc.h:155
uint32_t crc_poly_t
Provides a type to hold CRC polynomial value.
Definition crc.h:121
int crc_finish(const struct device *dev, struct crc_ctx *ctx)
Finalize CRC calculation.
crc_state
CRC state enumeration.
Definition crc.h:105
int(* crc_api_finish)(const struct device *dev, struct crc_ctx *ctx)
Callback API upon CRC calculation finish See crc_finish() for argument description.
Definition crc.h:168
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:259
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:109
@ CRC_STATE_IDLE
CRC device is in IDLE state.
Definition crc.h:107
crc_type
CRC algorithm enumeration.
Definition crc.h:104
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define EBUSY
Mount device busy.
Definition errno.h:54
#define EPERM
Not owner.
Definition errno.h:39
#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:136
crc_poly_t polynomial
CRC polynomial.
Definition crc.h:144
enum crc_state state
Current CRC device state.
Definition crc.h:140
crc_init_val_t seed
CRC initial seed value.
Definition crc.h:146
uint32_t reversed
CRC input/output reverse flags.
Definition crc.h:142
crc_result_t result
CRC result.
Definition crc.h:148
enum crc_type type
CRC calculation type.
Definition crc.h:138
Definition crc.h:170
crc_api_finish finish
Definition crc.h:173
crc_api_update update
Definition crc.h:172
crc_api_begin begin
Definition crc.h:171
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
CRC computation function.