APM:Libraries
system.cpp
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <stdio.h>
3 
4 #include <AP_HAL/AP_HAL.h>
5 #include <AP_HAL/system.h>
6 
7 #include "Scheduler.h"
8 
9 extern const AP_HAL::HAL& hal;
10 
11 using HALSITL::Scheduler;
12 
13 namespace AP_HAL {
14 
15 static struct {
16  struct timeval start_time;
17 } state;
18 
19 void init()
20 {
21  gettimeofday(&state.start_time, nullptr);
22 }
23 
24 void panic(const char *errormsg, ...)
25 {
26  va_list ap;
27 
28  fflush(stdout);
29  va_start(ap, errormsg);
30  vprintf(errormsg, ap);
31  va_end(ap);
32  printf("\n");
33 
34  for(;;);
35 }
36 
37 uint32_t micros()
38 {
39  return micros64() & 0xFFFFFFFF;
40 }
41 
42 uint32_t millis()
43 {
44  return millis64() & 0xFFFFFFFF;
45 }
46 
47 uint64_t micros64()
48 {
50  uint64_t stopped_usec = scheduler->stopped_clock_usec();
51  if (stopped_usec) {
52  return stopped_usec;
53  }
54 
55  struct timeval tp;
56  gettimeofday(&tp, nullptr);
57  uint64_t ret = 1.0e6 * ((tp.tv_sec + (tp.tv_usec * 1.0e-6)) -
58  (state.start_time.tv_sec +
59  (state.start_time.tv_usec * 1.0e-6)));
60  return ret;
61 }
62 
63 uint64_t millis64()
64 {
66  uint64_t stopped_usec = scheduler->stopped_clock_usec();
67  if (stopped_usec) {
68  return stopped_usec / 1000;
69  }
70 
71  struct timeval tp;
72  gettimeofday(&tp, nullptr);
73  uint64_t ret = 1.0e3*((tp.tv_sec + (tp.tv_usec*1.0e-6)) -
74  (state.start_time.tv_sec +
75  (state.start_time.tv_usec*1.0e-6)));
76  return ret;
77 }
78 
79 } // namespace AP_HAL
struct timespec start_time
Definition: system.cpp:16
uint64_t stopped_clock_usec() const
Definition: Scheduler.h:50
static struct AP_HAL::@102 state
int printf(const char *msg,...)
Definition: system.cpp:143
uint64_t millis64()
Definition: system.cpp:167
const AP_HAL::HAL & hal
Definition: AC_PID_test.cpp:14
uint32_t millis()
Definition: system.cpp:157
static Scheduler * from(AP_HAL::Scheduler *scheduler)
Definition: Scheduler.h:14
uint64_t micros64()
Definition: system.cpp:162
int vprintf(const char *fmt, va_list arg)
Definition: stdio.c:100
void init()
Generic board initialization function.
Definition: system.cpp:136
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140
uint32_t micros()
Definition: system.cpp:152
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114