summaryrefslogtreecommitdiff
path: root/cesar/hal/timer/timer.h
blob: e513c405a572d693a67ab13f2b10c8d6e620deaf (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
#ifndef hal_timer_timer_h
#define hal_timer_timer_h
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/timer/timer.h
 * \brief   API function to use a timer with several actors.
 * \ingroup hal_timer
 *
 * To use it, in the module context add a hal_timer_t variable.
 */
#include "hal/phy/phy.h"
#include "lib/heap.h"

/** Call back declaration. */
typedef void (*hal_timer_instance_cb_t) (void *user_data);

/** forward declarations. */
typedef struct hal_timer_t hal_timer_t;

struct hal_timer_instance_t
{
    /** Status. 0 not armed, 1 armed. */
    bool status;
    /** Date to expire the timer. */
    uint date;
    /** Function call back to call. */
    hal_timer_instance_cb_t cb;
    /** User data to provide. */
    void *user_data;
    /** heap node. */
    heap_node_t node;
};
typedef struct hal_timer_instance_t hal_timer_instance_t;

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

/**
 * Uninitialise the software timer.
 * \param  ctx  software timer context
 *
 * All timers should be stopped.
 */
void
hal_timer_uninit (hal_timer_t *ctx);

/**
 * 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);

/**
 * 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);

/**
 * 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);

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


/**
 * Get the status of the timer instance.
 *
 * \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);

#endif /* hal_timer_timer_h */