summaryrefslogtreecommitdiff
path: root/components/drivers/clock_time/clock_hrtimer.c
blob: 6fbfcecdeadfd1a039890764891637878dc24084 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright (c) 2006-2026, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-01-01     RT-Thread    Clock time hrtimer
 */

#include <rtdevice.h>
#include <rthw.h>
#include <rtthread.h>

#include <drivers/clock_time.h>

#define DBG_SECTION_NAME               "drv.clock_time"
#define DBG_LEVEL                      DBG_INFO
#include <rtdbg.h>

#define CLOCK_TIME_NSEC_PER_SEC (1000000000ULL)

#ifdef ARCH_CPU_64BIT
#define _HRTIMER_MAX_CNT UINT64_MAX
#else
#define _HRTIMER_MAX_CNT UINT32_MAX
#endif

static rt_list_t          _timer_list   = RT_LIST_OBJECT_INIT(_timer_list);
static RT_DEFINE_SPINLOCK(_spinlock);

rt_inline rt_clock_hrtimer_t _first_hrtimer(void)
{
    return rt_list_isempty(&_timer_list) ? RT_NULL : rt_list_first_entry(&_timer_list, struct rt_clock_hrtimer, node);
}

rt_inline unsigned long _clock_time_get_cnt(void)
{
    return rt_clock_time_get_counter();
}

rt_inline rt_bool_t _cnt_before(unsigned long a, unsigned long b)
{
    return ((rt_base_t)(a - b)) < 0;
}

rt_inline rt_bool_t _cnt_after(unsigned long a, unsigned long b)
{
    return _cnt_before(b, a);
}

rt_weak rt_uint64_t rt_clock_hrtimer_getres(void)
{
    return rt_clock_time_get_event_res_scaled();
}

rt_weak unsigned long rt_clock_hrtimer_getfrq(void)
{
    return (unsigned long)rt_clock_time_get_event_freq();
}

static rt_tick_t _hrtimer_cnt_to_tick(unsigned long cnt)
{
    rt_uint64_t res = rt_clock_hrtimer_getres();
    rt_uint64_t ns;

    if (res == 0)
    {
        return 0;
    }

    ns = ((rt_uint64_t)cnt * res) / RT_CLOCK_TIME_RESMUL;
    if (ns == 0)
    {
        return 1;
    }

    ns = (ns * RT_TICK_PER_SECOND + CLOCK_TIME_NSEC_PER_SEC - 1) / CLOCK_TIME_NSEC_PER_SEC;
    if (ns == 0)
    {
        return 1;
    }

    return (rt_tick_t)ns;
}

rt_weak rt_err_t rt_clock_hrtimer_settimeout(unsigned long cnt)
{
    static rt_timer_t timer = RT_NULL;
    static struct rt_timer _sh_rtimer;
    rt_tick_t tick;

    RT_ASSERT(cnt > 0);

    if (rt_clock_time_set_timeout(cnt) == RT_EOK)
    {
        return RT_EOK;
    }

    tick = _hrtimer_cnt_to_tick(cnt);
    if (tick == 0)
    {
        return -RT_ERROR;
    }

    if (timer == RT_NULL)
    {
        timer = &_sh_rtimer;
        rt_timer_init(timer, "shrtimer", (void (*)(void *))rt_clock_hrtimer_process, RT_NULL,
                      tick, RT_TIMER_FLAG_ONE_SHOT);
    }
    else
    {
        rt_timer_control(timer, RT_TIMER_CTRL_SET_TIME, &tick);
        rt_timer_control(timer, RT_TIMER_CTRL_SET_PARM, RT_NULL);
    }

    if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
    {
        rt_timer_stop(timer);
    }

    rt_timer_start(timer);
    return RT_EOK;
}

static unsigned long _cnt_convert(unsigned long cnt)
{
    unsigned long count;
    rt_uint64_t src_res;
    rt_uint64_t event_res;
    rt_uint64_t result;

    count = cnt - _clock_time_get_cnt();
    if (count > (_HRTIMER_MAX_CNT / 2))
    {
        return 0;
    }

    src_res = rt_clock_time_get_res_scaled();
    event_res = rt_clock_hrtimer_getres();
    if (src_res == 0 || event_res == 0)
    {
        return 0;
    }

    result = ((rt_uint64_t)count * src_res) / event_res;

    return result == 0 ? 1 : (unsigned long)result;
}

static void _sleep_timeout(void *parameter)
{
    struct rt_clock_hrtimer *timer = parameter;
    rt_completion_done(&timer->completion);
}

static void _insert_timer_to_list_locked(rt_clock_hrtimer_t timer)
{
    rt_clock_hrtimer_t iter;

    rt_list_for_each_entry(iter, &_timer_list, node)
    {
        if (_cnt_before(timer->timeout_cnt, iter->timeout_cnt))
        {
            break;
        }
    }
    rt_list_insert_before(&iter->node, &(timer->node));

    timer->flag |= RT_TIMER_FLAG_ACTIVATED;
}

static void _hrtimer_process_locked(void)
{
    rt_clock_hrtimer_t timer;

    while ((timer = _first_hrtimer()) != RT_NULL)
    {
        unsigned long now = _clock_time_get_cnt();

        if (_cnt_before(now, timer->timeout_cnt))
        {
            break;
        }

        rt_list_remove(&(timer->node));

        if (timer->flag & RT_TIMER_FLAG_PERIODIC)
        {
            timer->timeout_cnt = timer->delay_cnt + now;
            _insert_timer_to_list_locked(timer);
        }
        else
        {
            timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
        }

        if (timer->timeout_func)
        {
            timer->timeout_func(timer->parameter);
        }
    }
}

static void _set_next_timeout_locked(void)
{
    rt_clock_hrtimer_t timer;
    rt_ubase_t next_timeout_cnt;
    rt_bool_t find_next;

    do
    {
        find_next = RT_FALSE;
        if ((timer = _first_hrtimer()) != RT_NULL)
        {
            next_timeout_cnt = _cnt_convert(timer->timeout_cnt);
            if (next_timeout_cnt > 0)
            {
                rt_clock_hrtimer_settimeout(next_timeout_cnt);
            }
            else
            {
                _hrtimer_process_locked();
                find_next = RT_TRUE;
            }
        }
    }
    while (find_next);
}

void rt_clock_hrtimer_process(void)
{
    rt_base_t level = rt_spin_lock_irqsave(&_spinlock);

    _hrtimer_process_locked();
    _set_next_timeout_locked();

    rt_spin_unlock_irqrestore(&_spinlock, level);
}

void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer,
                           const char        *name,
                           rt_uint8_t         flag,
                           void (*timeout)(void *parameter),
                           void *parameter)
{
    RT_ASSERT(timer != RT_NULL);
    RT_ASSERT(timeout != RT_NULL);

    rt_memset(timer, 0, sizeof(struct rt_clock_hrtimer));

    timer->flag         = flag & ~RT_TIMER_FLAG_ACTIVATED;
    timer->timeout_func = timeout;
    timer->parameter    = parameter;
    rt_strncpy(timer->name, name, RT_NAME_MAX - 1);
    rt_list_init(&(timer->node));
    rt_completion_init(&timer->completion);
}

rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long delay_cnt)
{
    rt_base_t  level;

    RT_ASSERT(timer != RT_NULL);
    RT_ASSERT(delay_cnt < (_HRTIMER_MAX_CNT / 2));

    timer->delay_cnt    = delay_cnt;
    timer->timeout_cnt  = timer->delay_cnt + _clock_time_get_cnt();

    level = rt_spin_lock_irqsave(&_spinlock);

    if (timer->flag & RT_TIMER_FLAG_ACTIVATED)
    {
        rt_spin_unlock_irqrestore(&_spinlock, level);
        return -RT_ERROR;
    }

    _insert_timer_to_list_locked(timer);
    _set_next_timeout_locked();

    rt_spin_unlock_irqrestore(&_spinlock, level);

    return RT_EOK;
}

rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer)
{
    rt_base_t level;

    RT_ASSERT(timer != RT_NULL);

    level = rt_spin_lock_irqsave(&_spinlock);

    if (!(timer->flag & RT_TIMER_FLAG_ACTIVATED))
    {
        rt_spin_unlock_irqrestore(&_spinlock, level);
        return -RT_ERROR;
    }

    rt_list_remove(&timer->node);
    timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
    _set_next_timeout_locked();

    rt_spin_unlock_irqrestore(&_spinlock, level);

    return RT_EOK;
}

rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg)
{
    rt_base_t level;

    RT_ASSERT(timer != RT_NULL);

    level = rt_spin_lock_irqsave(&_spinlock);
    switch (cmd)
    {
    case RT_TIMER_CTRL_GET_TIME:
        *(unsigned long *)arg = timer->delay_cnt;
        break;

    case RT_TIMER_CTRL_SET_TIME:
        RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2));
        timer->delay_cnt    = *(unsigned long *)arg;
        timer->timeout_cnt  = *(unsigned long *)arg + _clock_time_get_cnt();
        break;

    case RT_TIMER_CTRL_SET_ONESHOT:
        timer->flag &= ~RT_TIMER_FLAG_PERIODIC;
        break;

    case RT_TIMER_CTRL_SET_PERIODIC:
        timer->flag |= RT_TIMER_FLAG_PERIODIC;
        break;

    case RT_TIMER_CTRL_GET_STATE:
        if (timer->flag & RT_TIMER_FLAG_ACTIVATED)
        {
            *(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
        }
        else
        {
            *(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
        }
        break;

    case RT_TIMER_CTRL_GET_REMAIN_TIME:
        *(unsigned long *)arg = timer->timeout_cnt;
        break;
    case RT_TIMER_CTRL_GET_FUNC:
        if (arg != RT_NULL)
        {
            *(void **)arg = (void *)timer->timeout_func;
        }
        break;

    case RT_TIMER_CTRL_SET_FUNC:
        timer->timeout_func = (void (*)(void *))arg;
        break;

    case RT_TIMER_CTRL_GET_PARM:
        *(void **)arg = timer->parameter;
        break;

    case RT_TIMER_CTRL_SET_PARM:
        timer->parameter = arg;
        break;

    default:
        break;
    }
    rt_spin_unlock_irqrestore(&_spinlock, level);

    return RT_EOK;
}

rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer)
{
    rt_base_t level;

    RT_ASSERT(timer != RT_NULL);

    rt_completion_wakeup_by_errno(&timer->completion, RT_ERROR);

    level = rt_spin_lock_irqsave(&_spinlock);

    timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
    if (timer->error == -RT_EINTR || timer->error == RT_EINTR)
    {
        rt_list_remove(&timer->node);
        _set_next_timeout_locked();
    }

    rt_spin_unlock_irqrestore(&_spinlock, level);

    return RT_EOK;
}

void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer)
{
    rt_clock_hrtimer_init(timer, "hrtimer_sleep", RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
                          _sleep_timeout, timer);
}

void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer)
{
    rt_clock_hrtimer_detach(timer);
}

rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt)
{
    rt_err_t err;

    if (cnt == 0)
    {
        return -RT_EINVAL;
    }

    err = rt_clock_hrtimer_start(timer, cnt);
    if (err)
    {
        return err;
    }

    err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER,
                                   RT_INTERRUPTIBLE);
    rt_clock_hrtimer_keep_errno(timer, err);

    return err;
}

rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns)
{
    rt_uint64_t res = rt_clock_time_get_res_scaled();
    if (res == 0)
    {
        return -RT_ERROR;
    }

    return rt_clock_hrtimer_sleep(timer, (ns * RT_CLOCK_TIME_RESMUL) / res);
}

rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us)
{
    return rt_clock_hrtimer_ndelay(timer, us * 1000);
}

rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms)
{
    return rt_clock_hrtimer_ndelay(timer, ms * 1000000);
}

void rt_clock_time_event_isr(void)
{
    rt_clock_hrtimer_process();
}