lpc_can.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. /**********************************************************************
  2. * $Id$ lpc_can.c 2011-06-02
  3. *//**
  4. * @file lpc_can.c
  5. * @brief Contains all functions support for CAN firmware library on
  6. * 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. /** @addtogroup CAN
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _CAN
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_can.h"
  44. #include "lpc_clkpwr.h"
  45. /* Private Variables ---------------------------------------------------------- */
  46. /** @defgroup CAN_Private_Variables CAN Private Variables
  47. * @{
  48. */
  49. FunctionalState FULLCAN_ENABLE;
  50. /* Counts number of filters (CAN message objects) used */
  51. uint16_t CANAF_FullCAN_cnt = 0;
  52. uint16_t CANAF_std_cnt = 0;
  53. uint16_t CANAF_gstd_cnt = 0;
  54. uint16_t CANAF_ext_cnt = 0;
  55. uint16_t CANAF_gext_cnt = 0;
  56. /* End of Private Variables ----------------------------------------------------*/
  57. /**
  58. * @}
  59. */
  60. /* Private Variables ---------------------------------------------------------- */
  61. static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId);
  62. static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate);
  63. /*********************************************************************//**
  64. * @brief Setting CAN baud rate (bps)
  65. * @param[in] canId point to LPC_CAN_TypeDef object, should be:
  66. * - LPC_CAN1: CAN1 peripheral
  67. * - LPC_CAN2: CAN2 peripheral
  68. * @return The pointer to CAN peripheral that's expected to use
  69. ***********************************************************************/
  70. static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId)
  71. {
  72. LPC_CAN_TypeDef* pCan;
  73. switch (canId)
  74. {
  75. case CAN_ID_1:
  76. pCan = LPC_CAN1;
  77. break;
  78. case CAN_ID_2:
  79. pCan = LPC_CAN2;
  80. break;
  81. default:
  82. pCan = NULL;
  83. break;
  84. }
  85. return pCan;
  86. }
  87. /*********************************************************************//**
  88. * @brief Setting CAN baud rate (bps)
  89. * @param[in] CANx point to LPC_CAN_TypeDef object, should be:
  90. * - LPC_CAN1: CAN1 peripheral
  91. * - LPC_CAN2: CAN2 peripheral
  92. * @param[in] baudrate: is the baud rate value will be set
  93. * @return None
  94. ***********************************************************************/
  95. static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate)
  96. {
  97. uint32_t result = 0;
  98. uint8_t NT, TSEG1, TSEG2;
  99. uint32_t CANPclk = 0;
  100. uint32_t BRP;
  101. CANPclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  102. result = CANPclk / baudrate;
  103. /* Calculate suitable nominal time value
  104. * NT (nominal time) = (TSEG1 + TSEG2 + 3)
  105. * NT <= 24
  106. * TSEG1 >= 2*TSEG2
  107. */
  108. for(NT = 24; NT > 0; NT = NT-2)
  109. {
  110. if ((result%NT) == 0)
  111. {
  112. BRP = result / NT - 1;
  113. NT--;
  114. TSEG2 = (NT/3) - 1;
  115. TSEG1 = NT -(NT/3) - 1;
  116. break;
  117. }
  118. }
  119. /* Enter reset mode */
  120. CANx->MOD = 0x01;
  121. /* Set bit timing
  122. * Default: SAM = 0x00;
  123. * SJW = 0x03;
  124. */
  125. CANx->BTR = (TSEG2 << 20) | (TSEG1 << 16) | (3 << 14) | BRP;
  126. /* Return to normal operating */
  127. CANx->MOD = 0;
  128. }
  129. /* End of Private Functions ----------------------------------------------------*/
  130. /* Public Functions ----------------------------------------------------------- */
  131. /** @addtogroup CAN_Public_Functions
  132. * @{
  133. */
  134. /********************************************************************//**
  135. * @brief Initialize CAN peripheral with given baudrate
  136. * @param[in] canId The Id of the expected CAN component
  137. *
  138. * @param[in] baudrate: the value of CAN baudrate will be set (bps)
  139. * @return None
  140. *********************************************************************/
  141. void CAN_Init(uint8_t canId, uint32_t baudrate)
  142. {
  143. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  144. uint16_t i;
  145. if(canId == CAN_ID_1)
  146. {
  147. /* Turn on power and clock for CAN1 */
  148. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
  149. }
  150. else if(canId == CAN_ID_2)
  151. {
  152. /* Turn on power and clock for CAN2 */
  153. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
  154. }
  155. else
  156. {
  157. return;
  158. }
  159. pCan->MOD = 1; // Enter Reset Mode
  160. pCan->IER = 0; // Disable All CAN Interrupts
  161. pCan->GSR = 0;
  162. /* Request command to release Rx, Tx buffer and clear data overrun */
  163. //pCan->CMR = CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO;
  164. pCan->CMR = (1 << 1) | (1 << 2) | (1 << 3);
  165. /* Read to clear interrupt pending in interrupt capture register */
  166. i = pCan->ICR;
  167. pCan->MOD = 0;// Return Normal operating
  168. //Reset CANAF value
  169. LPC_CANAF->AFMR = 0x01;
  170. //clear ALUT RAM
  171. for (i = 0; i < 512; i++)
  172. {
  173. LPC_CANAF_RAM->mask[i] = 0x00;
  174. }
  175. LPC_CANAF->SFF_sa = 0x00;
  176. LPC_CANAF->SFF_GRP_sa = 0x00;
  177. LPC_CANAF->EFF_sa = 0x00;
  178. LPC_CANAF->EFF_GRP_sa = 0x00;
  179. LPC_CANAF->ENDofTable = 0x00;
  180. LPC_CANAF->AFMR = 0x00;
  181. /* Set baudrate */
  182. can_SetBaudrate (pCan, baudrate);
  183. }
  184. /********************************************************************//**
  185. * @brief CAN deInit
  186. * @param[in] canId The Id of the expected CAN component
  187. *
  188. * @return None
  189. *********************************************************************/
  190. void CAN_DeInit(uint8_t canId)
  191. {
  192. if(canId == CAN_ID_1)
  193. {
  194. /* Turn on power and clock for CAN1 */
  195. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, DISABLE);
  196. }
  197. else if(canId == CAN_ID_2)
  198. {
  199. /* Turn on power and clock for CAN1 */
  200. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, DISABLE);
  201. }
  202. return;
  203. }
  204. /********************************************************************//**
  205. * @brief Setup Acceptance Filter Look-Up Table
  206. * @param[in] CANAFx pointer to LPC_CANAF_TypeDef
  207. * Should be: LPC_CANAF
  208. * @param[in] AFSection the pointer to AF_SectionDef structure
  209. * It contain information about 5 sections will be install in AFLUT
  210. * @return CAN Error could be:
  211. * - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
  212. * - CAN_AF_ENTRY_ERROR: table error-violation of ascending numerical order
  213. * - CAN_OK: ID is added into table successfully
  214. *********************************************************************/
  215. CAN_ERROR CAN_SetupAFLUT(AF_SectionDef* AFSection)
  216. {
  217. uint8_t ctrl1,ctrl2;
  218. uint8_t dis1, dis2;
  219. uint16_t SID, ID_temp,i, count = 0;
  220. uint32_t EID, entry, buf;
  221. uint16_t lowerSID, upperSID;
  222. uint32_t lowerEID, upperEID;
  223. LPC_CANAF->AFMR = 0x01;
  224. /***** setup FullCAN Table *****/
  225. if(AFSection->FullCAN_Sec == NULL)
  226. {
  227. FULLCAN_ENABLE = DISABLE;
  228. }
  229. else
  230. {
  231. FULLCAN_ENABLE = ENABLE;
  232. for(i = 0; i < (AFSection->FC_NumEntry); i++)
  233. {
  234. if(count + 1 > 64)
  235. {
  236. return CAN_OBJECTS_FULL_ERROR;
  237. }
  238. ctrl1 = AFSection->FullCAN_Sec->controller;
  239. SID = AFSection->FullCAN_Sec->id_11;
  240. dis1 = AFSection->FullCAN_Sec->disable;
  241. entry = 0x00; //reset entry value
  242. if((CANAF_FullCAN_cnt & 0x00000001)==0)
  243. {
  244. if(count != 0x00)
  245. {
  246. buf = LPC_CANAF_RAM->mask[count-1];
  247. ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
  248. if(ID_temp > ((ctrl1<<13)|SID))
  249. {
  250. return CAN_AF_ENTRY_ERROR;
  251. }
  252. }
  253. entry = (ctrl1<<29)|(dis1<<28)|(SID<<16)|(1<<27);
  254. LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;
  255. LPC_CANAF_RAM->mask[count] |= entry;
  256. CANAF_FullCAN_cnt++;
  257. if(CANAF_FullCAN_cnt == AFSection->FC_NumEntry) //this is the lastest FullCAN entry
  258. count++;
  259. }
  260. else
  261. {
  262. buf = LPC_CANAF_RAM->mask[count];
  263. ID_temp = (buf >>16) & 0xE7FF;
  264. if(ID_temp > ((ctrl1<<13)|SID))
  265. {
  266. return CAN_AF_ENTRY_ERROR;
  267. }
  268. entry = (ctrl1 << 13) | (dis1 << 12) | (SID << 0) | (1 << 11);
  269. LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;
  270. LPC_CANAF_RAM->mask[count]|= entry;
  271. count++;
  272. CANAF_FullCAN_cnt++;
  273. }
  274. AFSection->FullCAN_Sec = (FullCAN_Entry *)((uint32_t)(AFSection->FullCAN_Sec)+ sizeof(FullCAN_Entry));
  275. }
  276. }
  277. /***** Setup Explicit Standard Frame Format Section *****/
  278. if(AFSection->SFF_Sec != NULL)
  279. {
  280. for(i=0;i<(AFSection->SFF_NumEntry);i++)
  281. {
  282. if(count + 1 > 512)
  283. {
  284. return CAN_OBJECTS_FULL_ERROR;
  285. }
  286. ctrl1 = AFSection->SFF_Sec->controller;
  287. SID = AFSection->SFF_Sec->id_11;
  288. dis1 = AFSection->SFF_Sec->disable;
  289. entry = 0x00; //reset entry value
  290. if((CANAF_std_cnt & 0x00000001)==0)
  291. {
  292. if(CANAF_std_cnt !=0 )
  293. {
  294. buf = LPC_CANAF_RAM->mask[count-1];
  295. ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
  296. if(ID_temp > ((ctrl1<<13)|SID))
  297. {
  298. return CAN_AF_ENTRY_ERROR;
  299. }
  300. }
  301. entry = (ctrl1<<29)|(dis1<<28)|(SID<<16);
  302. LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;
  303. LPC_CANAF_RAM->mask[count] |= entry;
  304. CANAF_std_cnt++;
  305. if(CANAF_std_cnt == AFSection->SFF_NumEntry)//if this is the last SFF entry
  306. count++;
  307. }
  308. else
  309. {
  310. buf = LPC_CANAF_RAM->mask[count];
  311. ID_temp = (buf >>16) & 0xE7FF;
  312. if(ID_temp > ((ctrl1<<13)|SID))
  313. {
  314. return CAN_AF_ENTRY_ERROR;
  315. }
  316. entry = (ctrl1 << 13) | (dis1 << 12) | (SID << 0);
  317. LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;
  318. LPC_CANAF_RAM->mask[count] |= entry;
  319. count++;
  320. CANAF_std_cnt++;
  321. }
  322. AFSection->SFF_Sec = (SFF_Entry *)((uint32_t)(AFSection->SFF_Sec)+ sizeof(SFF_Entry));
  323. }
  324. }
  325. /***** Setup Group of Standard Frame Format Identifier Section *****/
  326. if(AFSection->SFF_GPR_Sec != NULL)
  327. {
  328. for(i=0;i<(AFSection->SFF_GPR_NumEntry);i++)
  329. {
  330. if(count + 1 > 512)
  331. {
  332. return CAN_OBJECTS_FULL_ERROR;
  333. }
  334. ctrl1 = AFSection->SFF_GPR_Sec->controller1;
  335. ctrl2 = AFSection->SFF_GPR_Sec->controller2;
  336. dis1 = AFSection->SFF_GPR_Sec->disable1;
  337. dis2 = AFSection->SFF_GPR_Sec->disable2;
  338. lowerSID = AFSection->SFF_GPR_Sec->lowerID;
  339. upperSID = AFSection->SFF_GPR_Sec->upperID;
  340. entry = 0x00;
  341. if(CANAF_gstd_cnt!=0)
  342. {
  343. buf = LPC_CANAF_RAM->mask[count-1];
  344. ID_temp = buf & 0xE7FF;
  345. if((ctrl1 != ctrl2)||(lowerSID > upperSID)||(ID_temp > ((ctrl1<<13)|lowerSID)))
  346. {
  347. return CAN_AF_ENTRY_ERROR;
  348. }
  349. }
  350. entry = (ctrl1 << 29)|(dis1 << 28)|(lowerSID << 16)| \
  351. (ctrl2 << 13)|(dis2 << 12)|(upperSID << 0);
  352. LPC_CANAF_RAM->mask[count] = entry;
  353. CANAF_gstd_cnt++;
  354. count++;
  355. AFSection->SFF_GPR_Sec = (SFF_GPR_Entry *)((uint32_t)(AFSection->SFF_GPR_Sec)+ sizeof(SFF_GPR_Entry));
  356. }
  357. }
  358. /***** Setup Explicit Extend Frame Format Identifier Section *****/
  359. if(AFSection->EFF_Sec != NULL)
  360. {
  361. for(i=0;i<(AFSection->EFF_NumEntry);i++)
  362. {
  363. if(count + 1 > 512)
  364. {
  365. return CAN_OBJECTS_FULL_ERROR;
  366. }
  367. EID = AFSection->EFF_Sec->ID_29;
  368. ctrl1 = AFSection->EFF_Sec->controller;
  369. entry = 0x00; //reset entry value
  370. entry = (ctrl1 << 29)|(EID << 0);
  371. if(CANAF_ext_cnt != 0)
  372. {
  373. buf = LPC_CANAF_RAM->mask[count-1];
  374. // EID_temp = buf & 0x0FFFFFFF;
  375. if(buf > entry)
  376. {
  377. return CAN_AF_ENTRY_ERROR;
  378. }
  379. }
  380. LPC_CANAF_RAM->mask[count] = entry;
  381. CANAF_ext_cnt ++;
  382. count++;
  383. AFSection->EFF_Sec = (EFF_Entry *)((uint32_t)(AFSection->EFF_Sec)+ sizeof(EFF_Entry));
  384. }
  385. }
  386. /***** Setup Group of Extended Frame Format Identifier Section *****/
  387. if(AFSection->EFF_GPR_Sec != NULL)
  388. {
  389. for(i=0;i<(AFSection->EFF_GPR_NumEntry);i++)
  390. {
  391. if(count + 2 > 512)
  392. {
  393. return CAN_OBJECTS_FULL_ERROR;
  394. }
  395. ctrl1 = AFSection->EFF_GPR_Sec->controller1;
  396. ctrl2 = AFSection->EFF_GPR_Sec->controller2;
  397. lowerEID = AFSection->EFF_GPR_Sec->lowerEID;
  398. upperEID = AFSection->EFF_GPR_Sec->upperEID;
  399. entry = 0x00;
  400. if(CANAF_gext_cnt != 0)
  401. {
  402. buf = LPC_CANAF_RAM->mask[count-1];
  403. // EID_temp = buf & 0x0FFFFFFF;
  404. if((ctrl1 != ctrl2) || (lowerEID > upperEID) || (buf > ((ctrl1 << 29)|(lowerEID << 0))))
  405. {
  406. return CAN_AF_ENTRY_ERROR;
  407. }
  408. }
  409. entry = (ctrl1 << 29)|(lowerEID << 0);
  410. LPC_CANAF_RAM->mask[count++] = entry;
  411. entry = (ctrl2 << 29)|(upperEID << 0);
  412. LPC_CANAF_RAM->mask[count++] = entry;
  413. CANAF_gext_cnt++;
  414. AFSection->EFF_GPR_Sec = (EFF_GPR_Entry *)((uint32_t)(AFSection->EFF_GPR_Sec)+ sizeof(EFF_GPR_Entry));
  415. }
  416. }
  417. //update address values
  418. LPC_CANAF->SFF_sa = ((CANAF_FullCAN_cnt + 1)>>1)<<2;
  419. LPC_CANAF->SFF_GRP_sa = LPC_CANAF->SFF_sa + (((CANAF_std_cnt+1)>>1)<< 2);
  420. LPC_CANAF->EFF_sa = LPC_CANAF->SFF_GRP_sa + (CANAF_gstd_cnt << 2);
  421. LPC_CANAF->EFF_GRP_sa = LPC_CANAF->EFF_sa + (CANAF_ext_cnt << 2);
  422. LPC_CANAF->ENDofTable = LPC_CANAF->EFF_GRP_sa + (CANAF_gext_cnt << 3);
  423. if(FULLCAN_ENABLE == DISABLE)
  424. {
  425. LPC_CANAF->AFMR = 0x00; // Normal mode
  426. }
  427. else
  428. {
  429. LPC_CANAF->AFMR = 0x04;
  430. }
  431. return CAN_OK;
  432. }
  433. /********************************************************************//**
  434. * @brief Add Explicit ID into AF Look-Up Table dynamically.
  435. * @param[in] canId The Id of the expected CAN component
  436. *
  437. * @param[in] id: The ID of entry will be added
  438. * @param[in] format: is the type of ID Frame Format, should be:
  439. * - STD_ID_FORMAT: 11-bit ID value
  440. * - EXT_ID_FORMAT: 29-bit ID value
  441. * @return CAN Error, could be:
  442. * - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
  443. * - CAN_ID_EXIT_ERROR: ID exited in table
  444. * - CAN_OK: ID is added into table successfully
  445. *********************************************************************/
  446. CAN_ERROR CAN_LoadExplicitEntry(uint8_t canId, uint32_t id, CAN_ID_FORMAT_Type format)
  447. {
  448. uint32_t buf0 = 0, buf1 = 0;
  449. int16_t cnt1 = 0, cnt2 = 0, bound1 = 0, total = 0;
  450. /* Acceptance Filter Memory full - return */
  451. total =((CANAF_FullCAN_cnt + 1) >> 1) + CANAF_FullCAN_cnt * 3 + ((CANAF_std_cnt + 1) >> 1) \
  452. + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);
  453. if (total >= 512)
  454. {
  455. //don't have enough space
  456. return CAN_OBJECTS_FULL_ERROR;
  457. }
  458. /* Setup Acceptance Filter Configuration
  459. Acceptance Filter Mode Register = Off */
  460. LPC_CANAF->AFMR = 0x00000001;
  461. /*********** Add Explicit Standard Identifier Frame Format entry *********/
  462. if(format == STD_ID_FORMAT)
  463. {
  464. id &= 0x07FF;
  465. id |= canId << 13;/* Add controller number */
  466. /* Move all remaining sections one place up
  467. if new entry will increase FullCAN list */
  468. if ((CANAF_std_cnt & 0x0001) == 0)
  469. {
  470. cnt1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
  471. bound1 = total - cnt1;
  472. buf0 = LPC_CANAF_RAM->mask[cnt1];
  473. while(bound1--)
  474. {
  475. cnt1++;
  476. buf1 = LPC_CANAF_RAM->mask[cnt1];
  477. LPC_CANAF_RAM->mask[cnt1] = buf0;
  478. buf0 = buf1;
  479. }
  480. }
  481. if (CANAF_std_cnt == 0)
  482. {
  483. cnt2 = (CANAF_FullCAN_cnt + 1)>>1;
  484. /* For entering first ID */
  485. LPC_CANAF_RAM->mask[cnt2] = 0x0000FFFF | (id << 16);
  486. }
  487. else if (CANAF_std_cnt == 1)
  488. {
  489. cnt2 = (CANAF_FullCAN_cnt + 1) >> 1;
  490. /* For entering second ID */
  491. if (((LPC_CANAF_RAM->mask[cnt2] >> 16)& 0xE7FF) > id)
  492. {
  493. LPC_CANAF_RAM->mask[cnt2] = (LPC_CANAF_RAM->mask[cnt2] >> 16) | (id << 16);
  494. }
  495. else
  496. {
  497. LPC_CANAF_RAM->mask[cnt2] = (LPC_CANAF_RAM->mask[cnt2] & 0xFFFF0000) | id;
  498. }
  499. }
  500. else
  501. {
  502. /* Find where to insert new ID */
  503. cnt1 = (CANAF_FullCAN_cnt+1)>>1;
  504. cnt2 = CANAF_std_cnt;
  505. bound1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
  506. while (cnt1 < bound1)
  507. {
  508. /* Loop through standard existing IDs */
  509. if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > id)
  510. {
  511. cnt2 = cnt1 * 2;
  512. break;
  513. }
  514. if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > id)
  515. {
  516. cnt2 = cnt1 * 2 + 1;
  517. break;
  518. }
  519. cnt1++;
  520. }
  521. /* cnt1 = U32 where to insert new ID */
  522. /* cnt2 = U16 where to insert new ID */
  523. if (cnt1 == bound1)
  524. {
  525. /* Adding ID as last entry */
  526. /* Even number of IDs exists */
  527. if ((CANAF_std_cnt & 0x0001) == 0)
  528. {
  529. LPC_CANAF_RAM->mask[cnt1] = 0x0000FFFF | (id << 16);
  530. }
  531. /* Odd number of IDs exists */
  532. else
  533. {
  534. LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id;
  535. }
  536. }
  537. else
  538. {
  539. buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */
  540. if ((cnt2 & 0x0001) == 0)
  541. {
  542. /* Insert new mask to even address*/
  543. buf1 = (id << 16) | (buf0 >> 16);
  544. }
  545. else
  546. {
  547. /* Insert new mask to odd address */
  548. buf1 = (buf0 & 0xFFFF0000) | id;
  549. }
  550. LPC_CANAF_RAM->mask[cnt1] = buf1;/* Insert mask */
  551. bound1 = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt+1) >> 1) - 1;
  552. /* Move all remaining standard mask entries one place up */
  553. while (cnt1 < bound1)
  554. {
  555. cnt1++;
  556. buf1 = LPC_CANAF_RAM->mask[cnt1];
  557. LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16);
  558. buf0 = buf1;
  559. }
  560. if ((CANAF_std_cnt & 0x0001) == 0)
  561. {
  562. /* Even number of IDs exists */
  563. LPC_CANAF_RAM->mask[cnt1+1] = (buf0 <<16) |(0x0000FFFF);
  564. }
  565. }
  566. }
  567. CANAF_std_cnt++;
  568. //update address values
  569. LPC_CANAF->SFF_GRP_sa += 0x04 ;
  570. LPC_CANAF->EFF_sa += 0x04 ;
  571. LPC_CANAF->EFF_GRP_sa += 0x04;
  572. LPC_CANAF->ENDofTable += 0x04;
  573. }
  574. /*********** Add Explicit Extended Identifier Frame Format entry *********/
  575. else
  576. {
  577. /* Add controller number */
  578. id |= canId << 29;
  579. cnt1 = ((CANAF_FullCAN_cnt+1) >> 1) + (((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt);
  580. cnt2 = 0;
  581. while (cnt2 < CANAF_ext_cnt)
  582. {
  583. /* Loop through extended existing masks*/
  584. if (LPC_CANAF_RAM->mask[cnt1] > id)
  585. {
  586. break;
  587. }
  588. cnt1++;/* cnt1 = U32 where to insert new mask */
  589. cnt2++;
  590. }
  591. buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */
  592. LPC_CANAF_RAM->mask[cnt1] = id; /* Insert mask */
  593. CANAF_ext_cnt++;
  594. bound1 = total;
  595. /* Move all remaining extended mask entries one place up*/
  596. while (cnt2 < bound1)
  597. {
  598. cnt1++;
  599. cnt2++;
  600. buf1 = LPC_CANAF_RAM->mask[cnt1];
  601. LPC_CANAF_RAM->mask[cnt1] = buf0;
  602. buf0 = buf1;
  603. }
  604. /* update address values */
  605. LPC_CANAF->EFF_GRP_sa += 4;
  606. LPC_CANAF->ENDofTable += 4;
  607. }
  608. if(CANAF_FullCAN_cnt == 0) //not use FullCAN mode
  609. {
  610. LPC_CANAF->AFMR = 0x00;//not use FullCAN mode
  611. }
  612. else
  613. {
  614. LPC_CANAF->AFMR = 0x04;
  615. }
  616. return CAN_OK;
  617. }
  618. /********************************************************************//**
  619. * @brief Load FullCAN entry into AFLUT
  620. * @param[in] canId The Id of the expected CAN component
  621. *
  622. * @param[in] id: identifier of entry that will be added
  623. * @return CAN_ERROR, could be:
  624. * - CAN_OK: loading is successful
  625. * - CAN_ID_EXIT_ERROR: ID exited in FullCAN Section
  626. * - CAN_OBJECTS_FULL_ERROR: no more space available
  627. *********************************************************************/
  628. CAN_ERROR CAN_LoadFullCANEntry (uint8_t canId, uint16_t id)
  629. {
  630. uint32_t buf0 = 0, buf1 = 0, buf2 = 0;
  631. uint32_t tmp0 = 0, tmp1 = 0, tmp2 = 0;
  632. int16_t cnt1 = 0, cnt2 = 0, bound1 = 0, total = 0;
  633. /* Acceptance Filter Memory full - return */
  634. total =((CANAF_FullCAN_cnt + 1) >> 1) + CANAF_FullCAN_cnt*3 + ((CANAF_std_cnt + 1) >> 1) \
  635. + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);
  636. //don't have enough space for this fullCAN Entry and its Object(3*32 bytes)
  637. if ((total >= 508) || (CANAF_FullCAN_cnt >= 64))
  638. {
  639. return CAN_OBJECTS_FULL_ERROR;
  640. }
  641. /* Setup Acceptance Filter Configuration
  642. Acceptance Filter Mode Register = Off */
  643. LPC_CANAF->AFMR = 0x00000001;
  644. /* Add mask for standard identifiers */
  645. id &= 0x07FF;
  646. id |= (canId << 13) | (1 << 11);
  647. /* Move all remaining sections one place up
  648. if new entry will increase FullCAN list */
  649. if (((CANAF_FullCAN_cnt & 0x0001) == 0)&&(total!=0))
  650. {
  651. //then remove remaining section
  652. cnt1 = (CANAF_FullCAN_cnt >> 1);
  653. bound1 = total;
  654. buf0 = LPC_CANAF_RAM->mask[cnt1];
  655. while (bound1--)
  656. {
  657. cnt1++;
  658. buf1 = LPC_CANAF_RAM->mask[cnt1];
  659. LPC_CANAF_RAM->mask[cnt1] = buf0;
  660. buf0 = buf1;
  661. }
  662. }
  663. if (CANAF_FullCAN_cnt == 0)
  664. {
  665. /* For entering first ID */
  666. LPC_CANAF_RAM->mask[0] = 0x0000FFFF | (id << 16);
  667. }
  668. else if (CANAF_FullCAN_cnt == 1)
  669. {
  670. /* For entering second ID */
  671. if (((LPC_CANAF_RAM->mask[0] >> 16)& 0xE7FF) > id)
  672. {
  673. LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] >> 16) | (id << 16);
  674. }
  675. else
  676. {
  677. LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] & 0xFFFF0000) | id;
  678. }
  679. }
  680. else
  681. {
  682. /* Find where to insert new ID */
  683. cnt1 = 0;
  684. cnt2 = CANAF_FullCAN_cnt;
  685. bound1 = (CANAF_FullCAN_cnt - 1) >> 1;
  686. while (cnt1 <= bound1)
  687. {
  688. /* Loop through standard existing IDs */
  689. if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > (id & 0xE7FF))
  690. {
  691. cnt2 = cnt1 * 2;
  692. break;
  693. }
  694. if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > (id & 0xE7FF))
  695. {
  696. cnt2 = cnt1 * 2 + 1;
  697. break;
  698. }
  699. cnt1++;
  700. }
  701. /* cnt1 = U32 where to insert new ID */
  702. /* cnt2 = U16 where to insert new ID */
  703. if (cnt1 > bound1)
  704. {
  705. /* Adding ID as last entry */
  706. /* Even number of IDs exists */
  707. if ((CANAF_FullCAN_cnt & 0x0001) == 0)
  708. {
  709. LPC_CANAF_RAM->mask[cnt1] = 0x0000FFFF | (id << 16);
  710. }
  711. /* Odd number of IDs exists */
  712. else
  713. {
  714. LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id;
  715. }
  716. }
  717. else
  718. {
  719. buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */
  720. if ((cnt2 & 0x0001) == 0)
  721. {
  722. /* Insert new mask to even address*/
  723. buf1 = (id << 16) | (buf0 >> 16);
  724. }
  725. else
  726. {
  727. /* Insert new mask to odd address */
  728. buf1 = (buf0 & 0xFFFF0000) | id;
  729. }
  730. LPC_CANAF_RAM->mask[cnt1] = buf1;/* Insert mask */
  731. bound1 = CANAF_FullCAN_cnt >> 1;
  732. /* Move all remaining standard mask entries one place up */
  733. while (cnt1 < bound1)
  734. {
  735. cnt1++;
  736. buf1 = LPC_CANAF_RAM->mask[cnt1];
  737. LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16);
  738. buf0 = buf1;
  739. }
  740. if ((CANAF_FullCAN_cnt & 0x0001) == 0)
  741. {
  742. /* Even number of IDs exists */
  743. LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000)
  744. | (0x0000FFFF);
  745. }
  746. }
  747. }
  748. //restruct FulCAN Object Section
  749. bound1 = CANAF_FullCAN_cnt - cnt2;
  750. cnt1 = total - (CANAF_FullCAN_cnt)*3 + cnt2*3 + 1;
  751. buf0 = LPC_CANAF_RAM->mask[cnt1];
  752. buf1 = LPC_CANAF_RAM->mask[cnt1+1];
  753. buf2 = LPC_CANAF_RAM->mask[cnt1+2];
  754. LPC_CANAF_RAM->mask[cnt1]=LPC_CANAF_RAM->mask[cnt1+1]= LPC_CANAF_RAM->mask[cnt1+2]=0x00;
  755. cnt1+=3;
  756. while(bound1--)
  757. {
  758. tmp0 = LPC_CANAF_RAM->mask[cnt1];
  759. tmp1 = LPC_CANAF_RAM->mask[cnt1+1];
  760. tmp2 = LPC_CANAF_RAM->mask[cnt1+2];
  761. LPC_CANAF_RAM->mask[cnt1]= buf0;
  762. LPC_CANAF_RAM->mask[cnt1+1]= buf1;
  763. LPC_CANAF_RAM->mask[cnt1+2]= buf2;
  764. buf0 = tmp0;
  765. buf1 = tmp1;
  766. buf2 = tmp2;
  767. cnt1+=3;
  768. }
  769. CANAF_FullCAN_cnt++;
  770. //update address values
  771. LPC_CANAF->SFF_sa += 0x04;
  772. LPC_CANAF->SFF_GRP_sa += 0x04 ;
  773. LPC_CANAF->EFF_sa += 0x04 ;
  774. LPC_CANAF->EFF_GRP_sa += 0x04;
  775. LPC_CANAF->ENDofTable += 0x04;
  776. LPC_CANAF->AFMR = 0x04;
  777. return CAN_OK;
  778. }
  779. /********************************************************************//**
  780. * @brief Load Group entry into AFLUT
  781. * @param[in] canId The Id of the expected CAN component
  782. *
  783. * @param[in] lowerID, upperID: lower and upper identifier of entry
  784. * @param[in] format: type of ID format, should be:
  785. * - STD_ID_FORMAT: Standard ID format (11-bit value)
  786. * - EXT_ID_FORMAT: Extended ID format (29-bit value)
  787. * @return CAN_ERROR, could be:
  788. * - CAN_OK: loading is successful
  789. * - CAN_CONFLICT_ID_ERROR: Conflict ID occurs
  790. * - CAN_OBJECTS_FULL_ERROR: no more space available
  791. *********************************************************************/
  792. CAN_ERROR CAN_LoadGroupEntry(uint8_t canId, uint32_t lowerID,
  793. uint32_t upperID, CAN_ID_FORMAT_Type format)
  794. {
  795. uint32_t buf0, buf1, entry1, entry2, LID,UID;
  796. int16_t cnt1, bound1, total;
  797. if(lowerID > upperID)
  798. return CAN_CONFLICT_ID_ERROR;
  799. total =((CANAF_FullCAN_cnt+1) >> 1)+ CANAF_FullCAN_cnt*3 +((CANAF_std_cnt + 1) >> 1) \
  800. + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
  801. /* Setup Acceptance Filter Configuration
  802. Acceptance Filter Mode Register = Off */
  803. LPC_CANAF->AFMR = 0x00000001;
  804. /*********Add Group of Standard Identifier Frame Format************/
  805. if(format == STD_ID_FORMAT)
  806. {
  807. if ((total >= 512))
  808. {
  809. //don't have enough space
  810. return CAN_OBJECTS_FULL_ERROR;
  811. }
  812. lowerID &=0x7FF; //mask ID
  813. upperID &=0x7FF;
  814. entry1 = (canId << 29) | (lowerID << 16) | (canId << 13)|(upperID << 0);
  815. cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1);
  816. //if this is the first Group standard ID entry
  817. if(CANAF_gstd_cnt == 0)
  818. {
  819. LPC_CANAF_RAM->mask[cnt1] = entry1;
  820. }
  821. else
  822. {
  823. //find the position to add new Group entry
  824. bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt;
  825. while(cnt1 < bound1)
  826. {
  827. //compare controller first
  828. while((LPC_CANAF_RAM->mask[cnt1] >> 29)< (entry1 >> 29))//increase until meet greater or equal controller
  829. cnt1++;
  830. buf0 = LPC_CANAF_RAM->mask[cnt1];
  831. if((LPC_CANAF_RAM->mask[cnt1] >> 29)> (entry1 >> 29)) //meet greater controller
  832. {
  833. //add at this position
  834. LPC_CANAF_RAM->mask[cnt1] = entry1;
  835. break;
  836. }
  837. else //meet equal controller
  838. {
  839. LID = (buf0 >> 16)&0x7FF;
  840. UID = buf0 & 0x7FF;
  841. if (upperID <= LID)
  842. {
  843. //add new entry before this entry
  844. LPC_CANAF_RAM->mask[cnt1] = entry1;
  845. break;
  846. }
  847. else if (lowerID >= UID)
  848. {
  849. //load next entry to compare
  850. cnt1 ++;
  851. }
  852. else
  853. return CAN_CONFLICT_ID_ERROR;
  854. }
  855. }
  856. if(cnt1 >= bound1)
  857. {
  858. //add new entry at the last position in this list
  859. buf0 = LPC_CANAF_RAM->mask[cnt1];
  860. LPC_CANAF_RAM->mask[cnt1] = entry1;
  861. }
  862. //remove all remaining entry of this section one place up
  863. bound1 = total - cnt1;
  864. while(bound1--)
  865. {
  866. cnt1++;
  867. buf1 = LPC_CANAF_RAM->mask[cnt1];
  868. LPC_CANAF_RAM->mask[cnt1] = buf0;
  869. buf0 = buf1;
  870. }
  871. }
  872. CANAF_gstd_cnt++;
  873. //update address values
  874. LPC_CANAF->EFF_sa +=0x04 ;
  875. LPC_CANAF->EFF_GRP_sa +=0x04;
  876. LPC_CANAF->ENDofTable +=0x04;
  877. }
  878. /*********Add Group of Extended Identifier Frame Format************/
  879. else
  880. {
  881. if ((total >= 511))
  882. {
  883. //don't have enough space
  884. return CAN_OBJECTS_FULL_ERROR;
  885. }
  886. lowerID &= 0x1FFFFFFF; //mask ID
  887. upperID &= 0x1FFFFFFF;
  888. entry1 = (canId << 29)|(lowerID << 0);
  889. entry2 = (canId << 29)|(upperID << 0);
  890. cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt + CANAF_ext_cnt;
  891. //if this is the first Group standard ID entry
  892. if(CANAF_gext_cnt == 0)
  893. {
  894. LPC_CANAF_RAM->mask[cnt1] = entry1;
  895. LPC_CANAF_RAM->mask[cnt1+1] = entry2;
  896. }
  897. else
  898. {
  899. //find the position to add new Group entry
  900. bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt \
  901. + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
  902. while(cnt1 < bound1)
  903. {
  904. while((LPC_CANAF_RAM->mask[cnt1] >>29)< canId) //increase until meet greater or equal controller
  905. cnt1++;
  906. buf0 = LPC_CANAF_RAM->mask[cnt1];
  907. buf1 = LPC_CANAF_RAM->mask[cnt1+1];
  908. if((LPC_CANAF_RAM->mask[cnt1] >> 29)> canId) //meet greater controller
  909. {
  910. //add at this position
  911. LPC_CANAF_RAM->mask[cnt1] = entry1;
  912. LPC_CANAF_RAM->mask[++cnt1] = entry2;
  913. break;
  914. }
  915. else //meet equal controller
  916. {
  917. LID = buf0 & 0x1FFFFFFF; //mask ID
  918. UID = buf1 & 0x1FFFFFFF;
  919. if (upperID <= LID)
  920. {
  921. //add new entry before this entry
  922. LPC_CANAF_RAM->mask[cnt1] = entry1;
  923. LPC_CANAF_RAM->mask[++cnt1] = entry2;
  924. break;
  925. }
  926. else if (lowerID >= UID)
  927. {
  928. //load next entry to compare
  929. cnt1 +=2;
  930. }
  931. else
  932. return CAN_CONFLICT_ID_ERROR;
  933. }
  934. }
  935. if(cnt1 >= bound1)
  936. {
  937. //add new entry at the last position in this list
  938. buf0 = LPC_CANAF_RAM->mask[cnt1];
  939. buf1 = LPC_CANAF_RAM->mask[cnt1+1];
  940. LPC_CANAF_RAM->mask[cnt1] = entry1;
  941. LPC_CANAF_RAM->mask[++cnt1] = entry2;
  942. }
  943. //remove all remaining entry of this section two place up
  944. bound1 = total - cnt1 + 1;
  945. cnt1++;
  946. while(bound1>0)
  947. {
  948. entry1 = LPC_CANAF_RAM->mask[cnt1];
  949. entry2 = LPC_CANAF_RAM->mask[cnt1+1];
  950. LPC_CANAF_RAM->mask[cnt1] = buf0;
  951. LPC_CANAF_RAM->mask[cnt1+1] = buf1;
  952. buf0 = entry1;
  953. buf1 = entry2;
  954. cnt1 +=2;
  955. bound1 -=2;
  956. }
  957. }
  958. CANAF_gext_cnt++;
  959. //update address values
  960. LPC_CANAF->ENDofTable +=0x08;
  961. }
  962. LPC_CANAF->AFMR = 0x04;
  963. return CAN_OK;
  964. }
  965. /********************************************************************//**
  966. * @brief Remove AFLUT entry (FullCAN entry and Explicit Standard entry)
  967. * @param[in] EntryType: the type of entry that want to remove, should be:
  968. * - FULLCAN_ENTRY
  969. * - EXPLICIT_STANDARD_ENTRY
  970. * - GROUP_STANDARD_ENTRY
  971. * - EXPLICIT_EXTEND_ENTRY
  972. * - GROUP_EXTEND_ENTRY
  973. * @param[in] position: the position of this entry in its section
  974. * Note: the first position is 0
  975. * @return CAN_ERROR, could be:
  976. * - CAN_OK: removing is successful
  977. * - CAN_ENTRY_NOT_EXIT_ERROR: entry want to remove is not exit
  978. *********************************************************************/
  979. CAN_ERROR CAN_RemoveEntry(AFLUT_ENTRY_Type EntryType, uint16_t position)
  980. {
  981. uint16_t cnt, bound, total;
  982. uint32_t buf0, buf1;
  983. /* Setup Acceptance Filter Configuration
  984. Acceptance Filter Mode Register = Off */
  985. LPC_CANAF->AFMR = 0x00000001;
  986. total = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1) + \
  987. + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);
  988. /************** Remove FullCAN Entry *************/
  989. if(EntryType == FULLCAN_ENTRY)
  990. {
  991. if((CANAF_FullCAN_cnt == 0)||(position >= CANAF_FullCAN_cnt))
  992. {
  993. return CAN_ENTRY_NOT_EXIT_ERROR;
  994. }
  995. else
  996. {
  997. cnt = position >> 1;
  998. buf0 = LPC_CANAF_RAM->mask[cnt];
  999. bound = (CANAF_FullCAN_cnt - position -1)>>1;
  1000. if((position & 0x0001) == 0) //event position
  1001. {
  1002. while(bound--)
  1003. {
  1004. //remove all remaining FullCAN entry one place down
  1005. buf1 = LPC_CANAF_RAM->mask[cnt+1];
  1006. LPC_CANAF_RAM->mask[cnt] = (buf1 >> 16) | (buf0 << 16);
  1007. buf0 = buf1;
  1008. cnt++;
  1009. }
  1010. }
  1011. else //odd position
  1012. {
  1013. while(bound--)
  1014. {
  1015. //remove all remaining FullCAN entry one place down
  1016. buf1 = LPC_CANAF_RAM->mask[cnt+1];
  1017. LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000)|(buf1 >> 16);
  1018. LPC_CANAF_RAM->mask[cnt+1] = LPC_CANAF_RAM->mask[cnt+1] << 16;
  1019. buf0 = buf1<<16;
  1020. cnt++;
  1021. }
  1022. }
  1023. if((CANAF_FullCAN_cnt & 0x0001) == 0)
  1024. {
  1025. if((position & 0x0001)==0)
  1026. LPC_CANAF_RAM->mask[cnt] = (buf0 << 16) | (0x0000FFFF);
  1027. else
  1028. LPC_CANAF_RAM->mask[cnt] = buf0 | 0x0000FFFF;
  1029. }
  1030. else
  1031. {
  1032. //remove all remaining section one place down
  1033. cnt = (CANAF_FullCAN_cnt + 1)>>1;
  1034. bound = total + CANAF_FullCAN_cnt * 3;
  1035. while(bound>cnt)
  1036. {
  1037. LPC_CANAF_RAM->mask[cnt-1] = LPC_CANAF_RAM->mask[cnt];
  1038. cnt++;
  1039. }
  1040. LPC_CANAF_RAM->mask[cnt-1]=0x00;
  1041. //update address values
  1042. LPC_CANAF->SFF_sa -= 0x04;
  1043. LPC_CANAF->SFF_GRP_sa -= 0x04 ;
  1044. LPC_CANAF->EFF_sa -= 0x04 ;
  1045. LPC_CANAF->EFF_GRP_sa -= 0x04;
  1046. LPC_CANAF->ENDofTable -= 0x04;
  1047. }
  1048. CANAF_FullCAN_cnt--;
  1049. //delete its FullCAN Object in the FullCAN Object section
  1050. //remove all remaining FullCAN Object three place down
  1051. cnt = total + position * 3;
  1052. bound = (CANAF_FullCAN_cnt - position + 1) * 3;
  1053. while(bound)
  1054. {
  1055. LPC_CANAF_RAM->mask[cnt]= LPC_CANAF_RAM->mask[cnt+3];;
  1056. LPC_CANAF_RAM->mask[cnt+1]= LPC_CANAF_RAM->mask[cnt+4];
  1057. LPC_CANAF_RAM->mask[cnt+2]= LPC_CANAF_RAM->mask[cnt+5];
  1058. bound -= 3;
  1059. cnt += 3;
  1060. }
  1061. }
  1062. }
  1063. /************** Remove Explicit Standard ID Entry *************/
  1064. else if(EntryType == EXPLICIT_STANDARD_ENTRY)
  1065. {
  1066. if((CANAF_std_cnt == 0)||(position >= CANAF_std_cnt))
  1067. {
  1068. return CAN_ENTRY_NOT_EXIT_ERROR;
  1069. }
  1070. else
  1071. {
  1072. cnt = ((CANAF_FullCAN_cnt+1) >> 1) + (position >> 1);
  1073. buf0 = LPC_CANAF_RAM->mask[cnt];
  1074. bound = (CANAF_std_cnt - position - 1) >> 1;
  1075. if((position & 0x0001) == 0) //event position
  1076. {
  1077. while(bound--)
  1078. {
  1079. //remove all remaining FullCAN entry one place down
  1080. buf1 = LPC_CANAF_RAM->mask[cnt + 1];
  1081. LPC_CANAF_RAM->mask[cnt] = (buf1 >> 16) | (buf0 << 16);
  1082. buf0 = buf1;
  1083. cnt++;
  1084. }
  1085. }
  1086. else //odd position
  1087. {
  1088. while(bound--)
  1089. {
  1090. //remove all remaining FullCAN entry one place down
  1091. buf1 = LPC_CANAF_RAM->mask[cnt + 1];
  1092. LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000) | (buf1 >> 16);
  1093. LPC_CANAF_RAM->mask[cnt + 1] = LPC_CANAF_RAM->mask[cnt + 1] << 16;
  1094. buf0 = buf1<<16;
  1095. cnt++;
  1096. }
  1097. }
  1098. if((CANAF_std_cnt & 0x0001) == 0)
  1099. {
  1100. if((position & 0x0001)==0)
  1101. LPC_CANAF_RAM->mask[cnt] = (buf0 << 16) | (0x0000FFFF);
  1102. else
  1103. LPC_CANAF_RAM->mask[cnt] = buf0 | 0x0000FFFF;
  1104. }
  1105. else
  1106. {
  1107. //remove all remaining section one place down
  1108. cnt = ((CANAF_FullCAN_cnt + 1)>>1) + ((CANAF_std_cnt + 1) >> 1);
  1109. bound = total + CANAF_FullCAN_cnt * 3;
  1110. while(bound>cnt)
  1111. {
  1112. LPC_CANAF_RAM->mask[cnt-1] = LPC_CANAF_RAM->mask[cnt];
  1113. cnt++;
  1114. }
  1115. LPC_CANAF_RAM->mask[cnt-1]=0x00;
  1116. //update address value
  1117. LPC_CANAF->SFF_GRP_sa -= 0x04 ;
  1118. LPC_CANAF->EFF_sa -= 0x04 ;
  1119. LPC_CANAF->EFF_GRP_sa -= 0x04;
  1120. LPC_CANAF->ENDofTable -= 0x04;
  1121. }
  1122. CANAF_std_cnt--;
  1123. }
  1124. }
  1125. /************** Remove Group of Standard ID Entry *************/
  1126. else if(EntryType == GROUP_STANDARD_ENTRY)
  1127. {
  1128. if((CANAF_gstd_cnt == 0)||(position >= CANAF_gstd_cnt))
  1129. {
  1130. return CAN_ENTRY_NOT_EXIT_ERROR;
  1131. }
  1132. else
  1133. {
  1134. cnt = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1)+ position + 1;
  1135. bound = total + CANAF_FullCAN_cnt * 3;
  1136. while (cnt < bound)
  1137. {
  1138. LPC_CANAF_RAM->mask[cnt - 1] = LPC_CANAF_RAM->mask[cnt];
  1139. cnt++;
  1140. }
  1141. LPC_CANAF_RAM->mask[cnt - 1]=0x00;
  1142. }
  1143. CANAF_gstd_cnt--;
  1144. //update address value
  1145. LPC_CANAF->EFF_sa -= 0x04;
  1146. LPC_CANAF->EFF_GRP_sa -= 0x04;
  1147. LPC_CANAF->ENDofTable -= 0x04;
  1148. }
  1149. /************** Remove Explicit Extended ID Entry *************/
  1150. else if(EntryType == EXPLICIT_EXTEND_ENTRY)
  1151. {
  1152. if((CANAF_ext_cnt == 0)||(position >= CANAF_ext_cnt))
  1153. {
  1154. return CAN_ENTRY_NOT_EXIT_ERROR;
  1155. }
  1156. else
  1157. {
  1158. cnt = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1)+ CANAF_gstd_cnt + position + 1;
  1159. bound = total + CANAF_FullCAN_cnt * 3;
  1160. while (cnt<bound)
  1161. {
  1162. LPC_CANAF_RAM->mask[cnt - 1] = LPC_CANAF_RAM->mask[cnt];
  1163. cnt++;
  1164. }
  1165. LPC_CANAF_RAM->mask[cnt - 1]=0x00;
  1166. }
  1167. CANAF_ext_cnt--;
  1168. LPC_CANAF->EFF_GRP_sa -= 0x04;
  1169. LPC_CANAF->ENDofTable -= 0x04;
  1170. }
  1171. /************** Remove Group of Extended ID Entry *************/
  1172. else
  1173. {
  1174. if((CANAF_gext_cnt == 0)||(position >= CANAF_gext_cnt))
  1175. {
  1176. return CAN_ENTRY_NOT_EXIT_ERROR;
  1177. }
  1178. else
  1179. {
  1180. cnt = total - (CANAF_gext_cnt << 1) + (position << 1);
  1181. bound = total + CANAF_FullCAN_cnt * 3;
  1182. while (cnt<bound)
  1183. {
  1184. //remove all remaining entry two place up
  1185. LPC_CANAF_RAM->mask[cnt] = LPC_CANAF_RAM->mask[cnt + 2];
  1186. LPC_CANAF_RAM->mask[cnt + 1] = LPC_CANAF_RAM->mask[cnt + 3];
  1187. cnt += 2;
  1188. }
  1189. }
  1190. CANAF_gext_cnt--;
  1191. LPC_CANAF->ENDofTable -= 0x08;
  1192. }
  1193. LPC_CANAF->AFMR = 0x04;
  1194. return CAN_OK;
  1195. }
  1196. /********************************************************************//**
  1197. * @brief Send message data
  1198. * @param[in] canId The Id of the expected CAN component
  1199. *
  1200. * @param[in] CAN_Msg point to the CAN_MSG_Type Structure, it contains message
  1201. * information such as: ID, DLC, RTR, ID Format
  1202. * @return Status:
  1203. * - SUCCESS: send message successfully
  1204. * - ERROR: send message unsuccessfully
  1205. *********************************************************************/
  1206. Status CAN_SendMsg (uint8_t canId, CAN_MSG_Type *CAN_Msg)
  1207. {
  1208. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1209. uint32_t data;
  1210. //Check status of Transmit Buffer 1
  1211. if (pCan->SR & (1 << 2))
  1212. {
  1213. /* Transmit Channel 1 is available */
  1214. /* Write frame informations and frame data into its CANxTFI1,
  1215. * CANxTID1, CANxTDA1, CANxTDB1 register */
  1216. pCan->TFI1 &= ~ 0x000F0000;
  1217. pCan->TFI1 |= (CAN_Msg->len) << 16;
  1218. if(CAN_Msg->type == REMOTE_FRAME)
  1219. {
  1220. pCan->TFI1 |= (1 << 30); //set bit RTR
  1221. }
  1222. else
  1223. {
  1224. pCan->TFI1 &= ~(1 << 30);
  1225. }
  1226. if(CAN_Msg->format == EXT_ID_FORMAT)
  1227. {
  1228. pCan->TFI1 |= (((uint32_t)1) << 31); //set bit FF
  1229. }
  1230. else
  1231. {
  1232. pCan->TFI1 &= ~(((uint32_t)1) << 31);
  1233. }
  1234. /* Write CAN ID*/
  1235. pCan->TID1 = CAN_Msg->id;
  1236. /*Write first 4 data bytes*/
  1237. data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1]))<< 8) | ((CAN_Msg->dataA[2]) << 16) | ((CAN_Msg->dataA[3]) << 24);
  1238. pCan->TDA1 = data;
  1239. /*Write second 4 data bytes*/
  1240. data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8)|((CAN_Msg->dataB[2]) << 16)|((CAN_Msg->dataB[3]) << 24);
  1241. pCan->TDB1 = data;
  1242. /*Write transmission request*/
  1243. pCan->CMR = 0x21;
  1244. return SUCCESS;
  1245. }
  1246. //check status of Transmit Buffer 2
  1247. else if((pCan->SR) & (1 << 10))
  1248. {
  1249. /* Transmit Channel 2 is available */
  1250. /* Write frame informations and frame data into its CANxTFI2,
  1251. * CANxTID2, CANxTDA2, CANxTDB2 register */
  1252. pCan->TFI2 &= ~0x000F0000;
  1253. pCan->TFI2 |= (CAN_Msg->len) << 16;
  1254. if(CAN_Msg->type == REMOTE_FRAME)
  1255. {
  1256. pCan->TFI2 |= (1 << 30); //set bit RTR
  1257. }
  1258. else
  1259. {
  1260. pCan->TFI2 &= ~(1 << 30);
  1261. }
  1262. if(CAN_Msg->format == EXT_ID_FORMAT)
  1263. {
  1264. pCan->TFI2 |= (((uint32_t)1) << 31); //set bit FF
  1265. }
  1266. else
  1267. {
  1268. pCan->TFI2 &= ~(((uint32_t)1) << 31);
  1269. }
  1270. /* Write CAN ID*/
  1271. pCan->TID2 = CAN_Msg->id;
  1272. /*Write first 4 data bytes*/
  1273. data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1])) << 8) | ((CAN_Msg->dataA[2]) << 16)|((CAN_Msg->dataA[3]) << 24);
  1274. pCan->TDA2 = data;
  1275. /*Write second 4 data bytes*/
  1276. data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8) | ((CAN_Msg->dataB[2]) << 16) | ((CAN_Msg->dataB[3]) << 24);
  1277. pCan->TDB2 = data;
  1278. /*Write transmission request*/
  1279. pCan->CMR = 0x41;
  1280. return SUCCESS;
  1281. }
  1282. //check status of Transmit Buffer 3
  1283. else if (pCan->SR & (1<<18))
  1284. {
  1285. /* Transmit Channel 3 is available */
  1286. /* Write frame informations and frame data into its CANxTFI3,
  1287. * CANxTID3, CANxTDA3, CANxTDB3 register */
  1288. pCan->TFI3 &= ~0x000F0000;
  1289. pCan->TFI3 |= (CAN_Msg->len) << 16;
  1290. if(CAN_Msg->type == REMOTE_FRAME)
  1291. {
  1292. pCan->TFI3 |= (1 << 30); //set bit RTR
  1293. }
  1294. else
  1295. {
  1296. pCan->TFI3 &= ~(1 << 30);
  1297. }
  1298. if(CAN_Msg->format == EXT_ID_FORMAT)
  1299. {
  1300. pCan->TFI3 |= (((uint32_t)1) << 31); //set bit FF
  1301. }
  1302. else
  1303. {
  1304. pCan->TFI3 &= ~(((uint32_t)1) << 31);
  1305. }
  1306. /* Write CAN ID*/
  1307. pCan->TID3 = CAN_Msg->id;
  1308. /*Write first 4 data bytes*/
  1309. data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1])) << 8) | ((CAN_Msg->dataA[2]) << 16) | ((CAN_Msg->dataA[3]) << 24);
  1310. pCan->TDA3 = data;
  1311. /*Write second 4 data bytes*/
  1312. data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8) | ((CAN_Msg->dataB[2]) << 16) | ((CAN_Msg->dataB[3]) << 24);
  1313. pCan->TDB3 = data;
  1314. /*Write transmission request*/
  1315. pCan->CMR = 0x81;
  1316. return SUCCESS;
  1317. }
  1318. else
  1319. {
  1320. return ERROR;
  1321. }
  1322. }
  1323. /********************************************************************//**
  1324. * @brief Receive message data
  1325. * @param[in] canId The Id of the expected CAN component
  1326. *
  1327. * @param[in] CAN_Msg point to the CAN_MSG_Type Struct, it will contain received
  1328. * message information such as: ID, DLC, RTR, ID Format
  1329. * @return Status:
  1330. * - SUCCESS: receive message successfully
  1331. * - ERROR: receive message unsuccessfully
  1332. *********************************************************************/
  1333. Status CAN_ReceiveMsg (uint8_t canId, CAN_MSG_Type *CAN_Msg)
  1334. {
  1335. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1336. uint32_t data;
  1337. //check status of Receive Buffer
  1338. if((pCan->SR &0x00000001))
  1339. {
  1340. /* Receive message is available */
  1341. /* Read frame informations */
  1342. CAN_Msg->format = (uint8_t)(((pCan->RFS) & 0x80000000) >> 31);
  1343. CAN_Msg->type = (uint8_t)(((pCan->RFS) & 0x40000000) >> 30);
  1344. CAN_Msg->len = (uint8_t)(((pCan->RFS) & 0x000F0000) >> 16);
  1345. /* Read CAN message identifier */
  1346. CAN_Msg->id = pCan->RID;
  1347. /* Read the data if received message was DATA FRAME */
  1348. if (CAN_Msg->type == DATA_FRAME)
  1349. {
  1350. /* Read first 4 data bytes */
  1351. data = pCan->RDA;
  1352. *((uint8_t *) &CAN_Msg->dataA[0])= data & 0x000000FF;
  1353. *((uint8_t *) &CAN_Msg->dataA[1])= (data & 0x0000FF00) >> 8;;
  1354. *((uint8_t *) &CAN_Msg->dataA[2])= (data & 0x00FF0000) >> 16;
  1355. *((uint8_t *) &CAN_Msg->dataA[3])= (data & 0xFF000000) >> 24;
  1356. /* Read second 4 data bytes */
  1357. data = pCan->RDB;
  1358. *((uint8_t *) &CAN_Msg->dataB[0])= data & 0x000000FF;
  1359. *((uint8_t *) &CAN_Msg->dataB[1])= (data & 0x0000FF00) >> 8;
  1360. *((uint8_t *) &CAN_Msg->dataB[2])= (data & 0x00FF0000) >> 16;
  1361. *((uint8_t *) &CAN_Msg->dataB[3])= (data & 0xFF000000) >> 24;
  1362. /*release receive buffer*/
  1363. pCan->CMR = 0x04;
  1364. }
  1365. else
  1366. {
  1367. /* Received Frame is a Remote Frame, not have data, we just receive
  1368. * message information only */
  1369. pCan->CMR = 0x04; /*release receive buffer*/
  1370. return SUCCESS;
  1371. }
  1372. }
  1373. else
  1374. {
  1375. // no receive message available
  1376. return ERROR;
  1377. }
  1378. return SUCCESS;
  1379. }
  1380. /********************************************************************//**
  1381. * @brief Receive FullCAN Object
  1382. * @param[in] CANAFx: CAN Acceptance Filter register, should be: LPC_CANAF
  1383. * @param[in] CAN_Msg point to the CAN_MSG_Type Struct, it will contain received
  1384. * message information such as: ID, DLC, RTR, ID Format
  1385. * @return CAN_ERROR, could be:
  1386. * - CAN_FULL_OBJ_NOT_RCV: FullCAN Object is not be received
  1387. * - CAN_OK: Received FullCAN Object successful
  1388. *
  1389. *********************************************************************/
  1390. CAN_ERROR FCAN_ReadObj (CAN_MSG_Type *CAN_Msg)
  1391. {
  1392. uint32_t *pSrc, data;
  1393. uint32_t interrut_word, msg_idx, test_bit, head_idx, tail_idx;
  1394. interrut_word = 0;
  1395. if (LPC_CANAF->FCANIC0 != 0)
  1396. {
  1397. interrut_word = LPC_CANAF->FCANIC0;
  1398. head_idx = 0;
  1399. tail_idx = 31;
  1400. }
  1401. else if (LPC_CANAF->FCANIC1 != 0)
  1402. {
  1403. interrut_word = LPC_CANAF->FCANIC1;
  1404. head_idx = 32;
  1405. tail_idx = 63;
  1406. }
  1407. if (interrut_word != 0)
  1408. {
  1409. /* Detect for interrupt pending */
  1410. msg_idx = 0;
  1411. for (msg_idx = head_idx; msg_idx <= tail_idx; msg_idx++)
  1412. {
  1413. test_bit = interrut_word & 0x1;
  1414. interrut_word = interrut_word >> 1;
  1415. if (test_bit)
  1416. {
  1417. pSrc = (uint32_t *) (LPC_CANAF->ENDofTable + LPC_CANAF_RAM_BASE + msg_idx * 12);
  1418. /* Has been finished updating the content */
  1419. if ((*pSrc & 0x03000000L) == 0x03000000L)
  1420. {
  1421. /*clear semaphore*/
  1422. *pSrc &= 0xFCFFFFFF;
  1423. /*Set to DatA*/
  1424. pSrc++;
  1425. /* Copy to dest buf */
  1426. data = *pSrc;
  1427. *((uint8_t *) &CAN_Msg->dataA[0])= data & 0x000000FF;
  1428. *((uint8_t *) &CAN_Msg->dataA[1])= (data & 0x0000FF00) >> 8;
  1429. *((uint8_t *) &CAN_Msg->dataA[2])= (data & 0x00FF0000) >> 16;
  1430. *((uint8_t *) &CAN_Msg->dataA[3])= (data & 0xFF000000) >> 24;
  1431. /*Set to DatB*/
  1432. pSrc++;
  1433. /* Copy to dest buf */
  1434. data = *pSrc;
  1435. *((uint8_t *) &CAN_Msg->dataB[0])= data & 0x000000FF;
  1436. *((uint8_t *) &CAN_Msg->dataB[1])= (data & 0x0000FF00) >> 8;
  1437. *((uint8_t *) &CAN_Msg->dataB[2])= (data & 0x00FF0000) >> 16;
  1438. *((uint8_t *) &CAN_Msg->dataB[3])= (data & 0xFF000000) >> 24;
  1439. /*Back to Dat1*/
  1440. pSrc -= 2;
  1441. CAN_Msg->id = *pSrc & 0x7FF;
  1442. CAN_Msg->len = (uint8_t) (*pSrc >> 16) & 0x0F;
  1443. CAN_Msg->format = 0; //FullCAN Object ID always is 11-bit value
  1444. CAN_Msg->type = (uint8_t)(*pSrc >> 30) &0x01;
  1445. /*Re-read semaphore*/
  1446. if ((*pSrc & 0x03000000L) == 0)
  1447. {
  1448. return CAN_OK;
  1449. }
  1450. }
  1451. }
  1452. }
  1453. }
  1454. return CAN_FULL_OBJ_NOT_RCV;
  1455. }
  1456. /********************************************************************//**
  1457. * @brief Get CAN Control Status
  1458. * @param[in] canId The Id of the expected CAN component
  1459. *
  1460. * @param[in] arg: type of CAN status to get from CAN status register
  1461. * Should be:
  1462. * - CANCTRL_GLOBAL_STS: CAN Global Status
  1463. * - CANCTRL_INT_CAP: CAN Interrupt and Capture
  1464. * - CANCTRL_ERR_WRN: CAN Error Warning Limit
  1465. * - CANCTRL_STS: CAN Control Status
  1466. * @return Current Control Status that you want to get value
  1467. *********************************************************************/
  1468. uint32_t CAN_GetCTRLStatus (uint8_t canId, CAN_CTRL_STS_Type arg)
  1469. {
  1470. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1471. switch (arg)
  1472. {
  1473. case CANCTRL_GLOBAL_STS:
  1474. return pCan->GSR;
  1475. case CANCTRL_INT_CAP:
  1476. return pCan->ICR;
  1477. case CANCTRL_ERR_WRN:
  1478. return pCan->EWL;
  1479. default: // CANCTRL_STS
  1480. return pCan->SR;
  1481. }
  1482. }
  1483. /********************************************************************//**
  1484. * @brief Get CAN Central Status
  1485. * @param[in] CANCRx point to LPC_CANCR_TypeDef, should be: LPC_CANCR
  1486. * @param[in] arg: type of CAN status to get from CAN Central status register
  1487. * Should be:
  1488. * - CANCR_TX_STS: Central CAN Tx Status
  1489. * - CANCR_RX_STS: Central CAN Rx Status
  1490. * - CANCR_MS: Central CAN Miscellaneous Status
  1491. * @return Current Central Status that you want to get value
  1492. *********************************************************************/
  1493. uint32_t CAN_GetCRStatus (CAN_CR_STS_Type arg)
  1494. {
  1495. switch (arg)
  1496. {
  1497. case CANCR_TX_STS:
  1498. return LPC_CANCR->TxSR;
  1499. case CANCR_RX_STS:
  1500. return LPC_CANCR->RxSR;
  1501. default: // CANCR_MS
  1502. return LPC_CANCR->MSR;
  1503. }
  1504. }
  1505. /********************************************************************//**
  1506. * @brief Enable/Disable CAN Interrupt
  1507. * @param[in] canId The Id of the expected CAN component
  1508. *
  1509. * @param[in] arg: type of CAN interrupt that you want to enable/disable
  1510. * Should be:
  1511. * - CANINT_RIE: CAN Receiver Interrupt Enable
  1512. * - CANINT_TIE1: CAN Transmit Interrupt Enable
  1513. * - CANINT_EIE: CAN Error Warning Interrupt Enable
  1514. * - CANINT_DOIE: CAN Data Overrun Interrupt Enable
  1515. * - CANINT_WUIE: CAN Wake-Up Interrupt Enable
  1516. * - CANINT_EPIE: CAN Error Passive Interrupt Enable
  1517. * - CANINT_ALIE: CAN Arbitration Lost Interrupt Enable
  1518. * - CANINT_BEIE: CAN Bus Error Interrupt Enable
  1519. * - CANINT_IDIE: CAN ID Ready Interrupt Enable
  1520. * - CANINT_TIE2: CAN Transmit Interrupt Enable for Buffer2
  1521. * - CANINT_TIE3: CAN Transmit Interrupt Enable for Buffer3
  1522. * - CANINT_FCE: FullCAN Interrupt Enable
  1523. * @param[in] NewState: New state of this function, should be:
  1524. * - ENABLE
  1525. * - DISABLE
  1526. * @return none
  1527. *********************************************************************/
  1528. void CAN_IRQCmd (uint8_t canId, CAN_INT_EN_Type arg, FunctionalState NewState)
  1529. {
  1530. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1531. if(NewState == ENABLE)
  1532. {
  1533. if(arg == CANINT_FCE)
  1534. {
  1535. LPC_CANAF->AFMR = 0x01;
  1536. LPC_CANAF->FCANIE = 0x01;
  1537. LPC_CANAF->AFMR = 0x04;
  1538. }
  1539. else
  1540. pCan->IER |= (1 << arg);
  1541. }
  1542. else
  1543. {
  1544. if(arg == CANINT_FCE)
  1545. {
  1546. LPC_CANAF->AFMR = 0x01;
  1547. LPC_CANAF->FCANIE = 0x01;
  1548. LPC_CANAF->AFMR = 0x00;
  1549. }
  1550. else
  1551. pCan->IER &= ~(1 << arg);
  1552. }
  1553. }
  1554. /********************************************************************//**
  1555. * @brief Setting Acceptance Filter mode
  1556. * @param[in] CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
  1557. * @param[in] AFMode: type of AF mode that you want to set, should be:
  1558. * - CAN_NORMAL: Normal mode
  1559. * - CAN_ACC_OFF: Acceptance Filter Off Mode
  1560. * - CAN_ACC_BP: Acceptance Fileter Bypass Mode
  1561. * - CAN_EFCAN: FullCAN Mode Enhancement
  1562. * @return none
  1563. *********************************************************************/
  1564. void CAN_SetAFMode (CAN_AFMODE_Type AFMode)
  1565. {
  1566. switch(AFMode)
  1567. {
  1568. case CAN_NORMAL:
  1569. LPC_CANAF->AFMR = 0x00;
  1570. break;
  1571. case CAN_ACC_OFF:
  1572. LPC_CANAF->AFMR = 0x01;
  1573. break;
  1574. case CAN_ACC_BP:
  1575. LPC_CANAF->AFMR = 0x02;
  1576. break;
  1577. case CAN_EFCAN:
  1578. LPC_CANAF->AFMR = 0x04;
  1579. break;
  1580. }
  1581. }
  1582. /********************************************************************//**
  1583. * @brief Enable/Disable CAN Mode
  1584. * @param[in] canId The Id of the expected CAN component
  1585. *
  1586. * @param[in] mode: type of CAN mode that you want to enable/disable, should be:
  1587. * - CAN_OPERATING_MODE: Normal Operating Mode
  1588. * - CAN_RESET_MODE: Reset Mode
  1589. * - CAN_LISTENONLY_MODE: Listen Only Mode
  1590. * - CAN_SELFTEST_MODE: Self Test Mode
  1591. * - CAN_TXPRIORITY_MODE: Transmit Priority Mode
  1592. * - CAN_SLEEP_MODE: Sleep Mode
  1593. * - CAN_RXPOLARITY_MODE: Receive Polarity Mode
  1594. * - CAN_TEST_MODE: Test Mode
  1595. * @param[in] NewState: New State of this function, should be:
  1596. * - ENABLE
  1597. * - DISABLE
  1598. * @return none
  1599. *********************************************************************/
  1600. void CAN_ModeConfig(uint8_t canId, CAN_MODE_Type mode, FunctionalState NewState)
  1601. {
  1602. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1603. switch(mode)
  1604. {
  1605. case CAN_OPERATING_MODE:
  1606. pCan->MOD = 0x00;
  1607. break;
  1608. case CAN_RESET_MODE:
  1609. if(NewState == ENABLE)
  1610. pCan->MOD |= CAN_MOD_RM;
  1611. else
  1612. pCan->MOD &= ~CAN_MOD_RM;
  1613. break;
  1614. case CAN_LISTENONLY_MODE:
  1615. pCan->MOD |=CAN_MOD_RM;//Enter Reset mode
  1616. if(NewState == ENABLE)
  1617. pCan->MOD |= CAN_MOD_LOM;
  1618. else
  1619. pCan->MOD &= ~ CAN_MOD_LOM;
  1620. pCan->MOD &= ~ CAN_MOD_RM;//Release Reset mode
  1621. break;
  1622. case CAN_SELFTEST_MODE:
  1623. pCan->MOD |= CAN_MOD_RM;//Enter Reset mode
  1624. if(NewState == ENABLE)
  1625. pCan->MOD |= CAN_MOD_STM;
  1626. else
  1627. pCan->MOD &= ~ CAN_MOD_STM;
  1628. pCan->MOD &= ~ CAN_MOD_RM;//Release Reset mode
  1629. break;
  1630. case CAN_TXPRIORITY_MODE:
  1631. if(NewState == ENABLE)
  1632. pCan->MOD |= CAN_MOD_TPM;
  1633. else
  1634. pCan->MOD &= ~ CAN_MOD_TPM;
  1635. break;
  1636. case CAN_SLEEP_MODE:
  1637. if(NewState == ENABLE)
  1638. pCan->MOD |= CAN_MOD_SM;
  1639. else
  1640. pCan->MOD &= ~ CAN_MOD_SM;
  1641. break;
  1642. case CAN_RXPOLARITY_MODE:
  1643. if(NewState == ENABLE)
  1644. pCan->MOD |= CAN_MOD_RPM;
  1645. else
  1646. pCan->MOD &= ~ CAN_MOD_RPM;
  1647. break;
  1648. case CAN_TEST_MODE:
  1649. if(NewState == ENABLE)
  1650. pCan->MOD |= CAN_MOD_TM;
  1651. else
  1652. pCan->MOD &= ~ CAN_MOD_TM;
  1653. break;
  1654. }
  1655. }
  1656. /*********************************************************************//**
  1657. * @brief Set CAN command request
  1658. * @param[in] canId The Id of the expected CAN component
  1659. *
  1660. * @param[in] CMRType command request type, should be:
  1661. * - CAN_CMR_TR: Transmission request
  1662. * - CAN_CMR_AT: Abort Transmission request
  1663. * - CAN_CMR_RRB: Release Receive Buffer request
  1664. * - CAN_CMR_CDO: Clear Data Overrun request
  1665. * - CAN_CMR_SRR: Self Reception request
  1666. * - CAN_CMR_STB1: Select Tx Buffer 1 request
  1667. * - CAN_CMR_STB2: Select Tx Buffer 2 request
  1668. * - CAN_CMR_STB3: Select Tx Buffer 3 request
  1669. * @return CANICR (CAN interrupt and Capture register) value
  1670. **********************************************************************/
  1671. void CAN_SetCommand(uint8_t canId, uint32_t CMRType)
  1672. {
  1673. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1674. pCan->CMR |= CMRType;
  1675. }
  1676. /*********************************************************************//**
  1677. * @brief Get CAN interrupt status
  1678. * @param[in] canId The Id of the expected CAN component
  1679. *
  1680. * @return CANICR (CAN interrupt and Capture register) value
  1681. **********************************************************************/
  1682. uint32_t CAN_IntGetStatus(uint8_t canId)
  1683. {
  1684. LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
  1685. return pCan->ICR;
  1686. }
  1687. /*********************************************************************//**
  1688. * @brief Check if FullCAN interrupt enable or not
  1689. * @param[in] CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
  1690. * @return IntStatus, could be:
  1691. * - SET: if FullCAN interrupt is enable
  1692. * - RESET: if FullCAN interrupt is disable
  1693. **********************************************************************/
  1694. IntStatus CAN_FullCANIntGetStatus (void)
  1695. {
  1696. if (LPC_CANAF->FCANIE)
  1697. return SET;
  1698. return RESET;
  1699. }
  1700. /*********************************************************************//**
  1701. * @brief Get value of FullCAN interrupt and capture register
  1702. * @param[in] CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
  1703. * @param[in] type: FullCAN IC type, should be:
  1704. * - FULLCAN_IC0: FullCAN Interrupt Capture 0
  1705. * - FULLCAN_IC1: FullCAN Interrupt Capture 1
  1706. * @return FCANIC0 or FCANIC1 (FullCAN interrupt and Capture register) value
  1707. **********************************************************************/
  1708. uint32_t CAN_FullCANPendGetStatus(FullCAN_IC_Type type)
  1709. {
  1710. if (type == FULLCAN_IC0)
  1711. return LPC_CANAF->FCANIC0;
  1712. return LPC_CANAF->FCANIC1;
  1713. }
  1714. /* End of Public Variables ---------------------------------------------------------- */
  1715. /**
  1716. * @}
  1717. */
  1718. #endif /*_CAN*/
  1719. /**
  1720. * @}
  1721. */
  1722. /* --------------------------------- End Of File ------------------------------ */