Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
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
23#include <zephyr/types.h>
24#include <stddef.h>
25#include <device.h>
26#include <stdbool.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
38#define COUNTER_CONFIG_INFO_COUNT_UP BIT(0)
39
52#define COUNTER_TOP_CFG_DONT_RESET BIT(0)
53
60#define COUNTER_TOP_CFG_RESET_WHEN_LATE BIT(1)
61
75#define COUNTER_ALARM_CFG_ABSOLUTE BIT(0)
76
83#define COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE BIT(1)
84
97#define COUNTER_GUARD_PERIOD_LATE_TO_SET BIT(0)
98
108typedef void (*counter_alarm_callback_t)(const struct device *dev,
109 uint8_t chan_id, uint32_t ticks,
110 void *user_data);
111
131};
132
138typedef void (*counter_top_callback_t)(const struct device *dev,
139 void *user_data);
140
154};
155
171};
172
173typedef int (*counter_api_start)(const struct device *dev);
174typedef int (*counter_api_stop)(const struct device *dev);
175typedef int (*counter_api_get_value)(const struct device *dev,
176 uint32_t *ticks);
177typedef int (*counter_api_set_alarm)(const struct device *dev,
178 uint8_t chan_id,
179 const struct counter_alarm_cfg *alarm_cfg);
180typedef int (*counter_api_cancel_alarm)(const struct device *dev,
181 uint8_t chan_id);
182typedef int (*counter_api_set_top_value)(const struct device *dev,
183 const struct counter_top_cfg *cfg);
184typedef uint32_t (*counter_api_get_pending_int)(const struct device *dev);
185typedef uint32_t (*counter_api_get_top_value)(const struct device *dev);
186typedef uint32_t (*counter_api_get_guard_period)(const struct device *dev,
188typedef int (*counter_api_set_guard_period)(const struct device *dev,
189 uint32_t ticks,
191
192__subsystem struct counter_driver_api {
203};
204
213__syscall bool counter_is_counting_up(const struct device *dev);
214
215static inline bool z_impl_counter_is_counting_up(const struct device *dev)
216{
217 const struct counter_config_info *config =
218 (const struct counter_config_info *)dev->config;
219
220 return config->flags & COUNTER_CONFIG_INFO_COUNT_UP;
221}
222
230__syscall uint8_t counter_get_num_of_channels(const struct device *dev);
231
232static inline uint8_t z_impl_counter_get_num_of_channels(const struct device *dev)
233{
234 const struct counter_config_info *config =
235 (const struct counter_config_info *)dev->config;
236
237 return config->channels;
238}
239
248__syscall uint32_t counter_get_frequency(const struct device *dev);
249
250static inline uint32_t z_impl_counter_get_frequency(const struct device *dev)
251{
252 const struct counter_config_info *config =
253 (const struct counter_config_info *)dev->config;
254
255 return config->freq;
256}
257
266__syscall uint32_t counter_us_to_ticks(const struct device *dev, uint64_t us);
267
268static inline uint32_t z_impl_counter_us_to_ticks(const struct device *dev,
269 uint64_t us)
270{
271 const struct counter_config_info *config =
272 (const struct counter_config_info *)dev->config;
273 uint64_t ticks = (us * config->freq) / USEC_PER_SEC;
274
275 return (ticks > (uint64_t)UINT32_MAX) ? UINT32_MAX : ticks;
276}
277
286__syscall uint64_t counter_ticks_to_us(const struct device *dev, uint32_t ticks);
287
288static inline uint64_t z_impl_counter_ticks_to_us(const struct device *dev,
289 uint32_t ticks)
290{
291 const struct counter_config_info *config =
292 (const struct counter_config_info *)dev->config;
293
294 return ((uint64_t)ticks * USEC_PER_SEC) / config->freq;
295}
296
304__syscall uint32_t counter_get_max_top_value(const struct device *dev);
305
306static inline uint32_t z_impl_counter_get_max_top_value(const struct device *dev)
307{
308 const struct counter_config_info *config =
309 (const struct counter_config_info *)dev->config;
310
311 return config->max_top_value;
312}
313
322__syscall int counter_start(const struct device *dev);
323
324static inline int z_impl_counter_start(const struct device *dev)
325{
326 const struct counter_driver_api *api =
327 (struct counter_driver_api *)dev->api;
328
329 return api->start(dev);
330}
331
341__syscall int counter_stop(const struct device *dev);
342
343static inline int z_impl_counter_stop(const struct device *dev)
344{
345 const struct counter_driver_api *api =
346 (struct counter_driver_api *)dev->api;
347
348 return api->stop(dev);
349}
350
359__syscall int counter_get_value(const struct device *dev, uint32_t *ticks);
360
361static inline int z_impl_counter_get_value(const struct device *dev,
362 uint32_t *ticks)
363{
364 const struct counter_driver_api *api =
365 (struct counter_driver_api *)dev->api;
366
367 return api->get_value(dev, ticks);
368}
369
389__syscall int counter_set_channel_alarm(const struct device *dev,
390 uint8_t chan_id,
391 const struct counter_alarm_cfg *alarm_cfg);
392
393static inline int z_impl_counter_set_channel_alarm(const struct device *dev,
394 uint8_t chan_id,
395 const struct counter_alarm_cfg *alarm_cfg)
396{
397 const struct counter_driver_api *api =
398 (struct counter_driver_api *)dev->api;
399
400 if (chan_id >= counter_get_num_of_channels(dev)) {
401 return -ENOTSUP;
402 }
403
404 return api->set_alarm(dev, chan_id, alarm_cfg);
405}
406
419__syscall int counter_cancel_channel_alarm(const struct device *dev,
420 uint8_t chan_id);
421
422static inline int z_impl_counter_cancel_channel_alarm(const struct device *dev,
423 uint8_t chan_id)
424{
425 const struct counter_driver_api *api =
426 (struct counter_driver_api *)dev->api;
427
428 if (chan_id >= counter_get_num_of_channels(dev)) {
429 return -ENOTSUP;
430 }
431
432 return api->cancel_alarm(dev, chan_id);
433}
434
459__syscall int counter_set_top_value(const struct device *dev,
460 const struct counter_top_cfg *cfg);
461
462static inline int z_impl_counter_set_top_value(const struct device *dev,
463 const struct counter_top_cfg
464 *cfg)
465{
466 const struct counter_driver_api *api =
467 (struct counter_driver_api *)dev->api;
468
469 if (cfg->ticks > counter_get_max_top_value(dev)) {
470 return -EINVAL;
471 }
472
473 return api->set_top_value(dev, cfg);
474}
475
489__syscall int counter_get_pending_int(const struct device *dev);
490
491static inline int z_impl_counter_get_pending_int(const struct device *dev)
492{
493 const struct counter_driver_api *api =
494 (struct counter_driver_api *)dev->api;
495
496 return api->get_pending_int(dev);
497}
498
506__syscall uint32_t counter_get_top_value(const struct device *dev);
507
508static inline uint32_t z_impl_counter_get_top_value(const struct device *dev)
509{
510 const struct counter_driver_api *api =
511 (struct counter_driver_api *)dev->api;
512
513 return api->get_top_value(dev);
514}
515
544__syscall int counter_set_guard_period(const struct device *dev,
545 uint32_t ticks,
547
548static inline int z_impl_counter_set_guard_period(const struct device *dev,
549 uint32_t ticks, uint32_t flags)
550{
551 const struct counter_driver_api *api =
552 (struct counter_driver_api *)dev->api;
553
554 if (!api->set_guard_period) {
555 return -ENOTSUP;
556 }
557
558 return api->set_guard_period(dev, ticks, flags);
559}
560
572__syscall uint32_t counter_get_guard_period(const struct device *dev,
574
575static inline uint32_t z_impl_counter_get_guard_period(const struct device *dev,
577{
578 const struct counter_driver_api *api =
579 (struct counter_driver_api *)dev->api;
580
581 return (api->get_guard_period) ? api->get_guard_period(dev, flags) : 0;
582}
583
584#ifdef __cplusplus
585}
586#endif
587
592#include <syscalls/counter.h>
593
594#endif /* ZEPHYR_INCLUDE_DRIVERS_COUNTER_H_ */
void
Definition: eswifi_shell.c:15
#define COUNTER_CONFIG_INFO_COUNT_UP
Counter count up flag.
Definition: counter.h:38
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:185
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:174
int counter_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg)
Set counter top value.
void(* counter_top_callback_t)(const struct device *dev, void *user_data)
Callback called when counter turns around.
Definition: counter.h:138
void(* counter_alarm_callback_t)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)
Alarm callback.
Definition: counter.h:108
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:188
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.
uint32_t(* counter_api_get_guard_period)(const struct device *dev, uint32_t flags)
Definition: counter.h:186
uint32_t(* counter_api_get_pending_int)(const struct device *dev)
Definition: counter.h:184
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:182
int(* counter_api_start)(const struct device *dev)
Definition: counter.h:173
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:180
int(* counter_api_get_value)(const struct device *dev, uint32_t *ticks)
Definition: counter.h:175
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:177
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
Definition: errno.h:61
#define ENOTSUP
Definition: errno.h:115
flags
Definition: http_parser.h:131
#define USEC_PER_SEC
Definition: sys_clock.h:133
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
#define UINT32_MAX
Definition: stdint.h:29
Alarm callback structure.
Definition: counter.h:126
uint32_t ticks
Definition: counter.h:128
uint32_t flags
Definition: counter.h:130
void * user_data
Definition: counter.h:129
counter_alarm_callback_t callback
Definition: counter.h:127
Structure with generic counter features.
Definition: counter.h:166
uint32_t max_top_value
Definition: counter.h:167
uint32_t freq
Definition: counter.h:168
uint8_t flags
Definition: counter.h:169
uint8_t channels
Definition: counter.h:170
Definition: counter.h:192
counter_api_get_top_value get_top_value
Definition: counter.h:200
counter_api_set_top_value set_top_value
Definition: counter.h:198
counter_api_set_alarm set_alarm
Definition: counter.h:196
counter_api_get_value get_value
Definition: counter.h:195
counter_api_get_pending_int get_pending_int
Definition: counter.h:199
counter_api_stop stop
Definition: counter.h:194
counter_api_set_guard_period set_guard_period
Definition: counter.h:202
counter_api_start start
Definition: counter.h:193
counter_api_get_guard_period get_guard_period
Definition: counter.h:201
counter_api_cancel_alarm cancel_alarm
Definition: counter.h:197
Top value configuration structure.
Definition: counter.h:149
uint32_t ticks
Definition: counter.h:150
uint32_t flags
Definition: counter.h:153
counter_top_callback_t callback
Definition: counter.h:151
void * user_data
Definition: counter.h:152
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
const void * config
Definition: device.h:371
static const intptr_t user_data[5]
Definition: main.c:590