Skip to content

Commit 1ade790

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

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

samples/subsys/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(subsys_crc_example)
6+
7+
target_sources(app PRIVATE src/main.c)

samples/subsys/crc/README.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.. zephyr:code-sample:: crc_subsys
2+
:name: Cyclic Redundancy Check Subsystem (CRC Subsys)
3+
:relevant-api: crc_new_interface
4+
5+
Compute and verify a CRC computation using the CRC subsys API.
6+
7+
Overview
8+
********
9+
10+
This sample demonstrates how to use the :ref:`CRC New Function API`
11+
If the board does not have a CRC chosen node, the sample will use a software-based
12+
CRC implementation from ``zephyr/lib/crc``.
13+
14+
Configuration Options
15+
*********************
16+
17+
This sample supports the following Kconfig options:
18+
19+
- ``CONFIG_CRC_NEW``: Enable CRC functionality.
20+
- ``CONFIG_CRC_SOFTWARE_BASED``: Use a software-based CRC implementation if a CRC chosen node is present.
21+
- ``CONFIG_CRC_HARDWARE_ACCELERATOR``: Use hardware acceleration for CRC if a CRC chosen node is not present.
22+
23+
These options can be modified in the project's ``prj.conf`` file or passed via CMake arguments.
24+
25+
Building and Running
26+
********************
27+
28+
Building and Running for Renesas RA8M1
29+
======================================
30+
31+
The sample can be built and executed for the
32+
:zephyr:board:`ek_ra8m1` as follows:
33+
34+
.. zephyr-app-commands::
35+
:zephyr-app: samples/subsys/crc
36+
:board: ek_ra8m1
37+
:goals: build flash
38+
:compact:
39+
40+
To build for another board, change "ek_ra8m1" above to that board's name.
41+
42+
Sample Output
43+
=============
44+
45+
.. code-block:: console
46+
47+
subsys_crc_example: Result of CRC32 IEEE: 0xCEA4A6C2
48+
subsys_crc_example: Result of CRC8 CCITT: 0x96
49+
subsys_crc_example: CRC computation completed successfully
50+
51+
.. note::
52+
If the board does not support a hardware CRC driver, the computation will fall
53+
back to a software-based implementation.
54+
55+
Expected Behavior
56+
*****************
57+
58+
When the sample runs, it should:
59+
60+
1. Compute the CRC32 and CRC8 values of predefined data.
61+
2. Print the computed CRC values.

samples/subsys/crc/prj.conf

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

samples/subsys/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 Subsystem
3+
tests:
4+
samples.subsys.crc:
5+
depends_on: crc
6+
tags:
7+
- subsys
8+
- crc
9+
harness: console
10+
harness_config:
11+
type: one_line
12+
regex:
13+
- "CRC computation completed successfully"

samples/subsys/crc/src/main.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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(subsys_crc_example, CONFIG_LOG_DEFAULT_LEVEL);
9+
10+
#include <zephyr/crc_new/crc_new.h>
11+
12+
int main(void)
13+
{
14+
uint32_t result;
15+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
16+
int ret;
17+
18+
/* CRC computation */
19+
ret = crc32_ieee_new(data, sizeof(data), &result);
20+
21+
if (ret != 0) {
22+
LOG_ERR("Failed compute CRC32_IEEE: %d", ret);
23+
return ret;
24+
}
25+
26+
LOG_INF("Result of CRC32 IEEE: 0x%08X", result);
27+
28+
/* CRC computation */
29+
ret = crc8_ccitt_new(0xFF, data, sizeof(data), (uint8_t *)&result);
30+
31+
if (ret != 0) {
32+
LOG_ERR("Failed compute CRC8_CCITT: %d", ret);
33+
return ret;
34+
}
35+
36+
LOG_INF("Result of CRC8 CCITT: 0x%02X", result & 0xFF);
37+
38+
LOG_INF("CRC computation completed successfully");
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)