drv_lpccan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * File : drv_lpccan.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2015-06-30 aubrcool@qq.com first version
  13. */
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #include <board.h>
  17. #include <drv_lpccan.h>
  18. #ifdef RT_USING_COMPONENTS_INIT
  19. #include <components.h>
  20. #endif
  21. #ifdef RT_USING_CAN
  22. #include "lpc_types.h"
  23. #include "lpc_can.h"
  24. #include "lpc_pinsel.h"
  25. #include "lpc_exti.h"
  26. #include "lpc_clkpwr.h"
  27. struct lpccandata {
  28. en_CAN_unitId id;
  29. };
  30. static const rt_uint32_t LPCBAUDTAB[] = {
  31. 1000000,
  32. 800000,
  33. 500000,
  34. 250000,
  35. 125000,
  36. 100000,
  37. 50000,
  38. 20000,
  39. 10000,
  40. };
  41. static LPC_CAN_TypeDef* lcpcan_get_reg_base(rt_uint32_t id)
  42. {
  43. LPC_CAN_TypeDef* pCan;
  44. switch (id) {
  45. case CAN_ID_1:
  46. pCan = LPC_CAN1;
  47. break;
  48. case CAN_ID_2:
  49. pCan = LPC_CAN2;
  50. break;
  51. default:
  52. pCan = NULL;
  53. }
  54. return pCan;
  55. }
  56. static void lpccan_irqstate_init(rt_uint32_t id)
  57. {
  58. LPC_CAN_TypeDef* pCan = lcpcan_get_reg_base(id);
  59. pCan->MOD = 1; // Enter Reset Mode
  60. pCan->IER = 0; // Disable All CAN Interrupts
  61. pCan->GSR = 0;
  62. /* Request command to release Rx, Tx buffer and clear data overrun */
  63. //pCan->CMR = CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO;
  64. pCan->CMR = (1 << 1) | (1 << 2) | (1 << 3);
  65. /* Read to clear interrupt pending in interrupt capture register */
  66. rt_int32_t i = pCan->ICR;
  67. pCan->MOD = 0;// Return Normal operating
  68. }
  69. static void lpccan_baud_set(rt_uint32_t id, rt_uint32_t baud)
  70. {
  71. uint32_t result = 0;
  72. uint8_t NT, TSEG1, TSEG2;
  73. uint32_t CANPclk = 0;
  74. uint32_t BRP;
  75. LPC_CAN_TypeDef* pCan = lcpcan_get_reg_base(id);
  76. CANPclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  77. result = CANPclk / LPCBAUDTAB[baud];
  78. /* Calculate suitable nominal time value
  79. * NT (nominal time) = (TSEG1 + TSEG2 + 3)
  80. * NT <= 24
  81. * TSEG1 >= 2*TSEG2
  82. */
  83. for(NT = 24; NT > 0; NT = NT-2)
  84. {
  85. if ((result%NT) == 0)
  86. {
  87. BRP = result / NT - 1;
  88. NT--;
  89. TSEG2 = (NT/3) - 1;
  90. TSEG1 = NT -(NT/3) - 1;
  91. break;
  92. }
  93. }
  94. /* Enter reset mode */
  95. pCan->MOD = 0x01;
  96. /* Set bit timing
  97. * Default: SAM = 0x00;
  98. * SJW = 0x03;
  99. */
  100. pCan->BTR = (TSEG2 << 20) | (TSEG1 << 16) | (3 << 14) | BRP;
  101. /* Return to normal operating */
  102. pCan->MOD = 0;
  103. }
  104. static void lpccan_init_alut_ram(void)
  105. {
  106. //Reset CANAF value
  107. LPC_CANAF->AFMR = 0x01;
  108. //clear ALUT RAM
  109. rt_memset((void *)LPC_CANAF_RAM->mask, 0, 2048);
  110. LPC_CANAF->SFF_sa = 0;
  111. LPC_CANAF->SFF_GRP_sa = 0;
  112. LPC_CANAF->EFF_sa = 0;
  113. LPC_CANAF->EFF_GRP_sa = 0;
  114. LPC_CANAF->ENDofTable = 0;
  115. LPC_CANAF->AFMR = 0x00;
  116. // Set AF Mode
  117. CAN_SetAFMode(CAN_NORMAL);
  118. }
  119. #ifdef RT_USING_LPCCAN1
  120. static void lpccan1_turnon_clk(void)
  121. {
  122. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
  123. }
  124. static void lpccan1_filter_init(struct rt_can_device *can)
  125. {
  126. }
  127. static void lpccan1_hw_init(enum CANBAUD baud, CAN_MODE_Type mode)
  128. {
  129. if(mode != CAN_SELFTEST_MODE) {
  130. #ifndef LPCCAN1_USEING_GPIO_SECOND
  131. PINSEL_ConfigPin (0, 0, 1);
  132. PINSEL_ConfigPin (0, 1, 1);
  133. #else
  134. PINSEL_ConfigPin (0, 21, 4);
  135. PINSEL_ConfigPin (0, 22, 4);
  136. #endif
  137. }
  138. lpccan1_turnon_clk();
  139. lpccan_irqstate_init(CAN_1);
  140. lpccan_init_alut_ram();
  141. lpccan1_turnon_clk();
  142. lpccan_baud_set(CAN_1, baud);
  143. CAN_ModeConfig(CAN_1, mode, ENABLE);
  144. if(mode == CAN_SELFTEST_MODE) {
  145. //CAN_ModeConfig(CAN_1, CAN_TEST_MODE, ENABLE);
  146. CAN_SetAFMode(CAN_ACC_BP);
  147. }
  148. }
  149. #endif /*RT_USING_LPCCAN1*/
  150. #ifdef RT_USING_LPCCAN2
  151. static void lpccan2_turnon_clk(void)
  152. {
  153. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
  154. }
  155. static void lpccan2_filter_init(struct rt_can_device *can)
  156. {
  157. }
  158. static void lpccan2_hw_init(enum CANBAUD baud, CAN_MODE_Type mode)
  159. {
  160. if(mode != CAN_SELFTEST_MODE) {
  161. #ifndef LPCCAN2_USEING_GPIO_SECOND
  162. PINSEL_ConfigPin (0, 4, 2);
  163. PINSEL_ConfigPin (0, 5, 2);
  164. #else
  165. PINSEL_ConfigPin (2, 7, 1);
  166. PINSEL_ConfigPin (2, 8, 1);
  167. #endif
  168. }
  169. lpccan2_turnon_clk();
  170. lpccan_irqstate_init(CAN_2);
  171. #ifndef RT_USING_LPCCAN1
  172. lpccan_init_alut_ram();
  173. #endif /*RT_USING_LPCCAN1*/
  174. lpccan_baud_set(CAN_2, baud);
  175. CAN_ModeConfig(CAN_2, mode, ENABLE);
  176. if(mode == CAN_SELFTEST_MODE) {
  177. CAN_SetAFMode(CAN_ACC_BP);
  178. }
  179. }
  180. #endif /*RT_USING_LPCCAN2*/
  181. static rt_err_t configure(struct rt_can_device *can, struct can_configure *cfg)
  182. {
  183. CAN_MODE_Type mode;
  184. switch(cfg->mode) {
  185. case RT_CAN_MODE_NORMAL:
  186. mode = CAN_OPERATING_MODE;
  187. break;
  188. case RT_CAN_MODE_LISEN:
  189. mode = CAN_LISTENONLY_MODE;
  190. break;
  191. case RT_CAN_MODE_LOOPBACKANLISEN:
  192. mode = CAN_SELFTEST_MODE;
  193. break;
  194. default:
  195. return RT_EIO;
  196. }
  197. rt_uint32_t canid;
  198. canid = ((struct lpccandata *) can->parent.user_data)->id;
  199. #ifdef RT_USING_LPCCAN1
  200. if(canid == CAN_1)
  201. {
  202. lpccan1_hw_init(cfg->baud_rate, mode);
  203. lpccan1_filter_init(can);
  204. }
  205. #endif /*RT_USING_LPCCAN1*/
  206. #ifdef RT_USING_LPCCAN2
  207. #ifdef RT_USING_LPCCAN1
  208. else
  209. #endif /*RT_USING_LPCCAN1*/
  210. {
  211. lpccan2_hw_init(cfg->baud_rate, mode);
  212. lpccan2_filter_init(can);
  213. }
  214. #endif /*RT_USING_LPCCAN2*/
  215. return RT_EOK;
  216. }
  217. static CAN_ERROR findfilter(struct lpccandata* plpccan, struct rt_can_filter_item* pitem, rt_int32_t* pos)
  218. {
  219. extern uint16_t CANAF_FullCAN_cnt;
  220. extern uint16_t CANAF_std_cnt;
  221. extern uint16_t CANAF_gstd_cnt;
  222. extern uint16_t CANAF_ext_cnt;
  223. extern uint16_t CANAF_gext_cnt;
  224. rt_uint32_t buf0 = 0, buf1 = 0;
  225. rt_int16_t cnt1 = 0, cnt2 = 0, bound1 = 0;
  226. CAN_ID_FORMAT_Type format;
  227. *pos = -1;
  228. if(pitem->ide) {
  229. format = EXT_ID_FORMAT;
  230. } else {
  231. format = STD_ID_FORMAT;
  232. }
  233. if(pitem->mode) {
  234. rt_uint32_t id = pitem->id;
  235. if(format == STD_ID_FORMAT)
  236. {
  237. id &= 0x07FF;
  238. id |= plpccan->id << 13;/* Add controller number */
  239. if (CANAF_std_cnt == 0)
  240. {
  241. return CAN_ENTRY_NOT_EXIT_ERROR;
  242. }
  243. else if (CANAF_std_cnt == 1)
  244. {
  245. cnt2 = (CANAF_FullCAN_cnt + 1) >> 1;
  246. if(id != LPC_CANAF_RAM->mask[cnt2] >> 16) {
  247. return CAN_ENTRY_NOT_EXIT_ERROR;
  248. }
  249. }
  250. else
  251. {
  252. cnt1 = (CANAF_FullCAN_cnt+1)>>1;
  253. bound1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
  254. while (cnt1 < bound1)
  255. {
  256. /* Loop through standard existing IDs */
  257. if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) == id)
  258. {
  259. *pos = cnt1 * 2;
  260. return CAN_OK;
  261. }
  262. if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) == id)
  263. {
  264. *pos = cnt1 * 2 + 1;
  265. return CAN_OK;
  266. }
  267. if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > id)
  268. {
  269. return CAN_ENTRY_NOT_EXIT_ERROR;
  270. }
  271. if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > id)
  272. {
  273. return CAN_ENTRY_NOT_EXIT_ERROR;
  274. }
  275. cnt1++;
  276. }
  277. return CAN_ENTRY_NOT_EXIT_ERROR;
  278. }
  279. }
  280. /*********** Add Explicit Extended Identifier Frame Format entry *********/
  281. else
  282. {
  283. /* Add controller number */
  284. id |= plpccan->id << 29;
  285. cnt1 = ((CANAF_FullCAN_cnt+1) >> 1) + (((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt);
  286. cnt2 = 0;
  287. while (cnt2 < CANAF_ext_cnt)
  288. {
  289. /* Loop through extended existing masks*/
  290. if (LPC_CANAF_RAM->mask[cnt1] == id)
  291. {
  292. *pos = cnt2;
  293. return CAN_OK;
  294. }
  295. if (LPC_CANAF_RAM->mask[cnt1] > id)
  296. {
  297. return CAN_ENTRY_NOT_EXIT_ERROR;
  298. }
  299. cnt1++;
  300. cnt2++;
  301. }
  302. }
  303. } else {
  304. rt_uint32_t lowerID = pitem->id;
  305. rt_uint32_t upperID = pitem->mask;
  306. rt_uint32_t LID,UID;
  307. if(lowerID > upperID)
  308. return CAN_CONFLICT_ID_ERROR;
  309. if(format == STD_ID_FORMAT)
  310. {
  311. lowerID &=0x7FF; //mask ID
  312. upperID &=0x7FF;
  313. cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1);
  314. if(CANAF_gstd_cnt == 0)
  315. {
  316. return CAN_ENTRY_NOT_EXIT_ERROR;
  317. }
  318. else
  319. {
  320. bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt;
  321. while(cnt1 < bound1)
  322. { //compare controller first
  323. while((LPC_CANAF_RAM->mask[cnt1] >> 29) < (plpccan->id))//increase until meet greater or equal controller
  324. cnt1++;
  325. buf0 = LPC_CANAF_RAM->mask[cnt1];
  326. if((LPC_CANAF_RAM->mask[cnt1] >> 29) > (plpccan->id)) //meet greater controller
  327. {
  328. return CAN_ENTRY_NOT_EXIT_ERROR;
  329. }
  330. else //meet equal controller
  331. {
  332. LID = (buf0 >> 16)&0x7FF;
  333. UID = buf0 & 0x7FF;
  334. if (upperID == LID && lowerID == UID)
  335. {
  336. *pos = cnt1;
  337. return CAN_OK;
  338. }
  339. if (upperID < LID)
  340. {
  341. return CAN_ENTRY_NOT_EXIT_ERROR;
  342. }
  343. else if (lowerID >= UID)
  344. {
  345. cnt1 ++;
  346. }
  347. else
  348. return CAN_CONFLICT_ID_ERROR;
  349. }
  350. }
  351. if(cnt1 >= bound1)
  352. {
  353. return CAN_ENTRY_NOT_EXIT_ERROR;
  354. }
  355. }
  356. }
  357. /*********Add Group of Extended Identifier Frame Format************/
  358. else
  359. {
  360. lowerID &= 0x1FFFFFFF; //mask ID
  361. upperID &= 0x1FFFFFFF;
  362. cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt + CANAF_ext_cnt;
  363. //if this is the first Group standard ID entry
  364. if(CANAF_gext_cnt == 0)
  365. {
  366. return CAN_ENTRY_NOT_EXIT_ERROR;
  367. }
  368. else
  369. {
  370. bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt \
  371. + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
  372. while(cnt1 < bound1)
  373. {
  374. while((LPC_CANAF_RAM->mask[cnt1] >>29)< plpccan->id ) //increase until meet greater or equal controller
  375. cnt1++;
  376. buf0 = LPC_CANAF_RAM->mask[cnt1];
  377. buf1 = LPC_CANAF_RAM->mask[cnt1+1];
  378. if((LPC_CANAF_RAM->mask[cnt1] >> 29) > plpccan->id ) //meet greater controller
  379. {
  380. return CAN_ENTRY_NOT_EXIT_ERROR;
  381. }
  382. else //meet equal controller
  383. {
  384. LID = buf0 & 0x1FFFFFFF; //mask ID
  385. UID = buf1 & 0x1FFFFFFF;
  386. if (upperID == LID && lowerID == UID)
  387. {
  388. *pos = cnt1;
  389. return CAN_OK;
  390. }
  391. if (upperID < LID)
  392. {
  393. return CAN_ENTRY_NOT_EXIT_ERROR;
  394. }
  395. else if (lowerID >= UID)
  396. {
  397. //load next entry to compare
  398. cnt1 +=2;
  399. }
  400. else
  401. return CAN_CONFLICT_ID_ERROR;
  402. }
  403. }
  404. if(cnt1 >= bound1)
  405. {
  406. return CAN_ENTRY_NOT_EXIT_ERROR;
  407. }
  408. }
  409. }
  410. }
  411. return CAN_ENTRY_NOT_EXIT_ERROR;
  412. }
  413. static rt_err_t setfilter(struct lpccandata* plpccan,struct rt_can_filter_config *pconfig)
  414. {
  415. struct rt_can_filter_item* pitem = pconfig->items;
  416. rt_uint32_t count = pconfig->count;
  417. rt_int32_t pos;
  418. CAN_ID_FORMAT_Type format;
  419. CAN_ERROR lpccanres;
  420. while(count) {
  421. if(pitem->ide) {
  422. format = EXT_ID_FORMAT;
  423. } else {
  424. format = STD_ID_FORMAT;
  425. }
  426. lpccanres = findfilter(plpccan, pitem, &pos);
  427. if(pconfig->actived && lpccanres != CAN_OK) {
  428. if(pitem->mode) {
  429. lpccanres = CAN_LoadGroupEntry(plpccan->id, pitem->id, pitem->mask, format);
  430. } else {
  431. lpccanres = CAN_LoadExplicitEntry(plpccan->id, pitem->id, format);
  432. }
  433. } else if(!pconfig->actived && lpccanres == CAN_OK) {
  434. AFLUT_ENTRY_Type type;
  435. if(pitem->mode) {
  436. if(format == EXT_ID_FORMAT) {
  437. type = GROUP_EXTEND_ENTRY;
  438. } else {
  439. type = GROUP_STANDARD_ENTRY;
  440. }
  441. } else {
  442. if(format == EXT_ID_FORMAT) {
  443. type = EXPLICIT_EXTEND_ENTRY;
  444. } else {
  445. type = EXPLICIT_STANDARD_ENTRY;
  446. }
  447. }
  448. lpccanres = CAN_RemoveEntry(type, (rt_uint16_t)(pos));
  449. } else if(!pconfig->actived && lpccanres != CAN_OK) {
  450. lpccanres = CAN_OK;
  451. }
  452. if(lpccanres != CAN_OK) {
  453. return RT_EIO;
  454. }
  455. pitem++;
  456. count--;
  457. }
  458. return RT_EOK;
  459. }
  460. static rt_err_t control(struct rt_can_device *can, int cmd, void *arg)
  461. {
  462. struct lpccandata* plpccan;
  463. rt_uint32_t argval;
  464. CAN_MODE_Type mode;
  465. plpccan = (struct lpccandata* ) can->parent.user_data;
  466. RT_ASSERT(plpccan != RT_NULL);
  467. switch (cmd)
  468. {
  469. case RT_DEVICE_CTRL_CLR_INT:
  470. argval = (rt_uint32_t) arg;
  471. if(argval == RT_DEVICE_FLAG_INT_RX)
  472. {
  473. CAN_IRQCmd(plpccan->id, CANINT_RIE, DISABLE);
  474. CAN_IRQCmd(plpccan->id, CANINT_DOIE, DISABLE);
  475. } else if(argval == RT_DEVICE_FLAG_INT_TX) {
  476. CAN_IRQCmd(plpccan->id, CANINT_TIE1, DISABLE);
  477. CAN_IRQCmd(plpccan->id, CANINT_TIE2, DISABLE);
  478. CAN_IRQCmd(plpccan->id, CANINT_TIE3, DISABLE);
  479. } else if(argval == RT_DEVICE_CAN_INT_ERR) {
  480. CAN_IRQCmd(plpccan->id, CANINT_EIE, DISABLE);
  481. }
  482. break;
  483. case RT_DEVICE_CTRL_SET_INT:
  484. argval = (rt_uint32_t) arg;
  485. if(argval == RT_DEVICE_FLAG_INT_RX)
  486. {
  487. CAN_IRQCmd(plpccan->id, CANINT_RIE, ENABLE);
  488. CAN_IRQCmd(plpccan->id, CANINT_DOIE, ENABLE);
  489. } else if(argval == RT_DEVICE_FLAG_INT_TX) {
  490. CAN_IRQCmd(plpccan->id, CANINT_TIE1, ENABLE);
  491. CAN_IRQCmd(plpccan->id, CANINT_TIE2, ENABLE);
  492. CAN_IRQCmd(plpccan->id, CANINT_TIE3, ENABLE);
  493. } else if(argval == RT_DEVICE_CAN_INT_ERR) {
  494. CAN_IRQCmd(plpccan->id, CANINT_EIE, ENABLE);
  495. }
  496. break;
  497. case RT_CAN_CMD_SET_FILTER:
  498. return setfilter(plpccan, (struct rt_can_filter_config*) arg);
  499. case RT_CAN_CMD_SET_MODE:
  500. argval = (rt_uint32_t) arg;
  501. if(argval != RT_CAN_MODE_NORMAL ||
  502. argval != RT_CAN_MODE_LISEN) {
  503. return RT_ERROR;
  504. }
  505. if(argval != can->config.mode)
  506. {
  507. can->config.mode = argval;
  508. switch(argval) {
  509. case RT_CAN_MODE_NORMAL:
  510. mode = CAN_OPERATING_MODE;
  511. break;
  512. case RT_CAN_MODE_LISEN:
  513. mode = CAN_LISTENONLY_MODE;
  514. break;
  515. case RT_CAN_MODE_LOOPBACKANLISEN:
  516. mode = CAN_SELFTEST_MODE;
  517. break;
  518. default:
  519. return RT_EIO;
  520. }
  521. CAN_ModeConfig(plpccan->id, mode, ENABLE);
  522. if(mode == CAN_SELFTEST_MODE) {
  523. //CAN_ModeConfig(CAN_1, CAN_TEST_MODE, ENABLE);
  524. CAN_SetAFMode(CAN_ACC_BP);
  525. }
  526. }
  527. break;
  528. case RT_CAN_CMD_SET_BAUD:
  529. argval = (rt_uint32_t) arg;
  530. if(argval != CAN1MBaud &&
  531. argval != CAN800kBaud &&
  532. argval != CAN500kBaud &&
  533. argval != CAN250kBaud &&
  534. argval != CAN125kBaud &&
  535. argval != CAN100kBaud &&
  536. argval != CAN50kBaud &&
  537. argval != CAN20kBaud &&
  538. argval != CAN10kBaud ) {
  539. return RT_ERROR;
  540. }
  541. if(argval != can->config.baud_rate)
  542. {
  543. can->config.baud_rate = argval;
  544. lpccan_baud_set(plpccan->id, (rt_uint32_t) arg);
  545. }
  546. break;
  547. case RT_CAN_CMD_SET_PRIV:
  548. argval = (rt_uint32_t) arg;
  549. if(argval != RT_CAN_MODE_PRIV ||
  550. argval != RT_CAN_MODE_NOPRIV) {
  551. return RT_ERROR;
  552. }
  553. if(argval != can->config.privmode)
  554. {
  555. can->config.privmode = argval;
  556. CAN_ModeConfig(plpccan->id, CAN_TXPRIORITY_MODE, ENABLE);
  557. }
  558. break;
  559. case RT_CAN_CMD_GET_STATUS:
  560. {
  561. rt_uint32_t errtype;
  562. can->status.rcverrcnt = 0;
  563. can->status.snderrcnt = 0;
  564. can->status.errcode = 0;
  565. if(arg != &can->status) {
  566. rt_memcpy(arg,&can->status,sizeof(can->status));
  567. }
  568. }
  569. break;
  570. }
  571. return RT_EOK;
  572. }
  573. static int sendmsg(struct rt_can_device *can, const void* buf, rt_uint32_t boxno)
  574. {
  575. struct lpccandata* plpccan;
  576. plpccan = (struct lpccandata* ) can->parent.user_data;
  577. RT_ASSERT(plpccan != RT_NULL);
  578. LPC_CAN_TypeDef* pCan = lcpcan_get_reg_base(plpccan->id);
  579. RT_ASSERT(pCan != RT_NULL);
  580. struct rt_can_msg* pmsg = (struct rt_can_msg*) buf;
  581. rt_uint32_t SR_Mask;
  582. if(boxno > 2) {
  583. return RT_ERROR;
  584. }
  585. rt_uint32_t CMRMsk = 0x01 | (0x01 << (boxno + 5));
  586. SR_Mask = 0x01 <<(boxno * 8 + 2);
  587. volatile unsigned int *pTFI = (&pCan->TFI1 + 0 + 4 * boxno);
  588. volatile unsigned int *pTID = (&pCan->TFI1 + 1 + 4 * boxno);
  589. volatile unsigned int *pTDA = (&pCan->TFI1 + 2 + 4 * boxno);
  590. volatile unsigned int *pTDB = (&pCan->TFI1 + 3 + 4 * boxno);
  591. rt_uint32_t data;
  592. if(pCan->SR & SR_Mask) {
  593. /* Transmit Channel 1 is available */
  594. /* Write frame informations and frame data into its CANxTFI1,
  595. * CANxTID1, CANxTDA1, CANxTDB1 register */
  596. *pTFI &= ~ 0x000F0000;
  597. *pTFI |= (pmsg->len) << 16;
  598. if(pmsg->rtr == REMOTE_FRAME)
  599. {
  600. *pTFI |= (1 << 30); //set bit RTR
  601. }
  602. else
  603. {
  604. *pTFI &= ~(1 << 30);
  605. }
  606. if(pmsg->ide == EXT_ID_FORMAT)
  607. {
  608. *pTFI |= (((uint32_t)1) << 31); //set bit FF
  609. }
  610. else
  611. {
  612. *pTFI &= ~(((uint32_t)1) << 31);
  613. }
  614. if(can->config.privmode) {
  615. *pTFI &= ~0x000000FF;
  616. *pTFI |= pmsg->priv;
  617. }
  618. /* Write CAN ID*/
  619. *pTID = pmsg->id;
  620. /*Write first 4 data bytes*/
  621. data = (pmsg->data[0]) | (((pmsg->data[1]))<< 8) | ((pmsg->data[2]) << 16) | ((pmsg->data[3]) << 24);
  622. *pTDA = data;
  623. /*Write second 4 data bytes*/
  624. data = (pmsg->data[4]) | (((pmsg->data[5])) << 8) | ((pmsg->data[6]) << 16) | ((pmsg->data[7]) << 24);
  625. *pTDB = data;
  626. /*Write transmission request*/
  627. pCan->CMR = CMRMsk;
  628. return RT_EOK;
  629. } else {
  630. return RT_ERROR;
  631. }
  632. }
  633. static int recvmsg(struct rt_can_device *can, void* buf, rt_uint32_t boxno)
  634. {
  635. struct lpccandata* plpccan;
  636. plpccan = (struct lpccandata* ) can->parent.user_data;
  637. RT_ASSERT(plpccan != RT_NULL);
  638. LPC_CAN_TypeDef* pCan = lcpcan_get_reg_base(plpccan->id);
  639. RT_ASSERT(pCan != RT_NULL);
  640. //CAN_ReceiveMsg
  641. uint32_t data;
  642. struct rt_can_msg* pmsg = (struct rt_can_msg*) buf;
  643. //check status of Receive Buffer
  644. if((pCan->SR &0x00000001))
  645. {
  646. /* Receive message is available */
  647. /* Read frame informations */
  648. pmsg->ide = (uint8_t)(((pCan->RFS) & 0x80000000) >> 31);
  649. pmsg->rtr = (uint8_t)(((pCan->RFS) & 0x40000000) >> 30);
  650. pmsg->len = (uint8_t)(((pCan->RFS) & 0x000F0000) >> 16);
  651. /* Read CAN message identifier */
  652. pmsg->id = pCan->RID;
  653. /* Read the data if received message was DATA FRAME */
  654. if (!pmsg->rtr)
  655. {
  656. /* Read first 4 data bytes */
  657. data = pCan->RDA;
  658. pmsg->data[0] = data & 0x000000FF;
  659. pmsg->data[1] = (data & 0x0000FF00) >> 8;
  660. pmsg->data[2] = (data & 0x00FF0000) >> 16;
  661. pmsg->data[3] = (data & 0xFF000000) >> 24;
  662. /* Read second 4 data bytes */
  663. if(pmsg->len > 4) {
  664. data = pCan->RDB;
  665. pmsg->data[4] = data & 0x000000FF;
  666. pmsg->data[5] = (data & 0x0000FF00) >> 8;
  667. pmsg->data[6] = (data & 0x00FF0000) >> 16;
  668. pmsg->data[7] = (data & 0xFF000000) >> 24;
  669. }
  670. pmsg->hdr = 0;
  671. /*release receive buffer*/
  672. pCan->CMR = 0x04;
  673. }
  674. else
  675. {
  676. /* Received Frame is a Remote Frame, not have data, we just receive
  677. * message information only */
  678. pCan->CMR = 0x04; /*release receive buffer*/
  679. return SUCCESS;
  680. }
  681. }
  682. else
  683. {
  684. // no receive message available
  685. return ERROR;
  686. }
  687. return RT_EOK;
  688. }
  689. static const struct rt_can_ops canops =
  690. {
  691. configure,
  692. control,
  693. sendmsg,
  694. recvmsg,
  695. };
  696. #ifdef RT_USING_LPCCAN1
  697. #ifdef RT_CAN_USING_LED
  698. #endif
  699. static struct lpccandata lpccandata1 = {
  700. CAN_1,
  701. };
  702. static struct rt_can_device lpccan1;
  703. #endif /*RT_USINGLPCCAN1*/
  704. #ifdef RT_USING_LPCCAN2
  705. #ifdef RT_CAN_USING_LED
  706. #endif
  707. static struct lpccandata lpccandata2 = {
  708. CAN_2,
  709. };
  710. static struct rt_can_device lpccan2;
  711. #endif /*RT_USINGLPCCAN2*/
  712. /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
  713. /*********************************************************************//**
  714. * @brief Event Router IRQ Handler
  715. * @param[in] None
  716. * @return None
  717. **********************************************************************/
  718. void CAN_IRQHandler(void)
  719. {
  720. rt_uint32_t IntStatus;
  721. #ifdef RT_USING_LPCCAN1
  722. IntStatus = CAN_IntGetStatus(CAN_1);
  723. //check receive interrupt
  724. if((IntStatus >> 0) & 0x01)
  725. {
  726. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_RX_IND | 0<<8);
  727. }
  728. //check Transmit Interrupt interrupt1
  729. if((IntStatus >> 1) & 0x01)
  730. {
  731. rt_uint32_t state = 0;
  732. state = CAN_GetCTRLStatus(CAN_1, CANCTRL_STS);
  733. if(state & (0x01 << 3))
  734. {
  735. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 0<<8);
  736. } else {
  737. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 0<<8);
  738. }
  739. }
  740. //check Error Warning Interrupt
  741. if((IntStatus >> 2) & 0x01)
  742. {
  743. rt_uint32_t errtype;
  744. errtype = (IntStatus >> 16);
  745. if(errtype & 0x1F && lpccan1.status.lasterrtype == (errtype & 0x1F)) {
  746. switch((errtype & 0x1F)) {
  747. case 00011: // Start of Frame
  748. case 00010: // ID28 ... ID21
  749. case 00110: //ID20 ... ID18
  750. case 00100: // SRTR Bit
  751. case 00101: // IDE bit
  752. case 00111: // ID17 ... 13
  753. case 01111: // ID12 ... ID5
  754. case 01110: // ID4 ... ID0
  755. case 01100: // RTR Bit
  756. case 01011: // Data Length Code
  757. case 01010: // Data Field
  758. lpccan1.status.formaterrcnt++;
  759. break;
  760. case 01101: // Reserved Bit 1
  761. case 01001: // Reserved Bit 0
  762. lpccan1.status.bitpaderrcnt++;
  763. break;
  764. case 01000: // CRC Sequence
  765. case 11000: // CRC Delimiter
  766. lpccan1.status.crcerrcnt++;
  767. break;
  768. case 11001: // Acknowledge Slot
  769. case 11011: // Acknowledge Delimiter
  770. lpccan1.status.ackerrcnt++;
  771. break;
  772. case 11010: // End of Frame
  773. case 10010: // Intermission
  774. lpccan1.status.formaterrcnt++;
  775. break;
  776. }
  777. lpccan1.status.lasterrtype = errtype & 0x1F;
  778. }
  779. rt_uint32_t state = 0;
  780. state = CAN_GetCTRLStatus(CAN_1, CANCTRL_GLOBAL_STS);
  781. lpccan1.status.rcverrcnt = (state >> 16) & 0xFF;
  782. lpccan1.status.snderrcnt = (state >> 24) & 0xFF;
  783. lpccan1.status.errcode = (state >> 5) & 0x06;
  784. }
  785. //check Data Overrun Interrupt Interrupt
  786. if((IntStatus >> 3) & 0x01)
  787. {
  788. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_RXOF_IND | 0<<8);
  789. }
  790. //check Transmit Interrupt interrupt2
  791. if((IntStatus >> 9) & 0x01)
  792. {
  793. rt_uint32_t state = 0;
  794. state = CAN_GetCTRLStatus(CAN_1, CANCTRL_STS);
  795. if(state & (0x01 << 11))
  796. {
  797. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 1<<8);
  798. } else {
  799. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 1<<8);
  800. }
  801. }
  802. //check Transmit Interrupt interrupt3
  803. if((IntStatus >> 10) & 0x01)
  804. {
  805. rt_uint32_t state = 0;
  806. state = CAN_GetCTRLStatus(CAN_1, CANCTRL_STS);
  807. if(state & (0x01 << 19))
  808. {
  809. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 2<<8);
  810. } else {
  811. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 2<<8);
  812. }
  813. }
  814. #endif /*RT_USING_LPCCAN1*/
  815. #ifdef RT_USING_LPCCAN2
  816. IntStatus = CAN_IntGetStatus(CAN_2);
  817. //check receive interrupt
  818. if((IntStatus >> 0) & 0x01)
  819. {
  820. rt_hw_can_isr(&lpccan2,RT_CAN_EVENT_RX_IND | 0<<8);
  821. }
  822. //check Transmit Interrupt interrupt1
  823. if((IntStatus >> 1) & 0x01)
  824. {
  825. rt_uint32_t state = 0;
  826. state = CAN_GetCTRLStatus(CAN_2, CANCTRL_STS);
  827. if(state & (0x01 << 3))
  828. {
  829. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 0<<8);
  830. } else {
  831. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 0<<8);
  832. }
  833. }
  834. //check Error Warning Interrupt
  835. if((IntStatus >> 2) & 0x01)
  836. {
  837. rt_uint32_t errtype;
  838. errtype = (IntStatus >> 16);
  839. if(errtype & 0x1F && lpccan2.status.lasterrtype == (errtype & 0x1F)) {
  840. switch((errtype & 0x1F)) {
  841. case 00011: // Start of Frame
  842. case 00010: // ID28 ... ID21
  843. case 00110: //ID20 ... ID18
  844. case 00100: // SRTR Bit
  845. case 00101: // IDE bit
  846. case 00111: // ID17 ... 13
  847. case 01111: // ID12 ... ID5
  848. case 01110: // ID4 ... ID0
  849. case 01100: // RTR Bit
  850. case 01011: // Data Length Code
  851. case 01010: // Data Field
  852. lpccan2.status.formaterrcnt++;
  853. break;
  854. case 01101: // Reserved Bit 1
  855. case 01001: // Reserved Bit 0
  856. lpccan2.status.bitpaderrcnt++;
  857. break;
  858. case 01000: // CRC Sequence
  859. case 11000: // CRC Delimiter
  860. lpccan2.status.crcerrcnt++;
  861. break;
  862. case 11001: // Acknowledge Slot
  863. case 11011: // Acknowledge Delimiter
  864. lpccan2.status.ackerrcnt++;
  865. break;
  866. case 11010: // End of Frame
  867. case 10010: // Intermission
  868. lpccan2.status.formaterrcnt++;
  869. break;
  870. }
  871. lpccan2.status.lasterrtype = errtype & 0x1F;
  872. }
  873. rt_uint32_t state = 0;
  874. state = CAN_GetCTRLStatus(CAN_2, CANCTRL_GLOBAL_STS);
  875. lpccan2.status.rcverrcnt = (state >> 16) & 0xFF;
  876. lpccan2.status.snderrcnt = (state >> 24) & 0xFF;
  877. lpccan2.status.errcode = (state >> 5) & 0x06;
  878. }
  879. //check Data Overrun Interrupt Interrupt
  880. if((IntStatus >> 3) & 0x01)
  881. {
  882. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_RXOF_IND | 0<<8);
  883. }
  884. //check Transmit Interrupt interrupt2
  885. if((IntStatus >> 9) & 0x01)
  886. {
  887. rt_uint32_t state = 0;
  888. state = CAN_GetCTRLStatus(CAN_2, CANCTRL_STS);
  889. if(state & (0x01 << 11))
  890. {
  891. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 1<<8);
  892. } else {
  893. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 1<<8);
  894. }
  895. }
  896. //check Transmit Interrupt interrupt3
  897. if((IntStatus >> 10) & 0x01)
  898. {
  899. rt_uint32_t state = 0;
  900. state = CAN_GetCTRLStatus(CAN_2, CANCTRL_STS);
  901. if(state & (0x01 << 19))
  902. {
  903. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_DONE | 2<<8);
  904. } else {
  905. rt_hw_can_isr(&lpccan1,RT_CAN_EVENT_TX_FAIL | 2<<8);
  906. }
  907. }
  908. #endif /*RT_USING_LPCCAN2*/
  909. }
  910. int lpc_can_init(void)
  911. {
  912. #ifdef RT_USING_LPCCAN1
  913. lpccan1.config.baud_rate=CAN1MBaud;
  914. lpccan1.config.msgboxsz=16;
  915. lpccan1.config.sndboxnumber=3;
  916. lpccan1.config.mode=RT_CAN_MODE_NORMAL;
  917. lpccan1.config.privmode=0;
  918. #ifdef RT_CAN_USING_LED
  919. #endif
  920. lpccan1.config.ticks = 50;
  921. #ifdef RT_CAN_USING_HDR
  922. #endif
  923. //Enable CAN Interrupt
  924. NVIC_EnableIRQ(CAN_IRQn);
  925. rt_hw_can_register(&lpccan1, "lpccan1", &canops, &lpccandata1);
  926. #endif
  927. #ifdef RT_USING_LPCCAN2
  928. lpccan2.config.baud_rate=CAN1MBaud;
  929. lpccan2.config.msgboxsz=16;
  930. lpccan2.config.sndboxnumber=3;
  931. lpccan2.config.mode=RT_CAN_MODE_NORMAL;
  932. lpccan2.config.privmode=0;
  933. #ifdef RT_CAN_USING_LED
  934. #endif
  935. lpccan2.config.ticks = 50;
  936. #ifdef RT_CAN_USING_HDR
  937. #endif
  938. #ifndef RT_USING_LPCCAN1
  939. //Enable CAN Interrupt
  940. NVIC_EnableIRQ(CAN_IRQn);
  941. #endif
  942. #ifdef RT_CAN_USING_HDR
  943. #endif
  944. rt_hw_can_register(&lpccan2, "lpccan2", &canops, &lpccandata2);
  945. #endif
  946. return RT_EOK;
  947. }
  948. INIT_BOARD_EXPORT(lpc_can_init);
  949. #endif /*RT_USING_CAN*/