hpl_can_async.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * \file
  3. *
  4. * \brief Control Area Network(CAN) functionality declaration.
  5. *
  6. * Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Subject to your compliance with these terms, you may use Microchip
  13. * software and any derivatives exclusively with Microchip products.
  14. * It is your responsibility to comply with third party license terms applicable
  15. * to your use of third party software (including open source software) that
  16. * may accompany Microchip software.
  17. *
  18. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  19. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  20. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  21. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  22. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  23. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  24. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  25. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  26. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  27. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  28. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  29. *
  30. * \asf_license_stop
  31. *
  32. */
  33. #ifndef HPL_CAN_ASYNC_H_INCLUDED
  34. #define HPL_CAN_ASYNC_H_INCLUDED
  35. #include <utils.h>
  36. #include <hpl_can.h>
  37. #include <hpl_irq.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**
  42. * \addtogroup hpl_can_async CAN HPL Driver
  43. *
  44. * \section can_rev Revision History
  45. * - v0.0.0.1 Initial Commit
  46. *
  47. *@{
  48. */
  49. /**
  50. * \brief CAN device descriptor forware declaration
  51. */
  52. struct _can_async_device;
  53. /**
  54. * \brief CAN callback types
  55. */
  56. enum can_async_callback_type {
  57. CAN_ASYNC_RX_CB, /*!< A new message arrived */
  58. CAN_ASYNC_TX_CB, /*!< A message transmitted */
  59. CAN_ASYNC_IRQ_CB /*!< Message error of some kind on the CAN bus IRQ */
  60. };
  61. enum can_async_interrupt_type {
  62. CAN_IRQ_EW, /*!< Error warning, Error counter has reached the
  63. error warning limit of 96, An error count value
  64. greater than about 96 indicates a heavily disturbed
  65. bus. It may be of advantage to provide means to test
  66. for this condition. Refer to ISO 11898-1 (Bosch CAN
  67. specification 2.0 part A,B)
  68. */
  69. CAN_IRQ_EA, /*!< Error Active State, The CAN node normally take
  70. part in bus communication and sends an ACTIVE ERROR
  71. FLAG when an error has been detected.
  72. Refer to ISO 11898-1 (7)
  73. */
  74. CAN_IRQ_EP, /*!< Error Passive State, The Can node goes into error
  75. passive state if at least one of its error counters is
  76. greater than 127. It still takes part in bus
  77. activities, but it sends a passive error frame only,
  78. on errors. Refer to ISO 11898-1 (7)
  79. */
  80. CAN_IRQ_BO, /*!< Bus Off State, The CAN node is 'bus off' when the
  81. TRANSMIT ERROR COUNT is greater than or equal to 256.
  82. Refer to ISO 11898-1 (7)
  83. */
  84. CAN_IRQ_DO /*!< Data Overrun in receive queue. A message was lost
  85. because the messages in the queue was not reading and
  86. releasing fast enough. There is not enough space for
  87. a new message in receive queue.
  88. */
  89. };
  90. /**
  91. * \brief CAN interrupt handlers structure
  92. */
  93. struct _can_async_callback {
  94. void (*tx_done)(struct _can_async_device *dev);
  95. void (*rx_done)(struct _can_async_device *dev);
  96. void (*irq_handler)(struct _can_async_device *dev, enum can_async_interrupt_type type);
  97. };
  98. /**
  99. * \brief CAN device descriptor
  100. */
  101. struct _can_async_device {
  102. void * hw; /*!< CAN hardware pointer */
  103. struct _can_async_callback cb; /*!< CAN interrupt handler */
  104. struct _irq_descriptor irq; /*!< Interrupt descriptor */
  105. void * context; /*!< CAN hardware context */
  106. };
  107. /**
  108. * \brief Initialize CAN.
  109. *
  110. * This function initializes the given CAN device descriptor.
  111. *
  112. * \param[in, out] dev A CAN device descriptor to initialize
  113. * \param[in] hw The pointer to hardware instance
  114. *
  115. * \return Initialization status.
  116. */
  117. int32_t _can_async_init(struct _can_async_device *const dev, void *const hw);
  118. /**
  119. * \brief Deinitialize CAN.
  120. *
  121. * This function deinitializes the given can device descriptor.
  122. *
  123. * \param[in] dev The CAN device descriptor to deinitialize
  124. *
  125. * \return De-initialization status.
  126. */
  127. int32_t _can_async_deinit(struct _can_async_device *const dev);
  128. /**
  129. * \brief Enable CAN
  130. *
  131. * This function enable CAN by the given can device descriptor.
  132. *
  133. * \param[in] dev The CAN device descriptor to enable
  134. *
  135. * \return Enabling status.
  136. */
  137. int32_t _can_async_enable(struct _can_async_device *const dev);
  138. /**
  139. * \brief Disable CAN
  140. *
  141. * This function disable CAN by the given can device descriptor.
  142. *
  143. * \param[in] dev The CAN descriptor to disable
  144. *
  145. * \return Disabling status.
  146. */
  147. int32_t _can_async_disable(struct _can_async_device *const dev);
  148. /**
  149. * \brief Read a CAN message
  150. *
  151. * \param[in] dev The CAN device descriptor to read message.
  152. * \param[in] msg The CAN message to read to.
  153. *
  154. * \return The status of read message.
  155. */
  156. int32_t _can_async_read(struct _can_async_device *const dev, struct can_message *msg);
  157. /**
  158. * \brief Write a CAN message
  159. *
  160. * \param[in] dev The CAN device descriptor to write message.
  161. * \param[in] msg The CAN message to write.
  162. *
  163. * \return The status of write message.
  164. */
  165. int32_t _can_async_write(struct _can_async_device *const dev, struct can_message *msg);
  166. /**
  167. * \brief Set CAN Interrupt State
  168. *
  169. * \param[in] dev The CAN device descriptor
  170. * \param[in] type Callback type
  171. * \param[in] state ture for enable or false for disable
  172. *
  173. */
  174. void _can_async_set_irq_state(struct _can_async_device *const dev, enum can_async_callback_type type, bool state);
  175. /**
  176. * \brief Return number of read errors
  177. *
  178. * This function return number of read errors
  179. *
  180. * \param[in] dev The CAN device descriptor pointer
  181. *
  182. * \return Number of read errors.
  183. */
  184. uint8_t _can_async_get_rxerr(struct _can_async_device *const dev);
  185. /**
  186. * \brief Return number of write errors
  187. *
  188. * This function return number of write errors
  189. *
  190. * \param[in] dev The CAN device descriptor pointer
  191. *
  192. * \return Number of write errors.
  193. */
  194. uint8_t _can_async_get_txerr(struct _can_async_device *const dev);
  195. /**
  196. * \brief Set CAN to the specified mode
  197. *
  198. * This function set CAN to a specified mode
  199. *
  200. * \param[in] dev The CAN device descriptor pointer
  201. * \param[in] mode CAN operation mode
  202. *
  203. * \return Status of the operation
  204. */
  205. int32_t _can_async_set_mode(struct _can_async_device *const dev, enum can_mode mode);
  206. /**
  207. * \brief Set CAN to the specified mode
  208. *
  209. * This function set CAN to a specified mode
  210. *
  211. * \param[in] dev The CAN device descriptor pointer
  212. * \param[in] index Index of Filter list
  213. * \param[in] filter CAN Filter struct, NULL for clear filter
  214. *
  215. * \return Status of the operation
  216. */
  217. int32_t _can_async_set_filter(struct _can_async_device *const dev, uint8_t index, enum can_format fmt,
  218. struct can_filter *filter);
  219. /**@}*/
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif /* HPL_CAN_ASYNC_H_INCLUDED */