Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
counter.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13#ifndef ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_
14#define ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_
15
25#include <errno.h>
26
27#include <zephyr/types.h>
28#include <stddef.h>
29#include <zephyr/device.h>
30#include <zephyr/sys_clock.h>
31#include <stdbool.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
46#define COUNTER_CONFIG_INFO_COUNT_UP BIT(0)
47
62#define COUNTER_TOP_CFG_DONT_RESET BIT(0)
63
70#define COUNTER_TOP_CFG_RESET_WHEN_LATE BIT(1)
71
87#define COUNTER_ALARM_CFG_ABSOLUTE BIT(0)
88
95#define COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE BIT(1)
96
111#define COUNTER_GUARD_PERIOD_LATE_TO_SET BIT(0)
112
122typedef void (*counter_alarm_callback_t)(const struct device *dev,
123 uint8_t chan_id, uint32_t ticks,
124 void *user_data);
125
159
165typedef void (*counter_top_callback_t)(const struct device *dev,
166 void *user_data);
167
188
211
212typedef int (*counter_api_start)(const struct device *dev);
213typedef int (*counter_api_stop)(const struct device *dev);
214typedef int (*counter_api_get_value)(const struct device *dev,
215 uint32_t *ticks);
216typedef int (*counter_api_get_value_64)(const struct device *dev,
217 uint64_t *ticks);
218typedef int (*counter_api_set_alarm)(const struct device *dev,
219 uint8_t chan_id,
220 const struct counter_alarm_cfg *alarm_cfg);
221typedef int (*counter_api_cancel_alarm)(const struct device *dev,
222 uint8_t chan_id);
223typedef int (*counter_api_set_top_value)(const struct device *dev,
224 const struct counter_top_cfg *cfg);
225typedef uint32_t (*counter_api_get_pending_int)(const struct device *dev);
226typedef uint32_t (*counter_api_get_top_value)(const struct device *dev);
227typedef uint32_t (*counter_api_get_guard_period)(const struct device *dev,
229typedef int (*counter_api_set_guard_period)(const struct device *dev,
230 uint32_t ticks,
232typedef uint32_t (*counter_api_get_freq)(const struct device *dev);
233
248
257__syscall bool counter_is_counting_up(const struct device *dev);
258
259static inline bool z_impl_counter_is_counting_up(const struct device *dev)
260{
261 const struct counter_config_info *config =
262 (const struct counter_config_info *)dev->config;
263
264 return config->flags & COUNTER_CONFIG_INFO_COUNT_UP;
265}
266
274__syscall uint8_t counter_get_num_of_channels(const struct device *dev);
275
276static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev)
277{
278 const struct counter_config_info *config =
279 (const struct counter_config_info *)dev->config;
280
281 return config->channels;
282}
283
292__syscall uint32_t counter_get_frequency(const struct device *dev);
293
294static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
295{
296 const struct counter_config_info *config =
297 (const struct counter_config_info *)dev->config;
298 const struct counter_driver_api *api =
299 (struct counter_driver_api *)dev->api;
300
301 return api->get_freq ? api->get_freq(dev) : config->freq;
302}
303
312__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
313
314static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev,
315 uint64_t us)
316{
317 uint64_t ticks = (us * z_impl_counter_get_frequency(dev)) / USEC_PER_SEC;
318
319 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
320}
321
330__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
331
332static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev,
333 uint32_t ticks)
334{
335 return ((uint64_t)ticks * USEC_PER_SEC) / z_impl_counter_get_frequency(dev);
336}
337
345__syscall uint32_t counter_get_max_top_value(const struct device *dev);
346
347static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev)
348{
349 const struct counter_config_info *config =
350 (const struct counter_config_info *)dev->config;
351
352 return config->max_top_value;
353}
354
363__syscall int counter_start(const struct device *dev);
364
365static inline int z_impl_counter_start(const struct device *dev)
366{
367 const struct counter_driver_api *api =
368 (struct counter_driver_api *)dev->api;
369
370 return api->start(dev);
371}
372
382__syscall int counter_stop(const struct device *dev);
383
384static inline int z_impl_counter_stop(const struct device *dev)
385{
386 const struct counter_driver_api *api =
387 (struct counter_driver_api *)dev->api;
388
389 return api->stop(dev);
390}
391
400__syscall int counter_get_value(const struct device *dev, uint32_t *ticks);
401
402static inline int z_impl_counter_get_value(const struct device *dev,
403 uint32_t *ticks)
404{
405 const struct counter_driver_api *api =
406 (struct counter_driver_api *)dev->api;
407
408 return api->get_value(dev, ticks);
409}
410
419__syscall int counter_get_value_64(const struct device *dev, uint64_t *ticks);
420
421static inline int z_impl_counter_get_value_64(const struct device *dev,
422 uint64_t *ticks)
423{
424 const struct counter_driver_api *api =
425 (struct counter_driver_api *)dev->api;
426
427 if (!api->get_value_64) {
428 return -ENOTSUP;
429 }
430
431 return api->get_value_64(dev, ticks);
432}
433
454__syscall int counter_set_channel_alarm(const struct device *dev,
455 uint8_t chan_id,
456 const struct counter_alarm_cfg *alarm_cfg);
457
458static inline int z_impl_counter_set_channel_alarm(const struct device *dev,
459 uint8_t chan_id,
460 const struct counter_alarm_cfg *alarm_cfg)
461{
462 const struct counter_driver_api *api =
463 (struct counter_driver_api *)dev->api;
464
465 if (chan_id >= counter_get_num_of_channels(dev)) {
466 return -ENOTSUP;
467 }
468
469 return api->set_alarm(dev, chan_id, alarm_cfg);
470}
471
484__syscall int counter_cancel_channel_alarm(const struct device *dev,
485 uint8_t chan_id);
486
487static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev,
488 uint8_t chan_id)
489{
490 const struct counter_driver_api *api =
491 (struct counter_driver_api *)dev->api;
492
493 if (chan_id >= counter_get_num_of_channels(dev)) {
494 return -ENOTSUP;
495 }
496
497 return api->cancel_alarm(dev, chan_id);
498}
499
524__syscall int counter_set_top_value(const struct device *dev,
525 const struct counter_top_cfg *cfg);
526
527static inline int z_impl_counter_set_top_value(const struct device *dev,
528 const struct counter_top_cfg
529 *cfg)
530{
531 const struct counter_driver_api *api =
532 (struct counter_driver_api *)dev->api;
533
534 if (cfg->ticks > counter_get_max_top_value(dev)) {
535 return -EINVAL;
536 }
537
538 return api->set_top_value(dev, cfg);
539}
540
554__syscall int counter_get_pending_int(const struct device *dev);
555
556static inline int z_impl_counter_get_pending_int(const struct device *dev)
557{
558 const struct counter_driver_api *api =
559 (struct counter_driver_api *)dev->api;
560
561 return api->get_pending_int(dev);
562}
563
571__syscall uint32_t counter_get_top_value(const struct device *dev);
572
573static inline uint32_t z_impl_counter_get_top_value(const struct device *dev)
574{
575 const struct counter_driver_api *api =
576 (struct counter_driver_api *)dev->api;
577
578 return api->get_top_value(dev);
579}
580
633__syscall int counter_set_guard_period(const struct device *dev,
634 uint32_t ticks,
636
637static inline int z_impl_counter_set_guard_period(const struct device *dev,
638 uint32_t ticks, uint32_t flags)
639{
640 const struct counter_driver_api *api =
641 (struct counter_driver_api *)dev->api;
642
643 if (!api->set_guard_period) {
644 return -ENOTSUP;
645 }
646
647 return api->set_guard_period(dev, ticks, flags);
648}
649
661__syscall uint32_t counter_get_guard_period(const struct device *dev,
663
664static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev,
666{
667 const struct counter_driver_api *api =
668 (struct counter_driver_api *)dev->api;
669
670 return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0;
671}
672
673#ifdef __cplusplus
674}
675#endif
676
681#include <zephyr/syscalls/counter.h>
682
683#endif /* ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_ */
System error numbers.
#define USEC_PER_SEC
number of microseconds per second
Definition sys_clock.h:110
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.
uint32_t(* counter_api_get_top_value)(const struct device *dev)
Definition counter.h:226
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_api_stop)(const struct device *dev)
Definition counter.h:213
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.
void(* counter_top_callback_t)(const struct device *dev, void *user_data)
Callback called when counter turns around.
Definition counter.h:165
void(* counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)
Alarm callback.
Definition counter.h:122
int counter_get_pending_int(const struct device *dev)
Function to get pending interrupts.
uint32_t counter_get_guard_period(const struct device *dev, uint32_t flags)
Return guard period.
int(* counter_api_set_guard_period)(const struct device *dev, uint32_t ticks, uint32_t flags)
Definition counter.h:229
uint32_t counter_get_frequency(const struct device *dev)
Function to get counter frequency.
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:46
uint32_t(* counter_api_get_guard_period)(const struct device *dev, uint32_t flags)
Definition counter.h:227
uint32_t(* counter_api_get_pending_int)(const struct device *dev)
Definition counter.h:225
uint32_t(* counter_api_get_freq)(const struct device *dev)
Definition counter.h:232
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)
Definition counter.h:223
int(* counter_api_start)(const struct device *dev)
Definition counter.h:212
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_api_cancel_alarm)(const struct device *dev, uint8_t chan_id)
Definition counter.h:221
int(* counter_api_get_value_64)(const struct device *dev, uint64_t *ticks)
Definition counter.h:216
int(* counter_api_get_value)(const struct device *dev, uint32_t *ticks)
Definition counter.h:214
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)
Definition counter.h:218
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 ENOTSUP
Unsupported value.
Definition errno.h:114
flags
Definition parser.h:96
__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:128
uint32_t ticks
Number of ticks that triggers the alarm.
Definition counter.h:149
uint32_t flags
Alarm flags (see COUNTER_ALARM_FLAGS).
Definition counter.h:157
void * user_data
User data returned in callback.
Definition counter.h:153
counter_alarm_callback_t callback
Callback called on alarm (cannot be NULL).
Definition counter.h:132
Structure with generic counter features.
Definition counter.h:191
uint32_t max_top_value
Maximal (default) top value on which counter is reset (cleared or reloaded).
Definition counter.h:195
uint32_t freq
Frequency of the source clock if synchronous events are counted.
Definition counter.h:199
uint8_t flags
Flags (see COUNTER_FLAGS).
Definition counter.h:203
uint8_t channels
Number of channels that can be used for setting alarm.
Definition counter.h:209
Definition counter.h:234
counter_api_get_value_64 get_value_64
Definition counter.h:238
counter_api_get_top_value get_top_value
Definition counter.h:243
counter_api_set_top_value set_top_value
Definition counter.h:241
counter_api_set_alarm set_alarm
Definition counter.h:239
counter_api_get_value get_value
Definition counter.h:237
counter_api_get_pending_int get_pending_int
Definition counter.h:242
counter_api_stop stop
Definition counter.h:236
counter_api_set_guard_period set_guard_period
Definition counter.h:245
counter_api_start start
Definition counter.h:235
counter_api_get_guard_period get_guard_period
Definition counter.h:244
counter_api_cancel_alarm cancel_alarm
Definition counter.h:240
counter_api_get_freq get_freq
Definition counter.h:246
Top value configuration structure.
Definition counter.h:170
uint32_t ticks
Top value.
Definition counter.h:174
uint32_t flags
Flags (see COUNTER_TOP_FLAGS).
Definition counter.h:186
counter_top_callback_t callback
Callback function (can be NULL).
Definition counter.h:178
void * user_data
User data passed to callback function (not valid if callback is NULL).
Definition counter.h:182
Runtime device structure (in ROM) per driver instance.
Definition device.h:411
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:417
const void * config
Address of device instance config information.
Definition device.h:415
Variables needed for system clock.