lpc_can.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /**********************************************************************
  2. * $Id$ lpc_can.h 2011-06-02
  3. *//**
  4. * @file lpc_can.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for CAN firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @defgroup CAN CAN (Controller Area Network)
  34. * @ingroup LPC_CMSIS_FwLib_Drivers
  35. * @{
  36. */
  37. #ifndef __LPC_CAN_H_
  38. #define __LPC_CAN_H_
  39. /* Includes ------------------------------------------------------------------- */
  40. #include "LPC407x_8x_177x_8x.h"
  41. #include "lpc_types.h"
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. /* Public Types --------------------------------------------------------------- */
  47. /** @defgroup CAN_Public_Macros CAN Public Macros
  48. * @{
  49. */
  50. /** Controller ID for CAN1 */
  51. #define CAN1_CTRL ((uint8_t)(0))
  52. /** Controller ID for CAN2 */
  53. #define CAN2_CTRL ((uint8_t)(1))
  54. /** Message(s) Acceptance is enabled */
  55. #define MSG_ENABLE ((uint8_t)(0))
  56. /** Message(s) Acceptance is disabled */
  57. #define MSG_DISABLE ((uint8_t)(1))
  58. /**
  59. * @}
  60. */
  61. /* Private Macros ------------------------------------------------------------- */
  62. /** @defgroup CAN_Private_Macros CAN Private Macros
  63. * @{
  64. */
  65. /* --------------------- BIT DEFINITIONS -------------------------------------- */
  66. /*********************************************************************//**
  67. * Macro defines for CAN Mode Register
  68. **********************************************************************/
  69. /** CAN Reset mode */
  70. #define CAN_MOD_RM ((uint32_t)(1<<0))
  71. /** CAN Listen Only Mode */
  72. #define CAN_MOD_LOM ((uint32_t)(1<<1))
  73. /** CAN Self Test mode */
  74. #define CAN_MOD_STM ((uint32_t)(1<<2))
  75. /** CAN Transmit Priority mode */
  76. #define CAN_MOD_TPM ((uint32_t)(1<<3))
  77. /** CAN Sleep mode */
  78. #define CAN_MOD_SM ((uint32_t)(1<<4))
  79. /** CAN Receive Polarity mode */
  80. #define CAN_MOD_RPM ((uint32_t)(1<<5))
  81. /** CAN Test mode */
  82. #define CAN_MOD_TM ((uint32_t)(1<<7))
  83. /*********************************************************************//**
  84. * Macro defines for CAN Command Register
  85. **********************************************************************/
  86. /** CAN Transmission Request */
  87. #define CAN_CMR_TR ((uint32_t)(1))
  88. /** CAN Abort Transmission */
  89. #define CAN_CMR_AT ((uint32_t)(1<<1))
  90. /** CAN Release Receive Buffer */
  91. #define CAN_CMR_RRB ((uint32_t)(1<<2))
  92. /** CAN Clear Data Overrun */
  93. #define CAN_CMR_CDO ((uint32_t)(1<<3))
  94. /** CAN Self Reception Request */
  95. #define CAN_CMR_SRR ((uint32_t)(1<<4))
  96. /** CAN Select Tx Buffer 1 */
  97. #define CAN_CMR_STB1 ((uint32_t)(1<<5))
  98. /** CAN Select Tx Buffer 2 */
  99. #define CAN_CMR_STB2 ((uint32_t)(1<<6))
  100. /** CAN Select Tx Buffer 3 */
  101. #define CAN_CMR_STB3 ((uint32_t)(1<<7))
  102. /*********************************************************************//**
  103. * Macro defines for CAN Global Status Register
  104. **********************************************************************/
  105. /** CAN Receive Buffer Status */
  106. #define CAN_GSR_RBS ((uint32_t)(1))
  107. /** CAN Data Overrun Status */
  108. #define CAN_GSR_DOS ((uint32_t)(1<<1))
  109. /** CAN Transmit Buffer Status */
  110. #define CAN_GSR_TBS ((uint32_t)(1<<2))
  111. /** CAN Transmit Complete Status */
  112. #define CAN_GSR_TCS ((uint32_t)(1<<3))
  113. /** CAN Receive Status */
  114. #define CAN_GSR_RS ((uint32_t)(1<<4))
  115. /** CAN Transmit Status */
  116. #define CAN_GSR_TS ((uint32_t)(1<<5))
  117. /** CAN Error Status */
  118. #define CAN_GSR_ES ((uint32_t)(1<<6))
  119. /** CAN Bus Status */
  120. #define CAN_GSR_BS ((uint32_t)(1<<7))
  121. /** CAN Current value of the Rx Error Counter */
  122. #define CAN_GSR_RXERR(n) ((uint32_t)((n&0xFF)<<16))
  123. /** CAN Current value of the Tx Error Counter */
  124. #define CAN_GSR_TXERR(n) ((uint32_t)(n&0xFF)<<24))
  125. /*********************************************************************//**
  126. * Macro defines for CAN Interrupt and Capture Register
  127. **********************************************************************/
  128. /** CAN Receive Interrupt */
  129. #define CAN_ICR_RI ((uint32_t)(1))
  130. /** CAN Transmit Interrupt 1 */
  131. #define CAN_ICR_TI1 ((uint32_t)(1<<1))
  132. /** CAN Error Warning Interrupt */
  133. #define CAN_ICR_EI ((uint32_t)(1<<2))
  134. /** CAN Data Overrun Interrupt */
  135. #define CAN_ICR_DOI ((uint32_t)(1<<3))
  136. /** CAN Wake-Up Interrupt */
  137. #define CAN_ICR_WUI ((uint32_t)(1<<4))
  138. /** CAN Error Passive Interrupt */
  139. #define CAN_ICR_EPI ((uint32_t)(1<<5))
  140. /** CAN Arbitration Lost Interrupt */
  141. #define CAN_ICR_ALI ((uint32_t)(1<<6))
  142. /** CAN Bus Error Interrupt */
  143. #define CAN_ICR_BEI ((uint32_t)(1<<7))
  144. /** CAN ID Ready Interrupt */
  145. #define CAN_ICR_IDI ((uint32_t)(1<<8))
  146. /** CAN Transmit Interrupt 2 */
  147. #define CAN_ICR_TI2 ((uint32_t)(1<<9))
  148. /** CAN Transmit Interrupt 3 */
  149. #define CAN_ICR_TI3 ((uint32_t)(1<<10))
  150. /** CAN Error Code Capture */
  151. #define CAN_ICR_ERRBIT(n) ((uint32_t)((n&0x1F)<<16))
  152. /** CAN Error Direction */
  153. #define CAN_ICR_ERRDIR ((uint32_t)(1<<21))
  154. /** CAN Error Capture */
  155. #define CAN_ICR_ERRC(n) ((uint32_t)((n&0x3)<<22))
  156. /** CAN Arbitration Lost Capture */
  157. #define CAN_ICR_ALCBIT(n) ((uint32_t)((n&0xFF)<<24))
  158. /*********************************************************************//**
  159. * Macro defines for CAN Interrupt Enable Register
  160. **********************************************************************/
  161. /** CAN Receive Interrupt Enable */
  162. #define CAN_IER_RIE ((uint32_t)(1))
  163. /** CAN Transmit Interrupt Enable for buffer 1 */
  164. #define CAN_IER_TIE1 ((uint32_t)(1<<1))
  165. /** CAN Error Warning Interrupt Enable */
  166. #define CAN_IER_EIE ((uint32_t)(1<<2))
  167. /** CAN Data Overrun Interrupt Enable */
  168. #define CAN_IER_DOIE ((uint32_t)(1<<3))
  169. /** CAN Wake-Up Interrupt Enable */
  170. #define CAN_IER_WUIE ((uint32_t)(1<<4))
  171. /** CAN Error Passive Interrupt Enable */
  172. #define CAN_IER_EPIE ((uint32_t)(1<<5))
  173. /** CAN Arbitration Lost Interrupt Enable */
  174. #define CAN_IER_ALIE ((uint32_t)(1<<6))
  175. /** CAN Bus Error Interrupt Enable */
  176. #define CAN_IER_BEIE ((uint32_t)(1<<7))
  177. /** CAN ID Ready Interrupt Enable */
  178. #define CAN_IER_IDIE ((uint32_t)(1<<8))
  179. /** CAN Transmit Enable Interrupt for Buffer 2 */
  180. #define CAN_IER_TIE2 ((uint32_t)(1<<9))
  181. /** CAN Transmit Enable Interrupt for Buffer 3 */
  182. #define CAN_IER_TIE3 ((uint32_t)(1<<10))
  183. /*********************************************************************//**
  184. * Macro defines for CAN Bus Timing Register
  185. **********************************************************************/
  186. /** CAN Baudrate Prescaler */
  187. #define CAN_BTR_BRP(n) ((uint32_t)(n&0x3FF))
  188. /** CAN Synchronization Jump Width */
  189. #define CAN_BTR_SJM(n) ((uint32_t)((n&0x3)<<14))
  190. /** CAN Time Segment 1 */
  191. #define CAN_BTR_TESG1(n) ((uint32_t)(n&0xF)<<16))
  192. /** CAN Time Segment 2 */
  193. #define CAN_BTR_TESG2(n) ((uint32_t)(n&0xF)<<20))
  194. /** CAN Sampling */
  195. #define CAN_BTR_SAM(n) ((uint32_t)(1<<23))
  196. /*********************************************************************//**
  197. * Macro defines for CAN Error Warning Limit Register
  198. **********************************************************************/
  199. /** CAN Error Warning Limit */
  200. #define CAN_EWL_EWL(n) ((uint32_t)(n&0xFF))
  201. /*********************************************************************//**
  202. * Macro defines for CAN Status Register
  203. **********************************************************************/
  204. /** CAN Receive Buffer Status */
  205. #define CAN_SR_RBS ((uint32_t)(1))
  206. /** CAN Data Overrun Status */
  207. #define CAN_SR_DOS ((uint32_t)(1<<1))
  208. /** CAN Transmit Buffer Status 1 */
  209. #define CAN_SR_TBS1 ((uint32_t)(1<<2))
  210. /** CAN Transmission Complete Status of Buffer 1 */
  211. #define CAN_SR_TCS1 ((uint32_t)(1<<3))
  212. /** CAN Receive Status */
  213. #define CAN_SR_RS ((uint32_t)(1<<4))
  214. /** CAN Transmit Status 1 */
  215. #define CAN_SR_TS1 ((uint32_t)(1<<5))
  216. /** CAN Error Status */
  217. #define CAN_SR_ES ((uint32_t)(1<<6))
  218. /** CAN Bus Status */
  219. #define CAN_SR_BS ((uint32_t)(1<<7))
  220. /** CAN Transmit Buffer Status 2 */
  221. #define CAN_SR_TBS2 ((uint32_t)(1<<10))
  222. /** CAN Transmission Complete Status of Buffer 2 */
  223. #define CAN_SR_TCS2 ((uint32_t)(1<<11))
  224. /** CAN Transmit Status 2 */
  225. #define CAN_SR_TS2 ((uint32_t)(1<<13))
  226. /** CAN Transmit Buffer Status 2 */
  227. #define CAN_SR_TBS3 ((uint32_t)(1<<18))
  228. /** CAN Transmission Complete Status of Buffer 2 */
  229. #define CAN_SR_TCS3 ((uint32_t)(1<<19))
  230. /** CAN Transmit Status 2 */
  231. #define CAN_SR_TS3 ((uint32_t)(1<<21))
  232. /*********************************************************************//**
  233. * Macro defines for CAN Receive Frame Status Register
  234. **********************************************************************/
  235. /** CAN ID Index */
  236. #define CAN_RFS_ID_INDEX(n) ((uint32_t)(n&0x3FF))
  237. /** CAN Bypass */
  238. #define CAN_RFS_BP ((uint32_t)(1<<10))
  239. /** CAN Data Length Code */
  240. #define CAN_RFS_DLC(n) ((uint32_t)((n&0xF)<<16)
  241. /** CAN Remote Transmission Request */
  242. #define CAN_RFS_RTR ((uint32_t)(1<<30))
  243. /** CAN control 11 bit or 29 bit Identifier */
  244. #define CAN_RFS_FF ((uint32_t)(1<<31))
  245. /*********************************************************************//**
  246. * Macro defines for CAN Receive Identifier Register
  247. **********************************************************************/
  248. /** CAN 11 bit Identifier */
  249. #define CAN_RID_ID_11(n) ((uint32_t)(n&0x7FF))
  250. /** CAN 29 bit Identifier */
  251. #define CAN_RID_ID_29(n) ((uint32_t)(n&0x1FFFFFFF))
  252. /*********************************************************************//**
  253. * Macro defines for CAN Receive Data A Register
  254. **********************************************************************/
  255. /** CAN Receive Data 1 */
  256. #define CAN_RDA_DATA1(n) ((uint32_t)(n&0xFF))
  257. /** CAN Receive Data 2 */
  258. #define CAN_RDA_DATA2(n) ((uint32_t)((n&0xFF)<<8))
  259. /** CAN Receive Data 3 */
  260. #define CAN_RDA_DATA3(n) ((uint32_t)((n&0xFF)<<16))
  261. /** CAN Receive Data 4 */
  262. #define CAN_RDA_DATA4(n) ((uint32_t)((n&0xFF)<<24))
  263. /*********************************************************************//**
  264. * Macro defines for CAN Receive Data B Register
  265. **********************************************************************/
  266. /** CAN Receive Data 5 */
  267. #define CAN_RDB_DATA5(n) ((uint32_t)(n&0xFF))
  268. /** CAN Receive Data 6 */
  269. #define CAN_RDB_DATA6(n) ((uint32_t)((n&0xFF)<<8))
  270. /** CAN Receive Data 7 */
  271. #define CAN_RDB_DATA7(n) ((uint32_t)((n&0xFF)<<16))
  272. /** CAN Receive Data 8 */
  273. #define CAN_RDB_DATA8(n) ((uint32_t)((n&0xFF)<<24))
  274. /*********************************************************************//**
  275. * Macro defines for CAN Transmit Frame Information Register
  276. **********************************************************************/
  277. /** CAN Priority */
  278. #define CAN_TFI_PRIO(n) ((uint32_t)(n&0xFF))
  279. /** CAN Data Length Code */
  280. #define CAN_TFI_DLC(n) ((uint32_t)((n&0xF)<<16))
  281. /** CAN Remote Frame Transmission */
  282. #define CAN_TFI_RTR ((uint32_t)(1<<30))
  283. /** CAN control 11-bit or 29-bit Identifier */
  284. #define CAN_TFI_FF ((uint32_t)(1<<31))
  285. /*********************************************************************//**
  286. * Macro defines for CAN Transmit Identifier Register
  287. **********************************************************************/
  288. /** CAN 11-bit Identifier */
  289. #define CAN_TID_ID11(n) ((uint32_t)(n&0x7FF))
  290. /** CAN 11-bit Identifier */
  291. #define CAN_TID_ID29(n) ((uint32_t)(n&0x1FFFFFFF))
  292. /*********************************************************************//**
  293. * Macro defines for CAN Transmit Data A Register
  294. **********************************************************************/
  295. /** CAN Transmit Data 1 */
  296. #define CAN_TDA_DATA1(n) ((uint32_t)(n&0xFF))
  297. /** CAN Transmit Data 2 */
  298. #define CAN_TDA_DATA2(n) ((uint32_t)((n&0xFF)<<8))
  299. /** CAN Transmit Data 3 */
  300. #define CAN_TDA_DATA3(n) ((uint32_t)((n&0xFF)<<16))
  301. /** CAN Transmit Data 4 */
  302. #define CAN_TDA_DATA4(n) ((uint32_t)((n&0xFF)<<24))
  303. /*********************************************************************//**
  304. * Macro defines for CAN Transmit Data B Register
  305. **********************************************************************/
  306. /** CAN Transmit Data 5 */
  307. #define CAN_TDA_DATA5(n) ((uint32_t)(n&0xFF))
  308. /** CAN Transmit Data 6 */
  309. #define CAN_TDA_DATA6(n) ((uint32_t)((n&0xFF)<<8))
  310. /** CAN Transmit Data 7 */
  311. #define CAN_TDA_DATA7(n) ((uint32_t)((n&0xFF)<<16))
  312. /** CAN Transmit Data 8 */
  313. #define CAN_TDA_DATA8(n) ((uint32_t)((n&0xFF)<<24))
  314. /*********************************************************************//**
  315. * Macro defines for CAN Sleep Clear Register
  316. **********************************************************************/
  317. /** CAN1 Sleep mode */
  318. #define CAN1SLEEPCLR ((uint32_t)(1<<1))
  319. /** CAN2 Sleep Mode */
  320. #define CAN2SLEEPCLR ((uint32_t)(1<<2))
  321. /*********************************************************************//**
  322. * Macro defines for CAN Wake up Flags Register
  323. **********************************************************************/
  324. /** CAN1 Sleep mode */
  325. #define CAN_WAKEFLAGES_CAN1WAKE ((uint32_t)(1<<1))
  326. /** CAN2 Sleep Mode */
  327. #define CAN_WAKEFLAGES_CAN2WAKE ((uint32_t)(1<<2))
  328. /*********************************************************************//**
  329. * Macro defines for Central transmit Status Register
  330. **********************************************************************/
  331. /** CAN Transmit 1 */
  332. #define CAN_TSR_TS1 ((uint32_t)(1))
  333. /** CAN Transmit 2 */
  334. #define CAN_TSR_TS2 ((uint32_t)(1<<1))
  335. /** CAN Transmit Buffer Status 1 */
  336. #define CAN_TSR_TBS1 ((uint32_t)(1<<8))
  337. /** CAN Transmit Buffer Status 2 */
  338. #define CAN_TSR_TBS2 ((uint32_t)(1<<9))
  339. /** CAN Transmission Complete Status 1 */
  340. #define CAN_TSR_TCS1 ((uint32_t)(1<<16))
  341. /** CAN Transmission Complete Status 2 */
  342. #define CAN_TSR_TCS2 ((uint32_t)(1<<17))
  343. /*********************************************************************//**
  344. * Macro defines for Central Receive Status Register
  345. **********************************************************************/
  346. /** CAN Receive Status 1 */
  347. #define CAN_RSR_RS1 ((uint32_t)(1))
  348. /** CAN Receive Status 1 */
  349. #define CAN_RSR_RS2 ((uint32_t)(1<<1))
  350. /** CAN Receive Buffer Status 1*/
  351. #define CAN_RSR_RB1 ((uint32_t)(1<<8))
  352. /** CAN Receive Buffer Status 2*/
  353. #define CAN_RSR_RB2 ((uint32_t)(1<<9))
  354. /** CAN Data Overrun Status 1 */
  355. #define CAN_RSR_DOS1 ((uint32_t)(1<<16))
  356. /** CAN Data Overrun Status 1 */
  357. #define CAN_RSR_DOS2 ((uint32_t)(1<<17))
  358. /*********************************************************************//**
  359. * Macro defines for Central Miscellaneous Status Register
  360. **********************************************************************/
  361. /** Same CAN Error Status in CAN1GSR */
  362. #define CAN_MSR_E1 ((uint32_t)(1))
  363. /** Same CAN Error Status in CAN2GSR */
  364. #define CAN_MSR_E2 ((uint32_t)(1<<1))
  365. /** Same CAN Bus Status in CAN1GSR */
  366. #define CAN_MSR_BS1 ((uint32_t)(1<<8))
  367. /** Same CAN Bus Status in CAN2GSR */
  368. #define CAN_MSR_BS2 ((uint32_t)(1<<9))
  369. /*********************************************************************//**
  370. * Macro defines for Acceptance Filter Mode Register
  371. **********************************************************************/
  372. /** CAN Acceptance Filter Off mode */
  373. #define CAN_AFMR_AccOff ((uint32_t)(1))
  374. /** CAN Acceptance File Bypass mode */
  375. #define CAN_AFMR_AccBP ((uint32_t)(1<<1))
  376. /** FullCAN Mode Enhancements */
  377. #define CAN_AFMR_eFCAN ((uint32_t)(1<<2))
  378. /*********************************************************************//**
  379. * Macro defines for Standard Frame Individual Start Address Register
  380. **********************************************************************/
  381. /** The start address of the table of individual Standard Identifier */
  382. #define CAN_STT_sa(n) ((uint32_t)((n&1FF)<<2))
  383. /*********************************************************************//**
  384. * Macro defines for Standard Frame Group Start Address Register
  385. **********************************************************************/
  386. /** The start address of the table of grouped Standard Identifier */
  387. #define CAN_SFF_GRP_sa(n) ((uint32_t)((n&3FF)<<2))
  388. /*********************************************************************//**
  389. * Macro defines for Extended Frame Start Address Register
  390. **********************************************************************/
  391. /** The start address of the table of individual Extended Identifier */
  392. #define CAN_EFF_sa(n) ((uint32_t)((n&1FF)<<2))
  393. /*********************************************************************//**
  394. * Macro defines for Extended Frame Group Start Address Register
  395. **********************************************************************/
  396. /** The start address of the table of grouped Extended Identifier */
  397. #define CAN_Eff_GRP_sa(n) ((uint32_t)((n&3FF)<<2))
  398. /*********************************************************************//**
  399. * Macro defines for End Of AF Table Register
  400. **********************************************************************/
  401. /** The End of Table of AF LookUp Table */
  402. #define CAN_EndofTable(n) ((uint32_t)((n&3FF)<<2))
  403. /*********************************************************************//**
  404. * Macro defines for LUT Error Address Register
  405. **********************************************************************/
  406. /** CAN Look-Up Table Error Address */
  407. #define CAN_LUTerrAd(n) ((uint32_t)((n&1FF)<<2))
  408. /*********************************************************************//**
  409. * Macro defines for LUT Error Register
  410. **********************************************************************/
  411. /** CAN Look-Up Table Error */
  412. #define CAN_LUTerr ((uint32_t)(1))
  413. /*********************************************************************//**
  414. * Macro defines for Global FullCANInterrupt Enable Register
  415. **********************************************************************/
  416. /** Global FullCANInterrupt Enable */
  417. #define CAN_FCANIE ((uint32_t)(1))
  418. /*********************************************************************//**
  419. * Macro defines for FullCAN Interrupt and Capture Register 0
  420. **********************************************************************/
  421. /** FullCAN Interrupt and Capture (0-31)*/
  422. #define CAN_FCANIC0_IntPnd(n) ((uint32_t)(1<<n))
  423. /*********************************************************************//**
  424. * Macro defines for FullCAN Interrupt and Capture Register 1
  425. **********************************************************************/
  426. /** FullCAN Interrupt and Capture (0-31)*/
  427. #define CAN_FCANIC1_IntPnd(n) ((uint32_t)(1<<(n-32)))
  428. /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
  429. /** Macro to determine if it is valid CAN peripheral or not */
  430. #define PARAM_CANx(x) ((((uint32_t*)x)==((uint32_t *)LPC_CAN1)) \
  431. ||(((uint32_t*)x)==((uint32_t *)LPC_CAN2)))
  432. /* Macro to determine if it is valid CANAF or not*/
  433. #define PARAM_CANAFx(x) (((uint32_t*)x)== ((uint32_t*)LPC_CANAF))
  434. /* Macro to determine if it is valid CANAF RAM or not*/
  435. #define PARAM_CANAFRAMx(x) (((uint32_t*)x)== (uint32_t*)LPC_CANAF_RAM)
  436. /* Macro to determine if it is valid CANCR or not*/
  437. #define PARAM_CANCRx(x) (((uint32_t*)x)==((uint32_t*)LPC_CANCR))
  438. /** Macro to check Data to send valid */
  439. #define PARAM_I2S_DATA(data) ((data>=0)&&(data <= 0xFFFFFFFF))
  440. /** Macro to check frequency value */
  441. #define PRAM_I2S_FREQ(freq) ((freq>=16000)&&(freq <= 96000))
  442. /** Macro to check Frame Identifier */
  443. #define PARAM_ID_11(n) ((n>>11)==0) /*-- 11 bit --*/
  444. #define PARAM_ID_29(n) ((n>>29)==0) /*-- 29 bit --*/
  445. /** Macro to check DLC value */
  446. #define PARAM_DLC(n) ((n>>4)==0) /*-- 4 bit --*/
  447. /** Macro to check ID format type */
  448. #define PARAM_ID_FORMAT(n) ((n==STD_ID_FORMAT)||(n==EXT_ID_FORMAT))
  449. /** Macro to check Group identifier */
  450. #define PARAM_GRP_ID(x, y) ((x<=y))
  451. /** Macro to check Frame type */
  452. #define PARAM_FRAME_TYPE(n) ((n==DATA_FRAME)||(n==REMOTE_FRAME))
  453. /** Macro to check Control/Central Status type parameter */
  454. #define PARAM_CTRL_STS_TYPE(n) ((n==CANCTRL_GLOBAL_STS)||(n==CANCTRL_INT_CAP) \
  455. ||(n==CANCTRL_ERR_WRN)||(n==CANCTRL_STS))
  456. /** Macro to check CR status type */
  457. #define PARAM_CR_STS_TYPE(n) ((n==CANCR_TX_STS)||(n==CANCR_RX_STS) \
  458. ||(n==CANCR_MS))
  459. /** Macro to check AF Mode type parameter */
  460. #define PARAM_AFMODE_TYPE(n) ((n==CAN_NORMAL)||(n==CAN_ACC_OFF) \
  461. ||(n==CAN_ACC_BP)||(n==CAN_EFCAN))
  462. /** Macro to check Operation Mode */
  463. #define PARAM_MODE_TYPE(n) ((n==CAN_OPERATING_MODE)||(n==CAN_RESET_MODE) \
  464. ||(n==CAN_LISTENONLY_MODE)||(n==CAN_SELFTEST_MODE) \
  465. ||(n==CAN_TXPRIORITY_MODE)||(n==CAN_SLEEP_MODE) \
  466. ||(n==CAN_RXPOLARITY_MODE)||(n==CAN_TEST_MODE))
  467. /** Macro define for struct AF_Section parameter */
  468. #define PARAM_CTRL(n) ((n==CAN1_CTRL)|(n==CAN2_CTRL))
  469. /** Macro define for struct AF_Section parameter */
  470. #define PARAM_MSG_DISABLE(n) ((n==MSG_ENABLE)|(n==MSG_DISABLE))
  471. /**Macro to check Interrupt Type parameter */
  472. #define PARAM_INT_EN_TYPE(n) ((n==CANINT_RIE)||(n==CANINT_TIE1) \
  473. ||(n==CANINT_EIE)||(n==CANINT_DOIE) \
  474. ||(n==CANINT_WUIE)||(n==CANINT_EPIE) \
  475. ||(n==CANINT_ALIE)||(n==CANINT_BEIE) \
  476. ||(n==CANINT_IDIE)||(n==CANINT_TIE2) \
  477. ||(n==CANINT_TIE3)||(n==CANINT_FCE))
  478. /** Macro to check AFLUT Entry type */
  479. #define PARAM_AFLUT_ENTRY_TYPE(n) ((n==FULLCAN_ENTRY)||(n==EXPLICIT_STANDARD_ENTRY)\
  480. ||(n==GROUP_STANDARD_ENTRY)||(n==EXPLICIT_EXTEND_ENTRY) \
  481. ||(n==GROUP_EXTEND_ENTRY))
  482. /** Macro to check position */
  483. #define PARAM_POSITION(n) ((n>=0)&&(n<512))
  484. /**
  485. * @}
  486. */
  487. /* Public Types --------------------------------------------------------------- */
  488. /** @defgroup CAN_Public_Types CAN Public Types
  489. * @{
  490. */
  491. /***********************************************************************
  492. * CAN device configuration commands (IOCTL commands and arguments)
  493. **********************************************************************/
  494. /** CAN peripheral ID 0 */
  495. #define CAN_1 0
  496. /** CAN peripheral ID 1 */
  497. #define CAN_2 1
  498. /**
  499. * @brief CAN peripheral ID no
  500. */
  501. typedef enum
  502. {
  503. CAN_ID_1 = CAN_1,
  504. CAN_ID_2 = CAN_2
  505. } en_CAN_unitId;
  506. /**
  507. * @brief CAN ID format definition
  508. */
  509. typedef enum
  510. {
  511. STD_ID_FORMAT = 0, /**< Use standard ID format (11 bit ID) */
  512. EXT_ID_FORMAT = 1 /**< Use extended ID format (29 bit ID) */
  513. } CAN_ID_FORMAT_Type;
  514. /**
  515. * @brief AFLUT Entry type definition
  516. */
  517. typedef enum
  518. {
  519. FULLCAN_ENTRY = 0,
  520. EXPLICIT_STANDARD_ENTRY,
  521. GROUP_STANDARD_ENTRY,
  522. EXPLICIT_EXTEND_ENTRY,
  523. GROUP_EXTEND_ENTRY,
  524. } AFLUT_ENTRY_Type;
  525. /**
  526. * @brief Symbolic names for type of CAN message
  527. */
  528. typedef enum
  529. {
  530. DATA_FRAME = 0, /**< Data frame */
  531. REMOTE_FRAME = 1 /**< Remote frame */
  532. } CAN_FRAME_Type;
  533. /**
  534. * @brief CAN Control status definition
  535. */
  536. typedef enum
  537. {
  538. CANCTRL_GLOBAL_STS = 0, /**< CAN Global Status */
  539. CANCTRL_INT_CAP, /**< CAN Interrupt and Capture */
  540. CANCTRL_ERR_WRN, /**< CAN Error Warning Limit */
  541. CANCTRL_STS /**< CAN Control Status */
  542. } CAN_CTRL_STS_Type;
  543. /**
  544. * @brief Central CAN status type definition
  545. */
  546. typedef enum
  547. {
  548. CANCR_TX_STS = 0, /**< Central CAN Tx Status */
  549. CANCR_RX_STS, /**< Central CAN Rx Status */
  550. CANCR_MS /**< Central CAN Miscellaneous Status */
  551. } CAN_CR_STS_Type;
  552. /**
  553. * @brief FullCAN Interrupt Capture type definition
  554. */
  555. typedef enum
  556. {
  557. FULLCAN_IC0, /**< FullCAN Interrupt and Capture 0 */
  558. FULLCAN_IC1 /**< FullCAN Interrupt and Capture 1 */
  559. }FullCAN_IC_Type;
  560. /**
  561. * @brief CAN interrupt enable type definition
  562. */
  563. typedef enum
  564. {
  565. CANINT_RIE = 0, /**< CAN Receiver Interrupt Enable */
  566. CANINT_TIE1, /**< CAN Transmit Interrupt Enable */
  567. CANINT_EIE, /**< CAN Error Warning Interrupt Enable */
  568. CANINT_DOIE, /**< CAN Data Overrun Interrupt Enable */
  569. CANINT_WUIE, /**< CAN Wake-Up Interrupt Enable */
  570. CANINT_EPIE, /**< CAN Error Passive Interrupt Enable */
  571. CANINT_ALIE, /**< CAN Arbitration Lost Interrupt Enable */
  572. CANINT_BEIE, /**< CAN Bus Error Inter rupt Enable */
  573. CANINT_IDIE, /**< CAN ID Ready Interrupt Enable */
  574. CANINT_TIE2, /**< CAN Transmit Interrupt Enable for Buffer2 */
  575. CANINT_TIE3, /**< CAN Transmit Interrupt Enable for Buffer3 */
  576. CANINT_FCE /**< FullCAN Interrupt Enable */
  577. } CAN_INT_EN_Type;
  578. /**
  579. * @brief Acceptance Filter Mode type definition
  580. */
  581. typedef enum
  582. {
  583. CAN_NORMAL = 0, /**< Normal Mode */
  584. CAN_ACC_OFF, /**< Acceptance Filter Off Mode */
  585. CAN_ACC_BP, /**< Acceptance Fileter Bypass Mode */
  586. CAN_EFCAN /**< FullCAN Mode Enhancement */
  587. } CAN_AFMODE_Type;
  588. /**
  589. * @brief CAN Mode Type definition
  590. */
  591. typedef enum
  592. {
  593. CAN_OPERATING_MODE = 0, /**< Operating Mode */
  594. CAN_RESET_MODE, /**< Reset Mode */
  595. CAN_LISTENONLY_MODE, /**< Listen Only Mode */
  596. CAN_SELFTEST_MODE, /**< Seft Test Mode */
  597. CAN_TXPRIORITY_MODE, /**< Transmit Priority Mode */
  598. CAN_SLEEP_MODE, /**< Sleep Mode */
  599. CAN_RXPOLARITY_MODE, /**< Receive Polarity Mode */
  600. CAN_TEST_MODE /**< Test Mode */
  601. } CAN_MODE_Type;
  602. /**
  603. * @brief Error values that functions can return
  604. */
  605. typedef enum
  606. {
  607. CAN_OK = 1, /**< No error */
  608. CAN_OBJECTS_FULL_ERROR, /**< No more rx or tx objects available */
  609. CAN_FULL_OBJ_NOT_RCV, /**< Full CAN object not received */
  610. CAN_NO_RECEIVE_DATA, /**< No have receive data available */
  611. CAN_AF_ENTRY_ERROR, /**< Entry load in AFLUT is unvalid */
  612. CAN_CONFLICT_ID_ERROR, /**< Conflict ID occur */
  613. CAN_ENTRY_NOT_EXIT_ERROR /**< Entry remove outo AFLUT is not exit */
  614. } CAN_ERROR;
  615. /**
  616. * @brief Pin Configuration structure
  617. */
  618. typedef struct
  619. {
  620. uint8_t RD; /**< Serial Inputs, from CAN transceivers, should be:
  621. ** For CAN1:
  622. - CAN_RD1_P0_0: RD pin is on P0.0
  623. - CAN_RD1_P0_21 : RD pin is on P0.21
  624. ** For CAN2:
  625. - CAN_RD2_P0_4: RD pin is on P0.4
  626. - CAN_RD2_P2_7: RD pin is on P2.7
  627. */
  628. uint8_t TD; /**< Serial Outputs, To CAN transceivers, should be:
  629. ** For CAN1:
  630. - CAN_TD1_P0_1: TD pin is on P0.1
  631. - CAN_TD1_P0_22: TD pin is on P0.22
  632. ** For CAN2:
  633. - CAN_TD2_P0_5: TD pin is on P0.5
  634. - CAN_TD2_P2_8: TD pin is on P2.8
  635. */
  636. } CAN_PinCFG_Type;
  637. /**
  638. * @brief CAN message object structure
  639. */
  640. typedef struct
  641. {
  642. uint32_t id; /**< 29 bit identifier, it depend on "format" value
  643. - if format = STD_ID_FORMAT, id should be 11 bit identifier
  644. - if format = EXT_ID_FORMAT, id should be 29 bit identifier
  645. */
  646. uint8_t dataA[4]; /**< Data field A */
  647. uint8_t dataB[4]; /**< Data field B */
  648. uint8_t len; /**< Length of data field in bytes, should be:
  649. - 0000b-0111b: 0-7 bytes
  650. - 1xxxb: 8 bytes
  651. */
  652. uint8_t format; /**< Identifier Format, should be:
  653. - STD_ID_FORMAT: Standard ID - 11 bit format
  654. - EXT_ID_FORMAT: Extended ID - 29 bit format
  655. */
  656. uint8_t type; /**< Remote Frame transmission, should be:
  657. - DATA_FRAME: the number of data bytes called out by the DLC
  658. field are send from the CANxTDA and CANxTDB registers
  659. - REMOTE_FRAME: Remote Frame is sent
  660. */
  661. } CAN_MSG_Type;
  662. /**
  663. * @brief FullCAN Entry structure
  664. */
  665. typedef struct
  666. {
  667. uint8_t controller; /**< CAN Controller, should be:
  668. - CAN1_CTRL: CAN1 Controller
  669. - CAN2_CTRL: CAN2 Controller
  670. */
  671. uint8_t disable; /**< Disable bit, should be:
  672. - MSG_ENABLE: disable bit = 0
  673. - MSG_DISABLE: disable bit = 1
  674. */
  675. uint16_t id_11; /**< Standard ID, should be 11-bit value */
  676. } FullCAN_Entry;
  677. /**
  678. * @brief Standard ID Frame Format Entry structure
  679. */
  680. typedef struct
  681. {
  682. uint8_t controller; /**< CAN Controller, should be:
  683. - CAN1_CTRL: CAN1 Controller
  684. - CAN2_CTRL: CAN2 Controller
  685. */
  686. uint8_t disable; /**< Disable bit, should be:
  687. - MSG_ENABLE: disable bit = 0
  688. - MSG_DISABLE: disable bit = 1
  689. */
  690. uint16_t id_11; /**< Standard ID, should be 11-bit value */
  691. } SFF_Entry;
  692. /**
  693. * @brief Group of Standard ID Frame Format Entry structure
  694. */
  695. typedef struct
  696. {
  697. uint8_t controller1; /**< First CAN Controller, should be:
  698. - CAN1_CTRL: CAN1 Controller
  699. - CAN2_CTRL: CAN2 Controller
  700. */
  701. uint8_t disable1; /**< First Disable bit, should be:
  702. - MSG_ENABLE: disable bit = 0)
  703. - MSG_DISABLE: disable bit = 1
  704. */
  705. uint16_t lowerID; /**< ID lower bound, should be 11-bit value */
  706. uint8_t controller2; /**< Second CAN Controller, should be:
  707. - CAN1_CTRL: CAN1 Controller
  708. - CAN2_CTRL: CAN2 Controller
  709. */
  710. uint8_t disable2; /**< Second Disable bit, should be:
  711. - MSG_ENABLE: disable bit = 0
  712. - MSG_DISABLE: disable bit = 1
  713. */
  714. uint16_t upperID; /**< ID upper bound, should be 11-bit value and
  715. equal or greater than lowerID
  716. */
  717. } SFF_GPR_Entry;
  718. /**
  719. * @brief Extended ID Frame Format Entry structure
  720. */
  721. typedef struct
  722. {
  723. uint8_t controller; /**< CAN Controller, should be:
  724. - CAN1_CTRL: CAN1 Controller
  725. - CAN2_CTRL: CAN2 Controller
  726. */
  727. uint32_t ID_29; /**< Extend ID, shoud be 29-bit value */
  728. } EFF_Entry;
  729. /**
  730. * @brief Group of Extended ID Frame Format Entry structure
  731. */
  732. typedef struct
  733. {
  734. uint8_t controller1; /**< First CAN Controller, should be:
  735. - CAN1_CTRL: CAN1 Controller
  736. - CAN2_CTRL: CAN2 Controller
  737. */
  738. uint8_t controller2; /**< Second Disable bit, should be:
  739. - MSG_ENABLE: disable bit = 0(default)
  740. - MSG_DISABLE: disable bit = 1
  741. */
  742. uint32_t lowerEID; /**< Extended ID lower bound, should be 29-bit value */
  743. uint32_t upperEID; /**< Extended ID upper bound, should be 29-bit value */
  744. } EFF_GPR_Entry;
  745. /**
  746. * @brief Acceptance Filter Section Table structure
  747. */
  748. typedef struct
  749. {
  750. FullCAN_Entry* FullCAN_Sec; /**< The pointer point to FullCAN_Entry */
  751. uint8_t FC_NumEntry; /**< FullCAN Entry Number */
  752. SFF_Entry* SFF_Sec; /**< The pointer point to SFF_Entry */
  753. uint8_t SFF_NumEntry; /**< Standard ID Entry Number */
  754. SFF_GPR_Entry* SFF_GPR_Sec; /**< The pointer point to SFF_GPR_Entry */
  755. uint8_t SFF_GPR_NumEntry; /**< Group Standard ID Entry Number */
  756. EFF_Entry* EFF_Sec; /**< The pointer point to EFF_Entry */
  757. uint8_t EFF_NumEntry; /**< Extended ID Entry Number */
  758. EFF_GPR_Entry* EFF_GPR_Sec; /**< The pointer point to EFF_GPR_Entry */
  759. uint8_t EFF_GPR_NumEntry; /**< Group Extended ID Entry Number */
  760. } AF_SectionDef;
  761. /**
  762. * @}
  763. */
  764. /* Public Functions ----------------------------------------------------------- */
  765. /** @defgroup CAN_Public_Functions CAN Public Functions
  766. * @{
  767. */
  768. /* Init/DeInit CAN peripheral -----------*/
  769. void CAN_Init(uint8_t canId, uint32_t baudrate);
  770. void CAN_DeInit(uint8_t canId);
  771. /* CAN messages functions ---------------*/
  772. Status CAN_SendMsg(uint8_t canId, CAN_MSG_Type *CAN_Msg);
  773. Status CAN_ReceiveMsg(uint8_t canId, CAN_MSG_Type *CAN_Msg);
  774. CAN_ERROR FCAN_ReadObj(CAN_MSG_Type *CAN_Msg);
  775. /* CAN configure functions ---------------*/
  776. void CAN_ModeConfig(uint8_t canId, CAN_MODE_Type mode,
  777. FunctionalState NewState);
  778. void CAN_SetAFMode(CAN_AFMODE_Type AFmode);
  779. void CAN_SetCommand(uint8_t canId, uint32_t CMRType);
  780. /* AFLUT functions ---------------------- */
  781. CAN_ERROR CAN_SetupAFLUT(AF_SectionDef* AFSection);
  782. CAN_ERROR CAN_LoadFullCANEntry(uint8_t canId, uint16_t ID);
  783. CAN_ERROR CAN_LoadExplicitEntry(uint8_t canId, uint32_t ID,
  784. CAN_ID_FORMAT_Type format);
  785. CAN_ERROR CAN_LoadGroupEntry(uint8_t canId, uint32_t lowerID,
  786. uint32_t upperID, CAN_ID_FORMAT_Type format);
  787. CAN_ERROR CAN_RemoveEntry(AFLUT_ENTRY_Type EntryType, uint16_t position);
  788. /* CAN interrupt functions -----------------*/
  789. void CAN_IRQCmd(uint8_t canId, CAN_INT_EN_Type arg, FunctionalState NewState);
  790. uint32_t CAN_IntGetStatus(uint8_t canId);
  791. /* CAN get status functions ----------------*/
  792. IntStatus CAN_FullCANIntGetStatus (void);
  793. uint32_t CAN_FullCANPendGetStatus (FullCAN_IC_Type type);
  794. uint32_t CAN_GetCTRLStatus(uint8_t canId, CAN_CTRL_STS_Type arg);
  795. uint32_t CAN_GetCRStatus(CAN_CR_STS_Type arg);
  796. /**
  797. * @}
  798. */
  799. #ifdef __cplusplus
  800. }
  801. #endif
  802. #endif /* LPC_CAN_H_ */
  803. /**
  804. * @}
  805. */
  806. /* --------------------------------- End Of File ------------------------------ */