RTEMS 6.1-rc6
Loading...
Searching...
No Matches
tlib.h
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Timer Library (TLIB)
5 *
6 * The Library rely on timer drivers, the timer presented by the
7 * timer driver must look like a down-counter timer, which generates
8 * interrupt (if configured) when underflown.
9 *
10 * If Timer hardware is an up-counter the Timer driver must recalculate
11 * into values that would match as if it was a down-counter.
12 *
13 * COPYRIGHT (c) 2011.
14 * Cobham Gaisler AB.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef __TLIB_H__
39#define __TLIB_H__
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45struct tlib_dev;
46
47typedef void (*tlib_isr_t)(void *data);
48
49enum {
50 TLIB_FLAGS_BROADCAST = 0x01
51};
52
53struct tlib_drv {
54 /*** Functions ***/
55 void (*reset)(struct tlib_dev *hand);
56 void (*get_freq)(
57 struct tlib_dev *hand,
58 unsigned int *basefreq,
59 unsigned int *tickrate);
60 int (*set_freq)(struct tlib_dev *hand, unsigned int tickrate);
61 void (*irq_reg)(struct tlib_dev *hand, tlib_isr_t func, void *data, int flags);
62 void (*irq_unreg)(struct tlib_dev *hand, tlib_isr_t func,void *data);
63 void (*start)(struct tlib_dev *hand, int once);
64 void (*stop)(struct tlib_dev *hand);
65 void (*restart)(struct tlib_dev *hand);
66 void (*get_counter)(struct tlib_dev *hand, unsigned int *counter);
67 int (*custom)(struct tlib_dev *hand, int cmd, void *arg);
68 int (*int_pend)(struct tlib_dev *hand, int ack);
69 void (*get_widthmask)(struct tlib_dev *hand, unsigned int *widthmask);
70};
71
72struct tlib_dev {
73 struct tlib_dev *next;
74 char status; /* 0=closed, 1=open, 2=timer started */
75 char index; /* Timer Index */
76 tlib_isr_t isr_func;
77 void *isr_data;
78 struct tlib_drv *drv;
79};
80
81#ifdef RTEMS_DRVMGR_STARTUP
82/* Clock Driver Timer register function. Only used when the TLIB-Clock
83 * driver is used. A specific Timer is registered as the System Clock
84 * timer.
85 */
86extern void Clock_timer_register(int timer_number);
87#endif
88
89/* Register Timer. Called by Timer Drivers in order to register
90 * a Timer to the Timer Library. The registration order determines
91 * the Timer Number used in tlib_open() to identify a specific
92 * Timer.
93 */
94extern int tlib_dev_reg(struct tlib_dev *newdev);
95
96/* Allocate a Timer.
97 *
98 * A Timer handle is returned identifying the timer in later calls.
99 */
100extern void *tlib_open(int timer_no);
101
102/* Close Timer */
103extern void tlib_close(void *hand);
104
105/* Returns Number of Timers currently registered to Timer Library */
106extern int tlib_ntimer(void);
107
108static inline void tlib_reset(void *hand)
109{
110 struct tlib_dev *dev = hand;
111
112 dev->drv->reset(dev);
113}
114/* Get Frequencies:
115 * - Base Frequency (unchangable base freq rate of timer, prescaler, clkinput)
116 * - Current Tick Rate [in multiples of Base Frequency]
117 */
118static inline void tlib_get_freq(
119 void *hand,
120 unsigned int *basefreq,
121 unsigned int *tickrate)
122{
123 struct tlib_dev *dev = hand;
124
125 dev->drv->get_freq(dev, basefreq, tickrate);
126}
127
128/* Set current Tick Rate in number of "Base-Frequency ticks" */
129static inline int tlib_set_freq(void *hand, unsigned int tickrate)
130{
131 struct tlib_dev *dev = hand;
132
133 return dev->drv->set_freq(dev, tickrate);
134}
135
136/* Register ISR at Timer ISR */
137static inline void tlib_irq_unregister(void *hand)
138{
139 struct tlib_dev *dev = hand;
140
141 if ( dev->isr_func ) {
142 dev->drv->irq_unreg(dev, dev->isr_func, dev->isr_data);
143 dev->isr_func = NULL;
144 }
145}
146
147/* Register ISR at Timer ISR */
148static inline void tlib_irq_register(void *hand, tlib_isr_t func, void *data, int flags)
149{
150 struct tlib_dev *dev = hand;
151
152 /* Unregister previous ISR if installed */
153 tlib_irq_unregister(hand);
154 dev->isr_func = func;
155 dev->isr_data = data;
156 dev->drv->irq_reg(dev, func, data, flags);
157}
158
159/* Start Timer, ISRs will be generated if enabled.
160 *
161 * once determines if timer should restart (=0) on underflow automatically,
162 * or stop when underflow is reached (=1).
163 */
164static inline void tlib_start(void *hand, int once)
165{
166 struct tlib_dev *dev = hand;
167
168 dev->drv->start(dev, once);
169}
170
171/* Stop Timer, no more ISRs will be generated */
172static inline void tlib_stop(void *hand)
173{
174 struct tlib_dev *dev = hand;
175
176 dev->drv->stop(dev);
177}
178
179/* Restart/Reload Timer, may be usefull if a Watchdog Timer */
180static inline void tlib_restart(void *hand)
181{
182 struct tlib_dev *dev = hand;
183
184 dev->drv->restart(dev);
185}
186
187/* Get current counter value (since last tick) */
188static inline void tlib_get_counter(void *hand, unsigned int *counter)
189{
190 struct tlib_dev *dev = hand;
191
192 dev->drv->get_counter(dev, counter);
193}
194
195/* Do a custom operation */
196static inline void tlib_custom(void *hand, int cmd, void *arg)
197{
198 struct tlib_dev *dev = hand;
199
200 dev->drv->custom(dev, cmd, arg);
201}
202
203static inline int tlib_interrupt_pending(void *hand, int ack)
204{
205 struct tlib_dev *dev = hand;
206
207 return dev->drv->int_pend(dev, ack);
208}
209
210static inline void tlib_get_widthmask(void *hand, unsigned int *widthmask)
211{
212 struct tlib_dev *dev = hand;
213
214 dev->drv->get_widthmask(dev, widthmask);
215}
216
217#ifdef __cplusplus
218}
219#endif
220
221#endif
Definition: tlib.h:72
Definition: tlib.h:53