summaryrefslogtreecommitdiff
path: root/cesar/hal/timer/test/src/leon_timer_stub.c
blob: b6d055bd7de97413b4e0625ce8437b33b91de46e (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/timer/test/src/timer.c
 * \brief   Test API timer
 * \ingroup « module »
 *
 * « long description »
 */
#include "common/std.h"
#include "hal/leon/timer.h"
#include "hal/phy/phy.h"
#include <cyg/kernel/kapi.h>

#define LEON_PRIO 0

struct leon_timer_t
{
    uint status;
    leon_timer_cb_t cb;
    void *user_data;
    u32 prgm_date;
};


static struct leon_timer_t leon_timer_global;
static u8 leon_stack [1024];
static cyg_handle_t handle_leon;
static cyg_thread leon;

void
leon_entry_function(cyg_addrword_t data)
{
    u32 current_date;
    while (true)
    {
        current_date = cyg_current_time ();
        if (less_mod2p32 (leon_timer_global.prgm_date, current_date)
            && (leon_timer_global.status))
            (*leon_timer_global.cb) (leon_timer_global.user_data);

        cyg_thread_delay (1);
    }
}

/**
 * Initialise Leon timer.
 * \param  user_data  user data passed to the callback
 * \param  cb  timer callback, called in ISR context
 * \param  phy  phy context used to get the phy date
 * \return  the newly created context
 */
leon_timer_t *
leon_timer_init (void *user_data, leon_timer_cb_t cb, phy_t *phy)
{
    cyg_thread_create(LEON_PRIO, &leon_entry_function,
                      0, "Leon", leon_stack,
                      1024,
                      &handle_leon, &leon);

    leon_timer_global.status = false;
    leon_timer_global.cb = cb;
    leon_timer_global.user_data = user_data;
    leon_timer_global.prgm_date = 0;

    cyg_thread_resume (handle_leon);

    return &leon_timer_global;
}

/**
 * Uninitialise the Leon timer.
 * \param  ctx  Leon timer context
 */
void
leon_timer_uninit (leon_timer_t *ctx)
{
    cyg_thread_suspend (handle_leon);
}

/**
 * Program the timer to the given date.
 * \param  ctx  Leon timer context
 * \param  sysdate  timer expiration date
 */
void
leon_timer_program (leon_timer_t *ctx, u32 sysdate)
{
    dbg_assert (ctx->status == false);
    ctx->prgm_date = sysdate;
    ctx->status = true;
}

/**
 * Cancel timer programmation.
 * \param  ctx  Leon timer context
 */
void
leon_timer_cancel (leon_timer_t *ctx)
{
    ctx->status = false;
}