Zephyr API Documentation 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/sys_clock.h>
38#include <stdbool.h>
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
49
53#define COUNTER_CONFIG_INFO_COUNT_UP BIT(0)
54
56
62
69#define COUNTER_TOP_CFG_DONT_RESET BIT(0)
70
77#define COUNTER_TOP_CFG_RESET_WHEN_LATE BIT(1)
78
80
87
94#define COUNTER_ALARM_CFG_ABSOLUTE BIT(0)
95
102#define COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE BIT(1)
103
105
113
118#define COUNTER_GUARD_PERIOD_LATE_TO_SET BIT(0)
119
121
129
133#define COUNTER_CAPTURE_RISING_EDGE BIT(0)
134
138#define COUNTER_CAPTURE_FALLING_EDGE BIT(1)
139
143#define COUNTER_CAPTURE_BOTH_EDGES (COUNTER_CAPTURE_FALLING_EDGE | COUNTER_CAPTURE_RISING_EDGE)
144
148#define COUNTER_CAPTURE_CONTINUOUS 0
149
153#define COUNTER_CAPTURE_SINGLE_SHOT BIT(2)
154
163
165
167
176typedef void (*counter_capture_cb_t)(const struct device *dev, uint8_t chan_id,
178 void *user_data);
179
187typedef void (*counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, uint32_t ticks,
188 void *user_data);
189
223
229typedef void (*counter_top_callback_t)(const struct device *dev, void *user_data);
230
251
258#ifdef CONFIG_COUNTER_64BITS_FREQ
260#else
262#endif
266#ifdef CONFIG_COUNTER_64BITS_TICKS
267 union {
268 uint64_t max_top_value_64;
269 struct {
270#ifdef CONFIG_BIG_ENDIAN
271 uint32_t reserved;
273#else
275 uint32_t reserved;
276#endif /* CONFIG_BIG_ENDIAN */
277 };
278 };
279#else
281#endif /* CONFIG_COUNTER_64BITS_TICKS */
292};
293
301typedef void (*counter_alarm_callback_64_t)(const struct device *dev, uint8_t chan_id,
302 uint64_t ticks, void *user_data);
303
337
358
367typedef void (*counter_capture_cb_64_t)(const struct device *dev, uint8_t chan_id,
369 void *user_data);
370
375
377typedef int (*counter_api_start)(const struct device *dev);
379typedef int (*counter_api_stop)(const struct device *dev);
381typedef int (*counter_api_get_value)(const struct device *dev, uint32_t *ticks);
383typedef int (*counter_api_reset)(const struct device *dev);
385typedef int (*counter_api_set_value)(const struct device *dev, uint32_t ticks);
387typedef int (*counter_api_set_alarm)(const struct device *dev, uint8_t chan_id,
388 const struct counter_alarm_cfg *alarm_cfg);
390typedef int (*counter_api_cancel_alarm)(const struct device *dev, uint8_t chan_id);
392typedef int (*counter_api_set_top_value)(const struct device *dev,
393 const struct counter_top_cfg *cfg);
395typedef uint32_t (*counter_api_get_pending_int)(const struct device *dev);
397typedef uint32_t (*counter_api_get_top_value)(const struct device *dev);
401typedef int (*counter_api_set_guard_period)(const struct device *dev, uint32_t ticks,
404typedef uint32_t (*counter_api_get_freq)(const struct device *dev);
406typedef uint64_t (*counter_api_get_freq_64)(const struct device *dev);
407
409typedef int (*counter_api_get_value_64)(const struct device *dev, uint64_t *ticks);
411typedef int (*counter_api_set_value_64)(const struct device *dev, uint64_t ticks);
413typedef int (*counter_api_set_alarm_64)(const struct device *dev, uint8_t chan_id,
414 const struct counter_alarm_cfg_64 *alarm_cfg);
418typedef int (*counter_api_set_guard_period_64)(const struct device *dev, uint64_t ticks,
421typedef uint64_t (*counter_api_get_top_value_64)(const struct device *dev);
423typedef int (*counter_api_set_top_value_64)(const struct device *dev,
424 const struct counter_top_cfg_64 *cfg);
426typedef int (*counter_api_capture_configure)(const struct device *dev, uint8_t chan_id,
428 counter_capture_cb_t cb, void *user_data);
430typedef int (*counter_api_capture_configure_64)(const struct device *dev, uint8_t chan_id,
432 counter_capture_cb_64_t cb, void *user_data);
434typedef int (*counter_api_enable_capture)(const struct device *dev, uint8_t chan_id);
436typedef int (*counter_api_disable_capture)(const struct device *dev, uint8_t chan_id);
437
441__subsystem struct counter_driver_api {
494#ifdef CONFIG_COUNTER_64BITS_FREQ
498 counter_api_get_freq_64 get_freq_64;
499#endif /* CONFIG_COUNTER_64BITS_FREQ */
500#ifdef CONFIG_COUNTER_64BITS_TICKS
504 counter_api_get_value_64 get_value_64;
508 counter_api_set_value_64 set_value_64;
512 counter_api_set_alarm_64 set_alarm_64;
516 counter_api_get_guard_period_64 get_guard_period_64;
520 counter_api_set_guard_period_64 set_guard_period_64;
524 counter_api_get_top_value_64 get_top_value_64;
528 counter_api_set_top_value_64 set_top_value_64;
529#endif /* CONFIG_COUNTER_64BITS_TICKS */
530#ifdef CONFIG_COUNTER_CAPTURE
534 counter_api_capture_configure capture_configure;
535#ifdef CONFIG_COUNTER_64BITS_TICKS
539 counter_api_capture_configure_64 capture_configure_64;
540#endif /* CONFIG_COUNTER_64BITS_TICKS */
544 counter_api_enable_capture enable_capture;
548 counter_api_disable_capture disable_capture;
549#endif /* CONFIG_COUNTER_CAPTURE */
550};
551
554
563__syscall bool counter_is_counting_up(const struct device *dev);
564
565static inline bool z_impl_counter_is_counting_up(const struct device *dev)
566{
567 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
568
569 return config->flags & COUNTER_CONFIG_INFO_COUNT_UP;
570}
571
579__syscall uint8_t counter_get_num_of_channels(const struct device *dev);
580
581static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev)
582{
583 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
584
585 return config->channels;
586}
587
588__syscall uint32_t counter_get_frequency(const struct device *dev);
589
590#ifdef CONFIG_COUNTER_64BITS_FREQ
601static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
602{
603 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
604 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
605
606 if (api->get_freq) {
607 return api->get_freq(dev);
608 } else {
609 return config->freq > UINT32_MAX ? UINT32_MAX : (uint32_t)config->freq;
610 }
611}
612
613#else
614
623static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
624{
625 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
626 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
627
628 return api->get_freq ? api->get_freq(dev) : config->freq;
629}
630#endif
631
640__syscall uint64_t counter_get_frequency_64(const struct device *dev);
641
642static inline uint64_t z_impl_counter_get_frequency_64(const struct device *dev)
643{
644#ifdef CONFIG_COUNTER_64BITS_FREQ
645 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
646 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
647
648 if (api->get_freq_64) {
649 return api->get_freq_64(dev);
650 } else if (api->get_freq) {
651 return (uint64_t)api->get_freq(dev);
652 } else {
653 return config->freq;
654 }
655#else
656 ARG_UNUSED(dev);
657 return -ENOTSUP;
658#endif
659}
660
661#ifdef CONFIG_COUNTER_64BITS_FREQ
662#define z_counter_get_frequency z_impl_counter_get_frequency_64
663#else
664#define z_counter_get_frequency z_impl_counter_get_frequency
665#endif
666
675__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
676
677static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us)
678{
679 uint64_t ticks = (us * z_counter_get_frequency(dev)) / USEC_PER_SEC;
680
681 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
682}
683
692__syscall uint64_t counter_us_to_ticks_64(const struct device *dev, uint64_t us);
693
694static inline uint64_t z_impl_counter_us_to_ticks_64(const struct device *dev, uint64_t us)
695{
696 return (us * z_counter_get_frequency(dev)) / USEC_PER_SEC;
697}
698
707__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
708
709static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, uint32_t ticks)
710{
711 return ((uint64_t)ticks * USEC_PER_SEC) / z_counter_get_frequency(dev);
712}
713
722__syscall uint64_t counter_ticks_to_us_64(const struct device *dev, uint64_t ticks);
723
724static inline uint64_t z_impl_counter_ticks_to_us_64(const struct device *dev, uint64_t ticks)
725{
726 return (ticks * USEC_PER_SEC) / z_counter_get_frequency(dev);
727}
728
737__syscall uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns);
738
739static inline uint32_t z_impl_counter_ns_to_ticks(const struct device *dev, uint64_t ns)
740{
741 uint64_t ticks = (ns * z_counter_get_frequency(dev)) / NSEC_PER_SEC;
742
743 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
744}
745
754__syscall uint64_t counter_ns_to_ticks_64(const struct device *dev, uint64_t ns);
755
756static inline uint64_t z_impl_counter_ns_to_ticks_64(const struct device *dev, uint64_t ns)
757{
758 return (ns * z_counter_get_frequency(dev)) / NSEC_PER_SEC;
759}
760
769__syscall uint64_t counter_ticks_to_ns(const struct device *dev, uint32_t ticks);
770
771static inline uint64_t z_impl_counter_ticks_to_ns(const struct device *dev, uint32_t ticks)
772{
773 return ((uint64_t)ticks * NSEC_PER_SEC) / z_counter_get_frequency(dev);
774}
775
784__syscall uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks);
785
786static inline uint64_t z_impl_counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks)
787{
788 return (ticks * NSEC_PER_SEC) / z_counter_get_frequency(dev);
789}
790
802__syscall uint32_t counter_get_max_top_value(const struct device *dev);
803
804static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev)
805{
806 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
807
808 return config->max_top_value;
809}
810
819__syscall int counter_start(const struct device *dev);
820
821static inline int z_impl_counter_start(const struct device *dev)
822{
823 return DEVICE_API_GET(counter, dev)->start(dev);
824}
825
835__syscall int counter_stop(const struct device *dev);
836
837static inline int z_impl_counter_stop(const struct device *dev)
838{
839 return DEVICE_API_GET(counter, dev)->stop(dev);
840}
841
850__syscall int counter_get_value(const struct device *dev, uint32_t *ticks);
851
852static inline int z_impl_counter_get_value(const struct device *dev, uint32_t *ticks)
853{
854 return DEVICE_API_GET(counter, dev)->get_value(dev, ticks);
855}
856
864__syscall int counter_reset(const struct device *dev);
865
866static inline int z_impl_counter_reset(const struct device *dev)
867{
868 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
869
870 if (!api->reset) {
871 return -ENOSYS;
872 }
873
874 return api->reset(dev);
875}
876
885__syscall int counter_set_value(const struct device *dev, uint32_t ticks);
886
887static inline int z_impl_counter_set_value(const struct device *dev, uint32_t ticks)
888{
889 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
890
891 if (!api->set_value) {
892 return -ENOSYS;
893 }
894
895 return api->set_value(dev, ticks);
896}
897
918__syscall int counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
919 const struct counter_alarm_cfg *alarm_cfg);
920
921static inline int z_impl_counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
922 const struct counter_alarm_cfg *alarm_cfg)
923{
924 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
925
926 if (chan_id >= counter_get_num_of_channels(dev)) {
927 return -ENOTSUP;
928 }
929
930 return api->set_alarm(dev, chan_id, alarm_cfg);
931}
932
945__syscall int counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id);
946
947static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id)
948{
949 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
950
951 if (chan_id >= counter_get_num_of_channels(dev)) {
952 return -ENOTSUP;
953 }
954
955 return api->cancel_alarm(dev, chan_id);
956}
957
986__syscall int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg);
987
988static inline int z_impl_counter_set_top_value(const struct device *dev,
989 const struct counter_top_cfg *cfg)
990{
991 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
992
993 if (cfg->ticks > counter_get_max_top_value(dev)) {
994 return -EINVAL;
995 }
996
997 return api->set_top_value(dev, cfg);
998}
999
1013__syscall uint32_t counter_get_pending_int(const struct device *dev);
1014
1015static inline uint32_t z_impl_counter_get_pending_int(const struct device *dev)
1016{
1017 return DEVICE_API_GET(counter, dev)->get_pending_int(dev);
1018}
1019
1027__syscall uint32_t counter_get_top_value(const struct device *dev);
1028
1029static inline uint32_t z_impl_counter_get_top_value(const struct device *dev)
1030{
1031 return DEVICE_API_GET(counter, dev)->get_top_value(dev);
1032}
1033
1092__syscall int counter_set_guard_period(const struct device *dev, uint32_t ticks, uint32_t flags);
1093
1094static inline int z_impl_counter_set_guard_period(const struct device *dev, uint32_t ticks,
1096{
1097 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1098
1099 if (!api->set_guard_period) {
1100 return -ENOSYS;
1101 }
1102
1103 return api->set_guard_period(dev, ticks, flags);
1104}
1105
1118
1119static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev, uint32_t flags)
1120{
1121 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1122
1123 return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0;
1124}
1125
1133__syscall uint64_t counter_get_max_top_value_64(const struct device *dev);
1134
1135static inline uint64_t z_impl_counter_get_max_top_value_64(const struct device *dev)
1136{
1137#ifdef CONFIG_COUNTER_64BITS_TICKS
1138 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
1139
1140 return config->max_top_value_64;
1141#else
1142 ARG_UNUSED(dev);
1143 return -ENOTSUP;
1144#endif
1145}
1146
1171__syscall int counter_set_top_value_64(const struct device *dev,
1172 const struct counter_top_cfg_64 *cfg);
1173
1174static inline int z_impl_counter_set_top_value_64(const struct device *dev,
1175 const struct counter_top_cfg_64 *cfg)
1176{
1177#ifdef CONFIG_COUNTER_64BITS_TICKS
1178 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1179
1180 if (cfg->ticks > counter_get_max_top_value_64(dev)) {
1181 return -EINVAL;
1182 }
1183
1184 return api->set_top_value_64(dev, cfg);
1185#else
1186 ARG_UNUSED(dev);
1187 ARG_UNUSED(cfg);
1188 return -ENOTSUP;
1189#endif
1190}
1191
1212__syscall int counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1213 const struct counter_alarm_cfg_64 *alarm_cfg);
1214
1215static inline int z_impl_counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1216 const struct counter_alarm_cfg_64 *alarm_cfg)
1217{
1218#ifdef CONFIG_COUNTER_64BITS_TICKS
1219 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1220
1221 if (chan_id >= counter_get_num_of_channels(dev)) {
1222 return -ENOTSUP;
1223 }
1224
1225 return api->set_alarm_64(dev, chan_id, alarm_cfg);
1226#else
1227 ARG_UNUSED(dev);
1228 ARG_UNUSED(chan_id);
1229 ARG_UNUSED(alarm_cfg);
1230 return -ENOTSUP;
1231#endif
1232}
1233
1241__syscall uint64_t counter_get_top_value_64(const struct device *dev);
1242
1243static inline uint64_t z_impl_counter_get_top_value_64(const struct device *dev)
1244{
1245#ifdef CONFIG_COUNTER_64BITS_TICKS
1246 return DEVICE_API_GET(counter, dev)->get_top_value_64(dev);
1247#else
1248 ARG_UNUSED(dev);
1249 return 0;
1250#endif
1251}
1252
1311__syscall int counter_set_guard_period_64(const struct device *dev, uint64_t ticks, uint32_t flags);
1312
1313static inline int z_impl_counter_set_guard_period_64(const struct device *dev, uint64_t ticks,
1315{
1316#ifdef CONFIG_COUNTER_64BITS_TICKS
1317 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1318
1319 if (!api->set_guard_period_64) {
1320 return -ENOSYS;
1321 }
1322
1323 return api->set_guard_period_64(dev, ticks, flags);
1324#else
1325 ARG_UNUSED(dev);
1326 ARG_UNUSED(ticks);
1327 ARG_UNUSED(flags);
1328 return -ENOTSUP;
1329#endif
1330}
1331
1344
1345static inline uint64_t z_impl_counter_get_guard_period_64(const struct device *dev, uint32_t flags)
1346{
1347#ifdef CONFIG_COUNTER_64BITS_TICKS
1348 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1349
1350 return (api->get_guard_period_64) ? api->get_guard_period_64(dev, flags) : 0;
1351#else
1352 ARG_UNUSED(dev);
1353 ARG_UNUSED(flags);
1354 return -ENOTSUP;
1355#endif
1356}
1357
1366__syscall int counter_get_value_64(const struct device *dev, uint64_t *ticks);
1367
1368static inline int z_impl_counter_get_value_64(const struct device *dev, uint64_t *ticks)
1369{
1370#ifdef CONFIG_COUNTER_64BITS_TICKS
1371 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1372
1373 if (!api->get_value_64) {
1374 return -ENOSYS;
1375 }
1376
1377 return api->get_value_64(dev, ticks);
1378#else
1379 ARG_UNUSED(dev);
1380 ARG_UNUSED(ticks);
1381 return -ENOTSUP;
1382#endif
1383}
1384
1393__syscall int counter_set_value_64(const struct device *dev, uint64_t ticks);
1394
1395static inline int z_impl_counter_set_value_64(const struct device *dev, uint64_t ticks)
1396{
1397#ifdef CONFIG_COUNTER_64BITS_TICKS
1398 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1399
1400 if (!api->set_value_64) {
1401 return -ENOSYS;
1402 }
1403
1404 return api->set_value_64(dev, ticks);
1405#else
1406 ARG_UNUSED(dev);
1407 ARG_UNUSED(ticks);
1408 return -ENOTSUP;
1409#endif
1410}
1411
1412#if defined(CONFIG_COUNTER_CAPTURE) || defined(__DOXYGEN__)
1422
1450static inline int counter_capture_configure(const struct device *dev, uint8_t chan_id,
1452 counter_capture_cb_t cb, void *user_data)
1453{
1454 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1455
1456 if (api->capture_configure == NULL) {
1457 return -ENOTSUP;
1458 }
1459
1460 if (chan_id >= counter_get_num_of_channels(dev)) {
1461 return -ENOTSUP;
1462 }
1463
1464 return api->capture_configure(dev, chan_id, flags, cb, user_data);
1465}
1466
1467#if defined(CONFIG_COUNTER_64BITS_TICKS) || defined(__DOXYGEN__)
1495static inline int counter_capture_configure_64(const struct device *dev, uint8_t chan_id,
1497 counter_capture_cb_64_t cb, void *user_data)
1498{
1499 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1500
1501 if (api->capture_configure_64 == NULL) {
1502 return -ENOTSUP;
1503 }
1504
1505 if (chan_id >= counter_get_num_of_channels(dev)) {
1506 return -ENOTSUP;
1507 }
1508
1509 return api->capture_configure_64(dev, chan_id, flags, cb, user_data);
1510}
1511#endif /* CONFIG_COUNTER_64BITS_TICKS */
1512
1522__syscall int counter_enable_capture(const struct device *dev, uint8_t chan_id);
1523
1524static inline int z_impl_counter_enable_capture(const struct device *dev, uint8_t chan_id)
1525{
1526 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1527
1528 if (api->enable_capture == NULL) {
1529 return -ENOTSUP;
1530 }
1531
1532 if (chan_id >= counter_get_num_of_channels(dev)) {
1533 return -ENOTSUP;
1534 }
1535
1536 return api->enable_capture(dev, chan_id);
1537}
1538
1548__syscall int counter_disable_capture(const struct device *dev, uint8_t chan_id);
1549
1550static inline int z_impl_counter_disable_capture(const struct device *dev, uint8_t chan_id)
1551{
1552 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1553
1554 if (api->disable_capture == NULL) {
1555 return -ENOTSUP;
1556 }
1557
1558 if (chan_id >= counter_get_num_of_channels(dev)) {
1559 return -ENOTSUP;
1560 }
1561
1562 return api->disable_capture(dev, chan_id);
1563}
1564
1566#endif /* CONFIG_COUNTER_CAPTURE */
1567
1568#ifdef __cplusplus
1569}
1570#endif
1571
1575
1576#include <zephyr/syscalls/counter.h>
1577
1578#endif /* ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
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_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:1450
int counter_disable_capture(const struct device *dev, uint8_t chan_id)
Disable capture on a channel.
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:1495
int counter_enable_capture(const struct device *dev, uint8_t chan_id)
Enable capture on a channel.
uint32_t(* counter_api_get_top_value)(const struct device *dev)
Callback API to retrieve the current top value.
Definition counter.h:397
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:423
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:421
int(* counter_api_set_value)(const struct device *dev, uint32_t ticks)
Callback API to set the current counter value.
Definition counter.h:385
int(* counter_api_stop)(const struct device *dev)
Callback API to stop the counter.
Definition counter.h:379
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:413
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:430
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:418
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:416
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:401
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:406
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:411
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:399
uint32_t(* counter_api_get_pending_int)(const struct device *dev)
Callback API to get pending counter interrupts.
Definition counter.h:395
uint32_t(* counter_api_get_freq)(const struct device *dev)
Callback API to get the counter frequency in Hz.
Definition counter.h:404
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:392
int(* counter_api_reset)(const struct device *dev)
Callback API to reset the counter to the initial value.
Definition counter.h:383
int(* counter_api_start)(const struct device *dev)
Callback API to start the counter.
Definition counter.h:377
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:426
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:390
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:434
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:409
int(* counter_api_get_value)(const struct device *dev, uint32_t *ticks)
Callback API to get the current counter value.
Definition counter.h:381
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:436
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:387
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.
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.
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.
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:229
void(* counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)
Alarm callback.
Definition counter.h:187
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:367
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:176
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:301
uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks)
Function to convert ticks with 64 bits to nanoseconds.
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:164
uint32_t counter_get_frequency(const struct device *dev)
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:53
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_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.
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.
uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns)
Function to convert nanoseconds to ticks.
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:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define NULL
Definition iar_missing_defs.h:20
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__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:306
void * user_data
User data returned in callback.
Definition counter.h:331
uint32_t flags
Alarm flags (see COUNTER_ALARM_FLAGS).
Definition counter.h:335
counter_alarm_callback_64_t callback
Callback called on alarm (cannot be NULL).
Definition counter.h:327
uint64_t ticks
Number of ticks that triggers the alarm.
Definition counter.h:323
Alarm callback structure.
Definition counter.h:192
uint32_t ticks
Number of ticks that triggers the alarm.
Definition counter.h:213
uint32_t flags
Alarm flags (see COUNTER_ALARM_FLAGS).
Definition counter.h:221
void * user_data
User data returned in callback.
Definition counter.h:217
counter_alarm_callback_t callback
Callback called on alarm (cannot be NULL).
Definition counter.h:196
Structure with generic counter features.
Definition counter.h:254
uint32_t max_top_value
Maximal (default) top value on which counter is reset (cleared or reloaded).
Definition counter.h:280
uint32_t freq
Frequency of the source clock if synchronous events are counted.
Definition counter.h:261
uint8_t flags
Flags (see COUNTER_FLAGS).
Definition counter.h:285
uint8_t channels
Number of channels that can be used for setting alarm.
Definition counter.h:291
<span class="mlabel">Driver Operations</span> Counter driver operations
Definition counter.h:441
counter_api_get_top_value get_top_value
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:481
counter_api_set_top_value set_top_value
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:473
counter_api_set_alarm set_alarm
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:465
counter_api_get_value get_value
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:453
counter_api_reset reset
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition counter.h:457
counter_api_get_pending_int get_pending_int
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:477
counter_api_stop stop
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:449
counter_api_set_guard_period set_guard_period
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition counter.h:489
counter_api_set_value set_value
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition counter.h:461
counter_api_start start
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:445
counter_api_get_guard_period get_guard_period
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition counter.h:485
counter_api_cancel_alarm cancel_alarm
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition counter.h:469
counter_api_get_freq get_freq
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition counter.h:493
Top value configuration structure.
Definition counter.h:340
void * user_data
User data passed to callback function (not valid if callback is NULL).
Definition counter.h:352
counter_top_callback_t callback
Callback function (can be NULL).
Definition counter.h:348
uint32_t flags
Flags (see COUNTER_TOP_FLAGS).
Definition counter.h:356
uint64_t ticks
Top value.
Definition counter.h:344
Top value configuration structure.
Definition counter.h:233
uint32_t ticks
Top value.
Definition counter.h:237
uint32_t flags
Flags (see COUNTER_TOP_FLAGS).
Definition counter.h:249
counter_top_callback_t callback
Callback function (can be NULL).
Definition counter.h:241
void * user_data
User data passed to callback function (not valid if callback is NULL).
Definition counter.h:245
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