Skip to content

Commit 705b355

Browse files
Duy Vothenguyenyf
authored andcommitted
samples: drivers: crc: add samples for CRC driver
Add samples for CRC driver Signed-off-by: Duy Vo <duy.vo.xc@bp.renesas.com>
1 parent ff11fb9 commit 705b355

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

samples/drivers/crc/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(driver_crc_example)
6+
7+
target_sources(app PRIVATE src/main.c)

samples/drivers/crc/README.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.. zephyr:code-sample:: crc_drivers
2+
:name: Cyclic Redundancy Check Drivers (CRC)
3+
:relevant-api: crc_interface
4+
5+
Compute and verify a CRC checksum using the CRC driver API.
6+
7+
Overview
8+
********
9+
10+
This sample demonstrates how to use the :ref:`CRC driver API <crc_api>`.
11+
12+
Configuration Options
13+
*********************
14+
15+
This sample supports the following Kconfig options:
16+
17+
- ``CONFIG_CRC_HW``: Enable CRC driver.
18+
19+
These options can be modified in the project's ``prj.conf`` file or passed via CMake arguments.
20+
21+
Building and Running
22+
********************
23+
24+
Building and Running for Renesas RA8M1
25+
======================================
26+
27+
The sample can be built and executed for the
28+
:zephyr:board:`ek_ra8m1` as follows:
29+
30+
.. zephyr-app-commands::
31+
:zephyr-app: samples/drivers/crc
32+
:board: ek_ra8m1
33+
:goals: build flash
34+
:compact:
35+
36+
To build for another board, change "ek_ra8m1" above to that board's name.
37+
38+
Sample Output
39+
=============
40+
41+
.. code-block:: console
42+
43+
crc_example: CRC verification succeed.
44+
45+
.. note:: If the CRC is not supported, the output will be an error message.
46+
47+
Expected Behavior
48+
*****************
49+
50+
When the sample runs, it should:
51+
52+
1. Compute the CRC8 values of predefined data.
53+
2. Verify the CRC result.

samples/drivers/crc/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_CRC_NEW=y
2+
CONFIG_CRC_LOG_LEVEL_INF=y
3+
CONFIG_LOG=y

samples/drivers/crc/sample.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sample:
2+
name: CRC Sample
3+
tests:
4+
sample.drivers.crc:
5+
depends_on: crc
6+
tags:
7+
- drivers
8+
- crc
9+
harness: console
10+
harness_config:
11+
type: one_line
12+
regex:
13+
- "CRC verification succeeded"

samples/drivers/crc/src/main.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
LOG_MODULE_REGISTER(crc_example, CONFIG_LOG_DEFAULT_LEVEL);
9+
10+
#include <zephyr/device.h>
11+
#include <zephyr/drivers/crc.h>
12+
13+
/* The devicetree node identifier for the "crc" */
14+
#define CRC_NODE DT_NODELABEL(crc)
15+
16+
/*
17+
* A build error on this line means your board is unsupported.
18+
* See the sample documentation for information on how to fix this.
19+
*/
20+
21+
int main(void)
22+
{
23+
static const struct device *const dev = DEVICE_DT_GET(CRC_NODE);
24+
/* Define the data to compute CRC */
25+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
26+
int ret;
27+
28+
if (!device_is_ready(dev)) {
29+
LOG_ERR("Device is not ready");
30+
return -ENODEV;
31+
}
32+
33+
struct crc_ctx ctx = {
34+
.type = CRC8,
35+
.polynomial = CRC8_POLY,
36+
.seed = CRC8_INIT_VAL,
37+
.reversed = CRC_FLAG_REVERSE_OUTPUT | CRC_FLAG_REVERSE_INPUT,
38+
};
39+
40+
/* Start CRC computation */
41+
ret = crc_begin(dev, &ctx);
42+
43+
if (ret != 0) {
44+
LOG_ERR("Failed to begin CRC: %d", ret);
45+
return ret;
46+
}
47+
48+
/* Update CRC computation */
49+
ret = crc_update(dev, &ctx, data, 8);
50+
51+
if (ret != 0) {
52+
LOG_ERR("Failed to update CRC: %d", ret);
53+
return ret;
54+
}
55+
56+
/* Finish CRC computation */
57+
ret = crc_finish(dev, &ctx);
58+
59+
if (ret != 0) {
60+
LOG_ERR("Failed to finish CRC: %d", ret);
61+
return ret;
62+
}
63+
/* Verify CRC computation
64+
* (example expected value: 0xB2 with LSB, 0x4D with MSB bit order)
65+
*/
66+
if (ctx.reversed != (CRC_FLAG_REVERSE_OUTPUT | CRC_FLAG_REVERSE_INPUT)) {
67+
ret = crc_verify(&ctx, 0x4D);
68+
69+
if (ret != 0) {
70+
LOG_ERR("CRC verification failed: %d", ret);
71+
return ret;
72+
}
73+
} else { /* Reversed is no reversed output */
74+
ret = crc_verify(&ctx, 0xB2);
75+
76+
if (ret != 0) {
77+
LOG_ERR("CRC verification failed: %d", ret);
78+
return ret;
79+
}
80+
}
81+
82+
LOG_INF("CRC verification succeeded");
83+
84+
return 0;
85+
}

0 commit comments

Comments
 (0)