Hardware Abstraction Layer for FreeRTOS
timer.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Author: Andreas Werner <kernel@andy89.org>
4  * Date: 2016
5  */
6 #ifndef TIMER_H_
7 #define TIMER_H_
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <hal.h>
24 struct timer;
30 struct timer_generic {
35  bool init;
36 #ifdef CONFIG_INSTANCE_NAME
37 
40  const char *name;
41 #endif
42 #ifdef CONFIG_TIMER_MULTI
43 
46  const struct timer_ops *ops;
47 #endif
48 };
49 #ifdef CONFIG_TIMER_MULTI
50 
53 struct timer_ops {
54  struct timer *(*timer_init)(uint32_t index, uint32_t prescaler, uint64_t basetime, int64_t adjust);
55  int32_t (*timer_deinit)(struct timer *timer);
56 
57  int32_t (*timer_setOverflowCallback)(struct timer *timer, bool (*callback)(struct timer *timer, void *data), void * data);
58  int32_t (*timer_start)(struct timer *timer);
59  int32_t (*timer_stop)(struct timer *timer);
60  int32_t (*timer_oneshot)(struct timer *timer, uint64_t us);
61  int32_t (*timer_periodic)(struct timer *timer, uint64_t us);
62  uint64_t (*timer_getTime)(struct timer *timer);
63 };
64 #endif
65 #ifndef CONFIG_TIMER_MULTI
66 
75 struct timer *timer_init(uint32_t index, uint32_t prescaler, uint64_t basetime, int64_t adjust);
81 int32_t timer_deinit(struct timer *timer);
89 int32_t timer_setOverflowCallback(struct timer *timer, bool (*callback)(struct timer *timer, void *data), void * data);
95 int32_t timer_start(struct timer *timer);
101 int32_t timer_stop(struct timer *timer);
108 int32_t timer_oneshot(struct timer *timer, uint64_t us);
115 int32_t timer_periodic(struct timer *timer, uint64_t us);
121 uint64_t timer_getTime(struct timer *timer);
122 #else
123 inline struct timer *timer_init(uint32_t index, uint32_t prescaler, uint64_t basetime, int64_t adjust) {
125  struct timer_generic *timer = (struct timer_generic *)HAL_GET_DEV(timer, index);
126  if (timer == NULL) {
127  return NULL;
128  }
129  return timer->ops->timer_init(index, prescaler, basetime, adjust);
130 }
131 inline int32_t timer_deinit(struct timer *timer) {
132  struct timer_generic *t = (struct timer_generic *) timer;
133  return t->ops->timer_deinit(timer);
134 }
135 
136 inline int32_t timer_setOverflowCallback(struct timer *timer, bool (*callback)(struct timer *timer, void *data), void * data) {
137  struct timer_generic *t = (struct timer_generic *) timer;
138  return t->ops->timer_setOverflowCallback(timer, callback, data);
139 }
140 inline int32_t timer_start(struct timer *timer) {
141  struct timer_generic *t = (struct timer_generic *) timer;
142  return t->ops->timer_start(timer);
143 }
144 inline int32_t timer_stop(struct timer *timer) {
145  struct timer_generic *t = (struct timer_generic *) timer;
146  return t->ops->timer_stop(timer);
147 }
148 inline int32_t timer_oneshot(struct timer *timer, uint64_t us) {
149  struct timer_generic *t = (struct timer_generic *) timer;
150  return t->ops->timer_oneshot(timer, us);
151 }
152 inline int32_t timer_periodic(struct timer *timer, uint64_t us) {
153  struct timer_generic *t = (struct timer_generic *) timer;
154  return t->ops->timer_periodic(timer, us);
155 }
156 inline uint64_t timer_getTime(struct timer *timer) {
157  struct timer_generic *t = (struct timer_generic *) timer;
158  return t->ops->timer_getTime(timer);
159 }
160 #endif
161 
162 #endif
hal.h
timer_generic
Definition: timer.h:30
HAL_DEFINE_GLOBAL_ARRAY
#define HAL_DEFINE_GLOBAL_ARRAY(gns)
Definition: hal.h:149
HAL_GET_DEV
#define HAL_GET_DEV(gns, index)
Definition: hal.h:182
timer_oneshot
int32_t timer_oneshot(struct timer *timer, uint64_t us)
timer_start
int32_t timer_start(struct timer *timer)
timer_generic::init
bool init
Definition: timer.h:35
timer_init
struct timer * timer_init(uint32_t index, uint32_t prescaler, uint64_t basetime, int64_t adjust)
timer_stop
int32_t timer_stop(struct timer *timer)
timer_setOverflowCallback
int32_t timer_setOverflowCallback(struct timer *timer, bool(*callback)(struct timer *timer, void *data), void *data)
timer_periodic
int32_t timer_periodic(struct timer *timer, uint64_t us)
timer_getTime
uint64_t timer_getTime(struct timer *timer)
timer_deinit
int32_t timer_deinit(struct timer *timer)