hal_can.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_can.h
  15. * @brief CAN Driver macros and structures.
  16. *
  17. * @addtogroup CAN
  18. * @{
  19. */
  20. #ifndef HAL_CAN_H
  21. #define HAL_CAN_H
  22. #if (HAL_USE_CAN == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name CAN status flags
  28. * @{
  29. */
  30. /**
  31. * @brief Errors rate warning.
  32. */
  33. #define CAN_LIMIT_WARNING 1U
  34. /**
  35. * @brief Errors rate error.
  36. */
  37. #define CAN_LIMIT_ERROR 2U
  38. /**
  39. * @brief Bus off condition reached.
  40. */
  41. #define CAN_BUS_OFF_ERROR 4U
  42. /**
  43. * @brief Framing error of some kind on the CAN bus.
  44. */
  45. #define CAN_FRAMING_ERROR 8U
  46. /**
  47. * @brief Overflow in receive queue.
  48. */
  49. #define CAN_OVERFLOW_ERROR 16U
  50. /** @} */
  51. /**
  52. * @brief Special mailbox identifier.
  53. */
  54. #define CAN_ANY_MAILBOX 0U
  55. /*===========================================================================*/
  56. /* Driver pre-compile time settings. */
  57. /*===========================================================================*/
  58. /**
  59. * @name CAN configuration options
  60. * @{
  61. */
  62. /**
  63. * @brief Sleep mode related APIs inclusion switch.
  64. * @details This option can only be enabled if the CAN implementation supports
  65. * the sleep mode, see the macro @p CAN_SUPPORTS_SLEEP exported by
  66. * the underlying implementation.
  67. */
  68. #if !defined(CAN_USE_SLEEP_MODE) || defined(__DOXYGEN__)
  69. #define CAN_USE_SLEEP_MODE TRUE
  70. #endif
  71. /**
  72. * @brief Enforces the driver to use direct callbacks rather than OSAL events.
  73. */
  74. #if !defined(CAN_ENFORCE_USE_CALLBACKS) || defined(__DOXYGEN__)
  75. #define CAN_ENFORCE_USE_CALLBACKS FALSE
  76. #endif
  77. /** @} */
  78. /*===========================================================================*/
  79. /* Derived constants and error checks. */
  80. /*===========================================================================*/
  81. /*===========================================================================*/
  82. /* Driver data structures and types. */
  83. /*===========================================================================*/
  84. /**
  85. * @brief Driver state machine possible states.
  86. */
  87. typedef enum {
  88. CAN_UNINIT = 0, /**< Not initialized. */
  89. CAN_STOP = 1, /**< Stopped. */
  90. CAN_STARTING = 2, /**< Starting. */
  91. CAN_READY = 3, /**< Ready. */
  92. CAN_SLEEP = 4 /**< Sleep state. */
  93. } canstate_t;
  94. #include "hal_can_lld.h"
  95. /*===========================================================================*/
  96. /* Driver macros. */
  97. /*===========================================================================*/
  98. /**
  99. * @name Macro Functions
  100. * @{
  101. */
  102. /**
  103. * @brief Converts a mailbox index to a bit mask.
  104. */
  105. #define CAN_MAILBOX_TO_MASK(mbx) (1U << ((mbx) - 1U))
  106. /**
  107. * @brief Legacy name for @p canTransmitTimeout().
  108. *
  109. * @deprecated
  110. */
  111. #define canTransmit(canp, mailbox, ctfp, timeout) \
  112. canTransmitTimeout(canp, mailbox, ctfp, timeout)
  113. /**
  114. * @brief Legacy name for @p canReceiveTimeout().
  115. *
  116. * @deprecated
  117. */
  118. #define canReceive(canp, mailbox, crfp, timeout) \
  119. canReceiveTimeout(canp, mailbox, crfp, timeout)
  120. /** @} */
  121. /**
  122. * @name Low level driver helper macros
  123. * @{
  124. */
  125. #if (CAN_ENFORCE_USE_CALLBACKS == FALSE) || defined(__DOXYGEN__)
  126. /**
  127. * @brief TX mailbox empty event.
  128. */
  129. #define _can_tx_empty_isr(canp, flags) { \
  130. osalSysLockFromISR(); \
  131. osalThreadDequeueAllI(&(canp)->txqueue, MSG_OK); \
  132. osalEventBroadcastFlagsI(&(canp)->txempty_event, flags); \
  133. osalSysUnlockFromISR(); \
  134. }
  135. /**
  136. * @brief RX mailbox empty full event.
  137. */
  138. #define _can_rx_full_isr(canp, flags) { \
  139. osalSysLockFromISR(); \
  140. osalThreadDequeueAllI(&(canp)->rxqueue, MSG_OK); \
  141. osalEventBroadcastFlagsI(&(canp)->rxfull_event, flags); \
  142. osalSysUnlockFromISR(); \
  143. }
  144. /**
  145. * @brief Error event.
  146. */
  147. #define _can_wakeup_isr(canp) { \
  148. osalSysLockFromISR(); \
  149. osalEventBroadcastFlagsI(&(canp)->wakeup_event, 0U); \
  150. osalSysUnlockFromISR(); \
  151. }
  152. /**
  153. * @brief Error event.
  154. */
  155. #define _can_error_isr(canp, flags) { \
  156. osalSysLockFromISR(); \
  157. osalEventBroadcastFlagsI(&(canp)->error_event, flags); \
  158. osalSysUnlockFromISR(); \
  159. }
  160. #else /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
  161. #define _can_tx_empty_isr(canp, flags) { \
  162. if ((canp)->txempty_cb != NULL) { \
  163. (canp)->txempty_cb(canp, flags); \
  164. } \
  165. osalSysLockFromISR(); \
  166. osalThreadDequeueAllI(&(canp)->txqueue, MSG_OK); \
  167. osalSysUnlockFromISR(); \
  168. }
  169. #define _can_rx_full_isr(canp, flags) { \
  170. if ((canp)->rxfull_cb != NULL) { \
  171. (canp)->rxfull_cb(canp, flags); \
  172. } \
  173. osalSysLockFromISR(); \
  174. osalThreadDequeueAllI(&(canp)->rxqueue, MSG_OK); \
  175. osalSysUnlockFromISR(); \
  176. }
  177. #define _can_wakeup_isr(canp) { \
  178. if ((canp)->wakeup_cb != NULL) { \
  179. (canp)->wakeup_cb(canp, 0U); \
  180. } \
  181. }
  182. #define _can_error_isr(canp, flags) { \
  183. if ((canp)->error_cb != NULL) { \
  184. (canp)->error_cb(canp, flags); \
  185. } \
  186. }
  187. #endif /* CAN_ENFORCE_USE_CALLBACKS == TRUE */
  188. /** @} */
  189. /*===========================================================================*/
  190. /* External declarations. */
  191. /*===========================================================================*/
  192. #ifdef __cplusplus
  193. extern "C" {
  194. #endif
  195. void canInit(void);
  196. void canObjectInit(CANDriver *canp);
  197. void canStart(CANDriver *canp, const CANConfig *config);
  198. void canStop(CANDriver *canp);
  199. bool canTryTransmitI(CANDriver *canp,
  200. canmbx_t mailbox,
  201. const CANTxFrame *ctfp);
  202. bool canTryReceiveI(CANDriver *canp,
  203. canmbx_t mailbox,
  204. CANRxFrame *crfp);
  205. msg_t canTransmitTimeout(CANDriver *canp,
  206. canmbx_t mailbox,
  207. const CANTxFrame *ctfp,
  208. sysinterval_t timeout);
  209. msg_t canReceiveTimeout(CANDriver *canp,
  210. canmbx_t mailbox,
  211. CANRxFrame *crfp,
  212. sysinterval_t timeout);
  213. #if CAN_USE_SLEEP_MODE
  214. void canSleep(CANDriver *canp);
  215. void canWakeup(CANDriver *canp);
  216. #endif
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif /* HAL_USE_CAN == TRUE */
  221. #endif /* HAL_CAN_H */
  222. /** @} */