can.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //###########################################################################
  2. //
  3. // FILE: can.h
  4. //
  5. // TITLE: Defines and Macros for the CAN controller.
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. #ifndef __CAN_H__
  43. #define __CAN_H__
  44. //*****************************************************************************
  45. //! \addtogroup can_api
  46. //! @{
  47. //*****************************************************************************
  48. //*****************************************************************************
  49. // If building with a C++ compiler, make all of the definitions in this header
  50. // have a C binding.
  51. //*****************************************************************************
  52. #ifdef __cplusplus
  53. extern "C"
  54. {
  55. #endif
  56. #define CAN_INDEX_TO_BASE(idx) ((idx == 0) ? CAN_A_BASE : CAN_B_BASE)
  57. #define CAN_INDEX_TO_MSG_RAM_BASE(idx) ((idx == 0) ? CAN_A_MSG_RAM : CAN_B_MSG_RAM)
  58. #define CAN_REG_WORD_MASK (0xFFFFU)
  59. //****************************************************************************
  60. // These are the Defines to select CAN pin muxing when calling the functions
  61. // ConfigCanPinMuxing(), ConfigGpioCanA() & ConfigGpioCanB() in F2837x_Can.c
  62. //****************************************************************************
  63. #define CAN_A_GPIO4_GPIO5 1 //switch case 1
  64. #define CAN_A_GPIO19_GPIO18 2 //switch case 2
  65. #define CAN_A_GPIO31_GPIO30 3 //switch case 3
  66. #define CAN_A_GPIO37_GPIO36 4 //switch case 4
  67. #define CAN_A_GPIO63_GPIO62 5 //switch case 5
  68. #define CAN_A_GPIO71_GPIO70 6 //switch case 6
  69. #define CAN_B_GPIO6_GPIO7 1 //switch case 1
  70. #define CAN_B_GPIO8_GPIO10 2 //switch case 2
  71. #define CAN_B_GPIO12_GPIO13 3 //switch case 3
  72. #define CAN_B_GPIO16_GPIO17 4 //switch case 4
  73. #define CAN_B_GPIO20_GPIO21 5 //switch case 5
  74. #define CAN_B_GPIO38_GPIO39 6 //switch case 6
  75. #define CAN_B_GPIO72_GPIO73 7 //switch case 7
  76. //*****************************************************************************
  77. // Miscellaneous defines for Message ID Types
  78. //*****************************************************************************
  79. //*****************************************************************************
  80. // These are the flags used by the tCANMsgObject.ui32Flags value when calling the
  81. // CANMessageSet() and CANMessageGet() functions.
  82. //*****************************************************************************
  83. //! This definition is used with the tCANMsgObject ui32Flags value and indicates
  84. //! that transmit interrupts should be enabled, or are enabled.
  85. #define MSG_OBJ_TX_INT_ENABLE 0x00000001
  86. //! This indicates that receive interrupts should be enabled, or are
  87. //! enabled.
  88. #define MSG_OBJ_RX_INT_ENABLE 0x00000002
  89. //! This indicates that a message object will use or is using an extended
  90. //! identifier.
  91. #define MSG_OBJ_EXTENDED_ID 0x00000004
  92. //! This indicates that a message object will use or is using filtering
  93. //! based on the object's message identifier.
  94. #define MSG_OBJ_USE_ID_FILTER 0x00000008
  95. //! This indicates that new data was available in the message object.
  96. #define MSG_OBJ_NEW_DATA 0x00000080
  97. //! This indicates that data was lost since this message object was last
  98. //! read.
  99. #define MSG_OBJ_DATA_LOST 0x00000100
  100. //! This indicates that a message object will use or is using filtering
  101. //! based on the direction of the transfer. If the direction filtering is
  102. //! used, then ID filtering must also be enabled.
  103. #define MSG_OBJ_USE_DIR_FILTER (0x00000010 | MSG_OBJ_USE_ID_FILTER)
  104. //! This indicates that a message object will use or is using message
  105. //! identifier filtering based on the extended identifier. If the extended
  106. //! identifier filtering is used, then ID filtering must also be enabled.
  107. #define MSG_OBJ_USE_EXT_FILTER (0x00000020 | MSG_OBJ_USE_ID_FILTER)
  108. //! This indicates that a message object is a remote frame.
  109. #define MSG_OBJ_REMOTE_FRAME 0x00000040
  110. //! This indicates that this message object is part of a FIFO structure and
  111. //! not the final message object in a FIFO.
  112. #define MSG_OBJ_FIFO 0x00000200
  113. //! This indicates that a message object has no flags set.
  114. #define MSG_OBJ_NO_FLAGS 0x00000000
  115. //*****************************************************************************
  116. //! This define is used with the flag values to allow checking only status
  117. //! flags and not configuration flags.
  118. //*****************************************************************************
  119. #define MSG_OBJ_STATUS_MASK (MSG_OBJ_NEW_DATA | MSG_OBJ_DATA_LOST)
  120. //*****************************************************************************
  121. //! The structure used for encapsulating all the items associated with a CAN
  122. //! message object in the CAN controller.
  123. //*****************************************************************************
  124. typedef struct
  125. {
  126. //! The CAN message identifier used for 11 or 29 bit identifiers.
  127. uint32_t ui32MsgID;
  128. //! The message identifier mask used when identifier filtering is enabled.
  129. uint32_t ui32MsgIDMask;
  130. //! This value holds various status flags and settings specified by
  131. //! tCANObjFlags.
  132. uint32_t ui32Flags;
  133. //! This value is the number of bytes of data in the message object.
  134. uint32_t ui32MsgLen;
  135. //! This is a pointer to the message object's data.
  136. unsigned char *pucMsgData;
  137. }
  138. tCANMsgObject;
  139. //*****************************************************************************
  140. //! This structure is used for encapsulating the values associated with setting
  141. //! up the bit timing for a CAN controller. The structure is used when calling
  142. //! the CANGetBitTiming and CANSetBitTiming functions.
  143. //*****************************************************************************
  144. typedef struct
  145. {
  146. //! This value holds the sum of the Synchronization, Propagation, and Phase
  147. //! Buffer 1 segments, measured in time quanta. The valid values for this
  148. //! setting range from 2 to 16.
  149. uint16_t uSyncPropPhase1Seg;
  150. //! This value holds the Phase Buffer 2 segment in time quanta. The valid
  151. //! values for this setting range from 1 to 8.
  152. uint16_t uPhase2Seg;
  153. //! This value holds the Resynchronization Jump Width in time quanta. The
  154. //! valid values for this setting range from 1 to 4.
  155. uint16_t uSJW;
  156. //! This value holds the CAN_CLK divider used to determine time quanta.
  157. //! The valid values for this setting range from 1 to 1023.
  158. uint16_t uQuantumPrescaler;
  159. }
  160. tCANBitClkParms;
  161. //*****************************************************************************
  162. //! This data type is used to identify the interrupt status register. This is
  163. //! used when calling the CANIntStatus() function.
  164. //*****************************************************************************
  165. typedef enum
  166. {
  167. //! Read the CAN interrupt status information.
  168. CAN_INT_STS_CAUSE,
  169. //! Read a message object's interrupt status.
  170. CAN_INT_STS_OBJECT
  171. }
  172. tCANIntStsReg;
  173. //*****************************************************************************
  174. //! This data type is used to identify which of several status registers to
  175. //! read when calling the CANStatusGet() function.
  176. //*****************************************************************************
  177. typedef enum
  178. {
  179. //! Read the full CAN controller status.
  180. CAN_STS_CONTROL,
  181. //! Read the full 32-bit mask of message objects with a transmit request
  182. //! set.
  183. CAN_STS_TXREQUEST,
  184. //! Read the full 32-bit mask of message objects with new data available.
  185. CAN_STS_NEWDAT,
  186. //! Read the full 32-bit mask of message objects that are enabled.
  187. CAN_STS_MSGVAL
  188. }
  189. tCANStsReg;
  190. //*****************************************************************************
  191. // These definitions are used to specify interrupt sources to CANIntEnable()
  192. // and CANIntDisable().
  193. //*****************************************************************************
  194. //! This flag is used to allow a CAN controller to generate error
  195. //! interrupts.
  196. #define CAN_INT_ERROR 0x00000008
  197. //! This flag is used to allow a CAN controller to generate status
  198. //! interrupts.
  199. #define CAN_INT_STATUS 0x00000004
  200. //! This flag is used to allow a CAN controller to generate interrupts
  201. //! on interrupt line 0
  202. #define CAN_INT_IE0 0x00000002
  203. //! This flag is used to allow a CAN controller to generate interrupts
  204. //! on interrupt line 1
  205. #define CAN_INT_IE1 0x00020000
  206. // Defined to maintain compatibility with Stellaris Examples
  207. #define CAN_INT_MASTER CAN_INT_IE0
  208. //*****************************************************************************
  209. // These definitions are used to specify the clock source to
  210. // CANClkSourceSelect()
  211. //*****************************************************************************
  212. //! This flag is used to clock the CAN controller Selected CPU SYSCLKOUT
  213. //! (CPU1.Sysclk or CPU2.Sysclk).
  214. #define CAN_CLK_CPU_SYSCLKOUT 0 // PERx.SYSCLK (default on reset)
  215. //! This flag is used to clock the CAN controller with the X1/X2 oscillator
  216. //! clock.
  217. #define CAN_CLK_EXT_OSC 1 // External Oscillator (XTAL)
  218. //! This flag is used to clock the CAN controller with the clock from
  219. //! AUXCLKIN (from GPIO)
  220. #define CAN_CLK_AUXCLKIN 2 // AUXCLKIN (from GPIO)
  221. //*****************************************************************************
  222. //! This definition is used to determine the type of message object that will
  223. //! be set up via a call to the CANMessageSet() API.
  224. //*****************************************************************************
  225. typedef enum
  226. {
  227. //! Transmit message object.
  228. MSG_OBJ_TYPE_TX,
  229. //! Transmit remote request message object
  230. MSG_OBJ_TYPE_TX_REMOTE,
  231. //! Receive message object.
  232. MSG_OBJ_TYPE_RX,
  233. //! Receive remote request message object.
  234. MSG_OBJ_TYPE_RX_REMOTE,
  235. //! Remote frame receive remote, with auto-transmit message object.
  236. MSG_OBJ_TYPE_RXTX_REMOTE
  237. }
  238. tMsgObjType;
  239. //*****************************************************************************
  240. // The following enumeration contains all error or status indicators that can
  241. // be returned when calling the CANStatusGet() function.
  242. //*****************************************************************************
  243. //! CAN controller is in local power down mode.
  244. #define CAN_STATUS_PDA 0x00000400
  245. //! CAN controller has initiated a system wakeup.
  246. #define CAN_STATUS_WAKE_UP 0x00000200
  247. //! CAN controller has detected a parity error.
  248. #define CAN_STATUS_PERR 0x00000100
  249. //! CAN controller has entered a Bus Off state.
  250. #define CAN_STATUS_BUS_OFF 0x00000080
  251. //! CAN controller error level has reached warning level.
  252. #define CAN_STATUS_EWARN 0x00000040
  253. //! CAN controller error level has reached error passive level.
  254. #define CAN_STATUS_EPASS 0x00000020
  255. //! A message was received successfully since the last read of this status.
  256. #define CAN_STATUS_RXOK 0x00000010
  257. //! A message was transmitted successfully since the last read of this
  258. //! status.
  259. #define CAN_STATUS_TXOK 0x00000008
  260. //! This is the mask for the last error code field.
  261. #define CAN_STATUS_LEC_MSK 0x00000007
  262. //! There was no error.
  263. #define CAN_STATUS_LEC_NONE 0x00000000
  264. //! A bit stuffing error has occurred.
  265. #define CAN_STATUS_LEC_STUFF 0x00000001
  266. //! A formatting error has occurred.
  267. #define CAN_STATUS_LEC_FORM 0x00000002
  268. //! An acknowledge error has occurred.
  269. #define CAN_STATUS_LEC_ACK 0x00000003
  270. //! The bus remained a bit level of 1 for longer than is allowed.
  271. #define CAN_STATUS_LEC_BIT1 0x00000004
  272. //! The bus remained a bit level of 0 for longer than is allowed.
  273. #define CAN_STATUS_LEC_BIT0 0x00000005
  274. //! A CRC error has occurred.
  275. #define CAN_STATUS_LEC_CRC 0x00000006
  276. //*****************************************************************************
  277. // The following macros are added for the new Global Interrupt EN/FLG/CLR
  278. // register
  279. //*****************************************************************************
  280. //CANINT0 global interrupt bit
  281. #define CAN_GLOBAL_INT_CANINT0 0x00000001
  282. //CANINT1 global interrupt bit
  283. #define CAN_GLOBAL_INT_CANINT1 0x00000002
  284. //*****************************************************************************
  285. // The following macros are missing in hw_can.h because of scripting
  286. // but driverlib can.c needs them
  287. //*****************************************************************************
  288. #define CAN_INT_INT0ID_STATUS 0x8000
  289. #define CAN_IF1ARB_STD_ID_S 18
  290. #define CAN_IF1ARB_STD_ID_M 0x1FFC0000 // Standard Message Identifier
  291. #define CAN_IF2ARB_STD_ID_S 18
  292. #define CAN_IF2ARB_STD_ID_M 0x1FFC0000 // Standard Message Identifier
  293. //*****************************************************************************
  294. // API Function prototypes
  295. //*****************************************************************************
  296. extern void CANClkSourceSelect(uint32_t ui32Base, uint16_t ucSource);
  297. extern void CANBitTimingGet(uint32_t ui32Base, tCANBitClkParms *pClkParms);
  298. extern void CANBitTimingSet(uint32_t ui32Base, tCANBitClkParms *pClkParms);
  299. extern uint32_t CANBitRateSet(uint32_t ui32Base, uint32_t ui32SourceClock,
  300. uint32_t ui32BitRate);
  301. extern void CANDisable(uint32_t ui32Base);
  302. extern void CANEnable(uint32_t ui32Base);
  303. extern bool CANErrCntrGet(uint32_t ui32Base, uint32_t *pui32RxCount,
  304. uint32_t *pui32TxCount);
  305. extern void CANInit(uint32_t ui32Base);
  306. extern void CANIntClear(uint32_t ui32Base, uint32_t ui32IntClr);
  307. extern void CANIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags);
  308. extern void CANIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags);
  309. extern void CANIntRegister(uint32_t ui32Base, unsigned char ucIntNumber,
  310. void (*pfnHandler)(void));
  311. extern uint32_t CANIntStatus(uint32_t ui32Base, tCANIntStsReg eIntStsReg);
  312. extern void CANIntUnregister(uint32_t ui32Base, unsigned char ucIntNumber);
  313. extern void CANMessageClear(uint32_t ui32Base, uint32_t ui32ObjID);
  314. extern void CANMessageGet(uint32_t ui32Base, uint32_t ui32ObjID,
  315. tCANMsgObject *pMsgObject, bool bClrPendingInt);
  316. extern void CANMessageSet(uint32_t ui32Base, uint32_t ui32ObjID,
  317. tCANMsgObject *pMsgObject, tMsgObjType eMsgType);
  318. extern bool CANRetryGet(uint32_t ui32Base);
  319. extern void CANRetrySet(uint32_t ui32Base, bool bAutoRetry);
  320. extern uint32_t CANStatusGet(uint32_t ui32Base, tCANStsReg eStatusReg);
  321. extern void CANGlobalIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags);
  322. extern void CANGlobalIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags);
  323. extern void CANGlobalIntClear(uint32_t ui32Base, uint32_t ui32IntFlags);
  324. extern bool CANGlobalIntstatusGet(uint32_t ui32Base, uint32_t ui32IntFlags);
  325. //*****************************************************************************
  326. // Mark the end of the C bindings section for C++ compilers.
  327. //*****************************************************************************
  328. #ifdef __cplusplus
  329. }
  330. #endif
  331. //*****************************************************************************
  332. // Close the Doxygen group.
  333. //! @}
  334. //*****************************************************************************
  335. #endif // __CAN_H__