Zephyr API Documentation 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
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#define CRC32_MPEG2_INIT_VAL 0xFFFFFFFFU
101
103
107
114
119
125
130
138
153
159
165typedef int (*crc_api_begin)(const struct device *dev, struct crc_ctx *ctx);
166
172typedef int (*crc_api_update)(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
173 size_t bufsize);
174
180typedef int (*crc_api_finish)(const struct device *dev, struct crc_ctx *ctx);
181
193
195
206__syscall int crc_begin(const struct device *dev, struct crc_ctx *ctx);
207
208static inline int z_impl_crc_begin(const struct device *dev, struct crc_ctx *ctx)
209{
210 const struct crc_driver_api *api = DEVICE_API_GET(crc, dev);
211
212 if (api->begin == NULL) {
213 return -ENOSYS;
214 }
215
216 return api->begin(dev, ctx);
217}
218
231__syscall int crc_update(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
232 size_t bufsize);
233
234static inline int z_impl_crc_update(const struct device *dev, struct crc_ctx *ctx,
235 const void *buffer, size_t bufsize)
236{
237 const struct crc_driver_api *api = DEVICE_API_GET(crc, dev);
238
239 if (api->update == NULL) {
240 return -ENOSYS;
241 }
242
243 return api->update(dev, ctx, buffer, bufsize);
244}
245
256__syscall int crc_finish(const struct device *dev, struct crc_ctx *ctx);
257
258static inline int z_impl_crc_finish(const struct device *dev, struct crc_ctx *ctx)
259{
260 const struct crc_driver_api *api = DEVICE_API_GET(crc, dev);
261
262 if (api->finish == NULL) {
263 return -ENOSYS;
264 }
265
266 return api->finish(dev, ctx);
267}
268
279static inline int crc_verify(struct crc_ctx *ctx, crc_result_t expected)
280{
281 if (ctx == NULL) {
282 return -EINVAL;
283 }
284
285 if (ctx->state == CRC_STATE_IN_PROGRESS) {
286 return -EBUSY;
287 }
288
289 if (expected != ctx->result) {
290 return -EPERM;
291 }
292
293 return 0;
294}
295
299
300#ifdef __cplusplus
301}
302#endif
303
304#include <zephyr/syscalls/crc.h>
305
306#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:1425
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:172
int(* crc_api_begin)(const struct device *dev, struct crc_ctx *ctx)
Callback API to configure the CRC unit for calculation.
Definition crc.h:165
int(* crc_api_finish)(const struct device *dev, struct crc_ctx *ctx)
Callback API to finalize CRC calculation.
Definition crc.h:180
uint32_t crc_init_val_t
Provides a type to hold CRC initial seed value.
Definition crc.h:118
uint32_t crc_result_t
Provides a type to hold CRC result value.
Definition crc.h:129
uint32_t crc_poly_t
Provides a type to hold CRC polynomial value.
Definition crc.h:124
int crc_finish(const struct device *dev, struct crc_ctx *ctx)
Finalize CRC calculation.
crc_state
CRC state enumeration.
Definition crc.h:108
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:279
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:112
@ CRC_STATE_IDLE
CRC device is in IDLE state.
Definition crc.h:110
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
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
CRC context structure.
Definition crc.h:139
crc_poly_t polynomial
CRC polynomial.
Definition crc.h:147
enum crc_state state
Current CRC device state.
Definition crc.h:143
crc_init_val_t seed
CRC initial seed value.
Definition crc.h:149
uint32_t reversed
CRC input/output reverse flags.
Definition crc.h:145
crc_result_t result
CRC result.
Definition crc.h:151
enum crc_type type
CRC calculation type.
Definition crc.h:141
<span class="mlabel">Driver Operations</span> CRC driver operations
Definition crc.h:185
crc_api_finish finish
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crc.h:191
crc_api_update update
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crc.h:189
crc_api_begin begin
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crc.h:187
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
CRC computation function.