can_hal.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. #pragma once
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #include <stddef.h>
  24. #include <stdbool.h>
  25. #include "hal/can_types.h"
  26. #include "hal/can_ll.h"
  27. /* ------------------------- Defines and Typedefs --------------------------- */
  28. //Error active interrupt related
  29. #define CAN_HAL_EVENT_BUS_OFF (1 << 0)
  30. #define CAN_HAL_EVENT_BUS_RECOV_CPLT (1 << 1)
  31. #define CAN_HAL_EVENT_BUS_RECOV_PROGRESS (1 << 2)
  32. #define CAN_HAL_EVENT_ABOVE_EWL (1 << 3)
  33. #define CAN_HAL_EVENT_BELOW_EWL (1 << 4)
  34. #define CAN_HAL_EVENT_ERROR_PASSIVE (1 << 5)
  35. #define CAN_HAL_EVENT_ERROR_ACTIVE (1 << 6)
  36. #define CAN_HAL_EVENT_BUS_ERR (1 << 7)
  37. #define CAN_HAL_EVENT_ARB_LOST (1 << 8)
  38. #define CAN_HAL_EVENT_RX_BUFF_FRAME (1 << 9)
  39. #define CAN_HAL_EVENT_TX_BUFF_FREE (1 << 10)
  40. typedef struct {
  41. can_dev_t *dev;
  42. } can_hal_context_t;
  43. typedef can_ll_frame_buffer_t can_hal_frame_t;
  44. /* ---------------------------- Init and Config ----------------------------- */
  45. /**
  46. * @brief Initialize CAN peripheral and HAL context
  47. *
  48. * Sets HAL context, puts CAN peripheral into reset mode, then sets some
  49. * registers with default values.
  50. *
  51. * @param hal_ctx Context of the HAL layer
  52. * @return True if successfully initialized, false otherwise.
  53. */
  54. bool can_hal_init(can_hal_context_t *hal_ctx);
  55. /**
  56. * @brief Deinitialize the CAN peripheral and HAL context
  57. *
  58. * Clears any unhandled interrupts and unsets HAL context
  59. *
  60. * @param hal_ctx Context of the HAL layer
  61. */
  62. void can_hal_deinit(can_hal_context_t *hal_ctx);
  63. /**
  64. * @brief Configure the CAN peripheral
  65. *
  66. * @param hal_ctx Context of the HAL layer
  67. * @param t_config Pointer to timing configuration structure
  68. * @param f_config Pointer to filter configuration structure
  69. * @param intr_mask Mask of interrupts to enable
  70. * @param clkout_divider Clock divider value for CLKOUT. Set to -1 to disable CLKOUT
  71. */
  72. void can_hal_configure(can_hal_context_t *hal_ctx, const can_timing_config_t *t_config, const can_filter_config_t *f_config, uint32_t intr_mask, uint32_t clkout_divider);
  73. /* -------------------------------- Actions --------------------------------- */
  74. /**
  75. * @brief Start the CAN peripheral
  76. *
  77. * Start the CAN peripheral by configuring its operating mode, then exiting
  78. * reset mode so that the CAN peripheral can participate in bus activities.
  79. *
  80. * @param hal_ctx Context of the HAL layer
  81. * @param mode Operating mode
  82. * @return True if successfully started, false otherwise.
  83. */
  84. bool can_hal_start(can_hal_context_t *hal_ctx, can_mode_t mode);
  85. /**
  86. * @brief Stop the CAN peripheral
  87. *
  88. * Stop the CAN peripheral by entering reset mode to stop any bus activity, then
  89. * setting the operating mode to Listen Only so that REC is frozen.
  90. *
  91. * @param hal_ctx Context of the HAL layer
  92. * @return True if successfully stopped, false otherwise.
  93. */
  94. bool can_hal_stop(can_hal_context_t *hal_ctx);
  95. /**
  96. * @brief Start bus recovery
  97. *
  98. * @param hal_ctx Context of the HAL layer
  99. * @return True if successfully started bus recovery, false otherwise.
  100. */
  101. static inline bool can_hal_start_bus_recovery(can_hal_context_t *hal_ctx)
  102. {
  103. return can_ll_exit_reset_mode(hal_ctx->dev);
  104. }
  105. /**
  106. * @brief Get the value of the TX Error Counter
  107. *
  108. * @param hal_ctx Context of the HAL layer
  109. * @return TX Error Counter Value
  110. */
  111. static inline uint32_t can_hal_get_tec(can_hal_context_t *hal_ctx)
  112. {
  113. return can_ll_get_tec((hal_ctx)->dev);
  114. }
  115. /**
  116. * @brief Get the value of the RX Error Counter
  117. *
  118. * @param hal_ctx Context of the HAL layer
  119. * @return RX Error Counter Value
  120. */
  121. static inline uint32_t can_hal_get_rec(can_hal_context_t *hal_ctx)
  122. {
  123. return can_ll_get_rec((hal_ctx)->dev);
  124. }
  125. /**
  126. * @brief Get the RX message count register
  127. *
  128. * @param hal_ctx Context of the HAL layer
  129. * @return RX message count
  130. */
  131. static inline uint32_t can_hal_get_rx_msg_count(can_hal_context_t *hal_ctx)
  132. {
  133. return can_ll_get_rx_msg_count((hal_ctx)->dev);
  134. }
  135. /**
  136. * @brief Check if the last transmitted frame was successful
  137. *
  138. * @param hal_ctx Context of the HAL layer
  139. * @return True if successful
  140. */
  141. static inline bool can_hal_check_last_tx_successful(can_hal_context_t *hal_ctx)
  142. {
  143. return can_ll_is_last_tx_successful((hal_ctx)->dev);
  144. }
  145. /* ----------------------------- Event Handling ----------------------------- */
  146. /**
  147. * @brief Decode current events that triggered an interrupt
  148. *
  149. * This function should be called on every CAN interrupt. It will read (and
  150. * thereby clear) the interrupt register, then determine what events have
  151. * occurred to trigger the interrupt.
  152. *
  153. * @param hal_ctx Context of the HAL layer
  154. * @param bus_recovering Whether the CAN peripheral was previous undergoing bus recovery
  155. * @return Bit mask of events that have occurred
  156. */
  157. uint32_t can_hal_decode_interrupt_events(can_hal_context_t *hal_ctx, bool bus_recovering);
  158. /**
  159. * @brief Handle bus recovery complete
  160. *
  161. * This function should be called on an bus recovery complete event. It simply
  162. * enters reset mode to stop bus activity.
  163. *
  164. * @param hal_ctx Context of the HAL layer
  165. * @return True if successfully handled bus recovery completion, false otherwise.
  166. */
  167. static inline bool can_hal_handle_bus_recov_cplt(can_hal_context_t *hal_ctx)
  168. {
  169. return can_ll_enter_reset_mode((hal_ctx)->dev);
  170. }
  171. /**
  172. * @brief Handle arbitration lost
  173. *
  174. * This function should be called on an arbitration lost event. It simply clears
  175. * the clears the ALC register.
  176. *
  177. * @param hal_ctx Context of the HAL layer
  178. */
  179. static inline void can_hal_handle_arb_lost(can_hal_context_t *hal_ctx)
  180. {
  181. can_ll_clear_arb_lost_cap((hal_ctx)->dev);
  182. }
  183. /**
  184. * @brief Handle bus error
  185. *
  186. * This function should be called on an bus error event. It simply clears
  187. * the clears the ECC register.
  188. *
  189. * @param hal_ctx Context of the HAL layer
  190. */
  191. static inline void can_hal_handle_bus_error(can_hal_context_t *hal_ctx)
  192. {
  193. can_ll_clear_err_code_cap((hal_ctx)->dev);
  194. }
  195. /**
  196. * @brief Handle BUS OFF
  197. *
  198. * This function should be called on a BUS OFF event. It simply changes the
  199. * mode to LOM to freeze REC
  200. *
  201. * @param hal_ctx Context of the HAL layer
  202. */
  203. static inline void can_hal_handle_bus_off(can_hal_context_t *hal_ctx)
  204. {
  205. can_ll_set_mode((hal_ctx)->dev, CAN_MODE_LISTEN_ONLY);
  206. }
  207. /* ------------------------------- TX and RX -------------------------------- */
  208. /**
  209. * @brief Format a CAN Frame
  210. *
  211. * This function takes a CAN message structure (containing ID, DLC, data, and
  212. * flags) and formats it to match the layout of the TX frame buffer.
  213. *
  214. * @param message Pointer to CAN message
  215. * @param frame Pointer to empty frame structure
  216. */
  217. static inline void can_hal_format_frame(const can_message_t *message, can_hal_frame_t *frame)
  218. {
  219. //Direct call to ll function
  220. can_ll_format_frame_buffer(message->identifier, message->data_length_code, message->data,
  221. message->flags, frame);
  222. }
  223. /**
  224. * @brief Parse a CAN Frame
  225. *
  226. * This function takes a CAN frame (in the format of the RX frame buffer) and
  227. * parses it to a CAN message (containing ID, DLC, data and flags).
  228. *
  229. * @param frame Pointer to frame structure
  230. * @param message Pointer to empty message structure
  231. */
  232. static inline void can_hal_parse_frame(can_hal_frame_t *frame, can_message_t *message)
  233. {
  234. //Direct call to ll function
  235. can_ll_prase_frame_buffer(frame, &message->identifier, &message->data_length_code,
  236. message->data, &message->flags);
  237. }
  238. /**
  239. * @brief Copy a frame into the TX buffer and transmit
  240. *
  241. * This function copies a formatted TX frame into the TX buffer, and the
  242. * transmit by setting the correct transmit command (e.g. normal, single shot,
  243. * self RX) in the command register.
  244. *
  245. * @param hal_ctx Context of the HAL layer
  246. * @param tx_frame Pointer to structure containing formatted TX frame
  247. */
  248. void can_hal_set_tx_buffer_and_transmit(can_hal_context_t *hal_ctx, can_hal_frame_t *tx_frame);
  249. /**
  250. * @brief Copy a frame from the RX buffer and release
  251. *
  252. * This function copies a frame from the RX buffer, then release the buffer (so
  253. * that it loads the next frame in the RX FIFO).
  254. *
  255. * @param hal_ctx Context of the HAL layer
  256. * @param rx_frame Pointer to structure to store RX frame
  257. */
  258. static inline void can_hal_read_rx_buffer_and_clear(can_hal_context_t *hal_ctx, can_hal_frame_t *rx_frame)
  259. {
  260. can_ll_get_rx_buffer(hal_ctx->dev, rx_frame);
  261. can_ll_set_cmd_release_rx_buffer(hal_ctx->dev);
  262. /*
  263. * Todo: Support overrun handling by:
  264. * - Check overrun status bit. Return false if overrun
  265. */
  266. }
  267. //Todo: Decode ALC register
  268. //Todo: Decode error code capture
  269. #ifdef __cplusplus
  270. }
  271. #endif