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