summaryrefslogtreecommitdiff
path: root/cesar/hal/timer/src/timer.c
blob: 3fe8c2b9503db0c843ba7c0c178f754e932b64aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/timer/src/timer.c
 * \brief   API source file for timers. 
 * \ingroup hal_timer
 *
 */
#include "common/std.h"
#include "hal/timer/timer.h"
#include "hal/leon/timer.h"

#include "hal/timer/inc/context.h"
#include "hal/timer/inc/timer.h"

static hal_timer_t hal_timer_global;

/**
 * Initialise software timer.
 * \param  phy  the phy context.
 * \return  the newly created context
 */
hal_timer_t *
hal_timer_init (phy_t *phy)
{
    dbg_assert (phy);

    hal_timer_global.leon_timer = leon_timer_init (&hal_timer_global,
                                                   (leon_timer_cb_t) hal_timer_instance_process,
                                                   phy);

    // initialise the heap.
    heap_init (&hal_timer_global.heap, hal_timer_instance_lesser);
    
    hal_timer_global.phy = phy;
    hal_timer_global.current_instance = NULL;

    return &hal_timer_global;
}

/**
 * Uninitialise the software timer.
 * \param  ctx  software timer context
 *
 * All timers should be stopped.
 */
void
hal_timer_uninit (hal_timer_t *ctx)
{
    dbg_assert (ctx);
    leon_timer_uninit(ctx->leon_timer);
}

/**
 * Initialise a new timer instance.
 * \param  ctx  software timer context
 * \param  instance  instance to initialise
 * \param  user_data  user data passed to the callback
 * \param  cb  timer instance callback, called in DSR context
 *
 * The instance is initialised to unprogrammed state.
 */
void
hal_timer_instance_init (hal_timer_t *ctx, hal_timer_instance_t *instance,
                         void *user_data, hal_timer_instance_cb_t cb)
{
    dbg_assert (ctx);
    dbg_assert (instance);
    dbg_assert (cb);

    instance->user_data = user_data;
    instance->cb = cb;
    // Timer not armed.
    instance->status = false;
}

/**
 * Uninitialise a timer instance.
 * \param  ctx  software timer context
 * \param  instance  instance to uninitialise
 *
 * The timer instance is canceled if necessary.
 */
void
hal_timer_instance_uninit (hal_timer_t *ctx, hal_timer_instance_t *instance)
{
    dbg_assert (ctx);
    dbg_assert (instance);

    instance->status = false;
}

/**
 * Program an instance at the given date.
 * \param  ctx  software timer context
 * \param  instance  instance to program
 * \param  date  instance expiration date
 */
void
hal_timer_instance_program (hal_timer_t *ctx,
                            hal_timer_instance_t *instance, u32 date)
{
    dbg_assert (ctx);
    dbg_assert (instance);
    dbg_assert (ctx->phy);
    dbg_assert (date > phy_date(ctx->phy));
    // The real timer as 24 bits register.
    dbg_assert (date < TIMER_MAX_TIME );

    // initialise the node.
    heap_node_init (&instance->node);
    instance->date = date;

    // lock the mutex to access to the heap.
    cyg_mutex_lock (&ctx->heap_mutex);

    // add the node to the heap.
    heap_insert (&ctx->heap, &instance->node);

    // unlock the mutex.
    cyg_mutex_unlock (&ctx->heap_mutex);

    // Modify the status of the timer.
    instance->status = true;

    // reprogram the leon timer
    if (ctx->current_instance && (ctx->current_instance->date > date))
    {
        ctx->current_instance = instance;
        leon_timer_cancel (ctx->leon_timer);
        leon_timer_program (ctx->leon_timer, instance->date);
    }
    else if (ctx->current_instance == NULL)
    {
        ctx->current_instance = instance;
        leon_timer_program (ctx->leon_timer, instance->date);
    }
}

/**
 * Get the next instance to program the timer.
 *
 * \param  ctx  the hal_timer context.
 */
void
hal_timer_reprogram (hal_timer_t *ctx)
{
    hal_timer_instance_t *instance;
    dbg_assert (ctx);

    // Verify if the heap contains any node.
    if (heap_empty(&ctx->heap))
        return;

    // lock the mutex to access to the heap.
    cyg_mutex_lock (&ctx->heap_mutex);

    // Get the root instance from the heap.
    instance = PARENT_OF (hal_timer_instance_t, node, heap_get_root(&ctx->heap));

    // unlock the mutex.
    cyg_mutex_unlock (&ctx->heap_mutex);

    if (instance)
    {
        // program the hardware timer.
        leon_timer_program (ctx->leon_timer, instance->date);
    }
}

/**
 * Cancel an instance programmation.
 * \param  ctx  software timer context
 * \param  instance  instance to cancel
 */
void
hal_timer_instance_cancel (hal_timer_t *ctx, hal_timer_instance_t *instance)
{
    dbg_assert (ctx);
    dbg_assert (instance);

    // verify the instance status.
    if (instance->status == false)
        return;

    // lock the mutex to access to the heap.
    cyg_mutex_lock (&ctx->heap_mutex);

    // add the node to the heap.
    if (!heap_empty (&ctx->heap))
        heap_remove (&ctx->heap, &instance->node);

    // unlock the mutex.
    cyg_mutex_unlock (&ctx->heap_mutex);

    // Cancel the timer.
    if (ctx->current_instance == instance)
    {
        leon_timer_cancel (ctx->leon_timer);
        hal_timer_reprogram (ctx);
    }

    instance->status = false;
}

/**
 * Process the instance when the timer as rised the Interruption.
 * This function will be call in dsr context.
 *
 * \param  ctx  the hal_timer context.
 */
void
hal_timer_instance_process (hal_timer_t *ctx)
{
    hal_timer_instance_t *instance;

    // lock the mutex to access
    cyg_mutex_lock (&ctx->heap_mutex);

    instance = PARENT_OF (hal_timer_instance_t, node, heap_get_root
                              (&ctx->heap));
    heap_remove (&ctx->heap, &instance->node);

    // unlock the mutex
    cyg_mutex_unlock (&ctx->heap_mutex);
    
    // call the callback function.
    (*instance->cb) (instance->user_data);
    instance->status = false;

    // Verify the other next instance in the heap, if it has a previous date
    // the callback will be called and the instance will be removed.
    while (!heap_empty(&ctx->heap))
    {
        // lock the mutex to access
        cyg_mutex_lock (&ctx->heap_mutex);

        instance = PARENT_OF (hal_timer_instance_t, node, heap_get_root
                              (&ctx->heap));
        // unlock the mutex
        cyg_mutex_unlock (&ctx->heap_mutex);

        if (instance->date < phy_date (ctx->phy))
        {
            // call the callback function.
            (*instance->cb) (instance->user_data);

            // remove the instance from the heap.
            cyg_mutex_lock (&ctx->heap_mutex);

            heap_remove (&ctx->heap, &instance->node);

            // unlock the mutex
            cyg_mutex_unlock (&ctx->heap_mutex);
            instance->status = false;
        }
        else
            break;
    }

    // program the timer with the next instance.
    hal_timer_reprogram (ctx);
}

/**
 * Compare the date of two node of the heap in the hal_timer context.
 * \param  left  left hand node
 * \param  right  right hand node
 * \return  true iff left is lesser than right
 */
bool
hal_timer_instance_lesser (heap_node_t *left, heap_node_t *right)
{
    hal_timer_instance_t *left_node;
    hal_timer_instance_t *right_node;


    if ((left == NULL) && (right != NULL))
        return false;
    else if ((left != NULL) && (right == NULL))
        return true;

    left_node = PARENT_OF (hal_timer_instance_t, node, left);
    right_node = PARENT_OF (hal_timer_instance_t, node, right);

    // compare the dates
    if (left_node->date < right_node->date)
        return true;
    else
        return false;
}

/**
 * Get the status of the timer intance.
 *
 * \return true  if the instance is programed.
 *         false  if the instance is not programed.
 */
bool
hal_timer_instance_get_status (hal_timer_instance_t *instance)
{
    dbg_assert (instance);

    return instance->status;
}