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 <errno.h>
|
34 |
|
|
|
35 |
|
|
#include "sys/alt_alarm.h"
|
36 |
|
|
#include "sys/alt_irq.h"
|
37 |
|
|
|
38 |
|
|
/*
|
39 |
|
|
* alt_alarm_start is called to register an alarm with the system. The
|
40 |
|
|
* "alarm" structure passed as an input argument does not need to be
|
41 |
|
|
* initialised by the user. This is done within this function.
|
42 |
|
|
*
|
43 |
|
|
* The remaining input arguments are:
|
44 |
|
|
*
|
45 |
|
|
* nticks - The time to elapse until the alarm executes. This is specified in
|
46 |
|
|
* system clock ticks.
|
47 |
|
|
* callback - The function to run when the indicated time has elapsed.
|
48 |
|
|
* context - An opaque value, passed to the callback function.
|
49 |
|
|
*
|
50 |
|
|
* Care should be taken when defining the callback function since it is
|
51 |
|
|
* likely to execute in interrupt context. In particular, this mean that
|
52 |
|
|
* library calls like printf() should not be made, since they can result in
|
53 |
|
|
* deadlock.
|
54 |
|
|
*
|
55 |
|
|
* The interval to be used for the next callback is the return
|
56 |
|
|
* value from the callback function. A return value of zero indicates that the
|
57 |
|
|
* alarm should be unregistered.
|
58 |
|
|
*
|
59 |
|
|
* alt_alarm_start() will fail if the timer facility has not been enabled
|
60 |
|
|
* (i.e. there is no system clock). Failure is indicated by a negative return
|
61 |
|
|
* value.
|
62 |
|
|
*/
|
63 |
|
|
|
64 |
|
|
int alt_alarm_start (alt_alarm* alarm, alt_u32 nticks,
|
65 |
|
|
alt_u32 (*callback) (void* context),
|
66 |
|
|
void* context)
|
67 |
|
|
{
|
68 |
|
|
alt_irq_context irq_context;
|
69 |
|
|
alt_u32 current_nticks = 0;
|
70 |
|
|
|
71 |
|
|
if (alt_ticks_per_second ())
|
72 |
|
|
{
|
73 |
|
|
if (alarm)
|
74 |
|
|
{
|
75 |
|
|
alarm->callback = callback;
|
76 |
|
|
alarm->context = context;
|
77 |
|
|
|
78 |
|
|
irq_context = alt_irq_disable_all ();
|
79 |
|
|
|
80 |
|
|
current_nticks = alt_nticks();
|
81 |
|
|
|
82 |
|
|
alarm->time = nticks + current_nticks + 1;
|
83 |
|
|
|
84 |
|
|
/*
|
85 |
|
|
* If the desired alarm time causes a roll-over, set the rollover
|
86 |
|
|
* flag. This will prevent the subsequent tick event from causing
|
87 |
|
|
* an alarm too early.
|
88 |
|
|
*/
|
89 |
|
|
if(alarm->time < current_nticks)
|
90 |
|
|
{
|
91 |
|
|
alarm->rollover = 1;
|
92 |
|
|
}
|
93 |
|
|
else
|
94 |
|
|
{
|
95 |
|
|
alarm->rollover = 0;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
alt_llist_insert (&alt_alarm_list, &alarm->llist);
|
99 |
|
|
alt_irq_enable_all (irq_context);
|
100 |
|
|
|
101 |
|
|
return 0;
|
102 |
|
|
}
|
103 |
|
|
else
|
104 |
|
|
{
|
105 |
|
|
return -EINVAL;
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
else
|
109 |
|
|
{
|
110 |
|
|
return -ENOTSUP;
|
111 |
|
|
}
|
112 |
|
|
}
|