xref: /nrf52832-nimble/packages/NimBLE-latest/porting/nimble/src/os_cputime.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 #include <string.h>
21 #include <stdint.h>
22 #include <assert.h>
23 #include "syscfg/syscfg.h"
24 #include "os/os_cputime.h"
25 #include "hal/hal_timer.h"
26 
27 #if defined(OS_CPUTIME_FREQ_HIGH)
28 struct os_cputime_data g_os_cputime;
29 #endif
30 
31 int
os_cputime_init(uint32_t clock_freq)32 os_cputime_init(uint32_t clock_freq)
33 {
34     int rc;
35 
36     /* Set the ticks per microsecond. */
37 #if defined(OS_CPUTIME_FREQ_HIGH)
38     g_os_cputime.ticks_per_usec = clock_freq / 1000000U;
39 #endif
40     rc = hal_timer_config(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM), clock_freq);
41     return rc;
42 }
43 
44 /**
45  * Wait until the number of ticks has elapsed. This is a blocking delay.
46  *
47  * @param ticks The number of ticks to wait.
48  */
49 void
os_cputime_delay_ticks(uint32_t ticks)50 os_cputime_delay_ticks(uint32_t ticks)
51 {
52     uint32_t until;
53 
54     until = os_cputime_get32() + ticks;
55     while ((int32_t)(os_cputime_get32() - until) < 0) {
56         /* Loop here till finished */
57     }
58 }
59 
60 #if !defined(OS_CPUTIME_FREQ_PWR2)
61 void
os_cputime_delay_nsecs(uint32_t nsecs)62 os_cputime_delay_nsecs(uint32_t nsecs)
63 {
64     uint32_t ticks;
65 
66     ticks = os_cputime_nsecs_to_ticks(nsecs);
67     os_cputime_delay_ticks(ticks);
68 }
69 #endif
70 
71 void
os_cputime_delay_usecs(uint32_t usecs)72 os_cputime_delay_usecs(uint32_t usecs)
73 {
74     uint32_t ticks;
75 
76     ticks = os_cputime_usecs_to_ticks(usecs);
77     os_cputime_delay_ticks(ticks);
78 }
79 
80 void
os_cputime_timer_init(struct hal_timer * timer,hal_timer_cb fp,void * arg)81 os_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp, void *arg)
82 {
83     assert(timer != NULL);
84     assert(fp != NULL);
85 
86     hal_timer_set_cb(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM), timer, fp, arg);
87 }
88 
89 int
os_cputime_timer_start(struct hal_timer * timer,uint32_t cputime)90 os_cputime_timer_start(struct hal_timer *timer, uint32_t cputime)
91 {
92     int rc;
93 
94     rc = hal_timer_start_at(timer, cputime);
95     return rc;
96 }
97 
98 int
os_cputime_timer_relative(struct hal_timer * timer,uint32_t usecs)99 os_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs)
100 {
101     int rc;
102     uint32_t cputime;
103 
104     assert(timer != NULL);
105 
106     cputime = os_cputime_get32() + os_cputime_usecs_to_ticks(usecs);
107     rc = hal_timer_start_at(timer, cputime);
108 
109     return rc;
110 }
111 
112 void
os_cputime_timer_stop(struct hal_timer * timer)113 os_cputime_timer_stop(struct hal_timer *timer)
114 {
115     hal_timer_stop(timer);
116 }
117 
118 uint32_t
os_cputime_get32(void)119 os_cputime_get32(void)
120 {
121     uint32_t cpu_time;
122 
123     cpu_time = hal_timer_read(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM));
124     return cpu_time;
125 }
126 
127