summaryrefslogtreecommitdiff
path: root/cesar/hal/timer/test/src/leon_timer_stub.c
blob: e85576bf34632cc51e5fbcb982da0be91f9a4ad9 (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
/* 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>

struct leon_timer_t
{
    leon_timer_cb_t cb;
    void *user_data;
    phy_t *phy;
    uint delay;
    uint status;
};


static leon_timer_t leon_timer_global;
static int my_leon_stack[32*1024];
static cyg_handle_t my_leon_handle;
static cyg_thread my_leon_obj;

/**
 * my leon empty function for the leon thread
 */
void
my_leon (cyg_addrword_t data)
{
    while (leon_timer_global.status)
    {
        cyg_thread_delay (leon_timer_global.delay);

        leon_timer_global.status = false;
        (*leon_timer_global.cb) (leon_timer_global.user_data);
    }
}

/**
 * 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)
{
    dbg_assert (cb);
    dbg_assert (phy);

    leon_timer_global.phy = phy;
    leon_timer_global.cb = cb;
    leon_timer_global.user_data = user_data;

    // Thread Creation
    cyg_thread_create(12, my_leon, (cyg_addrword_t) 0,
                      "My Thread", &my_leon_stack, 32*1024,
                      &my_leon_handle, &my_leon_obj);

    leon_timer_global.status = false;
    return &leon_timer_global;
}

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

    cyg_thread_suspend (my_leon_handle);
    cyg_thread_delete (my_leon_handle);
}

/**
 * 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);
    dbg_assert (sysdate > phy_date(ctx->phy));

    ctx->delay = sysdate - phy_date (ctx->phy);
    ctx->status = true;
    cyg_thread_resume (my_leon_handle);
}

/**
 * Cancel timer programmation.
 * \param  ctx  Leon timer context
 */
void
leon_timer_cancel (leon_timer_t *ctx)
{
    dbg_assert (ctx);

    ctx->status = false;
    if (leon_timer_global.status == true)
        cyg_thread_suspend(my_leon_handle);
}