dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_delay.c
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 
31 /* Project Includes */
32 #include "pios.h"
33 #include "time.h"
34 
35 #include <time.h>
36 
42 static uint32_t base_time;
43 
44 #ifdef DRONIN_GETTIME
45 int clock_gettime(clockid_t clk_id, struct timespec *t)
46 {
47  (void) clk_id;
48 
49  uint64_t tm = mach_absolute_time();
50  static int numer = 0, denom = 0;
51 
52  if (!numer) {
53  mach_timebase_info_data_t tb;
54 
55  kern_return_t ret = mach_timebase_info(&tb);
56 
57  if (ret != KERN_SUCCESS) abort();
58 
59  numer = tb.numer;
60  denom = tb.denom;
61  }
62 
63  tm *= numer;
64  tm /= denom;
65 
66  t->tv_nsec = tm % 1000000000;
67  t->tv_sec = tm / 1000000000;
68 
69  return 0;
70 }
71 
72 #endif /* CLOCK_MONOTONIC */
73 static uint32_t get_monotonic_us_time(void) {
75 
76 #ifdef CLOCK_BOOTTIME
77  id = CLOCK_BOOTTIME;
78 #endif /* CLOCK_BOOTTIME; assumes it's a define */
79 
80  struct timespec tp;
81 
82  if (clock_gettime(id, &tp)) {
83  perror("clock_gettime");
84  abort();
85  }
86 
87  uint32_t val = tp.tv_sec * 1000000 + tp.tv_nsec / 1000;
88 
89  return val;
90 }
91 
98 int32_t PIOS_DELAY_Init(void)
99 {
101 
102  /* No error */
103  return 0;
104 }
105 
116 int32_t PIOS_DELAY_WaituS(uint32_t uS)
117 {
118  struct timespec wait,rest;
119  wait.tv_sec=0;
120  wait.tv_nsec=1000*uS;
121  while (nanosleep(&wait,&rest)) {
122  wait=rest;
123  }
124 
125  /* No error */
126  return 0;
127 }
128 
129 
140 int32_t PIOS_DELAY_WaitmS(uint32_t mS)
141 {
142  struct timespec wait,rest;
143  wait.tv_sec=mS/1000;
144  wait.tv_nsec=(mS%1000)*1000000;
145  while (nanosleep(&wait, &rest)) {
146  wait=rest;
147  }
148 
149  /* No error */
150  return 0;
151 }
152 
154 {
155  uint32_t raw_us = get_monotonic_us_time() - base_time;
156  return raw_us;
157 }
158 
159 uint32_t PIOS_DELAY_DiffuS(uint32_t ref)
160 {
161  return PIOS_DELAY_DiffuS2(ref, PIOS_DELAY_GetRaw());
162 }
163 
164 uint32_t PIOS_DELAY_DiffuS2(uint32_t raw, uint32_t later) {
165  uint32_t diff = later - raw;
166  return diff;
167 }
168 
174 {
175  return PIOS_DELAY_GetRaw();
176 }
177 
183 uint32_t PIOS_DELAY_GetuSSince(uint32_t t)
184 {
185  return PIOS_DELAY_GetuS() - t;
186 }
187 
193 uint32_t PIOS_DELAY_GetuSExpired(uint32_t t)
194 {
195  return PIOS_DELAY_GetuSSince(t) < 0x80000000;
196 }
uint32_t PIOS_DELAY_DiffuS(uint32_t raw)
Subtract raw time from now and convert to us.
Definition: pios_delay.c:159
int clock_gettime(clockid_t clk_id, struct timespec *t)
uint32_t PIOS_DELAY_GetuS()
Query the Delay timer for the current uS.
Definition: pios_delay.c:173
Main PiOS header to include all the compiled in PiOS options.
static uint32_t get_monotonic_us_time(void)
Definition: pios_delay.c:73
int clockid_t
Definition: pios_posix.h:66
uint32_t PIOS_DELAY_DiffuS2(uint32_t raw, uint32_t baseline)
Subrtact two raw times and convert to us.
Definition: pios_delay.c:164
uint32_t PIOS_DELAY_GetuSExpired(uint32_t t)
Calculates whether a given time has passed.
Definition: pios_delay.c:193
uint32_t PIOS_DELAY_GetuSSince(uint32_t t)
Calculate time in microseconds since a previous time.
Definition: pios_delay.c:183
int32_t PIOS_DELAY_Init(void)
Definition: pios_delay.c:98
static uint32_t base_time
Definition: pios_delay.c:42
#define CLOCK_MONOTONIC
Definition: pios_posix.h:60
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
Definition: pios_delay.c:140
int32_t PIOS_DELAY_WaituS(uint32_t uS)
Definition: pios_delay.c:116
uint32_t PIOS_DELAY_GetRaw()
Get the raw delay timer, useful for timing.
Definition: pios_delay.c:153