00001
00045 #ifndef TIMER_H_INCLUDED
00046 #define TIMER_H_INCLUDED
00047
00048 #include <types.h>
00049 #include <assert.h>
00050 #include <interrupt.h>
00051 #include <irq_handler.h>
00052
00053 typedef uint32_t counter_t;
00054
00056 typedef uint16_t counter_high_t;
00058 #define TIMER_MAX_DELAY (1UL << ((8*sizeof(counter_t))-1))
00059
00060 typedef void (*timer_callback_t)(void *data);
00061
00062 struct timer_task {
00064 struct timer *timer;
00066 counter_t time_stamp;
00068 timer_callback_t callback;
00070 void *data;
00072 struct timer_task *next;
00073 };
00074
00075 struct timer {
00077 void *port;
00079 uint32_t frequency;
00081 counter_high_t counter_high;
00083 bool action_pending;
00085 bool is_running;
00087 struct timer_task *first_task;
00088 };
00089
00090
00091 #ifdef __cplusplus
00092 extern "C" {
00093 #endif
00094
00095 static inline uint32_t timer_get_frequency(struct timer *timer)
00096 {
00097 assert(timer);
00098 return timer->frequency;
00099 }
00100
00101 static inline struct timer *timer_task_get_timer(struct timer_task *task)
00102 {
00103 assert(task);
00104 return task->timer;
00105 }
00106
00107 static inline void *timer_task_get_data(struct timer_task *task)
00108 {
00109 assert(task);
00110 return task->data;
00111 }
00112
00121 static inline void timer_reset_task(struct timer_task *task)
00122 {
00123 assert(task);
00124
00125
00126 task->next = NULL;
00127 }
00128
00140 static inline void timer_init_task(
00141 struct timer_task *task,
00142 timer_callback_t callback,
00143 void *data)
00144 {
00145 timer_reset_task(task);
00146
00147 task->callback = callback;
00148 task->data = data;
00149 }
00150
00151 void timer_init(struct timer *timer, void *port, uint32_t resolution);
00152
00153 extern counter_t timer_convert_us(struct timer *timer, uint32_t us);
00154
00155 void timer_add_task_ticks(
00156 struct timer *timer,
00157 struct timer_task *task,
00158 counter_t delay);
00159
00174 static inline void timer_add_task_us(
00175 struct timer *timer,
00176 struct timer_task *task,
00177 uint32_t delay_us)
00178 {
00179 timer_add_task_ticks(timer, task, timer_convert_us(timer, delay_us));
00180 }
00181
00182
00183 #ifdef __cplusplus
00184 }
00185 #endif
00186
00187 #endif // TIMER_H_INCLUDED