cmem7_can.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. *****************************************************************************
  3. * @file cmem7_can.h
  4. *
  5. * @brief CMEM7 CAN header file
  6. *
  7. *
  8. * @version V1.0
  9. * @date 3. September 2013
  10. *
  11. * @note
  12. *
  13. *****************************************************************************
  14. * @attention
  15. *
  16. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  18. * TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  20. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  21. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *
  23. * <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
  24. *****************************************************************************
  25. */
  26. #ifndef __CMEM7_CAN_H
  27. #define __CMEM7_CAN_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "cmem7.h"
  32. #include "cmem7_conf.h"
  33. #define IS_CAN_ALL_PERIPH(PERIPH) (((PERIPH) == CAN0) || \
  34. ((PERIPH) == CAN1))
  35. /** @defgroup CAN_FLT
  36. * @{
  37. */
  38. #define CAN_FLT_STANDARD_SINGLE 0x00000000
  39. #define CAN_FLT_STANDARD_DUAL 0x00000001
  40. #define CAN_FLT_EXTENDED_SINGLE 0x00000002
  41. #define CAN_FLT_EXTENDED_DUAL 0x00000003
  42. #define IS_CAN_FLT_TYPE(FILTER) (((FILTER) == CAN_FLT_STANDARD_SINGLE) || \
  43. ((FILTER) == CAN_FLT_STANDARD_DUAL) || \
  44. ((FILTER) == CAN_FLT_EXTENDED_SINGLE) || \
  45. ((FILTER) == CAN_FLT_EXTENDED_DUAL))
  46. #define IS_CAN_FLT_SINGLE(FILTER) (((FILTER) == CAN_FLT_STANDARD_SINGLE) || \
  47. ((FILTER) == CAN_FLT_EXTENDED_SINGLE))
  48. #define IS_CAN_FLT_DUAL(FILTER) (IS_CAN_FLT_TYPE(FILTER) && \
  49. !IS_CAN_FLT_SINGLE(FILTER))
  50. /**
  51. * @}
  52. */
  53. /** @defgroup CAN_CDR_DIV
  54. * @{
  55. */
  56. #define CAN_CDR_DIV_1_2 0x0
  57. #define CAN_CDR_DIV_1_4 0x1
  58. #define CAN_CDR_DIV_1_6 0x2
  59. #define CAN_CDR_DIV_1_8 0x3
  60. #define CAN_CDR_DIV_1_10 0x4
  61. #define CAN_CDR_DIV_1_12 0x5
  62. #define CAN_CDR_DIV_1_14 0x6
  63. #define CAN_CDR_DIV_1_1 0x7
  64. #define IS_CAN_CDR_DIV(DIV) (((DIV) == CAN_CDR_DIV_1_2) || \
  65. ((DIV) == CAN_CDR_DIV_1_4) || \
  66. ((DIV) == CAN_CDR_DIV_1_6) || \
  67. ((DIV) == CAN_CDR_DIV_1_8) || \
  68. ((DIV) == CAN_CDR_DIV_1_10) || \
  69. ((DIV) == CAN_CDR_DIV_1_12) || \
  70. ((DIV) == CAN_CDR_DIV_1_14) || \
  71. ((DIV) == CAN_CDR_DIV_1_1))
  72. /**
  73. * @}
  74. */
  75. /** @defgroup CAN_INT
  76. * @{
  77. */
  78. #define CAN_INT_RBNF 0x01
  79. #define CAN_INT_TB_UNLOCK 0x02
  80. #define CAN_INT_ERR 0x04
  81. #define CAN_INT_DATA_OVERRUN 0x08
  82. #define CAN_INT_WAKEUP 0x10
  83. #define CAN_INT_ERR_PASSIVE 0x20
  84. #define CAN_INT_ARBITRATION_LOST 0x40
  85. #define CAN_INT_BUS_ERR 0x80
  86. #define CAN_INT_All 0xFF
  87. #define IS_CAN_INT(INT) (((INT) != 0) && (((INT) & ~CAN_INT_All) == 0))
  88. /**
  89. * @}
  90. */
  91. /**
  92. * @brief CAN standard filter structure
  93. */
  94. typedef struct {
  95. uint16_t ID28_18; /*!< 11 bits */
  96. BOOL RTR; /*!< if remote frame */
  97. uint8_t data1; /*!< data byte 1, if not 2nd CAN_FLT_STANDARD_DUAL*/
  98. uint8_t data2; /*!< data byte 2, if CAN_FLT_STANDARD_SINGLE */
  99. } CAN_STANDARD_FILTER;
  100. /**
  101. * @brief CAN extended filter structure
  102. */
  103. typedef struct {
  104. uint16_t ID28_13; /*!< 16 bits */
  105. uint16_t ID12_0; /*!< 13 bits, if CAN_FLT_EXTENDED_SINGLE */
  106. BOOL RTR; /*!< if remote frame, if CAN_FLT_EXTENDED_SINGLE */
  107. } CAN_EXTENDED_FILTER;
  108. /**
  109. * @brief CAN filter structure
  110. */
  111. typedef struct {
  112. uint8_t type; /*!< Filter type, which is a value of @ref CAN_FLT */
  113. /**
  114. * @brief accepted filter
  115. */
  116. union {
  117. CAN_STANDARD_FILTER sf;
  118. CAN_EXTENDED_FILTER ef;
  119. } ACCEPT;
  120. /**
  121. * @brief filter mask
  122. */
  123. union {
  124. CAN_STANDARD_FILTER sf;
  125. CAN_EXTENDED_FILTER ef;
  126. } MASK;
  127. } CAN_FILTER;
  128. /**
  129. * @brief CAN initialization structure
  130. */
  131. typedef struct {
  132. BOOL CAN_TxEn; /*!< if transmission is enable */
  133. BOOL CAN_Loopback; /*!< loop back mode without phy */
  134. uint8_t CAN_ClockDiv; /*!< input clock divider, ref as @ref CAN_CDR_DIV */
  135. uint16_t CAN_Prescaler; /*!< Specifies the length of a time quantum.
  136. Time quantum = (CAN_Prescaler + 1) * 2 * input clock */
  137. uint8_t CAN_SJW; /*!< Specifies the maximum number of time quanta
  138. the CAN hardware is allowed to lengthen or
  139. shorten a bit to perform resynchronization. */
  140. uint8_t CAN_TSEG1; /*!< the maximum number of time quanta of
  141. propagation and 1st phase segment */
  142. uint8_t CAN_TSEG2; /*!< the maximum number of time quanta of 2nd
  143. phase segment */
  144. BOOL CAN_HighSpeed; /*!< if CAN is running on high speed bus (class C) */
  145. } CAN_InitTypeDef;
  146. /**
  147. * @brief CAN frame structure
  148. */
  149. typedef struct
  150. {
  151. BOOL SFF; /*!< If standard or extended frame format */
  152. uint32_t Id; /*!< Specifies the identifier.This parameter can
  153. be a value between 0 to 0x1FFFFFFF. */
  154. BOOL RTR; /*!< Specifies if the frame is a remote frame */
  155. uint8_t DLC; /*!< Specifies the length of the frame, which is
  156. a value between 0 to 8 */
  157. uint8_t Data[8]; /*!< Frame data */
  158. } CAN_Frame;
  159. /**
  160. * @brief CAN initialization
  161. * @note This function should be called at first before any other interfaces.
  162. * @param[in] CANx CAN peripheral, which is CAN0 or CAN1
  163. * @param[in] Init A pointer to structure CAN_InitTypeDef
  164. * @param[in] f1 A pointer to structure CAN_FILTER
  165. * @param[in] f2 NULL if single filter, or a pointer to second filter while dual filters
  166. * @retval BOOL The bit indicates if specific CAN is initialized or not
  167. */
  168. BOOL CAN_Init(CAN0_Type* CANx, CAN_InitTypeDef* Init,
  169. CAN_FILTER *f1, CAN_FILTER *f2);
  170. /**
  171. * @brief CAN is set to sleep or wake up
  172. * @param[in] CANx CAN peripheral, which is CAN0 or CAN1
  173. * @param[in] Enable The bit indicates if sleep mode is enable or not
  174. * @retval None
  175. */
  176. void CAN_SetSleepMode(CAN0_Type* CANx, BOOL enable);
  177. /**
  178. * @brief Enable or disable UART interrupt.
  179. * @param[in] CANx CAN peripheral, which is CAN0 or CAN1
  180. * @param[in] Int interrupt mask bits, which can be the combination of @ref CAN_INT
  181. * @param[in] Enable The bit indicates if specific interrupts are enable or not
  182. * @retval None
  183. */
  184. void CAN_EnableInt(CAN0_Type* CANx, uint32_t Int, BOOL enable);
  185. /**
  186. * @brief Check specific interrupts are set or not
  187. * @note All interrupts except for receive int are cleared after call this func.
  188. * @param[in] CANx CAN peripheral, which is CAN0 or CAN1
  189. * @retval uint8_t CAN interrupt bits, which can be the combination of @ref CAN_INT
  190. */
  191. uint8_t CAN_GetIntStatus(CAN0_Type* CANx);
  192. /**
  193. * @brief CAN perform to transmit data
  194. * @param[in] CANx CAN peripheral, which is CAN0 or CAN1
  195. * @param[in] frame A pointer to the CAN_Frame to be transmitted
  196. * @retval BOOL The bit indicates if data is transmitted or not
  197. */
  198. BOOL CAN_Transmit(CAN0_Type* CANx, CAN_Frame* frame);
  199. /**
  200. * @brief CAN perform to receive data
  201. * @param[in] CANx CAN peripheral, which is CAN0 or CAN1
  202. * @param[out] frame A user-allocated buffer to fetch received frame
  203. * @retval BOOL The bit indicates if data is recieved or not
  204. */
  205. BOOL CAN_Receive(CAN0_Type* CANx, CAN_Frame* frame);
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif /* __CMEM7_CAN_H */