Skip to content

Commit d99de4a

Browse files
author
Zoe Kaute
committed
samples: drivers: crc Add sample for hardware based CRC API
Introduces a new sample that demonstrates how to use the hardware-based CRC API. Signed-off-by: Zoe Kaute <zoe.kaute@brillpower.com>
1 parent 4b51773 commit d99de4a

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

samples/drivers/crc/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
6+
project(crc_sample)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/drivers/crc/README.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. zephyr:code-sample:: crc
2+
:name: Cyclic Redundancy Check (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+
Building and Running
13+
********************
14+
15+
Building and Running for ST Nucleo G474RE
16+
=========================================
17+
The sample can be built and executed for the
18+
:ref:`nucleo_g474re_board` as follows:
19+
20+
.. zephyr-app-commands::
21+
:zephyr-app: samples/drivers/crc
22+
:board: nucleo_g474re
23+
:goals: build flash
24+
:compact:

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_HW=y
2+
CONFIG_CRC_HW_LOG_LEVEL_INF=y
3+
CONFIG_LOG=y

samples/drivers/crc/sample.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
sample:
4+
name: CRC Sample
5+
tests:
6+
crc_sample.test:
7+
depends_on: crc
8+
tags:
9+
- drivers
10+
- crc

samples/drivers/crc/src/main.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2024 Brill Power Ltd.
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/drivers/crc.h>
11+
#include <zephyr/device.h>
12+
#include <zephyr/kernel.h>
13+
14+
int main(void)
15+
{
16+
/* Retrieve the CRC device */
17+
static const struct device *dev = DEVICE_DT_GET(DT_ALIAS(crc0));
18+
19+
/* Ensure the device is ready */
20+
if (!device_is_ready(dev)) {
21+
LOG_ERR("CRC device not found or not ready\n");
22+
return -ENODEV;
23+
}
24+
25+
/* Define the data to compute CRC */
26+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
27+
28+
/* Define the CRC context */
29+
struct crc_ctx ctx = {.type = CRC8_CCITT_HW,
30+
.flags = 0,
31+
.polynomial = CRC8_CCITT_POLY,
32+
.initial_value = CRC8_CCITT_INIT_VAL};
33+
34+
/* Start CRC computation */
35+
int ret = crc_begin(dev, &ctx);
36+
37+
if (ret != 0) {
38+
LOG_ERR("Failed to begin CRC: %d\n", ret);
39+
return ret;
40+
}
41+
42+
/* Update CRC computation */
43+
ret = crc_update(dev, &ctx, data, 8);
44+
45+
if (ret != 0) {
46+
LOG_ERR("Failed to update CRC: %d\n", ret);
47+
return ret;
48+
}
49+
50+
/* Finish CRC computation */
51+
ret = crc_finish(dev, &ctx);
52+
53+
if (ret != 0) {
54+
LOG_ERR("Failed to finish CRC: %d\n", ret);
55+
return ret;
56+
}
57+
58+
/* Verify the computed CRC (example expected value: 0x4D) */
59+
ret = crc_verify(&ctx, 0x4D);
60+
61+
if (ret != 0) {
62+
LOG_ERR("CRC verification failed: %d\n", ret);
63+
return ret;
64+
}
65+
66+
LOG_INF("CRC verification succeeded");
67+
68+
return ret;
69+
}

0 commit comments

Comments
 (0)