PX4 Firmware
PX4 Autopilot Software http://px4.io
uc_kinetis_thread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014, 2018 Pavel Kirienko <pavel.kirienko@gmail.com>
3  * Kinetis Port Author David Sidrane <david_s5@nscdg.com>
4  */
5 
8 #include <uavcan_kinetis/can.hpp>
9 #include "internal.hpp"
10 
11 namespace uavcan_kinetis
12 {
13 
15 {
16  sem_init(&sem_, 0, 0);
17  sem_setprotocol(&sem_, SEM_PRIO_NONE);
18 }
19 
21 {
22  sem_destroy(&sem_);
23 }
24 
25 bool BusEvent::wait(uavcan::MonotonicDuration duration)
26 {
27  if (duration.isPositive()) {
28  timespec abstime;
29 
30  if (clock_gettime(CLOCK_REALTIME, &abstime) == 0) {
31  const unsigned billion = 1000 * 1000 * 1000;
32  uint64_t nsecs = abstime.tv_nsec + (uint64_t)duration.toUSec() * 1000;
33  abstime.tv_sec += nsecs / billion;
34  nsecs -= (nsecs / billion) * billion;
35  abstime.tv_nsec = nsecs;
36 
37  int ret;
38 
39  while ((ret = sem_timedwait(&sem_, &abstime)) == -1 && errno == EINTR);
40 
41  if (ret == -1) { // timed out or error
42  return false;
43  }
44 
45  return true;
46  }
47  }
48 
49  return false;
50 }
51 
53 {
54  if (sem_.semcount <= 0) {
55  (void)sem_post(&sem_);
56  }
57 
58  if (signal_cb_) {
59  signal_cb_();
60  }
61 }
62 
63 }
BusEvent(CanDriver &can_driver)
CAN driver, incorporates all available CAN ifaces.
Definition: can.hpp:269
SignalCallbackHandler signal_cb_
Definition: thread.hpp:33
bool wait(uavcan::MonotonicDuration duration)