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#if defined(CONFIG_COUNTER_64BITS_FREQ) || defined(__DOXYGEN__)
804#endif /* CONFIG_COUNTER_64BITS_FREQ */
805#if defined(CONFIG_COUNTER_64BITS_TICKS) || defined(__DOXYGEN__)
841#endif /* CONFIG_COUNTER_64BITS_TICKS */
842#if defined(CONFIG_COUNTER_CAPTURE) || defined(__DOXYGEN__)
848#if defined(CONFIG_COUNTER_64BITS_TICKS) || defined(__DOXYGEN__)
854#endif /* CONFIG_COUNTER_64BITS_TICKS */
865#endif /* CONFIG_COUNTER_CAPTURE */
866#if defined(CONFIG_COUNTER_CALIBRATION) || defined(__DOXYGEN__)
877#endif /* CONFIG_COUNTER_CALIBRATION */
878};
879
882
891__syscall bool counter_is_counting_up(const struct device *dev);
892
893static inline bool z_impl_counter_is_counting_up(const struct device *dev)
894{
895 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
896
897 return config->flags & COUNTER_CONFIG_INFO_COUNT_UP;
898}
899
907__syscall uint8_t counter_get_num_of_channels(const struct device *dev);
908
909static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev)
910{
911 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
912
913 return config->channels;
914}
915
916__syscall uint32_t counter_get_frequency(const struct device *dev);
917
918#ifdef CONFIG_COUNTER_64BITS_FREQ
929static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
930{
931 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
932 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
933
934 if (api->get_freq) {
935 return api->get_freq(dev);
936 } else {
937 return config->freq > UINT32_MAX ? UINT32_MAX : (uint32_t)config->freq;
938 }
939}
940
941#else
942
951static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
952{
953 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
954 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
955
956 return api->get_freq ? api->get_freq(dev) : config->freq;
957}
958#endif
959
969__syscall uint64_t counter_get_frequency_64(const struct device *dev);
970
971static inline uint64_t z_impl_counter_get_frequency_64(const struct device *dev)
972{
973#ifdef CONFIG_COUNTER_64BITS_FREQ
974 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
975 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
976
977 if (api->get_freq_64) {
978 return api->get_freq_64(dev);
979 } else if (api->get_freq) {
980 return (uint64_t)api->get_freq(dev);
981 } else {
982 return config->freq;
983 }
984#else
985 return z_impl_counter_get_frequency(dev);
986#endif
987}
988
989#ifdef CONFIG_COUNTER_64BITS_FREQ
990#define z_counter_get_frequency z_impl_counter_get_frequency_64
991#else
992#define z_counter_get_frequency z_impl_counter_get_frequency
993#endif
994
1003__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
1004
1005static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us)
1006{
1007 uint64_t freq = z_counter_get_frequency(dev);
1008 uint64_t whole = us / USEC_PER_SEC;
1009 uint64_t ticks;
1010
1011 /* Saturate early: whole * freq alone can wrap the uint64_t math below. */
1012 if ((freq != 0U) && (whole > (uint64_t)UINT32_MAX / freq)) {
1013 return UINT32_MAX;
1014 }
1015
1016 ticks = whole * freq + ((us % USEC_PER_SEC) * freq) / USEC_PER_SEC;
1017
1018 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
1019}
1020
1029__syscall uint64_t counter_us_to_ticks_64(const struct device *dev, uint64_t us);
1030
1031static inline uint64_t z_impl_counter_us_to_ticks_64(const struct device *dev, uint64_t us)
1032{
1033 uint64_t freq = z_counter_get_frequency(dev);
1034
1035 return (us / USEC_PER_SEC) * freq + ((us % USEC_PER_SEC) * freq) / USEC_PER_SEC;
1036}
1037
1046__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
1047
1048static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, uint32_t ticks)
1049{
1050 return ((uint64_t)ticks * USEC_PER_SEC) / z_counter_get_frequency(dev);
1051}
1052
1061__syscall uint64_t counter_ticks_to_us_64(const struct device *dev, uint64_t ticks);
1062
1063static inline uint64_t z_impl_counter_ticks_to_us_64(const struct device *dev, uint64_t ticks)
1064{
1065 uint64_t freq = z_counter_get_frequency(dev);
1066
1067 return (ticks / freq) * USEC_PER_SEC + ((ticks % freq) * USEC_PER_SEC) / freq;
1068}
1069
1078__syscall uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns);
1079
1080static inline uint32_t z_impl_counter_ns_to_ticks(const struct device *dev, uint64_t ns)
1081{
1082 uint64_t freq = z_counter_get_frequency(dev);
1083 uint64_t whole = ns / NSEC_PER_SEC;
1084 uint64_t ticks;
1085
1086 /* Saturate early: whole * freq alone can wrap the uint64_t math below. */
1087 if ((freq != 0U) && (whole > (uint64_t)UINT32_MAX / freq)) {
1088 return UINT32_MAX;
1089 }
1090
1091 ticks = whole * freq + ((ns % NSEC_PER_SEC) * freq) / NSEC_PER_SEC;
1092
1093 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
1094}
1095
1104__syscall uint64_t counter_ns_to_ticks_64(const struct device *dev, uint64_t ns);
1105
1106static inline uint64_t z_impl_counter_ns_to_ticks_64(const struct device *dev, uint64_t ns)
1107{
1108 uint64_t freq = z_counter_get_frequency(dev);
1109
1110 return (ns / NSEC_PER_SEC) * freq + ((ns % NSEC_PER_SEC) * freq) / NSEC_PER_SEC;
1111}
1112
1121__syscall uint64_t counter_ticks_to_ns(const struct device *dev, uint32_t ticks);
1122
1123static inline uint64_t z_impl_counter_ticks_to_ns(const struct device *dev, uint32_t ticks)
1124{
1125 return ((uint64_t)ticks * NSEC_PER_SEC) / z_counter_get_frequency(dev);
1126}
1127
1136__syscall uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks);
1137
1138static inline uint64_t z_impl_counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks)
1139{
1140 uint64_t freq = z_counter_get_frequency(dev);
1141
1142 return (ticks / freq) * NSEC_PER_SEC + ((ticks % freq) * NSEC_PER_SEC) / freq;
1143}
1144
1156__syscall uint32_t counter_get_max_top_value(const struct device *dev);
1157
1158static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev)
1159{
1160 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
1161
1162 return config->max_top_value;
1163}
1164
1172__syscall int counter_start(const struct device *dev);
1173
1174static inline int z_impl_counter_start(const struct device *dev)
1175{
1176 return DEVICE_API_GET(counter, dev)->start(dev);
1177}
1178
1188__syscall int counter_stop(const struct device *dev);
1189
1190static inline int z_impl_counter_stop(const struct device *dev)
1191{
1192 return DEVICE_API_GET(counter, dev)->stop(dev);
1193}
1194
1202__syscall int counter_get_value(const struct device *dev, uint32_t *ticks);
1203
1204static inline int z_impl_counter_get_value(const struct device *dev, uint32_t *ticks)
1205{
1206 return DEVICE_API_GET(counter, dev)->get_value(dev, ticks);
1207}
1208
1215__syscall int counter_reset(const struct device *dev);
1216
1217static inline int z_impl_counter_reset(const struct device *dev)
1218{
1219 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1220
1221 if (!api->reset) {
1222 return -ENOSYS;
1223 }
1224
1225 return api->reset(dev);
1226}
1227
1235__syscall int counter_set_value(const struct device *dev, uint32_t ticks);
1236
1237static inline int z_impl_counter_set_value(const struct device *dev, uint32_t ticks)
1238{
1239 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1240
1241 if (!api->set_value) {
1242 return -ENOSYS;
1243 }
1244
1245 return api->set_value(dev, ticks);
1246}
1247
1268__syscall int counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
1269 const struct counter_alarm_cfg *alarm_cfg);
1270
1271static inline int z_impl_counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
1272 const struct counter_alarm_cfg *alarm_cfg)
1273{
1274 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1275
1276 if (chan_id >= counter_get_num_of_channels(dev)) {
1277 return -ENOTSUP;
1278 }
1279
1280 return api->set_alarm(dev, chan_id, alarm_cfg);
1281}
1282
1295__syscall int counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id);
1296
1297static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id)
1298{
1299 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1300
1301 if (chan_id >= counter_get_num_of_channels(dev)) {
1302 return -ENOTSUP;
1303 }
1304
1305 return api->cancel_alarm(dev, chan_id);
1306}
1307
1336__syscall int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg);
1337
1338static inline int z_impl_counter_set_top_value(const struct device *dev,
1339 const struct counter_top_cfg *cfg)
1340{
1341 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1342
1343 if (cfg->ticks > counter_get_max_top_value(dev)) {
1344 return -EINVAL;
1345 }
1346
1347 return api->set_top_value(dev, cfg);
1348}
1349
1363__syscall uint32_t counter_get_pending_int(const struct device *dev);
1364
1365static inline uint32_t z_impl_counter_get_pending_int(const struct device *dev)
1366{
1367 return DEVICE_API_GET(counter, dev)->get_pending_int(dev);
1368}
1369
1377__syscall uint32_t counter_get_top_value(const struct device *dev);
1378
1379static inline uint32_t z_impl_counter_get_top_value(const struct device *dev)
1380{
1381 return DEVICE_API_GET(counter, dev)->get_top_value(dev);
1382}
1383
1442__syscall int counter_set_guard_period(const struct device *dev, uint32_t ticks, uint32_t flags);
1443
1444static inline int z_impl_counter_set_guard_period(const struct device *dev, uint32_t ticks,
1446{
1447 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1448
1449 if (!api->set_guard_period) {
1450 return -ENOSYS;
1451 }
1452
1453 return api->set_guard_period(dev, ticks, flags);
1454}
1455
1468
1469static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev, uint32_t flags)
1470{
1471 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1472
1473 return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0;
1474}
1475
1484__syscall uint64_t counter_get_max_top_value_64(const struct device *dev);
1485
1486static inline uint64_t z_impl_counter_get_max_top_value_64(const struct device *dev)
1487{
1488#ifdef CONFIG_COUNTER_64BITS_TICKS
1489 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
1490
1491 return config->max_top_value_64;
1492#else
1493 return z_impl_counter_get_max_top_value(dev);
1494#endif
1495}
1496
1521__syscall int counter_set_top_value_64(const struct device *dev,
1522 const struct counter_top_cfg_64 *cfg);
1523
1524static inline int z_impl_counter_set_top_value_64(const struct device *dev,
1525 const struct counter_top_cfg_64 *cfg)
1526{
1527#ifdef CONFIG_COUNTER_64BITS_TICKS
1528 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1529
1530 if (cfg->ticks > counter_get_max_top_value_64(dev)) {
1531 return -EINVAL;
1532 }
1533
1534 return api->set_top_value_64(dev, cfg);
1535#else
1536 ARG_UNUSED(dev);
1537 ARG_UNUSED(cfg);
1538 return -ENOTSUP;
1539#endif
1540}
1541
1562__syscall int counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1563 const struct counter_alarm_cfg_64 *alarm_cfg);
1564
1565static inline int z_impl_counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1566 const struct counter_alarm_cfg_64 *alarm_cfg)
1567{
1568#ifdef CONFIG_COUNTER_64BITS_TICKS
1569 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1570
1571 if (chan_id >= counter_get_num_of_channels(dev)) {
1572 return -ENOTSUP;
1573 }
1574
1575 return api->set_alarm_64(dev, chan_id, alarm_cfg);
1576#else
1577 ARG_UNUSED(dev);
1578 ARG_UNUSED(chan_id);
1579 ARG_UNUSED(alarm_cfg);
1580 return -ENOTSUP;
1581#endif
1582}
1583
1591__syscall uint64_t counter_get_top_value_64(const struct device *dev);
1592
1593static inline uint64_t z_impl_counter_get_top_value_64(const struct device *dev)
1594{
1595#ifdef CONFIG_COUNTER_64BITS_TICKS
1596 return DEVICE_API_GET(counter, dev)->get_top_value_64(dev);
1597#else
1598 ARG_UNUSED(dev);
1599 return 0;
1600#endif
1601}
1602
1661__syscall int counter_set_guard_period_64(const struct device *dev, uint64_t ticks, uint32_t flags);
1662
1663static inline int z_impl_counter_set_guard_period_64(const struct device *dev, uint64_t ticks,
1665{
1666#ifdef CONFIG_COUNTER_64BITS_TICKS
1667 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1668
1669 if (!api->set_guard_period_64) {
1670 return -ENOSYS;
1671 }
1672
1673 return api->set_guard_period_64(dev, ticks, flags);
1674#else
1675 ARG_UNUSED(dev);
1676 ARG_UNUSED(ticks);
1677 ARG_UNUSED(flags);
1678 return -ENOTSUP;
1679#endif
1680}
1681
1695
1696static inline uint64_t z_impl_counter_get_guard_period_64(const struct device *dev, uint32_t flags)
1697{
1698#ifdef CONFIG_COUNTER_64BITS_TICKS
1699 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1700
1701 return (api->get_guard_period_64) ? api->get_guard_period_64(dev, flags) : 0;
1702#else
1703 return z_impl_counter_get_guard_period(dev, flags);
1704#endif
1705}
1706
1714__syscall int counter_get_value_64(const struct device *dev, uint64_t *ticks);
1715
1716static inline int z_impl_counter_get_value_64(const struct device *dev, uint64_t *ticks)
1717{
1718#ifdef CONFIG_COUNTER_64BITS_TICKS
1719 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1720
1721 if (!api->get_value_64) {
1722 return -ENOSYS;
1723 }
1724
1725 return api->get_value_64(dev, ticks);
1726#else
1727 ARG_UNUSED(dev);
1728 ARG_UNUSED(ticks);
1729 return -ENOTSUP;
1730#endif
1731}
1732
1740__syscall int counter_set_value_64(const struct device *dev, uint64_t ticks);
1741
1742static inline int z_impl_counter_set_value_64(const struct device *dev, uint64_t ticks)
1743{
1744#ifdef CONFIG_COUNTER_64BITS_TICKS
1745 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1746
1747 if (!api->set_value_64) {
1748 return -ENOSYS;
1749 }
1750
1751 return api->set_value_64(dev, ticks);
1752#else
1753 ARG_UNUSED(dev);
1754 ARG_UNUSED(ticks);
1755 return -ENOTSUP;
1756#endif
1757}
1758
1759#if defined(CONFIG_COUNTER_CAPTURE) || defined(__DOXYGEN__)
1769
1799static inline int counter_capture_configure(const struct device *dev, uint8_t chan_id,
1801 counter_capture_cb_t cb, void *user_data)
1802{
1803 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1804
1805 if (api->capture_configure == NULL) {
1806 return -ENOTSUP;
1807 }
1808
1809 if (chan_id >= counter_get_num_of_channels(dev)) {
1810 return -ENOTSUP;
1811 }
1812
1813 return api->capture_configure(dev, chan_id, flags, cb, user_data);
1814}
1815
1828static inline int counter_capture_configure_dt(const struct counter_capture_dt_spec *spec,
1829 counter_capture_cb_t cb, void *user_data)
1830{
1831 return counter_capture_configure(spec->dev, spec->chan_id, spec->flags, cb, user_data);
1832}
1833
1834#if defined(CONFIG_COUNTER_64BITS_TICKS) || defined(__DOXYGEN__)
1864static inline int counter_capture_configure_64(const struct device *dev, uint8_t chan_id,
1866 counter_capture_cb_64_t cb, void *user_data)
1867{
1868 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1869
1870 if (api->capture_configure_64 == NULL) {
1871 return -ENOTSUP;
1872 }
1873
1874 if (chan_id >= counter_get_num_of_channels(dev)) {
1875 return -ENOTSUP;
1876 }
1877
1878 return api->capture_configure_64(dev, chan_id, flags, cb, user_data);
1879}
1880
1894 counter_capture_cb_64_t cb, void *user_data)
1895{
1896 return counter_capture_configure_64(spec->dev, spec->chan_id, spec->flags, cb,
1897 user_data);
1898}
1899#endif /* CONFIG_COUNTER_64BITS_TICKS */
1900
1912__syscall int counter_enable_capture(const struct device *dev, uint8_t chan_id);
1913
1914static inline int z_impl_counter_enable_capture(const struct device *dev, uint8_t chan_id)
1915{
1916 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1917
1918 if (api->enable_capture == NULL) {
1919 return -ENOTSUP;
1920 }
1921
1922 if (chan_id >= counter_get_num_of_channels(dev)) {
1923 return -ENOTSUP;
1924 }
1925
1926 return api->enable_capture(dev, chan_id);
1927}
1928
1939static inline int counter_enable_capture_dt(const struct counter_capture_dt_spec *spec)
1940{
1941 return counter_enable_capture(spec->dev, spec->chan_id);
1942}
1943
1955__syscall int counter_disable_capture(const struct device *dev, uint8_t chan_id);
1956
1957static inline int z_impl_counter_disable_capture(const struct device *dev, uint8_t chan_id)
1958{
1959 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1960
1961 if (api->disable_capture == NULL) {
1962 return -ENOTSUP;
1963 }
1964
1965 if (chan_id >= counter_get_num_of_channels(dev)) {
1966 return -ENOTSUP;
1967 }
1968
1969 return api->disable_capture(dev, chan_id);
1970}
1971
1982static inline int counter_disable_capture_dt(const struct counter_capture_dt_spec *spec)
1983{
1984 return counter_disable_capture(spec->dev, spec->chan_id);
1985}
1986
1988#endif /* CONFIG_COUNTER_CAPTURE */
1989
1990#if defined(CONFIG_COUNTER_CALIBRATION) || defined(__DOXYGEN__)
2006__syscall int counter_set_calibration(const struct device *dev, int32_t calibration);
2007
2008static inline int z_impl_counter_set_calibration(const struct device *dev, int32_t calibration)
2009{
2010 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
2011
2012 if (!api->set_calibration) {
2013 return -ENOSYS;
2014 }
2015
2016 return api->set_calibration(dev, calibration);
2017}
2018
2030__syscall int counter_get_calibration(const struct device *dev, int32_t *calibration);
2031
2032static inline int z_impl_counter_get_calibration(const struct device *dev, int32_t *calibration)
2033{
2034 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
2035
2036 if (!api->get_calibration) {
2037 return -ENOSYS;
2038 }
2039
2040 return api->get_calibration(dev, calibration);
2041}
2042#endif /* CONFIG_COUNTER_CALIBRATION */
2043
2044#ifdef __cplusplus
2045}
2046#endif
2047
2051
2052#include <zephyr/syscalls/counter.h>
2053
2054#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:1461
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:1939
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:1982
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:1799
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:1893
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:1828
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:1864
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_value_64 get_value_64
@driver_ops_optional Get current counter 64-bit value.
Definition counter.h:810
counter_api_get_calibration get_calibration
@driver_ops_optional Get counter calibration value.
Definition counter.h:876
counter_api_disable_capture disable_capture
@driver_ops_mandatory Disable capture on a channel.
Definition counter.h:864
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_get_guard_period_64 get_guard_period_64
@driver_ops_optional Return guard period for 64 bits.
Definition counter.h:825
counter_api_set_alarm set_alarm
@driver_ops_mandatory Set a single shot alarm on a channel.
Definition counter.h:769
counter_api_get_freq_64 get_freq_64
@driver_ops_optional Function to get counter frequency in 64bits.
Definition counter.h:803
counter_api_capture_configure capture_configure
@driver_ops_mandatory Configure a capture channel and register its callback.
Definition counter.h:847
counter_api_get_top_value_64 get_top_value_64
@driver_ops_mandatory Function to retrieve current top value for 64 bits.
Definition counter.h:835
counter_api_enable_capture enable_capture
@driver_ops_mandatory Enable capture on a channel.
Definition counter.h:859
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_set_alarm_64 set_alarm_64
@driver_ops_mandatory Set a single shot alarm on a channel for 64 bits.
Definition counter.h:820
counter_api_capture_configure_64 capture_configure_64
@driver_ops_mandatory Configure a capture channel and register its callback for 64 bits ticks.
Definition counter.h:853
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:871
counter_api_set_top_value_64 set_top_value_64
@driver_ops_mandatory Set counter top value for 64 bits.
Definition counter.h:840
counter_api_set_guard_period_64 set_guard_period_64
@driver_ops_optional Set guard period in counter ticks for 64 bits.
Definition counter.h:830
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_set_value_64 set_value_64
@driver_ops_optional Set current counter 64-bit value.
Definition counter.h:815
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:525
const void * config
Address of device instance config information.
Definition device.h:529