Skip to content

Commit 32b2679

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 04443b2 commit 32b2679

File tree

5 files changed

+122
-0
lines changed

5 files changed

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