Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
counter.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation
3 * Copyright (c) 2018 Nordic Semiconductor ASA
4 * Copyright (c) 2026 Meta Platforms
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
14
15#ifndef ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_
16#define ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_
17
31
32#include <errno.h>
33
34#include <zephyr/types.h>
35#include <stddef.h>
36#include <zephyr/device.h>
37#include <zephyr/devicetree.h>
39#include <zephyr/sys/clock.h>
40#include <stdbool.h>
41
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
53
57#define COUNTER_CONFIG_INFO_COUNT_UP BIT(0)
58
60
66
73#define COUNTER_TOP_CFG_DONT_RESET BIT(0)
74
81#define COUNTER_TOP_CFG_RESET_WHEN_LATE BIT(1)
82
84
91
98#define COUNTER_ALARM_CFG_ABSOLUTE BIT(0)
99
106#define COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE BIT(1)
107
109
117
122#define COUNTER_GUARD_PERIOD_LATE_TO_SET BIT(0)
123
125
133
134/* Bit 0 and 1 are used for COUNTER_CAPTURE_RISING_EDGE/COUNTER_CAPTURE_FALLING_EDGE */
135
139#define COUNTER_CAPTURE_CONTINUOUS 0
140
144#define COUNTER_CAPTURE_SINGLE_SHOT BIT(2)
145
154
156
179
225#define COUNTER_CAPTURE_DT_SPEC_GET_BY_NAME(node_id, prop, name) \
226 { \
227 .dev = DEVICE_DT_GET(DT_COUNTER_CAPTURES_CTLR_BY_NAME(node_id, prop, name)), \
228 .chan_id = (uint8_t)DT_COUNTER_CAPTURES_CHANNEL_BY_NAME(node_id, prop, name), \
229 .flags = DT_COUNTER_CAPTURES_FLAGS_BY_NAME(node_id, prop, name), \
230 }
231
246#define COUNTER_CAPTURE_DT_SPEC_INST_GET_BY_NAME(inst, prop, name) \
247 COUNTER_CAPTURE_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), prop, name)
248
270#define COUNTER_CAPTURE_DT_SPEC_GET_BY_NAME_OR(node_id, prop, name, default_value) \
271 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
272 (COUNTER_CAPTURE_DT_SPEC_GET_BY_NAME(node_id, prop, name)), \
273 (default_value))
274
290#define COUNTER_CAPTURE_DT_SPEC_INST_GET_BY_NAME_OR(inst, prop, name, default_value) \
291 COUNTER_CAPTURE_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), prop, name, default_value)
292
336#define COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
337 { \
338 .dev = DEVICE_DT_GET(DT_COUNTER_CAPTURES_CTLR_BY_IDX(node_id, prop, idx)), \
339 .chan_id = (uint8_t)DT_COUNTER_CAPTURES_CHANNEL_BY_IDX(node_id, prop, idx), \
340 .flags = DT_COUNTER_CAPTURES_FLAGS_BY_IDX(node_id, prop, idx), \
341 }
342
356#define COUNTER_CAPTURE_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
357 COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
358
379#define COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
380 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
381 (COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
382 (default_value))
383
398#define COUNTER_CAPTURE_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
399 COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), prop, idx, default_value)
400
413#define COUNTER_CAPTURE_DT_SPEC_GET(node_id, prop) \
414 COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
415
428#define COUNTER_CAPTURE_DT_SPEC_INST_GET(inst, prop) \
429 COUNTER_CAPTURE_DT_SPEC_GET(DT_DRV_INST(inst), prop)
430
445#define COUNTER_CAPTURE_DT_SPEC_GET_OR(node_id, prop, default_value) \
446 COUNTER_CAPTURE_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
447
462#define COUNTER_CAPTURE_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
463 COUNTER_CAPTURE_DT_SPEC_GET_OR(DT_DRV_INST(inst), prop, default_value)
464
466
475typedef void (*counter_capture_cb_t)(const struct device *dev, uint8_t chan_id,
477 void *user_data);
478
486typedef void (*counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, uint32_t ticks,
487 void *user_data);
488
522
528typedef void (*counter_top_callback_t)(const struct device *dev, void *user_data);
529
550
557#ifdef CONFIG_COUNTER_64BITS_FREQ
559#else
561#endif
565#ifdef CONFIG_COUNTER_64BITS_TICKS
566 union {
567 uint64_t max_top_value_64;
568 struct {
569#ifdef CONFIG_BIG_ENDIAN
570 uint32_t reserved;
572#else
574 uint32_t reserved;
575#endif /* CONFIG_BIG_ENDIAN */
576 };
577 };
578#else
580#endif /* CONFIG_COUNTER_64BITS_TICKS */
591};
592
600typedef void (*counter_alarm_callback_64_t)(const struct device *dev, uint8_t chan_id,
601 uint64_t ticks, void *user_data);
602
636
657
666typedef void (*counter_capture_cb_64_t)(const struct device *dev, uint8_t chan_id,
668 void *user_data);
669
674
676typedef int (*counter_api_start)(const struct device *dev);
678typedef int (*counter_api_stop)(const struct device *dev);
680typedef int (*counter_api_get_value)(const struct device *dev, uint32_t *ticks);
682typedef int (*counter_api_reset)(const struct device *dev);
684typedef int (*counter_api_set_value)(const struct device *dev, uint32_t ticks);
686typedef int (*counter_api_set_alarm)(const struct device *dev, uint8_t chan_id,
687 const struct counter_alarm_cfg *alarm_cfg);
689typedef int (*counter_api_cancel_alarm)(const struct device *dev, uint8_t chan_id);
691typedef int (*counter_api_set_top_value)(const struct device *dev,
692 const struct counter_top_cfg *cfg);
694typedef uint32_t (*counter_api_get_pending_int)(const struct device *dev);
696typedef uint32_t (*counter_api_get_top_value)(const struct device *dev);
700typedef int (*counter_api_set_guard_period)(const struct device *dev, uint32_t ticks,
703typedef uint32_t (*counter_api_get_freq)(const struct device *dev);
705typedef uint64_t (*counter_api_get_freq_64)(const struct device *dev);
706
708typedef int (*counter_api_get_value_64)(const struct device *dev, uint64_t *ticks);
710typedef int (*counter_api_set_value_64)(const struct device *dev, uint64_t ticks);
712typedef int (*counter_api_set_alarm_64)(const struct device *dev, uint8_t chan_id,
713 const struct counter_alarm_cfg_64 *alarm_cfg);
717typedef int (*counter_api_set_guard_period_64)(const struct device *dev, uint64_t ticks,
720typedef uint64_t (*counter_api_get_top_value_64)(const struct device *dev);
722typedef int (*counter_api_set_top_value_64)(const struct device *dev,
723 const struct counter_top_cfg_64 *cfg);
725typedef int (*counter_api_capture_configure)(const struct device *dev, uint8_t chan_id,
727 counter_capture_cb_t cb, void *user_data);
729typedef int (*counter_api_capture_configure_64)(const struct device *dev, uint8_t chan_id,
731 counter_capture_cb_64_t cb, void *user_data);
733typedef int (*counter_api_enable_capture)(const struct device *dev, uint8_t chan_id);
735typedef int (*counter_api_disable_capture)(const struct device *dev, uint8_t chan_id);
736
738typedef int (*counter_api_set_calibration)(const struct device *dev, int32_t calibration);
740typedef int (*counter_api_get_calibration)(const struct device *dev, int32_t *calibration);
741
745__subsystem struct counter_driver_api {
798#ifdef CONFIG_COUNTER_64BITS_FREQ
802 counter_api_get_freq_64 get_freq_64;
803#endif /* CONFIG_COUNTER_64BITS_FREQ */
804#ifdef CONFIG_COUNTER_64BITS_TICKS
808 counter_api_get_value_64 get_value_64;
812 counter_api_set_value_64 set_value_64;
816 counter_api_set_alarm_64 set_alarm_64;
820 counter_api_get_guard_period_64 get_guard_period_64;
824 counter_api_set_guard_period_64 set_guard_period_64;
828 counter_api_get_top_value_64 get_top_value_64;
832 counter_api_set_top_value_64 set_top_value_64;
833#endif /* CONFIG_COUNTER_64BITS_TICKS */
834#ifdef CONFIG_COUNTER_CAPTURE
838 counter_api_capture_configure capture_configure;
839#ifdef CONFIG_COUNTER_64BITS_TICKS
843 counter_api_capture_configure_64 capture_configure_64;
844#endif /* CONFIG_COUNTER_64BITS_TICKS */
848 counter_api_enable_capture enable_capture;
852 counter_api_disable_capture disable_capture;
853#endif /* CONFIG_COUNTER_CAPTURE */
854#if defined(CONFIG_COUNTER_CALIBRATION) || defined(__DOXYGEN__)
863#endif /* CONFIG_COUNTER_CALIBRATION */
864};
865
868
877__syscall bool counter_is_counting_up(const struct device *dev);
878
879static inline bool z_impl_counter_is_counting_up(const struct device *dev)
880{
881 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
882
883 return config->flags & COUNTER_CONFIG_INFO_COUNT_UP;
884}
885
893__syscall uint8_t counter_get_num_of_channels(const struct device *dev);
894
895static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev)
896{
897 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
898
899 return config->channels;
900}
901
902__syscall uint32_t counter_get_frequency(const struct device *dev);
903
904#ifdef CONFIG_COUNTER_64BITS_FREQ
915static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
916{
917 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
918 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
919
920 if (api->get_freq) {
921 return api->get_freq(dev);
922 } else {
923 return config->freq > UINT32_MAX ? UINT32_MAX : (uint32_t)config->freq;
924 }
925}
926
927#else
928
937static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
938{
939 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
940 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
941
942 return api->get_freq ? api->get_freq(dev) : config->freq;
943}
944#endif
945
955__syscall uint64_t counter_get_frequency_64(const struct device *dev);
956
957static inline uint64_t z_impl_counter_get_frequency_64(const struct device *dev)
958{
959#ifdef CONFIG_COUNTER_64BITS_FREQ
960 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
961 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
962
963 if (api->get_freq_64) {
964 return api->get_freq_64(dev);
965 } else if (api->get_freq) {
966 return (uint64_t)api->get_freq(dev);
967 } else {
968 return config->freq;
969 }
970#else
971 return z_impl_counter_get_frequency(dev);
972#endif
973}
974
975#ifdef CONFIG_COUNTER_64BITS_FREQ
976#define z_counter_get_frequency z_impl_counter_get_frequency_64
977#else
978#define z_counter_get_frequency z_impl_counter_get_frequency
979#endif
980
989__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
990
991static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us)
992{
993 uint64_t freq = z_counter_get_frequency(dev);
994 uint64_t whole = us / USEC_PER_SEC;
995 uint64_t ticks;
996
997 /* Saturate early: whole * freq alone can wrap the uint64_t math below. */
998 if ((freq != 0U) && (whole > (uint64_t)UINT32_MAX / freq)) {
999 return UINT32_MAX;
1000 }
1001
1002 ticks = whole * freq + ((us % USEC_PER_SEC) * freq) / USEC_PER_SEC;
1003
1004 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
1005}
1006
1015__syscall uint64_t counter_us_to_ticks_64(const struct device *dev, uint64_t us);
1016
1017static inline uint64_t z_impl_counter_us_to_ticks_64(const struct device *dev, uint64_t us)
1018{
1019 uint64_t freq = z_counter_get_frequency(dev);
1020
1021 return (us / USEC_PER_SEC) * freq + ((us % USEC_PER_SEC) * freq) / USEC_PER_SEC;
1022}
1023
1032__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
1033
1034static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, uint32_t ticks)
1035{
1036 return ((uint64_t)ticks * USEC_PER_SEC) / z_counter_get_frequency(dev);
1037}
1038
1047__syscall uint64_t counter_ticks_to_us_64(const struct device *dev, uint64_t ticks);
1048
1049static inline uint64_t z_impl_counter_ticks_to_us_64(const struct device *dev, uint64_t ticks)
1050{
1051 uint64_t freq = z_counter_get_frequency(dev);
1052
1053 return (ticks / freq) * USEC_PER_SEC + ((ticks % freq) * USEC_PER_SEC) / freq;
1054}
1055
1064__syscall uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns);
1065
1066static inline uint32_t z_impl_counter_ns_to_ticks(const struct device *dev, uint64_t ns)
1067{
1068 uint64_t freq = z_counter_get_frequency(dev);
1069 uint64_t whole = ns / NSEC_PER_SEC;
1070 uint64_t ticks;
1071
1072 /* Saturate early: whole * freq alone can wrap the uint64_t math below. */
1073 if ((freq != 0U) && (whole > (uint64_t)UINT32_MAX / freq)) {
1074 return UINT32_MAX;
1075 }
1076
1077 ticks = whole * freq + ((ns % NSEC_PER_SEC) * freq) / NSEC_PER_SEC;
1078
1079 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
1080}
1081
1090__syscall uint64_t counter_ns_to_ticks_64(const struct device *dev, uint64_t ns);
1091
1092static inline uint64_t z_impl_counter_ns_to_ticks_64(const struct device *dev, uint64_t ns)
1093{
1094 uint64_t freq = z_counter_get_frequency(dev);
1095
1096 return (ns / NSEC_PER_SEC) * freq + ((ns % NSEC_PER_SEC) * freq) / NSEC_PER_SEC;
1097}
1098
1107__syscall uint64_t counter_ticks_to_ns(const struct device *dev, uint32_t ticks);
1108
1109static inline uint64_t z_impl_counter_ticks_to_ns(const struct device *dev, uint32_t ticks)
1110{
1111 return ((uint64_t)ticks * NSEC_PER_SEC) / z_counter_get_frequency(dev);
1112}
1113
1122__syscall uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks);
1123
1124static inline uint64_t z_impl_counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks)
1125{
1126 uint64_t freq = z_counter_get_frequency(dev);
1127
1128 return (ticks / freq) * NSEC_PER_SEC + ((ticks % freq) * NSEC_PER_SEC) / freq;
1129}
1130
1142__syscall uint32_t counter_get_max_top_value(const struct device *dev);
1143
1144static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev)
1145{
1146 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
1147
1148 return config->max_top_value;
1149}
1150
1158__syscall int counter_start(const struct device *dev);
1159
1160static inline int z_impl_counter_start(const struct device *dev)
1161{
1162 return DEVICE_API_GET(counter, dev)->start(dev);
1163}
1164
1174__syscall int counter_stop(const struct device *dev);
1175
1176static inline int z_impl_counter_stop(const struct device *dev)
1177{
1178 return DEVICE_API_GET(counter, dev)->stop(dev);
1179}
1180
1188__syscall int counter_get_value(const struct device *dev, uint32_t *ticks);
1189
1190static inline int z_impl_counter_get_value(const struct device *dev, uint32_t *ticks)
1191{
1192 return DEVICE_API_GET(counter, dev)->get_value(dev, ticks);
1193}
1194
1201__syscall int counter_reset(const struct device *dev);
1202
1203static inline int z_impl_counter_reset(const struct device *dev)
1204{
1205 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1206
1207 if (!api->reset) {
1208 return -ENOSYS;
1209 }
1210
1211 return api->reset(dev);
1212}
1213
1221__syscall int counter_set_value(const struct device *dev, uint32_t ticks);
1222
1223static inline int z_impl_counter_set_value(const struct device *dev, uint32_t ticks)
1224{
1225 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1226
1227 if (!api->set_value) {
1228 return -ENOSYS;
1229 }
1230
1231 return api->set_value(dev, ticks);
1232}
1233
1254__syscall int counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
1255 const struct counter_alarm_cfg *alarm_cfg);
1256
1257static inline int z_impl_counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
1258 const struct counter_alarm_cfg *alarm_cfg)
1259{
1260 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1261
1262 if (chan_id >= counter_get_num_of_channels(dev)) {
1263 return -ENOTSUP;
1264 }
1265
1266 return api->set_alarm(dev, chan_id, alarm_cfg);
1267}
1268
1281__syscall int counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id);
1282
1283static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id)
1284{
1285 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1286
1287 if (chan_id >= counter_get_num_of_channels(dev)) {
1288 return -ENOTSUP;
1289 }
1290
1291 return api->cancel_alarm(dev, chan_id);
1292}
1293
1322__syscall int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg);
1323
1324static inline int z_impl_counter_set_top_value(const struct device *dev,
1325 const struct counter_top_cfg *cfg)
1326{
1327 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1328
1329 if (cfg->ticks > counter_get_max_top_value(dev)) {
1330 return -EINVAL;
1331 }
1332
1333 return api->set_top_value(dev, cfg);
1334}
1335
1349__syscall uint32_t counter_get_pending_int(const struct device *dev);
1350
1351static inline uint32_t z_impl_counter_get_pending_int(const struct device *dev)
1352{
1353 return DEVICE_API_GET(counter, dev)->get_pending_int(dev);
1354}
1355
1363__syscall uint32_t counter_get_top_value(const struct device *dev);
1364
1365static inline uint32_t z_impl_counter_get_top_value(const struct device *dev)
1366{
1367 return DEVICE_API_GET(counter, dev)->get_top_value(dev);
1368}
1369
1428__syscall int counter_set_guard_period(const struct device *dev, uint32_t ticks, uint32_t flags);
1429
1430static inline int z_impl_counter_set_guard_period(const struct device *dev, uint32_t ticks,
1432{
1433 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1434
1435 if (!api->set_guard_period) {
1436 return -ENOSYS;
1437 }
1438
1439 return api->set_guard_period(dev, ticks, flags);
1440}
1441
1454
1455static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev, uint32_t flags)
1456{
1457 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1458
1459 return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0;
1460}
1461
1470__syscall uint64_t counter_get_max_top_value_64(const struct device *dev);
1471
1472static inline uint64_t z_impl_counter_get_max_top_value_64(const struct device *dev)
1473{
1474#ifdef CONFIG_COUNTER_64BITS_TICKS
1475 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
1476
1477 return config->max_top_value_64;
1478#else
1479 return z_impl_counter_get_max_top_value(dev);
1480#endif
1481}
1482
1507__syscall int counter_set_top_value_64(const struct device *dev,
1508 const struct counter_top_cfg_64 *cfg);
1509
1510static inline int z_impl_counter_set_top_value_64(const struct device *dev,
1511 const struct counter_top_cfg_64 *cfg)
1512{
1513#ifdef CONFIG_COUNTER_64BITS_TICKS
1514 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1515
1516 if (cfg->ticks > counter_get_max_top_value_64(dev)) {
1517 return -EINVAL;
1518 }
1519
1520 return api->set_top_value_64(dev, cfg);
1521#else
1522 ARG_UNUSED(dev);
1523 ARG_UNUSED(cfg);
1524 return -ENOTSUP;
1525#endif
1526}
1527
1548__syscall int counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1549 const struct counter_alarm_cfg_64 *alarm_cfg);
1550
1551static inline int z_impl_counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1552 const struct counter_alarm_cfg_64 *alarm_cfg)
1553{
1554#ifdef CONFIG_COUNTER_64BITS_TICKS
1555 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1556
1557 if (chan_id >= counter_get_num_of_channels(dev)) {
1558 return -ENOTSUP;
1559 }
1560
1561 return api->set_alarm_64(dev, chan_id, alarm_cfg);
1562#else
1563 ARG_UNUSED(dev);
1564 ARG_UNUSED(chan_id);
1565 ARG_UNUSED(alarm_cfg);
1566 return -ENOTSUP;
1567#endif
1568}
1569
1577__syscall uint64_t counter_get_top_value_64(const struct device *dev);
1578
1579static inline uint64_t z_impl_counter_get_top_value_64(const struct device *dev)
1580{
1581#ifdef CONFIG_COUNTER_64BITS_TICKS
1582 return DEVICE_API_GET(counter, dev)->get_top_value_64(dev);
1583#else
1584 ARG_UNUSED(dev);
1585 return 0;
1586#endif
1587}
1588
1647__syscall int counter_set_guard_period_64(const struct device *dev, uint64_t ticks, uint32_t flags);
1648
1649static inline int z_impl_counter_set_guard_period_64(const struct device *dev, uint64_t ticks,
1651{
1652#ifdef CONFIG_COUNTER_64BITS_TICKS
1653 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1654
1655 if (!api->set_guard_period_64) {
1656 return -ENOSYS;
1657 }
1658
1659 return api->set_guard_period_64(dev, ticks, flags);
1660#else
1661 ARG_UNUSED(dev);
1662 ARG_UNUSED(ticks);
1663 ARG_UNUSED(flags);
1664 return -ENOTSUP;
1665#endif
1666}
1667
1681
1682static inline uint64_t z_impl_counter_get_guard_period_64(const struct device *dev, uint32_t flags)
1683{
1684#ifdef CONFIG_COUNTER_64BITS_TICKS
1685 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1686
1687 return (api->get_guard_period_64) ? api->get_guard_period_64(dev, flags) : 0;
1688#else
1689 return z_impl_counter_get_guard_period(dev, flags);
1690#endif
1691}
1692
1700__syscall int counter_get_value_64(const struct device *dev, uint64_t *ticks);
1701
1702static inline int z_impl_counter_get_value_64(const struct device *dev, uint64_t *ticks)
1703{
1704#ifdef CONFIG_COUNTER_64BITS_TICKS
1705 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1706
1707 if (!api->get_value_64) {
1708 return -ENOSYS;
1709 }
1710
1711 return api->get_value_64(dev, ticks);
1712#else
1713 ARG_UNUSED(dev);
1714 ARG_UNUSED(ticks);
1715 return -ENOTSUP;
1716#endif
1717}
1718
1726__syscall int counter_set_value_64(const struct device *dev, uint64_t ticks);
1727
1728static inline int z_impl_counter_set_value_64(const struct device *dev, uint64_t ticks)
1729{
1730#ifdef CONFIG_COUNTER_64BITS_TICKS
1731 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1732
1733 if (!api->set_value_64) {
1734 return -ENOSYS;
1735 }
1736
1737 return api->set_value_64(dev, ticks);
1738#else
1739 ARG_UNUSED(dev);
1740 ARG_UNUSED(ticks);
1741 return -ENOTSUP;
1742#endif
1743}
1744
1745#if defined(CONFIG_COUNTER_CAPTURE) || defined(__DOXYGEN__)
1755
1783static inline int counter_capture_configure(const struct device *dev, uint8_t chan_id,
1785 counter_capture_cb_t cb, void *user_data)
1786{
1787 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1788
1789 if (api->capture_configure == NULL) {
1790 return -ENOTSUP;
1791 }
1792
1793 if (chan_id >= counter_get_num_of_channels(dev)) {
1794 return -ENOTSUP;
1795 }
1796
1797 return api->capture_configure(dev, chan_id, flags, cb, user_data);
1798}
1799
1810static inline int counter_capture_configure_dt(const struct counter_capture_dt_spec *spec,
1811 counter_capture_cb_t cb, void *user_data)
1812{
1813 return counter_capture_configure(spec->dev, spec->chan_id, spec->flags, cb, user_data);
1814}
1815
1816#if defined(CONFIG_COUNTER_64BITS_TICKS) || defined(__DOXYGEN__)
1844static inline int counter_capture_configure_64(const struct device *dev, uint8_t chan_id,
1846 counter_capture_cb_64_t cb, void *user_data)
1847{
1848 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1849
1850 if (api->capture_configure_64 == NULL) {
1851 return -ENOTSUP;
1852 }
1853
1854 if (chan_id >= counter_get_num_of_channels(dev)) {
1855 return -ENOTSUP;
1856 }
1857
1858 return api->capture_configure_64(dev, chan_id, flags, cb, user_data);
1859}
1860
1872 counter_capture_cb_64_t cb, void *user_data)
1873{
1874 return counter_capture_configure_64(spec->dev, spec->chan_id, spec->flags, cb,
1875 user_data);
1876}
1877#endif /* CONFIG_COUNTER_64BITS_TICKS */
1878
1888__syscall int counter_enable_capture(const struct device *dev, uint8_t chan_id);
1889
1890static inline int z_impl_counter_enable_capture(const struct device *dev, uint8_t chan_id)
1891{
1892 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1893
1894 if (api->enable_capture == NULL) {
1895 return -ENOTSUP;
1896 }
1897
1898 if (chan_id >= counter_get_num_of_channels(dev)) {
1899 return -ENOTSUP;
1900 }
1901
1902 return api->enable_capture(dev, chan_id);
1903}
1904
1913static inline int counter_enable_capture_dt(const struct counter_capture_dt_spec *spec)
1914{
1915 return counter_enable_capture(spec->dev, spec->chan_id);
1916}
1917
1927__syscall int counter_disable_capture(const struct device *dev, uint8_t chan_id);
1928
1929static inline int z_impl_counter_disable_capture(const struct device *dev, uint8_t chan_id)
1930{
1931 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1932
1933 if (api->disable_capture == NULL) {
1934 return -ENOTSUP;
1935 }
1936
1937 if (chan_id >= counter_get_num_of_channels(dev)) {
1938 return -ENOTSUP;
1939 }
1940
1941 return api->disable_capture(dev, chan_id);
1942}
1943
1952static inline int counter_disable_capture_dt(const struct counter_capture_dt_spec *spec)
1953{
1954 return counter_disable_capture(spec->dev, spec->chan_id);
1955}
1956
1958#endif /* CONFIG_COUNTER_CAPTURE */
1959
1960#if defined(CONFIG_COUNTER_CALIBRATION) || defined(__DOXYGEN__)
1974__syscall int counter_set_calibration(const struct device *dev, int32_t calibration);
1975
1976static inline int z_impl_counter_set_calibration(const struct device *dev, int32_t calibration)
1977{
1978 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1979
1980 if (!api->set_calibration) {
1981 return -ENOSYS;
1982 }
1983
1984 return api->set_calibration(dev, calibration);
1985}
1986
1996__syscall int counter_get_calibration(const struct device *dev, int32_t *calibration);
1997
1998static inline int z_impl_counter_get_calibration(const struct device *dev, int32_t *calibration)
1999{
2000 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
2001
2002 if (!api->get_calibration) {
2003 return -ENOSYS;
2004 }
2005
2006 return api->get_calibration(dev, calibration);
2007}
2008#endif /* CONFIG_COUNTER_CALIBRATION */
2009
2010#ifdef __cplusplus
2011}
2012#endif
2013
2017
2018#include <zephyr/syscalls/counter.h>
2019
2020#endif /* ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_ */
System clock APIs.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1449
Counter capture Devicetree macro public API header file.
Devicetree main header.
Counter capture flags for use in devicetree.
System error numbers.
#define NSEC_PER_SEC
number of nanoseconds per second
Definition clock.h:113
#define USEC_PER_SEC
number of microseconds per second
Definition clock.h:110
static int counter_enable_capture_dt(const struct counter_capture_dt_spec *spec)
Enable capture on a channel using a DT spec.
Definition counter.h:1913
static int counter_disable_capture_dt(const struct counter_capture_dt_spec *spec)
Disable capture on a channel using a DT spec.
Definition counter.h:1952
static int counter_capture_configure(const struct device *dev, uint8_t chan_id, counter_capture_flags_t flags, counter_capture_cb_t cb, void *user_data)
Configure a capture channel and register its callback.
Definition counter.h:1783
int counter_disable_capture(const struct device *dev, uint8_t chan_id)
Disable capture on a channel.
static int counter_capture_configure_64_dt(const struct counter_capture_dt_spec *spec, counter_capture_cb_64_t cb, void *user_data)
Configure a capture channel and register its 64-bit callback using a DT spec.
Definition counter.h:1871
static int counter_capture_configure_dt(const struct counter_capture_dt_spec *spec, counter_capture_cb_t cb, void *user_data)
Configure a capture channel and register its callback using a DT spec.
Definition counter.h:1810
static int counter_capture_configure_64(const struct device *dev, uint8_t chan_id, counter_capture_flags_t flags, counter_capture_cb_64_t cb, void *user_data)
Configure a capture channel and register its callback for 64 bits ticks.
Definition counter.h:1844
int counter_enable_capture(const struct device *dev, uint8_t chan_id)
Enable capture on a channel.
int counter_set_channel_alarm(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)
Set a single shot alarm on a channel.
int(* counter_api_get_calibration)(const struct device *dev, int32_t *calibration)
Callback API to get counter calibration in parts per billion.
Definition counter.h:740
uint32_t(* counter_api_get_top_value)(const struct device *dev)
Callback API to retrieve the current top value.
Definition counter.h:696
uint8_t counter_get_num_of_channels(const struct device *dev)
Function to get number of alarm channels.
int counter_start(const struct device *dev)
Start counter device in free running mode.
uint32_t counter_get_top_value(const struct device *dev)
Function to retrieve current top value.
int counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg_64 *alarm_cfg)
Set a single shot alarm on a channel for 64 bits.
uint32_t counter_get_pending_int(const struct device *dev)
Function to get pending interrupts.
int(* counter_api_set_top_value_64)(const struct device *dev, const struct counter_top_cfg_64 *cfg)
Callback API to set the counter top value (64 bits).
Definition counter.h:722
uint64_t(* counter_api_get_top_value_64)(const struct device *dev)
Callback API to retrieve the current top value (64 bits).
Definition counter.h:720
int(* counter_api_set_value)(const struct device *dev, uint32_t ticks)
Callback API to set the current counter value.
Definition counter.h:684
uint64_t counter_ticks_to_us_64(const struct device *dev, uint64_t ticks)
Function to convert ticks with 64 bits to microseconds.
int counter_reset(const struct device *dev)
Reset the counter to the initial value.
int(* counter_api_stop)(const struct device *dev)
Callback API to stop the counter.
Definition counter.h:678
uint64_t counter_ns_to_ticks_64(const struct device *dev, uint64_t ns)
Function to convert nanoseconds to ticks with 64 bits.
int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg)
Set counter top value.
int counter_get_value_64(const struct device *dev, uint64_t *ticks)
Get current counter 64-bit value.
int counter_set_top_value_64(const struct device *dev, const struct counter_top_cfg_64 *cfg)
Set counter top value for 64 bits.
uint64_t counter_us_to_ticks_64(const struct device *dev, uint64_t us)
Function to convert microseconds to ticks with 64 bits.
void(* counter_top_callback_t)(const struct device *dev, void *user_data)
Callback called when counter turns around.
Definition counter.h:528
void(* counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)
Alarm callback.
Definition counter.h:486
int(* counter_api_set_alarm_64)(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg_64 *alarm_cfg)
Callback API to set a single shot alarm on a channel (64 bits).
Definition counter.h:712
uint64_t counter_get_max_top_value_64(const struct device *dev)
Function to retrieve maximum top value that can be set for 64 bits.
uint64_t counter_get_guard_period_64(const struct device *dev, uint32_t flags)
Return guard period for 64 bits.
uint64_t counter_get_frequency_64(const struct device *dev)
Function to get counter frequency in 64bits.
void(* counter_capture_cb_64_t)(const struct device *dev, uint8_t chan_id, counter_capture_flags_t flags, uint64_t ticks, void *user_data)
Counter capture callback for 64 bits ticks.
Definition counter.h:666
int(* counter_api_capture_configure_64)(const struct device *dev, uint8_t chan_id, counter_capture_flags_t flags, counter_capture_cb_64_t cb, void *user_data)
Callback API to configure counter capture on a channel with 64-bit ticks.
Definition counter.h:729
void(* counter_capture_cb_t)(const struct device *dev, uint8_t chan_id, counter_capture_flags_t flags, uint32_t ticks, void *user_data)
Counter capture callback.
Definition counter.h:475
void(* counter_alarm_callback_64_t)(const struct device *dev, uint8_t chan_id, uint64_t ticks, void *user_data)
Alarm callback.
Definition counter.h:600
uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks)
Function to convert ticks with 64 bits to nanoseconds.
int(* counter_api_set_guard_period_64)(const struct device *dev, uint64_t ticks, uint32_t flags)
Callback API to set the guard period in ticks (64 bits).
Definition counter.h:717
uint32_t counter_get_guard_period(const struct device *dev, uint32_t flags)
Return guard period.
int counter_set_value(const struct device *dev, uint32_t ticks)
Set current counter value.
uint32_t counter_capture_flags_t
Provides a type to hold Counter Capture configuration flags.
Definition counter.h:155
uint64_t(* counter_api_get_guard_period_64)(const struct device *dev, uint32_t flags)
Callback API to retrieve the guard period in ticks (64 bits).
Definition counter.h:715
int(* counter_api_set_guard_period)(const struct device *dev, uint32_t ticks, uint32_t flags)
Callback API to set the guard period in ticks.
Definition counter.h:700
uint64_t(* counter_api_get_freq_64)(const struct device *dev)
Callback API to get the counter frequency in Hz (64 bits).
Definition counter.h:705
uint32_t counter_get_frequency(const struct device *dev)
int(* counter_api_set_value_64)(const struct device *dev, uint64_t ticks)
Callback API to set the current counter value (64 bits).
Definition counter.h:710
int counter_get_value(const struct device *dev, uint32_t *ticks)
Get current counter value.
#define COUNTER_CONFIG_INFO_COUNT_UP
Counter count up flag.
Definition counter.h:57
int counter_set_calibration(const struct device *dev, int32_t calibration)
Set counter calibration value.
uint32_t(* counter_api_get_guard_period)(const struct device *dev, uint32_t flags)
Callback API to retrieve the guard period in ticks.
Definition counter.h:698
uint32_t(* counter_api_get_pending_int)(const struct device *dev)
Callback API to get pending counter interrupts.
Definition counter.h:694
uint32_t(* counter_api_get_freq)(const struct device *dev)
Callback API to get the counter frequency in Hz.
Definition counter.h:703
int counter_set_value_64(const struct device *dev, uint64_t ticks)
Set current counter 64-bit value.
uint64_t counter_get_top_value_64(const struct device *dev)
Function to retrieve current top value for 64 bits.
uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks)
Function to convert ticks to microseconds.
int(* counter_api_set_top_value)(const struct device *dev, const struct counter_top_cfg *cfg)
Callback API to set the counter top value.
Definition counter.h:691
int(* counter_api_reset)(const struct device *dev)
Callback API to reset the counter to the initial value.
Definition counter.h:682
int(* counter_api_start)(const struct device *dev)
@def_driverbackendgroup{Counter,counter_interface}
Definition counter.h:676
int(* counter_api_capture_configure)(const struct device *dev, uint8_t chan_id, counter_capture_flags_t flags, counter_capture_cb_t cb, void *user_data)
Callback API to configure counter capture on a channel.
Definition counter.h:725
int counter_set_guard_period(const struct device *dev, uint32_t ticks, uint32_t flags)
Set guard period in counter ticks.
uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us)
Function to convert microseconds to ticks.
bool counter_is_counting_up(const struct device *dev)
Function to check if counter is counting up.
int counter_set_guard_period_64(const struct device *dev, uint64_t ticks, uint32_t flags)
Set guard period in counter ticks for 64 bits.
int(* counter_api_cancel_alarm)(const struct device *dev, uint8_t chan_id)
Callback API to cancel an alarm on a channel.
Definition counter.h:689
int(* counter_api_enable_capture)(const struct device *dev, uint8_t chan_id)
Callback API to enable counter capture on a channel.
Definition counter.h:733
int(* counter_api_get_value_64)(const struct device *dev, uint64_t *ticks)
Callback API to get the current counter value (64 bits).
Definition counter.h:708
int(* counter_api_get_value)(const struct device *dev, uint32_t *ticks)
Callback API to get the current counter value.
Definition counter.h:680
int(* counter_api_disable_capture)(const struct device *dev, uint8_t chan_id)
Callback API to disable counter capture on a channel.
Definition counter.h:735
int counter_get_calibration(const struct device *dev, int32_t *calibration)
Get counter calibration value.
uint64_t counter_ticks_to_ns(const struct device *dev, uint32_t ticks)
Function to convert ticks to nanoseconds.
int counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id)
Cancel an alarm on a channel.
int(* counter_api_set_alarm)(const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)
Callback API to set a single shot alarm on a channel.
Definition counter.h:686
uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns)
Function to convert nanoseconds to ticks.
int(* counter_api_set_calibration)(const struct device *dev, int32_t calibration)
Callback API to set counter calibration in parts per billion.
Definition counter.h:738
int counter_stop(const struct device *dev)
Stop counter device.
uint32_t counter_get_max_top_value(const struct device *dev)
Function to retrieve maximum top value that can be set.
#define EINVAL
Invalid argument.
Definition errno.h:61
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define ENOTSUP
Unsupported value.
Definition errno.h:115
#define NULL
Definition iar_missing_defs.h:20
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
#define UINT32_MAX
Definition stdint.h:29
Alarm callback structure.
Definition counter.h:605
void * user_data
User data returned in callback.
Definition counter.h:630
uint32_t flags
Alarm flags (see COUNTER_ALARM_FLAGS).
Definition counter.h:634
counter_alarm_callback_64_t callback
Callback called on alarm (cannot be NULL).
Definition counter.h:626
uint64_t ticks
Number of ticks that triggers the alarm.
Definition counter.h:622
Alarm callback structure.
Definition counter.h:491
uint32_t ticks
Number of ticks that triggers the alarm.
Definition counter.h:512
uint32_t flags
Alarm flags (see COUNTER_ALARM_FLAGS).
Definition counter.h:520
void * user_data
User data returned in callback.
Definition counter.h:516
counter_alarm_callback_t callback
Callback called on alarm (cannot be NULL).
Definition counter.h:495
Container for counter capture information specified in devicetree.
Definition counter.h:171
counter_capture_flags_t flags
Capture flags.
Definition counter.h:175
const struct device * dev
Counter device instance.
Definition counter.h:173
uint8_t chan_id
Channel ID.
Definition counter.h:177
Structure with generic counter features.
Definition counter.h:553
uint32_t max_top_value
Maximal (default) top value on which counter is reset (cleared or reloaded).
Definition counter.h:579
uint32_t freq
Frequency of the source clock if synchronous events are counted.
Definition counter.h:560
uint8_t flags
Flags (see COUNTER_FLAGS).
Definition counter.h:584
uint8_t channels
Number of channels that can be used for setting alarm.
Definition counter.h:590
@driver_ops{Counter}
Definition counter.h:745
counter_api_get_calibration get_calibration
@driver_ops_optional Get counter calibration value.
Definition counter.h:862
counter_api_get_top_value get_top_value
@driver_ops_mandatory Function to retrieve current top value.
Definition counter.h:785
counter_api_set_top_value set_top_value
@driver_ops_mandatory Set counter top value.
Definition counter.h:777
counter_api_set_alarm set_alarm
@driver_ops_mandatory Set a single shot alarm on a channel.
Definition counter.h:769
counter_api_get_value get_value
@driver_ops_mandatory Get current counter value.
Definition counter.h:757
counter_api_reset reset
@driver_ops_optional Reset the counter to the initial value.
Definition counter.h:761
counter_api_get_pending_int get_pending_int
@driver_ops_mandatory Function to get pending interrupts.
Definition counter.h:781
counter_api_stop stop
@driver_ops_mandatory Stop counter device.
Definition counter.h:753
counter_api_set_guard_period set_guard_period
@driver_ops_optional Set guard period in counter ticks.
Definition counter.h:793
counter_api_set_value set_value
@driver_ops_optional Set current counter value.
Definition counter.h:765
counter_api_start start
@driver_ops_mandatory Start counter device in free running mode.
Definition counter.h:749
counter_api_set_calibration set_calibration
@driver_ops_optional Set counter calibration value.
Definition counter.h:858
counter_api_get_guard_period get_guard_period
@driver_ops_optional Return guard period.
Definition counter.h:789
counter_api_cancel_alarm cancel_alarm
@driver_ops_mandatory Cancel an alarm on a channel.
Definition counter.h:773
counter_api_get_freq get_freq
@driver_ops_optional
Definition counter.h:797
Top value configuration structure.
Definition counter.h:639
void * user_data
User data passed to callback function (not valid if callback is NULL).
Definition counter.h:651
counter_top_callback_t callback
Callback function (can be NULL).
Definition counter.h:647
uint32_t flags
Flags (see COUNTER_TOP_FLAGS).
Definition counter.h:655
uint64_t ticks
Top value.
Definition counter.h:643
Top value configuration structure.
Definition counter.h:532
uint32_t ticks
Top value.
Definition counter.h:536
uint32_t flags
Flags (see COUNTER_TOP_FLAGS).
Definition counter.h:548
counter_top_callback_t callback
Callback function (can be NULL).
Definition counter.h:540
void * user_data
User data passed to callback function (not valid if callback is NULL).
Definition counter.h:544
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
const void * config
Address of device instance config information.
Definition device.h:517