NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_ll_tmr.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef H_BLE_LL_TMR_
21#define H_BLE_LL_TMR_
22
23#include "nimble/porting/nimble/include/os/os_cputime.h"
24#include "ble_ll.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#define USECS_PER_TICK ((1000000 + MYNEWT_VAL(OS_CPUTIME_FREQ) - 1) / \
31 MYNEWT_VAL(OS_CPUTIME_FREQ))
32
33#define LL_TMR_LT(_t1, _t2) ((int32_t)((_t1) - (_t2)) < 0)
34#define LL_TMR_GT(_t1, _t2) ((int32_t)((_t1) - (_t2)) > 0)
35#define LL_TMR_GEQ(_t1, _t2) ((int32_t)((_t1) - (_t2)) >= 0)
36#define LL_TMR_LEQ(_t1, _t2) ((int32_t)((_t1) - (_t2)) <= 0)
37
38typedef void (ble_ll_tmr_cb)(void *arg);
39
40struct ble_ll_tmr {
41 struct hal_timer t;
42};
43
44static inline uint32_t
45ble_ll_tmr_get(void)
46{
47 return os_cputime_get32();
48}
49
50static inline uint32_t
51ble_ll_tmr_t2u(uint32_t ticks)
52{
53#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 31250
54 return ticks * 32;
55#endif
56
57 return os_cputime_ticks_to_usecs(ticks);
58}
59
60static inline uint32_t
61ble_ll_tmr_u2t(uint32_t usecs)
62{
63#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 31250
64 return usecs / 32;
65#endif
66#if MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768
67 if (usecs <= 31249) {
68 return (usecs * 137439) / 4194304;
69 }
70#endif
71
72 return os_cputime_usecs_to_ticks(usecs);
73}
74
75static inline uint32_t
76ble_ll_tmr_u2t_up(uint32_t usecs)
77{
78 return ble_ll_tmr_u2t(usecs + (USECS_PER_TICK - 1));
79}
80
81static inline uint32_t
82ble_ll_tmr_u2t_r(uint32_t usecs, uint8_t *rem_us)
83{
84 uint32_t ticks;
85
86 ticks = ble_ll_tmr_u2t(usecs);
87 *rem_us = usecs - ble_ll_tmr_t2u(ticks);
88 if (*rem_us == USECS_PER_TICK) {
89 *rem_us = 0;
90 ticks++;
91 }
92
93 return ticks;
94}
95
96static inline void
97ble_ll_tmr_add(uint32_t *ticks, uint8_t *rem_us, uint32_t usecs)
98{
99 uint32_t t_ticks;
100 uint8_t t_rem_us;
101
102 t_ticks = ble_ll_tmr_u2t_r(usecs, &t_rem_us);
103
104 *ticks += t_ticks;
105 *rem_us += t_rem_us;
106 if (*rem_us >= USECS_PER_TICK) {
107 *rem_us -= USECS_PER_TICK;
108 *ticks += 1;
109 }
110}
111
112static inline void
113ble_ll_tmr_add_u(uint32_t *ticks, uint8_t *rem_us, uint8_t usecs)
114{
115 BLE_LL_ASSERT(usecs < USECS_PER_TICK);
116
117 *rem_us += usecs;
118 if (*rem_us >= USECS_PER_TICK) {
119 *rem_us -= USECS_PER_TICK;
120 *ticks += 1;
121 }
122}
123
124static inline void
125ble_ll_tmr_sub(uint32_t *ticks, uint8_t *rem_us, uint32_t usecs)
126{
127 uint32_t t_ticks;
128 uint8_t t_rem_us;
129
130 if (usecs <= *rem_us) {
131 *rem_us -= usecs;
132 return;
133 }
134
135 usecs -= *rem_us;
136 *rem_us = 0;
137
138 t_ticks = ble_ll_tmr_u2t_r(usecs, &t_rem_us);
139 if (t_rem_us) {
140 t_ticks += 1;
141 *rem_us = USECS_PER_TICK - t_rem_us;
142 }
143
144 *ticks -= t_ticks;
145}
146
147static inline void
148ble_ll_tmr_init(struct ble_ll_tmr *tmr, ble_ll_tmr_cb *cb, void *arg)
149{
150 os_cputime_timer_init(&tmr->t, cb, arg);
151}
152
153static inline void
154ble_ll_tmr_start(struct ble_ll_tmr *tmr, uint32_t tgt)
155{
156 os_cputime_timer_start(&tmr->t, tgt);
157}
158
159static inline void
160ble_ll_tmr_stop(struct ble_ll_tmr *tmr)
161{
162 os_cputime_timer_stop(&tmr->t);
163}
164
165#ifdef __cplusplus
166}
167#endif
168
169#endif /* H_BLE_LL_TMR_ */
uint32_t os_cputime_ticks_to_usecs(uint32_t ticks)
int os_cputime_timer_start(struct hal_timer *timer, uint32_t cputime)
Definition os_cputime.c:90
void os_cputime_timer_stop(struct hal_timer *timer)
Definition os_cputime.c:113
uint32_t os_cputime_usecs_to_ticks(uint32_t usecs)
void os_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp, void *arg)
Definition os_cputime.c:81
uint32_t os_cputime_get32(void)
Definition os_cputime.c:119
Definition hal_timer.h:51