zephyr/samples/sensor/clock
Mark Chen f4da9b9705 drivers: sensor: Add sensor clock API support
This commit introduces a new Sensor Clock API, enabling the retrieval
of cycle counts and conversion to nanoseconds based on the system or
external clock. The API includes:

- `sensor_clock_get_cycles()` to get the current cycle count from the
  sensor clock.
- `sensor_clock_cycles_to_ns()` to convert cycles to nanoseconds using
  the clock's frequency.

The implementation supports both system clocks and external clocks
defined in the device tree, making the sensor clock integration more
flexible for various sensor use cases.

Signed-off-by: Mark Chen <mark.chen@cienet.com>
2025-01-15 19:03:13 +01:00
..
boards
src
CMakeLists.txt
prj.conf
README.rst
sample.yaml

.. zephyr:code-sample:: sensor_clock
   :name: Sensor Clock
   :relevant-api: sensor_interface

   Test and debug Sensor Clock functionality.

Overview
********

This sample application demonstrates how to select the sensor clock source
and utilize the Sensor Clock API.

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

The sample below uses the :ref:`nrf52840dk_nrf52840` and :ref:`nrf52833dk_nrf52833` boards.

To run this sample, ensure the following configurations:

    * Enable one of the Kconfig options:
      :kconfig:option:`CONFIG_SENSOR_CLOCK_COUNTER`,
      :kconfig:option:`CONFIG_SENSOR_CLOCK_RTC`, or
      :kconfig:option:`CONFIG_SENSOR_CLOCK_SYSTEM`.

Build and run the sample with the following command:

.. zephyr-app-commands::
   :zephyr-app: samples/sensor/clock
   :board: <board to use>
   :goals: build flash

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

The application will print the current sensor clock cycles and
their corresponding time in nanoseconds.

.. code-block:: console

   Cycles: 143783087
   Nanoseconds: 8986442937
   Cycles: 159776386
   Nanoseconds: 9986024125
   Cycles: 175772543
   Nanoseconds: 10985783937
   Cycles: 191771203
   Nanoseconds: 11985700187
   Cycles: 207758870
   Nanoseconds: 12984929375
   Cycles: 223752074
   Nanoseconds: 13984504625

   ...