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/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
439typedef int (*counter_api_set_calibration)(const struct device *dev, int32_t calibration);
441typedef int (*counter_api_get_calibration)(const struct device *dev, int32_t *calibration);
442
446__subsystem struct counter_driver_api {
499#ifdef CONFIG_COUNTER_64BITS_FREQ
503 counter_api_get_freq_64 get_freq_64;
504#endif /* CONFIG_COUNTER_64BITS_FREQ */
505#ifdef CONFIG_COUNTER_64BITS_TICKS
509 counter_api_get_value_64 get_value_64;
513 counter_api_set_value_64 set_value_64;
517 counter_api_set_alarm_64 set_alarm_64;
521 counter_api_get_guard_period_64 get_guard_period_64;
525 counter_api_set_guard_period_64 set_guard_period_64;
529 counter_api_get_top_value_64 get_top_value_64;
533 counter_api_set_top_value_64 set_top_value_64;
534#endif /* CONFIG_COUNTER_64BITS_TICKS */
535#ifdef CONFIG_COUNTER_CAPTURE
539 counter_api_capture_configure capture_configure;
540#ifdef CONFIG_COUNTER_64BITS_TICKS
544 counter_api_capture_configure_64 capture_configure_64;
545#endif /* CONFIG_COUNTER_64BITS_TICKS */
549 counter_api_enable_capture enable_capture;
553 counter_api_disable_capture disable_capture;
554#endif /* CONFIG_COUNTER_CAPTURE */
555#if defined(CONFIG_COUNTER_CALIBRATION) || defined(__DOXYGEN__)
564#endif /* CONFIG_COUNTER_CALIBRATION */
565};
566
569
578__syscall bool counter_is_counting_up(const struct device *dev);
579
580static inline bool z_impl_counter_is_counting_up(const struct device *dev)
581{
582 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
583
584 return config->flags & COUNTER_CONFIG_INFO_COUNT_UP;
585}
586
594__syscall uint8_t counter_get_num_of_channels(const struct device *dev);
595
596static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev)
597{
598 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
599
600 return config->channels;
601}
602
603__syscall uint32_t counter_get_frequency(const struct device *dev);
604
605#ifdef CONFIG_COUNTER_64BITS_FREQ
616static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
617{
618 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
619 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
620
621 if (api->get_freq) {
622 return api->get_freq(dev);
623 } else {
624 return config->freq > UINT32_MAX ? UINT32_MAX : (uint32_t)config->freq;
625 }
626}
627
628#else
629
638static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
639{
640 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
641 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
642
643 return api->get_freq ? api->get_freq(dev) : config->freq;
644}
645#endif
646
655__syscall uint64_t counter_get_frequency_64(const struct device *dev);
656
657static inline uint64_t z_impl_counter_get_frequency_64(const struct device *dev)
658{
659#ifdef CONFIG_COUNTER_64BITS_FREQ
660 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
661 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
662
663 if (api->get_freq_64) {
664 return api->get_freq_64(dev);
665 } else if (api->get_freq) {
666 return (uint64_t)api->get_freq(dev);
667 } else {
668 return config->freq;
669 }
670#else
671 ARG_UNUSED(dev);
672 return -ENOTSUP;
673#endif
674}
675
676#ifdef CONFIG_COUNTER_64BITS_FREQ
677#define z_counter_get_frequency z_impl_counter_get_frequency_64
678#else
679#define z_counter_get_frequency z_impl_counter_get_frequency
680#endif
681
690__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
691
692static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev, uint64_t us)
693{
694 uint64_t ticks = (us * z_counter_get_frequency(dev)) / USEC_PER_SEC;
695
696 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
697}
698
707__syscall uint64_t counter_us_to_ticks_64(const struct device *dev, uint64_t us);
708
709static inline uint64_t z_impl_counter_us_to_ticks_64(const struct device *dev, uint64_t us)
710{
711 uint64_t freq = z_counter_get_frequency(dev);
712
713 return (us / USEC_PER_SEC) * freq + ((us % USEC_PER_SEC) * freq) / USEC_PER_SEC;
714}
715
724__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
725
726static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev, uint32_t ticks)
727{
728 return ((uint64_t)ticks * USEC_PER_SEC) / z_counter_get_frequency(dev);
729}
730
739__syscall uint64_t counter_ticks_to_us_64(const struct device *dev, uint64_t ticks);
740
741static inline uint64_t z_impl_counter_ticks_to_us_64(const struct device *dev, uint64_t ticks)
742{
743 uint64_t freq = z_counter_get_frequency(dev);
744
745 return (ticks / freq) * USEC_PER_SEC + ((ticks % freq) * USEC_PER_SEC) / freq;
746}
747
756__syscall uint32_t counter_ns_to_ticks(const struct device *dev, uint64_t ns);
757
758static inline uint32_t z_impl_counter_ns_to_ticks(const struct device *dev, uint64_t ns)
759{
760 uint64_t ticks = (ns * z_counter_get_frequency(dev)) / NSEC_PER_SEC;
761
762 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
763}
764
773__syscall uint64_t counter_ns_to_ticks_64(const struct device *dev, uint64_t ns);
774
775static inline uint64_t z_impl_counter_ns_to_ticks_64(const struct device *dev, uint64_t ns)
776{
777 uint64_t freq = z_counter_get_frequency(dev);
778
779 return (ns / NSEC_PER_SEC) * freq + ((ns % NSEC_PER_SEC) * freq) / NSEC_PER_SEC;
780}
781
790__syscall uint64_t counter_ticks_to_ns(const struct device *dev, uint32_t ticks);
791
792static inline uint64_t z_impl_counter_ticks_to_ns(const struct device *dev, uint32_t ticks)
793{
794 return ((uint64_t)ticks * NSEC_PER_SEC) / z_counter_get_frequency(dev);
795}
796
805__syscall uint64_t counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks);
806
807static inline uint64_t z_impl_counter_ticks_to_ns_64(const struct device *dev, uint64_t ticks)
808{
809 uint64_t freq = z_counter_get_frequency(dev);
810
811 return (ticks / freq) * NSEC_PER_SEC + ((ticks % freq) * NSEC_PER_SEC) / freq;
812}
813
825__syscall uint32_t counter_get_max_top_value(const struct device *dev);
826
827static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev)
828{
829 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
830
831 return config->max_top_value;
832}
833
842__syscall int counter_start(const struct device *dev);
843
844static inline int z_impl_counter_start(const struct device *dev)
845{
846 return DEVICE_API_GET(counter, dev)->start(dev);
847}
848
858__syscall int counter_stop(const struct device *dev);
859
860static inline int z_impl_counter_stop(const struct device *dev)
861{
862 return DEVICE_API_GET(counter, dev)->stop(dev);
863}
864
873__syscall int counter_get_value(const struct device *dev, uint32_t *ticks);
874
875static inline int z_impl_counter_get_value(const struct device *dev, uint32_t *ticks)
876{
877 return DEVICE_API_GET(counter, dev)->get_value(dev, ticks);
878}
879
887__syscall int counter_reset(const struct device *dev);
888
889static inline int z_impl_counter_reset(const struct device *dev)
890{
891 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
892
893 if (!api->reset) {
894 return -ENOSYS;
895 }
896
897 return api->reset(dev);
898}
899
908__syscall int counter_set_value(const struct device *dev, uint32_t ticks);
909
910static inline int z_impl_counter_set_value(const struct device *dev, uint32_t ticks)
911{
912 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
913
914 if (!api->set_value) {
915 return -ENOSYS;
916 }
917
918 return api->set_value(dev, ticks);
919}
920
941__syscall int counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
942 const struct counter_alarm_cfg *alarm_cfg);
943
944static inline int z_impl_counter_set_channel_alarm(const struct device *dev, uint8_t chan_id,
945 const struct counter_alarm_cfg *alarm_cfg)
946{
947 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
948
949 if (chan_id >= counter_get_num_of_channels(dev)) {
950 return -ENOTSUP;
951 }
952
953 return api->set_alarm(dev, chan_id, alarm_cfg);
954}
955
968__syscall int counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id);
969
970static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev, uint8_t chan_id)
971{
972 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
973
974 if (chan_id >= counter_get_num_of_channels(dev)) {
975 return -ENOTSUP;
976 }
977
978 return api->cancel_alarm(dev, chan_id);
979}
980
1009__syscall int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg);
1010
1011static inline int z_impl_counter_set_top_value(const struct device *dev,
1012 const struct counter_top_cfg *cfg)
1013{
1014 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1015
1016 if (cfg->ticks > counter_get_max_top_value(dev)) {
1017 return -EINVAL;
1018 }
1019
1020 return api->set_top_value(dev, cfg);
1021}
1022
1036__syscall uint32_t counter_get_pending_int(const struct device *dev);
1037
1038static inline uint32_t z_impl_counter_get_pending_int(const struct device *dev)
1039{
1040 return DEVICE_API_GET(counter, dev)->get_pending_int(dev);
1041}
1042
1050__syscall uint32_t counter_get_top_value(const struct device *dev);
1051
1052static inline uint32_t z_impl_counter_get_top_value(const struct device *dev)
1053{
1054 return DEVICE_API_GET(counter, dev)->get_top_value(dev);
1055}
1056
1115__syscall int counter_set_guard_period(const struct device *dev, uint32_t ticks, uint32_t flags);
1116
1117static inline int z_impl_counter_set_guard_period(const struct device *dev, uint32_t ticks,
1119{
1120 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1121
1122 if (!api->set_guard_period) {
1123 return -ENOSYS;
1124 }
1125
1126 return api->set_guard_period(dev, ticks, flags);
1127}
1128
1141
1142static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev, uint32_t flags)
1143{
1144 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1145
1146 return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0;
1147}
1148
1156__syscall uint64_t counter_get_max_top_value_64(const struct device *dev);
1157
1158static inline uint64_t z_impl_counter_get_max_top_value_64(const struct device *dev)
1159{
1160#ifdef CONFIG_COUNTER_64BITS_TICKS
1161 const struct counter_config_info *config = (const struct counter_config_info *)dev->config;
1162
1163 return config->max_top_value_64;
1164#else
1165 ARG_UNUSED(dev);
1166 return -ENOTSUP;
1167#endif
1168}
1169
1194__syscall int counter_set_top_value_64(const struct device *dev,
1195 const struct counter_top_cfg_64 *cfg);
1196
1197static inline int z_impl_counter_set_top_value_64(const struct device *dev,
1198 const struct counter_top_cfg_64 *cfg)
1199{
1200#ifdef CONFIG_COUNTER_64BITS_TICKS
1201 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1202
1203 if (cfg->ticks > counter_get_max_top_value_64(dev)) {
1204 return -EINVAL;
1205 }
1206
1207 return api->set_top_value_64(dev, cfg);
1208#else
1209 ARG_UNUSED(dev);
1210 ARG_UNUSED(cfg);
1211 return -ENOTSUP;
1212#endif
1213}
1214
1235__syscall int counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1236 const struct counter_alarm_cfg_64 *alarm_cfg);
1237
1238static inline int z_impl_counter_set_channel_alarm_64(const struct device *dev, uint8_t chan_id,
1239 const struct counter_alarm_cfg_64 *alarm_cfg)
1240{
1241#ifdef CONFIG_COUNTER_64BITS_TICKS
1242 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1243
1244 if (chan_id >= counter_get_num_of_channels(dev)) {
1245 return -ENOTSUP;
1246 }
1247
1248 return api->set_alarm_64(dev, chan_id, alarm_cfg);
1249#else
1250 ARG_UNUSED(dev);
1251 ARG_UNUSED(chan_id);
1252 ARG_UNUSED(alarm_cfg);
1253 return -ENOTSUP;
1254#endif
1255}
1256
1264__syscall uint64_t counter_get_top_value_64(const struct device *dev);
1265
1266static inline uint64_t z_impl_counter_get_top_value_64(const struct device *dev)
1267{
1268#ifdef CONFIG_COUNTER_64BITS_TICKS
1269 return DEVICE_API_GET(counter, dev)->get_top_value_64(dev);
1270#else
1271 ARG_UNUSED(dev);
1272 return 0;
1273#endif
1274}
1275
1334__syscall int counter_set_guard_period_64(const struct device *dev, uint64_t ticks, uint32_t flags);
1335
1336static inline int z_impl_counter_set_guard_period_64(const struct device *dev, uint64_t ticks,
1338{
1339#ifdef CONFIG_COUNTER_64BITS_TICKS
1340 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1341
1342 if (!api->set_guard_period_64) {
1343 return -ENOSYS;
1344 }
1345
1346 return api->set_guard_period_64(dev, ticks, flags);
1347#else
1348 ARG_UNUSED(dev);
1349 ARG_UNUSED(ticks);
1350 ARG_UNUSED(flags);
1351 return -ENOTSUP;
1352#endif
1353}
1354
1367
1368static inline uint64_t z_impl_counter_get_guard_period_64(const struct device *dev, uint32_t flags)
1369{
1370#ifdef CONFIG_COUNTER_64BITS_TICKS
1371 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1372
1373 return (api->get_guard_period_64) ? api->get_guard_period_64(dev, flags) : 0;
1374#else
1375 ARG_UNUSED(dev);
1376 ARG_UNUSED(flags);
1377 return -ENOTSUP;
1378#endif
1379}
1380
1389__syscall int counter_get_value_64(const struct device *dev, uint64_t *ticks);
1390
1391static inline int z_impl_counter_get_value_64(const struct device *dev, uint64_t *ticks)
1392{
1393#ifdef CONFIG_COUNTER_64BITS_TICKS
1394 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1395
1396 if (!api->get_value_64) {
1397 return -ENOSYS;
1398 }
1399
1400 return api->get_value_64(dev, ticks);
1401#else
1402 ARG_UNUSED(dev);
1403 ARG_UNUSED(ticks);
1404 return -ENOTSUP;
1405#endif
1406}
1407
1416__syscall int counter_set_value_64(const struct device *dev, uint64_t ticks);
1417
1418static inline int z_impl_counter_set_value_64(const struct device *dev, uint64_t ticks)
1419{
1420#ifdef CONFIG_COUNTER_64BITS_TICKS
1421 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1422
1423 if (!api->set_value_64) {
1424 return -ENOSYS;
1425 }
1426
1427 return api->set_value_64(dev, ticks);
1428#else
1429 ARG_UNUSED(dev);
1430 ARG_UNUSED(ticks);
1431 return -ENOTSUP;
1432#endif
1433}
1434
1435#if defined(CONFIG_COUNTER_CAPTURE) || defined(__DOXYGEN__)
1445
1473static inline int counter_capture_configure(const struct device *dev, uint8_t chan_id,
1475 counter_capture_cb_t cb, void *user_data)
1476{
1477 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1478
1479 if (api->capture_configure == NULL) {
1480 return -ENOTSUP;
1481 }
1482
1483 if (chan_id >= counter_get_num_of_channels(dev)) {
1484 return -ENOTSUP;
1485 }
1486
1487 return api->capture_configure(dev, chan_id, flags, cb, user_data);
1488}
1489
1490#if defined(CONFIG_COUNTER_64BITS_TICKS) || defined(__DOXYGEN__)
1518static inline int counter_capture_configure_64(const struct device *dev, uint8_t chan_id,
1520 counter_capture_cb_64_t cb, void *user_data)
1521{
1522 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1523
1524 if (api->capture_configure_64 == NULL) {
1525 return -ENOTSUP;
1526 }
1527
1528 if (chan_id >= counter_get_num_of_channels(dev)) {
1529 return -ENOTSUP;
1530 }
1531
1532 return api->capture_configure_64(dev, chan_id, flags, cb, user_data);
1533}
1534#endif /* CONFIG_COUNTER_64BITS_TICKS */
1535
1545__syscall int counter_enable_capture(const struct device *dev, uint8_t chan_id);
1546
1547static inline int z_impl_counter_enable_capture(const struct device *dev, uint8_t chan_id)
1548{
1549 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1550
1551 if (api->enable_capture == NULL) {
1552 return -ENOTSUP;
1553 }
1554
1555 if (chan_id >= counter_get_num_of_channels(dev)) {
1556 return -ENOTSUP;
1557 }
1558
1559 return api->enable_capture(dev, chan_id);
1560}
1561
1571__syscall int counter_disable_capture(const struct device *dev, uint8_t chan_id);
1572
1573static inline int z_impl_counter_disable_capture(const struct device *dev, uint8_t chan_id)
1574{
1575 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1576
1577 if (api->disable_capture == NULL) {
1578 return -ENOTSUP;
1579 }
1580
1581 if (chan_id >= counter_get_num_of_channels(dev)) {
1582 return -ENOTSUP;
1583 }
1584
1585 return api->disable_capture(dev, chan_id);
1586}
1587
1589#endif /* CONFIG_COUNTER_CAPTURE */
1590
1591#if defined(CONFIG_COUNTER_CALIBRATION) || defined(__DOXYGEN__)
1605__syscall int counter_set_calibration(const struct device *dev, int32_t calibration);
1606
1607static inline int z_impl_counter_set_calibration(const struct device *dev, int32_t calibration)
1608{
1609 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1610
1611 if (!api->set_calibration) {
1612 return -ENOSYS;
1613 }
1614
1615 return api->set_calibration(dev, calibration);
1616}
1617
1627__syscall int counter_get_calibration(const struct device *dev, int32_t *calibration);
1628
1629static inline int z_impl_counter_get_calibration(const struct device *dev, int32_t *calibration)
1630{
1631 const struct counter_driver_api *api = DEVICE_API_GET(counter, dev);
1632
1633 if (!api->get_calibration) {
1634 return -ENOSYS;
1635 }
1636
1637 return api->get_calibration(dev, calibration);
1638}
1639#endif /* CONFIG_COUNTER_CALIBRATION */
1640
1641#ifdef __cplusplus
1642}
1643#endif
1644
1648
1649#include <zephyr/syscalls/counter.h>
1650
1651#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:1473
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:1518
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:441
uint32_t(* counter_api_get_top_value)(const struct device *dev)
Callback API to retrieve the current top value.
Definition counter.h:397
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: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
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:379
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
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
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
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
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.
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
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
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
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:411
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_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: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_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: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)
@def_driverbackendgroup{Counter,counter_interface}
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_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: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_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:387
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:439
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
__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: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
@driver_ops{Counter}
Definition counter.h:446
counter_api_get_calibration get_calibration
@driver_ops_optional Get counter calibration value.
Definition counter.h:563
counter_api_get_top_value get_top_value
@driver_ops_mandatory Function to retrieve current top value.
Definition counter.h:486
counter_api_set_top_value set_top_value
@driver_ops_mandatory Set counter top value.
Definition counter.h:478
counter_api_set_alarm set_alarm
@driver_ops_mandatory Set a single shot alarm on a channel.
Definition counter.h:470
counter_api_get_value get_value
@driver_ops_mandatory Get current counter value.
Definition counter.h:458
counter_api_reset reset
@driver_ops_optional Reset the counter to the initial value.
Definition counter.h:462
counter_api_get_pending_int get_pending_int
@driver_ops_mandatory Function to get pending interrupts.
Definition counter.h:482
counter_api_stop stop
@driver_ops_mandatory Stop counter device.
Definition counter.h:454
counter_api_set_guard_period set_guard_period
@driver_ops_optional Set guard period in counter ticks.
Definition counter.h:494
counter_api_set_value set_value
@driver_ops_optional Set current counter value.
Definition counter.h:466
counter_api_start start
@driver_ops_mandatory Start counter device in free running mode.
Definition counter.h:450
counter_api_set_calibration set_calibration
@driver_ops_optional Set counter calibration value.
Definition counter.h:559
counter_api_get_guard_period get_guard_period
@driver_ops_optional Return guard period.
Definition counter.h:490
counter_api_cancel_alarm cancel_alarm
@driver_ops_mandatory Cancel an alarm on a channel.
Definition counter.h:474
counter_api_get_freq get_freq
@driver_ops_optional
Definition counter.h:498
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