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>
29 lines
532 B
C
29 lines
532 B
C
/*
|
|
* Copyright (c) 2024 Cienet
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/drivers/sensor_clock.h>
|
|
#include <zephyr/sys_clock.h>
|
|
|
|
int sensor_clock_get_cycles(uint64_t *cycles)
|
|
{
|
|
if (cycles == NULL) {
|
|
return -EINVAL;
|
|
}
|
|
|
|
#ifdef CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER
|
|
*cycles = k_cycle_get_64();
|
|
#else
|
|
*cycles = (uint64_t)k_cycle_get_32();
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
uint64_t sensor_clock_cycles_to_ns(uint64_t cycles)
|
|
{
|
|
return (cycles * NSEC_PER_SEC) / sys_clock_hw_cycles_per_sec();
|
|
}
|