Commit 121aeab9 authored by Duy Vo's avatar Duy Vo Committed by Benjamin Cabé
Browse files

samples: drivers: crc: add samples for CRC driver



Add samples for CRC driver

Signed-off-by: default avatarDuy Vo <duy.vo.xc@bp.renesas.com>
parent 0c5af6f2
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(driver_crc_example)

target_sources(app PRIVATE src/main.c)
+44 −0
Original line number Diff line number Diff line
.. zephyr:code-sample:: crc_drivers
   :name: Cyclic Redundancy Check Drivers (CRC)
   :relevant-api: crc_interface

   Compute and verify a CRC checksum using the CRC driver API.

Overview
********

This sample demonstrates how to use the :ref:`CRC driver API <crc_api>`.

Building and Running
********************

Building and Running for Renesas RA8M1
======================================

The sample can be built and executed for the
:zephyr:board:`ek_ra8m1` as follows:

.. zephyr-app-commands::
   :zephyr-app: samples/drivers/crc
   :board: ek_ra8m1
   :goals: build flash
   :compact:

To build for another board, change "ek_ra8m1" above to that board's name.

Sample Output
=============

.. code-block:: console

   crc_example: CRC verification succeed.

.. note:: If the CRC is not supported, the output will be an error message.

Expected Behavior
*****************

When the sample runs, it should:

1. Compute the CRC8 values of predefined data.
2. Verify the CRC result.
+2 −0
Original line number Diff line number Diff line
CONFIG_CRC=y
CONFIG_LOG=y
+13 −0
Original line number Diff line number Diff line
sample:
  name: CRC Sample
tests:
  sample.drivers.crc:
    depends_on: crc
    tags:
      - drivers
      - crc
    harness: console
    harness_config:
      type: one_line
      regex:
        - "CRC verification succeeded"
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2025 Renesas Electronics Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(crc_example, CONFIG_LOG_DEFAULT_LEVEL);

#include <zephyr/device.h>
#include <zephyr/drivers/crc.h>

/* The devicetree node identifier for the "crc" */
#define CRC_NODE DT_CHOSEN(zephyr_crc)

/*
 * A build error on this line means your board is unsupported.
 * See the sample documentation for information on how to fix this.
 */

int main(void)
{
	static const struct device *const dev = DEVICE_DT_GET(CRC_NODE);
	/* Define the data to compute CRC */
	uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
	int ret;

	if (!device_is_ready(dev)) {
		LOG_ERR("Device is not ready");
		return -ENODEV;
	}

	struct crc_ctx ctx = {
		.type = CRC8,
		.polynomial = CRC8_POLY,
		.seed = CRC8_INIT_VAL,
		.reversed = CRC_FLAG_REVERSE_OUTPUT | CRC_FLAG_REVERSE_INPUT,
	};

	/* Start CRC computation */
	ret = crc_begin(dev, &ctx);

	if (ret != 0) {
		LOG_ERR("Failed to begin CRC: %d", ret);
		return ret;
	}

	/* Update CRC computation */
	ret = crc_update(dev, &ctx, data, 8);

	if (ret != 0) {
		LOG_ERR("Failed to update CRC: %d", ret);
		return ret;
	}

	/* Finish CRC computation */
	ret = crc_finish(dev, &ctx);

	if (ret != 0) {
		LOG_ERR("Failed to finish CRC: %d", ret);
		return ret;
	}
	/* Verify CRC computation
	 * (example expected value: 0xB2 with LSB, 0x4D with MSB bit order)
	 */
	if (ctx.reversed != (CRC_FLAG_REVERSE_OUTPUT | CRC_FLAG_REVERSE_INPUT)) {
		ret = crc_verify(&ctx, 0x4D);

		if (ret != 0) {
			LOG_ERR("CRC verification failed: %d", ret);
			return ret;
		}
	} else { /* Reversed is no reversed output */
		ret = crc_verify(&ctx, 0xB2);

		if (ret != 0) {
			LOG_ERR("CRC verification failed: %d", ret);
			return ret;
		}
	}

	LOG_INF("CRC verification succeeded");

	return 0;
}