zhangbo
2025-02-13 b32910bdb85c6e9d19abf97f1465c573a0bf9d38
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
#include "wsf_queue.h"
#include "wsf_timer.h"
#include "mk_common.h"
#include "mk_misc.h"
// #include "mk_trace.h"
 
/**************************************************************************************************
  Global Variables
**************************************************************************************************/
 
static wsfQueue_t wsfTimerTimerQueue; /*!< Timer queue */
 
/*************************************************************************************************/
/*!
 *  \brief  Remove a timer from queue.  Note this function does not lock task scheduling.
 *
 *  \param  pTimer  Pointer to timer.
 */
/*************************************************************************************************/
static void wsfTimerRemove(wsfTimer_t *pTimer)
{
    wsfTimer_t *pElem;
    wsfTimer_t *pPrev = NULL;
 
    pElem = (wsfTimer_t *)wsfTimerTimerQueue.pHead;
 
    /* find timer in queue */
    while (pElem != NULL)
    {
        if (pElem == pTimer)
        {
            break;
        }
        pPrev = pElem;
        pElem = pElem->pNext;
    }
 
    /* if timer found, remove from queue */
    if (pElem != NULL)
    {
        WsfQueueRemove(&wsfTimerTimerQueue, pTimer, pPrev);
 
        pTimer->isStarted = FALSE;
    }
}
 
/*************************************************************************************************/
/*!
 *  \brief  Insert a timer into the queue sorted by the timer expiration.
 *
 *  \param  pTimer  Pointer to timer.
 *  \param  ticks   Timer ticks until expiration.
 *  \param  mode    Timer work mode.
 */
/*************************************************************************************************/
static void wsfTimerInsert(wsfTimer_t *pTimer, uint32_t ticks, uint8_t mode)
{
    wsfTimer_t *pElem;
    wsfTimer_t *pPrev = NULL;
 
    uint32_t lock = int_lock();
 
    /* if timer is already running stop it first */
    if (pTimer->isStarted)
    {
        wsfTimerRemove(pTimer);
    }
 
    pTimer->isStarted = TRUE;
    pTimer->ticks = ticks;
    pTimer->count = ticks;
    pTimer->mode = mode;
 
    pElem = (wsfTimer_t *)wsfTimerTimerQueue.pHead;
 
    /* find insertion point in queue */
    while (pElem != NULL)
    {
        if (pTimer->ticks < pElem->ticks)
        {
            break;
        }
        pPrev = pElem;
        pElem = pElem->pNext;
    }
 
    WsfQueueInsert(&wsfTimerTimerQueue, pTimer, pPrev);
    int_unlock(lock);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Return the number of ticks until the next timer expiration.  Note that this
 *          function can return zero even if a timer is running, indicating a timer
 *          has expired but has not yet been serviced.
 *
 *  \return The number of ticks until the next timer expiration.
 */
/*************************************************************************************************/
uint32_t WsfTimerNextExpiration(void)
{
    uint32_t ticks;
    uint32_t lock = int_lock();
 
    if (wsfTimerTimerQueue.pHead == NULL)
    {
        ticks = UINT32_MAX;
    }
    else
    {
        ticks = ((wsfTimer_t *)wsfTimerTimerQueue.pHead)->ticks;
    }
 
    int_unlock(lock);
    return ticks;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Initialize the timer service.  This function should only be called once
 *          upon system initialization.
 */
/*************************************************************************************************/
void WsfTimerInit(void)
{
    WSF_QUEUE_INIT(&wsfTimerTimerQueue);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Start a timer in units of 32768Hz ticks.
 *
 *  \param  pTimer  Pointer to timer.
 *  \param  ticks   32768Hz ticks until expiration.
 *  \param  mode    Timer work mode.
 */
/*************************************************************************************************/
void WsfTimerStartTick(wsfTimer_t *pTimer, uint32_t ticks, uint8_t mode)
{
    wsfTimerInsert(pTimer, ticks, mode);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Start a timer in units of milliseconds.
 *
 *  \param  pTimer  Pointer to timer.
 *  \param  ms     Milliseconds until expiration.
 *  \param  mode    Timer work mode.
 */
/*************************************************************************************************/
void WsfTimerStartMs(wsfTimer_t *pTimer, uint32_t ms, uint8_t mode)
{
    wsfTimerInsert(pTimer, __MS_TO_32K_CNT(ms), mode);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Stop a timer.
 *
 *  \param  pTimer  Pointer to timer.
 */
/*************************************************************************************************/
void WsfTimerStop(wsfTimer_t *pTimer)
{
    uint32_t lock = int_lock();
 
    wsfTimerRemove(pTimer);
 
    int_unlock(lock);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Update the timer service with the number of elapsed ticks.
 *
 *  \param  ticks  Number of ticks since last update.
 */
/*************************************************************************************************/
void WsfTimerUpdate(uint32_t ticks)
{
    wsfTimer_t *pElem;
 
    uint32_t lock = int_lock();
 
    pElem = (wsfTimer_t *)wsfTimerTimerQueue.pHead;
 
    /* iterate over timer queue */
    while (pElem != NULL)
    {
        /* decrement ticks while preventing underflow */
        if (pElem->ticks > ticks)
        {
            pElem->ticks -= ticks;
        }
        else
        {
            pElem->ticks = 0;
        }
 
        if (pElem->ticks < POWER_DOWN_TIME_TICK_MIN)
        {
            pElem->ticks = 0;
            /* timer expired; set task for this timer as ready */
            WsfTaskSetReady(pElem->handlerId, WSF_TIMER_EVENT);
        }
 
        // LOG_INFO(TRACE_MODULE_OS, "WsfTimerUpdate  %u %u %u %u\r\n", pElem->handlerId, pElem->msg.event, pElem->ticks, ticks);
 
        pElem = pElem->pNext;
    }
 
    int_unlock(lock);
}
 
/*************************************************************************************************/
/*!
 *  \brief  Service expired timers for the given task.
 *
 *  \return Pointer to timer or NULL.
 */
/*************************************************************************************************/
wsfTimer_t *WsfTimerServiceExpired(void)
{
    wsfTimer_t *pElem;
    wsfTimer_t *pPrev = NULL;
 
    uint32_t lock = int_lock();
 
    /* find expired timers in queue */
    pElem = (wsfTimer_t *)wsfTimerTimerQueue.pHead;
    if ((pElem != NULL) && (pElem->ticks == 0))
    {
        if (pElem->mode == WSF_TIMER_PERIODIC)
        {
            wsfTimerInsert(pElem, pElem->count, WSF_TIMER_PERIODIC);
        }
        else
        {
            WsfQueueRemove(&wsfTimerTimerQueue, pElem, pPrev);
            pElem->isStarted = FALSE;
        }
 
        int_unlock(lock);
        return pElem;
    }
 
    int_unlock(lock);
    return NULL;
}