NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
os_cputime.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
27#ifndef H_OS_CPUTIME_
28#define H_OS_CPUTIME_
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include "../syscfg/syscfg.h"
35#include "../hal/hal_timer.h"
36
37/*
38 * NOTE: these definitions allow one to override the cputime frequency used.
39 * The reason these definitions exist is to make the code more
40 * efficient/smaller when CPUTIME counts at 1 MHz.
41 *
42 * For those who want a different cputime frequency, you can set the config
43 * definition for OS_CPUTIME_FREQ to the desired frequency in your project,
44 * target or bsp.
45 */
46#if (MYNEWT_VAL(OS_CPUTIME_FREQ) == 1000000)
47
48#define OS_CPUTIME_FREQ_1MHZ
49
50#elif MYNEWT_VAL(OS_CPUTIME_FREQ) == 256 || \
51 MYNEWT_VAL(OS_CPUTIME_FREQ) == 512 || \
52 MYNEWT_VAL(OS_CPUTIME_FREQ) == 1024 || \
53 MYNEWT_VAL(OS_CPUTIME_FREQ) == 2048 || \
54 MYNEWT_VAL(OS_CPUTIME_FREQ) == 4096 || \
55 MYNEWT_VAL(OS_CPUTIME_FREQ) == 8192 || \
56 MYNEWT_VAL(OS_CPUTIME_FREQ) == 16384 || \
57 MYNEWT_VAL(OS_CPUTIME_FREQ) == 32768 || \
58 MYNEWT_VAL(OS_CPUTIME_FREQ) == 32000 || \
59 MYNEWT_VAL(OS_CPUTIME_FREQ) == 65536 || \
60 MYNEWT_VAL(OS_CPUTIME_FREQ) == 131072 || \
61 MYNEWT_VAL(OS_CPUTIME_FREQ) == 262144 || \
62 MYNEWT_VAL(OS_CPUTIME_FREQ) == 524288
63
64#define OS_CPUTIME_FREQ_PWR2
65
66#elif MYNEWT_VAL(OS_CPUTIME_FREQ) > 1000000
67
68#define OS_CPUTIME_FREQ_HIGH
69
70#else
71
72#error "Invalid OS_CPUTIME_FREQ value. Value must be one of a) a power of 2" \
73 ">= 256Hz, or b) any value >= 1MHz"
74
75#endif
76
77#if defined(OS_CPUTIME_FREQ_HIGH)
78/* CPUTIME data. */
79struct os_cputime_data
80{
81 uint32_t ticks_per_usec; /* number of ticks per usec */
82};
83extern struct os_cputime_data g_os_cputime;
84#endif
85
86/* Helpful macros to compare cputimes */
88#define CPUTIME_LT(__t1, __t2) ((int32_t) ((__t1) - (__t2)) < 0)
90#define CPUTIME_GT(__t1, __t2) ((int32_t) ((__t1) - (__t2)) > 0)
92#define CPUTIME_GEQ(__t1, __t2) ((int32_t) ((__t1) - (__t2)) >= 0)
94#define CPUTIME_LEQ(__t1, __t2) ((int32_t) ((__t1) - (__t2)) <= 0)
95
105int os_cputime_init(uint32_t clock_freq);
106
112uint32_t os_cputime_get32(void);
113
114#if !defined(OS_CPUTIME_FREQ_PWR2)
123uint32_t os_cputime_nsecs_to_ticks(uint32_t nsecs);
124
133uint32_t os_cputime_ticks_to_nsecs(uint32_t ticks);
134
142void os_cputime_delay_nsecs(uint32_t nsecs);
143#endif
144
145#if defined(OS_CPUTIME_FREQ_1MHZ)
146#define os_cputime_usecs_to_ticks(x) (x)
147#define os_cputime_ticks_to_usecs(x) (x)
148#else
149
157uint32_t os_cputime_usecs_to_ticks(uint32_t usecs);
158
166uint32_t os_cputime_ticks_to_usecs(uint32_t ticks);
167#endif
168
174void os_cputime_delay_ticks(uint32_t ticks);
175
181void os_cputime_delay_usecs(uint32_t usecs);
182
190void os_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp,
191 void *arg);
192
206int os_cputime_timer_start(struct hal_timer *timer, uint32_t cputime);
207
220int os_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs);
221
229void os_cputime_timer_stop(struct hal_timer *timer);
230
231#ifdef __cplusplus
232}
233#endif
234
235#endif /* H_OS_CPUTIME_ */
236
int os_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs)
Definition os_cputime.c:99
uint32_t os_cputime_ticks_to_usecs(uint32_t ticks)
void os_cputime_delay_usecs(uint32_t usecs)
Definition os_cputime.c:72
int os_cputime_init(uint32_t clock_freq)
Definition os_cputime.c:32
int os_cputime_timer_start(struct hal_timer *timer, uint32_t cputime)
Definition os_cputime.c:90
uint32_t os_cputime_ticks_to_nsecs(uint32_t ticks)
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
void os_cputime_delay_nsecs(uint32_t nsecs)
Definition os_cputime.c:62
void os_cputime_delay_ticks(uint32_t ticks)
Definition os_cputime.c:50
uint32_t os_cputime_nsecs_to_ticks(uint32_t nsecs)
uint32_t os_cputime_get32(void)
Definition os_cputime.c:119
Definition hal_timer.h:51