CANThread.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014 Pavel Kirienko
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*
  24. * This file is free software: you can redistribute it and/or modify it
  25. * under the terms of the GNU General Public License as published by the
  26. * Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This file is distributed in the hope that it will be useful, but
  30. * WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  32. * See the GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License along
  35. * with this program. If not, see <http://www.gnu.org/licenses/>.
  36. *
  37. * Modified for Ardupilot by Siddharth Bharat Purohit
  38. */
  39. #include "AP_HAL_ChibiOS.h"
  40. #if HAL_WITH_UAVCAN
  41. #include "CANThread.h"
  42. #include "CANClock.h"
  43. #include "CANIface.h"
  44. #include "CANInternal.h"
  45. namespace ChibiOS_CAN {
  46. /*
  47. * BusEvent
  48. */
  49. bool BusEvent::wait(uavcan::MonotonicDuration duration)
  50. {
  51. // set maximum time to allow for 16 bit timers running at 1MHz
  52. static const uavcan::int64_t MaxDelayUSec = 0x000FFFF;
  53. const uavcan::int64_t usec = duration.toUSec();
  54. msg_t ret = msg_t();
  55. if (usec <= 0) {
  56. # if (CH_KERNEL_MAJOR == 2)
  57. ret = sem_.waitTimeout(TIME_IMMEDIATE);
  58. # else // ChibiOS 3+
  59. ret = sem_.wait(TIME_IMMEDIATE);
  60. # endif
  61. }
  62. else {
  63. # if (CH_KERNEL_MAJOR == 2)
  64. ret = sem_.waitTimeout((usec > MaxDelayUSec) ? US2ST(MaxDelayUSec) : US2ST(usec));
  65. # elif (CH_KERNEL_MAJOR >= 5)
  66. ret = sem_.wait((usec > MaxDelayUSec) ? chTimeUS2I(MaxDelayUSec) : chTimeUS2I(usec));
  67. # else // ChibiOS 3+
  68. ret = sem_.wait((usec > MaxDelayUSec) ? US2ST(MaxDelayUSec) : US2ST(usec));
  69. # endif
  70. }
  71. # if (CH_KERNEL_MAJOR == 2)
  72. return ret == RDY_OK;
  73. # else // ChibiOS 3+
  74. return ret == MSG_OK;
  75. # endif
  76. }
  77. void BusEvent::signal()
  78. {
  79. sem_.signal();
  80. }
  81. void BusEvent::signalFromInterrupt()
  82. {
  83. # if (CH_KERNEL_MAJOR == 2)
  84. chSysLockFromIsr();
  85. sem_.signalI();
  86. chSysUnlockFromIsr();
  87. # else // ChibiOS 3+
  88. chSysLockFromISR();
  89. sem_.signalI();
  90. chSysUnlockFromISR();
  91. # endif
  92. }
  93. /*
  94. * Mutex
  95. */
  96. void Mutex::lock()
  97. {
  98. mtx_.lock();
  99. }
  100. void Mutex::unlock()
  101. {
  102. # if (CH_KERNEL_MAJOR == 2)
  103. chibios_rt::BaseThread::unlockMutex();
  104. # else // ChibiOS 3+
  105. mtx_.unlock();
  106. # endif
  107. }
  108. }
  109. #endif //HAL_WITH_UAVCAN