Ticket #16: timer.h

File timer.h, 5.4 KB (added by becoulet, 14 years ago)

Request based device timer API proposal

Line 
1/*
2    This file is part of MutekH.
3
4    MutekH is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    MutekH is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with MutekH; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18    Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2006
19
20*/
21
22/**
23 * @file
24 * @module{Device drivers}
25 * @short Timer device driver API
26 */
27
28#ifndef __DEVICE_TIMER_H__
29#define __DEVICE_TIMER_H__
30
31#ifdef __DRIVER_H__
32# error This header must not be included after "device/driver.h"
33#endif
34
35#include <hexo/types.h>
36#include <hexo/error.h>
37#include <hexo/gpct_platform_hexo.h>
38#include <gpct/cont_clist.h>
39
40struct device_s;
41struct driver_s;
42struct dev_timer_rq_s;
43
44/** timer absolute value type */
45typedef uint64_t dev_timer_value_t;
46/** timer relative value type */
47typedef uint32_t dev_timer_delay_t;
48
49/** timer device class callback function template */
50#define DEVTIMER_CALLBACK(n)    bool_t (n) (struct device_s *dev, struct dev_timer_rq_s *rq)
51/** Timer device request callback. This function is called when the
52    timer deadline is reached. The request is rescheduled if the
53    function return true and the @tt rq->delay field was not zero when registered. */
54typedef DEVTIMER_CALLBACK(devtimer_callback_t);
55
56/** Timer request @csee devtimer_request_t */
57struct dev_timer_rq_s
58{
59  dev_timer_value_t             deadline;    //< absolute timer deadline
60  dev_timer_delay_t             delay;       //< timer delay
61  devtimer_callback_t           *callback;   //< callback function
62  void                          *pvdata;     //< pv data for callback
63  void                          *drvdata;    //< driver private data
64  CONTAINER_ENTRY_TYPE(CLIST)   queue_entry; //< used by driver to enqueue requests
65};
66
67
68CONTAINER_TYPE(dev_timer_queue, CLIST, struct dev_timer_rq_s, queue_entry);
69CONTAINER_FUNC(dev_timer_queue, CLIST, static inline, dev_timer_queue);
70
71CONTAINER_KEY_TYPE(dev_timer_queue, PTR, SCALAR, deadline);
72CONTAINER_KEY_FUNC(dev_timer_queue, CLIST, static inline, dev_timer_queue, deadline);
73
74
75/** Timer device class request() function template. */
76#define DEVTIMER_REQUEST(n)     error_t  (n) (struct device_s *dev, struct dev_timer_rq_s *rq)
77
78/** Timer device class request() methode shortcut */
79#define dev_timer_request(dev, ...) (dev)->drv->f.timer.f_request(dev, __VA_ARGS__ )
80
81
82/**
83   Timer device class request function. Enqueue a timer request.
84
85   @param dev pointer to device descriptor
86   @param rq pointer to request. @tt rq->delay and @tt rq->callback fields
87     must be initialized. If @tt rq->delay is zero, @tt rq->deadline must be
88     initialized with absolute deadline timer value.
89
90   Request callback will be called immediately from within this
91   function if the deadline has already been reached.
92
93   @csee #DEVTIMER_REQUEST @csee #dev_timer_request
94*/
95typedef DEVTIMER_REQUEST(devtimer_request_t);
96
97
98/** Timer device class cancel() function template. */
99#define DEVTIMER_CANCEL(n)      error_t  (n) (struct device_s *dev, struct dev_timer_rq_s *rq)
100
101/** Timer device class cancel() methode shortcut */
102#define dev_timer_cancel(dev, ...) (dev)->drv->f.timer.f_cancel(dev, __VA_ARGS__ )
103
104/**
105   Timer device class cancel function. Cancel a queued timer request.
106
107   @param dev pointer to device descriptor
108   @param rq pointer to cancel.
109
110   @return @tt -ENOENT if the request was not found (already reached).
111
112   @csee #DEVTIMER_CANCEL @csee #dev_timer_cancel
113*/
114typedef DEVTIMER_CANCEL(devtimer_cancel_t);
115
116
117
118/** Timer device class getvalue() function template. */
119#define DEVTIMER_GETVALUE(n)    void (n) (struct device_s *dev, dev_timer_value_t *value)
120
121/** Timer device class getvalue() methode shortcut */
122#define dev_timer_getvalue(dev, ...) (dev)->drv->f.timer.f_getvalue(dev, __VA_ARGS__ )
123
124/**
125   Timer device class value read.
126
127   @param dev pointer to device descriptor
128   @param value pointer to value storage.
129
130   @csee #DEVTIMER_GETVALUE @csee #dev_timer_getvalue
131*/
132typedef DEVTIMER_GETVALUE(devtimer_getvalue_t);
133
134
135
136/** @see dev_timer_info_s */
137enum dev_timer_flags_e {
138  DEV_TIMER_CPUCYCLES       = 1,
139  DEV_TIMER_USECONDS        = 2,
140};
141
142struct dev_timer_info_s
143{
144  uint_fast8_t      flags;       //< timer flags described in @ref dev_timer_flags_e
145  dev_timer_delay_t resolution;  //< timer resolution
146};
147
148/** Timer device class getinfo() function template. */
149#define DEVTIMER_GETINFO(n)     void (n) (struct device_s *dev, struct dev_timer_info_s *info)
150
151/** Timer device class getinfo() methode shortcut */
152#define dev_timer_getinfo(dev, ...) (dev)->drv->f.timer.f_getinfo(dev, __VA_ARGS__ )
153
154/**
155   Timer device class info read.
156
157   @param dev pointer to device descriptor
158   @param info pointer to info storage.
159
160   @csee #DEVTIMER_GETINFO @csee #dev_timer_getinfo
161   @csee dev_timer_info_s
162*/
163typedef DEVTIMER_GETINFO(devtimer_getinfo_t);
164
165
166
167/** TIMER device class methods */
168
169struct dev_class_timer_s
170{
171  devtimer_request_t                    *f_request;
172  devtimer_cancel_t                     *f_cancel;
173  devtimer_getvalue_t                   *f_getvalue;
174  devtimer_getinfo_t                    *f_getinfo;
175};
176
177#endif
178