can.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //*****************************************************************************
  2. //
  3. // can.h - Defines and Macros for the CAN controller.
  4. //
  5. // Copyright (c) 2006-2009 Luminary Micro, Inc. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Luminary Micro, Inc. (LMI) is supplying this software for use solely and
  9. // exclusively on LMI's microcontroller products.
  10. //
  11. // The software is owned by LMI and/or its suppliers, and is protected under
  12. // applicable copyright laws. All rights are reserved. You may not combine
  13. // this software with "viral" open-source software in order to form a larger
  14. // program. Any use in violation of the foregoing restrictions may subject
  15. // the user to criminal sanctions under applicable laws, as well as to civil
  16. // liability for the breach of the terms and conditions of this license.
  17. //
  18. // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
  19. // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  20. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
  21. // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
  22. // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  23. //
  24. // This is part of revision 4694 of the Stellaris Peripheral Driver Library.
  25. //
  26. //*****************************************************************************
  27. #ifndef __CAN_H__
  28. #define __CAN_H__
  29. //*****************************************************************************
  30. //
  31. //! \addtogroup can_api
  32. //! @{
  33. //
  34. //*****************************************************************************
  35. //*****************************************************************************
  36. //
  37. // If building with a C++ compiler, make all of the definitions in this header
  38. // have a C binding.
  39. //
  40. //*****************************************************************************
  41. #ifdef __cplusplus
  42. extern "C"
  43. {
  44. #endif
  45. //*****************************************************************************
  46. //
  47. // Miscellaneous defines for Message ID Types
  48. //
  49. //*****************************************************************************
  50. //*****************************************************************************
  51. //
  52. //! These are the flags used by the tCANMsgObject variable when calling the
  53. //! CANMessageSet() and CANMessageGet() functions.
  54. //
  55. //*****************************************************************************
  56. typedef enum
  57. {
  58. //
  59. //! This indicates that transmit interrupts should be enabled, or are
  60. //! enabled.
  61. //
  62. MSG_OBJ_TX_INT_ENABLE = 0x00000001,
  63. //
  64. //! This indicates that receive interrupts should be enabled, or are
  65. //! enabled.
  66. //
  67. MSG_OBJ_RX_INT_ENABLE = 0x00000002,
  68. //
  69. //! This indicates that a message object will use or is using an extended
  70. //! identifier.
  71. //
  72. MSG_OBJ_EXTENDED_ID = 0x00000004,
  73. //
  74. //! This indicates that a message object will use or is using filtering
  75. //! based on the object's message identifier.
  76. //
  77. MSG_OBJ_USE_ID_FILTER = 0x00000008,
  78. //
  79. //! This indicates that new data was available in the message object.
  80. //
  81. MSG_OBJ_NEW_DATA = 0x00000080,
  82. //
  83. //! This indicates that data was lost since this message object was last
  84. //! read.
  85. //
  86. MSG_OBJ_DATA_LOST = 0x00000100,
  87. //
  88. //! This indicates that a message object will use or is using filtering
  89. //! based on the direction of the transfer. If the direction filtering is
  90. //! used, then ID filtering must also be enabled.
  91. //
  92. MSG_OBJ_USE_DIR_FILTER = (0x00000010 | MSG_OBJ_USE_ID_FILTER),
  93. //
  94. //! This indicates that a message object will use or is using message
  95. //! identifier filtering based on the extended identifier. If the extended
  96. //! identifier filtering is used, then ID filtering must also be enabled.
  97. //
  98. MSG_OBJ_USE_EXT_FILTER = (0x00000020 | MSG_OBJ_USE_ID_FILTER),
  99. //
  100. //! This indicates that a message object is a remote frame.
  101. //
  102. MSG_OBJ_REMOTE_FRAME = 0x00000040,
  103. //
  104. //! This indicates that a message object has no flags set.
  105. //
  106. MSG_OBJ_NO_FLAGS = 0x00000000
  107. }
  108. tCANObjFlags;
  109. //*****************************************************************************
  110. //
  111. //! This define is used with the #tCANObjFlags enumerated values to allow
  112. //! checking only status flags and not configuration flags.
  113. //
  114. //*****************************************************************************
  115. #define MSG_OBJ_STATUS_MASK (MSG_OBJ_NEW_DATA | MSG_OBJ_DATA_LOST)
  116. //*****************************************************************************
  117. //
  118. //! The structure used for encapsulating all the items associated with a CAN
  119. //! message object in the CAN controller.
  120. //
  121. //*****************************************************************************
  122. typedef struct
  123. {
  124. //
  125. //! The CAN message identifier used for 11 or 29 bit identifiers.
  126. //
  127. unsigned long ulMsgID;
  128. //
  129. //! The message identifier mask used when identifier filtering is enabled.
  130. //
  131. unsigned long ulMsgIDMask;
  132. //
  133. //! This value holds various status flags and settings specified by
  134. //! tCANObjFlags.
  135. //
  136. unsigned long ulFlags;
  137. //
  138. //! This value is the number of bytes of data in the message object.
  139. //
  140. unsigned long ulMsgLen;
  141. //
  142. //! This is a pointer to the message object's data.
  143. //
  144. unsigned char *pucMsgData;
  145. }
  146. tCANMsgObject;
  147. //*****************************************************************************
  148. //
  149. //! This structure is used for encapsulating the values associated with setting
  150. //! up the bit timing for a CAN controller. The structure is used when calling
  151. //! the CANGetBitTiming and CANSetBitTiming functions.
  152. //
  153. //*****************************************************************************
  154. typedef struct
  155. {
  156. //
  157. //! This value holds the sum of the Synchronization, Propagation, and Phase
  158. //! Buffer 1 segments, measured in time quanta. The valid values for this
  159. //! setting range from 2 to 16.
  160. //
  161. unsigned int uSyncPropPhase1Seg;
  162. //
  163. //! This value holds the Phase Buffer 2 segment in time quanta. The valid
  164. //! values for this setting range from 1 to 8.
  165. //
  166. unsigned int uPhase2Seg;
  167. //
  168. //! This value holds the Resynchronization Jump Width in time quanta. The
  169. //! valid values for this setting range from 1 to 4.
  170. //
  171. unsigned int uSJW;
  172. //
  173. //! This value holds the CAN_CLK divider used to determine time quanta.
  174. //! The valid values for this setting range from 1 to 1023.
  175. //
  176. unsigned int uQuantumPrescaler;
  177. }
  178. tCANBitClkParms;
  179. //*****************************************************************************
  180. //
  181. //! This data type is used to identify the interrupt status register. This is
  182. //! used when calling the CANIntStatus() function.
  183. //
  184. //*****************************************************************************
  185. typedef enum
  186. {
  187. //
  188. //! Read the CAN interrupt status information.
  189. //
  190. CAN_INT_STS_CAUSE,
  191. //
  192. //! Read a message object's interrupt status.
  193. //
  194. CAN_INT_STS_OBJECT
  195. }
  196. tCANIntStsReg;
  197. //*****************************************************************************
  198. //
  199. //! This data type is used to identify which of several status registers to
  200. //! read when calling the CANStatusGet() function.
  201. //
  202. //*****************************************************************************
  203. typedef enum
  204. {
  205. //
  206. //! Read the full CAN controller status.
  207. //
  208. CAN_STS_CONTROL,
  209. //
  210. //! Read the full 32-bit mask of message objects with a transmit request
  211. //! set.
  212. //
  213. CAN_STS_TXREQUEST,
  214. //
  215. //! Read the full 32-bit mask of message objects with new data available.
  216. //
  217. CAN_STS_NEWDAT,
  218. //
  219. //! Read the full 32-bit mask of message objects that are enabled.
  220. //
  221. CAN_STS_MSGVAL
  222. }
  223. tCANStsReg;
  224. //*****************************************************************************
  225. //
  226. //! These definitions are used to specify interrupt sources to CANIntEnable()
  227. //! and CANIntDisable().
  228. //
  229. //*****************************************************************************
  230. typedef enum
  231. {
  232. //
  233. //! This flag is used to allow a CAN controller to generate error
  234. //! interrupts.
  235. //
  236. CAN_INT_ERROR = 0x00000008,
  237. //
  238. //! This flag is used to allow a CAN controller to generate status
  239. //! interrupts.
  240. //
  241. CAN_INT_STATUS = 0x00000004,
  242. //
  243. //! This flag is used to allow a CAN controller to generate any CAN
  244. //! interrupts. If this is not set, then no interrupts will be generated
  245. //! by the CAN controller.
  246. //
  247. CAN_INT_MASTER = 0x00000002
  248. }
  249. tCANIntFlags;
  250. //*****************************************************************************
  251. //
  252. //! This definition is used to determine the type of message object that will
  253. //! be set up via a call to the CANMessageSet() API.
  254. //
  255. //*****************************************************************************
  256. typedef enum
  257. {
  258. //
  259. //! Transmit message object.
  260. //
  261. MSG_OBJ_TYPE_TX,
  262. //
  263. //! Transmit remote request message object
  264. //
  265. MSG_OBJ_TYPE_TX_REMOTE,
  266. //
  267. //! Receive message object.
  268. //
  269. MSG_OBJ_TYPE_RX,
  270. //
  271. //! Receive remote request message object.
  272. //
  273. MSG_OBJ_TYPE_RX_REMOTE,
  274. //
  275. //! Remote frame receive remote, with auto-transmit message object.
  276. //
  277. MSG_OBJ_TYPE_RXTX_REMOTE
  278. }
  279. tMsgObjType;
  280. //*****************************************************************************
  281. //
  282. //! The following enumeration contains all error or status indicators that can
  283. //! be returned when calling the CANStatusGet() function.
  284. //
  285. //*****************************************************************************
  286. typedef enum
  287. {
  288. //
  289. //! CAN controller has entered a Bus Off state.
  290. //
  291. CAN_STATUS_BUS_OFF = 0x00000080,
  292. //
  293. //! CAN controller error level has reached warning level.
  294. //
  295. CAN_STATUS_EWARN = 0x00000040,
  296. //
  297. //! CAN controller error level has reached error passive level.
  298. //
  299. CAN_STATUS_EPASS = 0x00000020,
  300. //
  301. //! A message was received successfully since the last read of this status.
  302. //
  303. CAN_STATUS_RXOK = 0x00000010,
  304. //
  305. //! A message was transmitted successfully since the last read of this
  306. //! status.
  307. //
  308. CAN_STATUS_TXOK = 0x00000008,
  309. //
  310. //! This is the mask for the last error code field.
  311. //
  312. CAN_STATUS_LEC_MSK = 0x00000007,
  313. //
  314. //! There was no error.
  315. //
  316. CAN_STATUS_LEC_NONE = 0x00000000,
  317. //
  318. //! A bit stuffing error has occurred.
  319. //
  320. CAN_STATUS_LEC_STUFF = 0x00000001,
  321. //
  322. //! A formatting error has occurred.
  323. //
  324. CAN_STATUS_LEC_FORM = 0x00000002,
  325. //
  326. //! An acknowledge error has occurred.
  327. //
  328. CAN_STATUS_LEC_ACK = 0x00000003,
  329. //
  330. //! The bus remained a bit level of 1 for longer than is allowed.
  331. //
  332. CAN_STATUS_LEC_BIT1 = 0x00000004,
  333. //
  334. //! The bus remained a bit level of 0 for longer than is allowed.
  335. //
  336. CAN_STATUS_LEC_BIT0 = 0x00000005,
  337. //
  338. //! A CRC error has occurred.
  339. //
  340. CAN_STATUS_LEC_CRC = 0x00000006,
  341. //
  342. //! This is the mask for the CAN Last Error Code (LEC).
  343. //
  344. CAN_STATUS_LEC_MASK = 0x00000007
  345. }
  346. tCANStatusCtrl;
  347. //*****************************************************************************
  348. //
  349. // API Function prototypes
  350. //
  351. //*****************************************************************************
  352. extern void CANBitTimingGet(unsigned long ulBase, tCANBitClkParms *pClkParms);
  353. extern void CANBitTimingSet(unsigned long ulBase, tCANBitClkParms *pClkParms);
  354. extern unsigned long CANBitRateSet(unsigned long ulBase,
  355. unsigned long ulSourceClock,
  356. unsigned long ulBitRate);
  357. extern void CANDisable(unsigned long ulBase);
  358. extern void CANEnable(unsigned long ulBase);
  359. extern tBoolean CANErrCntrGet(unsigned long ulBase, unsigned long *pulRxCount,
  360. unsigned long *pulTxCount);
  361. extern void CANInit(unsigned long ulBase);
  362. extern void CANIntClear(unsigned long ulBase, unsigned long ulIntClr);
  363. extern void CANIntDisable(unsigned long ulBase, unsigned long ulIntFlags);
  364. extern void CANIntEnable(unsigned long ulBase, unsigned long ulIntFlags);
  365. extern void CANIntRegister(unsigned long ulBase, void (*pfnHandler)(void));
  366. extern unsigned long CANIntStatus(unsigned long ulBase,
  367. tCANIntStsReg eIntStsReg);
  368. extern void CANIntUnregister(unsigned long ulBase);
  369. extern void CANMessageClear(unsigned long ulBase, unsigned long ulObjID);
  370. extern void CANMessageGet(unsigned long ulBase, unsigned long ulObjID,
  371. tCANMsgObject *pMsgObject, tBoolean bClrPendingInt);
  372. extern void CANMessageSet(unsigned long ulBase, unsigned long ulObjID,
  373. tCANMsgObject *pMsgObject, tMsgObjType eMsgType);
  374. extern tBoolean CANRetryGet(unsigned long ulBase);
  375. extern void CANRetrySet(unsigned long ulBase, tBoolean bAutoRetry);
  376. extern unsigned long CANStatusGet(unsigned long ulBase, tCANStsReg eStatusReg);
  377. //*****************************************************************************
  378. //
  379. // Several CAN APIs have been renamed, with the original function name being
  380. // deprecated. These defines provide backward compatibility.
  381. //
  382. //*****************************************************************************
  383. #ifndef DEPRECATED
  384. #define CANSetBitTiming(a, b) CANBitTimingSet(a, b)
  385. #define CANGetBitTiming(a, b) CANBitTimingGet(a, b)
  386. #endif
  387. //*****************************************************************************
  388. //
  389. // Mark the end of the C bindings section for C++ compilers.
  390. //
  391. //*****************************************************************************
  392. #ifdef __cplusplus
  393. }
  394. #endif
  395. //*****************************************************************************
  396. //
  397. // Close the Doxygen group.
  398. //! @}
  399. //
  400. //*****************************************************************************
  401. #endif // __CAN_H__