Zephyr API Documentation 4.4.0-rc2
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
156
162typedef int (*crc_api_begin)(const struct device *dev, struct crc_ctx *ctx);
163
169typedef int (*crc_api_update)(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
170 size_t bufsize);
171
177typedef int (*crc_api_finish)(const struct device *dev, struct crc_ctx *ctx);
178
190
192
203__syscall int crc_begin(const struct device *dev, struct crc_ctx *ctx);
204
205static inline int z_impl_crc_begin(const struct device *dev, struct crc_ctx *ctx)
206{
207 const struct crc_driver_api *api = (const struct crc_driver_api *)dev->api;
208
209 if (api->begin == NULL) {
210 return -ENOSYS;
211 }
212
213 return api->begin(dev, ctx);
214}
215
228__syscall int crc_update(const struct device *dev, struct crc_ctx *ctx, const void *buffer,
229 size_t bufsize);
230
231static inline int z_impl_crc_update(const struct device *dev, struct crc_ctx *ctx,
232 const void *buffer, size_t bufsize)
233{
234 const struct crc_driver_api *api = (const struct crc_driver_api *)dev->api;
235
236 if (api->update == NULL) {
237 return -ENOSYS;
238 }
239
240 return api->update(dev, ctx, buffer, bufsize);
241}
242
253__syscall int crc_finish(const struct device *dev, struct crc_ctx *ctx);
254
255static inline int z_impl_crc_finish(const struct device *dev, struct crc_ctx *ctx)
256{
257 const struct crc_driver_api *api = (const struct crc_driver_api *)dev->api;
258
259 if (api->finish == NULL) {
260 return -ENOSYS;
261 }
262
263 return api->finish(dev, ctx);
264}
265
276static inline int crc_verify(struct crc_ctx *ctx, crc_result_t expected)
277{
278 if (ctx == NULL) {
279 return -EINVAL;
280 }
281
282 if (ctx->state == CRC_STATE_IN_PROGRESS) {
283 return -EBUSY;
284 }
285
286 if (expected != ctx->result) {
287 return -EPERM;
288 }
289
290 return 0;
291}
292
296
297#ifdef __cplusplus
298}
299#endif
300
301#include <zephyr/syscalls/crc.h>
302
303#endif /* ZEPHYR_INCLUDE_DRIVERS_CRC_H */
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:169
int(* crc_api_begin)(const struct device *dev, struct crc_ctx *ctx)
Callback API to configure the CRC unit for calculation.
Definition crc.h:162
int(* crc_api_finish)(const struct device *dev, struct crc_ctx *ctx)
Callback API to finalize CRC calculation.
Definition crc.h:177
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
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_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:276
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
<span class="mlabel">Driver Operations</span> CRC driver operations
Definition crc.h:182
crc_api_finish finish
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crc.h:188
crc_api_update update
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crc.h:186
crc_api_begin begin
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition crc.h:184
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.