1 |
2 |
alfik |
/******************************************************************************
|
2 |
|
|
* *
|
3 |
|
|
* License Agreement *
|
4 |
|
|
* *
|
5 |
|
|
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
|
6 |
|
|
* All rights reserved. *
|
7 |
|
|
* *
|
8 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
9 |
|
|
* copy of this software and associated documentation files (the "Software"), *
|
10 |
|
|
* to deal in the Software without restriction, including without limitation *
|
11 |
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
12 |
|
|
* and/or sell copies of the Software, and to permit persons to whom the *
|
13 |
|
|
* Software is furnished to do so, subject to the following conditions: *
|
14 |
|
|
* *
|
15 |
|
|
* The above copyright notice and this permission notice shall be included in *
|
16 |
|
|
* all copies or substantial portions of the Software. *
|
17 |
|
|
* *
|
18 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
21 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
22 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
23 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
24 |
|
|
* DEALINGS IN THE SOFTWARE. *
|
25 |
|
|
* *
|
26 |
|
|
* This agreement shall be governed in all respects by the laws of the State *
|
27 |
|
|
* of California and by the laws of the United States of America. *
|
28 |
|
|
* *
|
29 |
|
|
* Altera does not recommend, suggest or require that this reference design *
|
30 |
|
|
* file be used in conjunction or combination with any other product. *
|
31 |
|
|
******************************************************************************/
|
32 |
|
|
|
33 |
|
|
#include "sys/alt_irq.h"
|
34 |
|
|
#include "sys/alt_alarm.h"
|
35 |
|
|
#include "os/alt_hooks.h"
|
36 |
|
|
#include "alt_types.h"
|
37 |
|
|
|
38 |
|
|
/*
|
39 |
|
|
* "_alt_tick_rate" is used to store the value of the system clock frequency
|
40 |
|
|
* in ticks per second. It is initialised to zero, which corresponds to there
|
41 |
|
|
* being no system clock facility available.
|
42 |
|
|
*/
|
43 |
|
|
|
44 |
|
|
alt_u32 _alt_tick_rate = 0;
|
45 |
|
|
|
46 |
|
|
/*
|
47 |
|
|
* "_alt_nticks" is the number of system clock ticks that have elapsed since
|
48 |
|
|
* reset.
|
49 |
|
|
*/
|
50 |
|
|
|
51 |
|
|
volatile alt_u32 _alt_nticks = 0;
|
52 |
|
|
|
53 |
|
|
/*
|
54 |
|
|
* "alt_alarm_list" is the head of a linked list of registered alarms. This is
|
55 |
|
|
* initialised to be an empty list.
|
56 |
|
|
*/
|
57 |
|
|
|
58 |
|
|
ALT_LLIST_HEAD(alt_alarm_list);
|
59 |
|
|
|
60 |
|
|
/*
|
61 |
|
|
* alt_alarm_stop() is called to remove an alarm from the list of registered
|
62 |
|
|
* alarms. Alternatively an alarm can unregister itself by returning zero when
|
63 |
|
|
* the alarm executes.
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
void alt_alarm_stop (alt_alarm* alarm)
|
67 |
|
|
{
|
68 |
|
|
alt_irq_context irq_context;
|
69 |
|
|
|
70 |
|
|
irq_context = alt_irq_disable_all();
|
71 |
|
|
alt_llist_remove (&alarm->llist);
|
72 |
|
|
alt_irq_enable_all (irq_context);
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/*
|
76 |
|
|
* alt_tick() is periodically called by the system clock driver in order to
|
77 |
|
|
* process the registered list of alarms. Each alarm is registed with a
|
78 |
|
|
* callback interval, and a callback function, "callback".
|
79 |
|
|
*
|
80 |
|
|
* The return value of the callback function indicates how many ticks are to
|
81 |
|
|
* elapse until the next callback. A return value of zero indicates that the
|
82 |
|
|
* alarm should be deactivated.
|
83 |
|
|
*
|
84 |
|
|
* alt_tick() is expected to run at interrupt level.
|
85 |
|
|
*/
|
86 |
|
|
|
87 |
|
|
void alt_tick (void)
|
88 |
|
|
{
|
89 |
|
|
alt_alarm* next;
|
90 |
|
|
alt_alarm* alarm = (alt_alarm*) alt_alarm_list.next;
|
91 |
|
|
|
92 |
|
|
alt_u32 next_callback;
|
93 |
|
|
|
94 |
|
|
/* update the tick counter */
|
95 |
|
|
|
96 |
|
|
_alt_nticks++;
|
97 |
|
|
|
98 |
|
|
/* process the registered callbacks */
|
99 |
|
|
|
100 |
|
|
while (alarm != (alt_alarm*) &alt_alarm_list)
|
101 |
|
|
{
|
102 |
|
|
next = (alt_alarm*) alarm->llist.next;
|
103 |
|
|
|
104 |
|
|
/*
|
105 |
|
|
* Upon the tick-counter rolling over it is safe to clear the
|
106 |
|
|
* roll-over flag; once the flag is cleared this (or subsequnt)
|
107 |
|
|
* tick events are enabled to generate an alarm event.
|
108 |
|
|
*/
|
109 |
|
|
if ((alarm->rollover) && (_alt_nticks == 0))
|
110 |
|
|
{
|
111 |
|
|
alarm->rollover = 0;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/* if the alarm period has expired, make the callback */
|
115 |
|
|
if ((alarm->time <= _alt_nticks) && (alarm->rollover == 0))
|
116 |
|
|
{
|
117 |
|
|
next_callback = alarm->callback (alarm->context);
|
118 |
|
|
|
119 |
|
|
/* deactivate the alarm if the return value is zero */
|
120 |
|
|
|
121 |
|
|
if (next_callback == 0)
|
122 |
|
|
{
|
123 |
|
|
alt_alarm_stop (alarm);
|
124 |
|
|
}
|
125 |
|
|
else
|
126 |
|
|
{
|
127 |
|
|
alarm->time += next_callback;
|
128 |
|
|
|
129 |
|
|
/*
|
130 |
|
|
* If the desired alarm time causes a roll-over, set the rollover
|
131 |
|
|
* flag. This will prevent the subsequent tick event from causing
|
132 |
|
|
* an alarm too early.
|
133 |
|
|
*/
|
134 |
|
|
if(alarm->time < _alt_nticks)
|
135 |
|
|
{
|
136 |
|
|
alarm->rollover = 1;
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
alarm = next;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
/*
|
144 |
|
|
* Update the operating system specific timer facilities.
|
145 |
|
|
*/
|
146 |
|
|
|
147 |
|
|
ALT_OS_TIME_TICK();
|
148 |
|
|
}
|
149 |
|
|
|