can.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-05-14 aubrcool@qq.com first version
  9. * 2015-07-06 Bernard code cleanup and remove RT_CAN_USING_LED;
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #define CAN_LOCK(can) rt_mutex_take(&(can->lock), RT_WAITING_FOREVER)
  15. #define CAN_UNLOCK(can) rt_mutex_release(&(can->lock))
  16. static rt_err_t rt_can_init(struct rt_device *dev)
  17. {
  18. rt_err_t result = RT_EOK;
  19. struct rt_can_device *can;
  20. RT_ASSERT(dev != RT_NULL);
  21. can = (struct rt_can_device *)dev;
  22. /* initialize rx/tx */
  23. can->can_rx = RT_NULL;
  24. can->can_tx = RT_NULL;
  25. #ifdef RT_CAN_USING_HDR
  26. can->hdr = RT_NULL;
  27. #endif
  28. /* apply configuration */
  29. if (can->ops->configure)
  30. result = can->ops->configure(can, &can->config);
  31. else
  32. result = -RT_ENOSYS;
  33. return result;
  34. }
  35. /*
  36. * can interrupt routines
  37. */
  38. rt_inline int _can_int_rx(struct rt_can_device *can, struct rt_can_msg *data, int msgs)
  39. {
  40. int size;
  41. struct rt_can_rx_fifo *rx_fifo;
  42. RT_ASSERT(can != RT_NULL);
  43. size = msgs;
  44. rx_fifo = (struct rt_can_rx_fifo *) can->can_rx;
  45. RT_ASSERT(rx_fifo != RT_NULL);
  46. /* read from software FIFO */
  47. while (msgs)
  48. {
  49. rt_base_t level;
  50. #ifdef RT_CAN_USING_HDR
  51. rt_int8_t hdr;
  52. #endif /*RT_CAN_USING_HDR*/
  53. struct rt_can_msg_list *listmsg = RT_NULL;
  54. /* disable interrupt */
  55. level = rt_hw_interrupt_disable();
  56. #ifdef RT_CAN_USING_HDR
  57. hdr = data->hdr_index;
  58. if (hdr >= 0 && can->hdr && hdr < can->config.maxhdr && !rt_list_isempty(&can->hdr[hdr].list))
  59. {
  60. listmsg = rt_list_entry(can->hdr[hdr].list.next, struct rt_can_msg_list, hdrlist);
  61. rt_list_remove(&listmsg->list);
  62. rt_list_remove(&listmsg->hdrlist);
  63. if (can->hdr[hdr].msgs)
  64. {
  65. can->hdr[hdr].msgs--;
  66. }
  67. listmsg->owner = RT_NULL;
  68. }
  69. else if (hdr == -1)
  70. #endif /*RT_CAN_USING_HDR*/
  71. {
  72. if (!rt_list_isempty(&rx_fifo->uselist))
  73. {
  74. listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
  75. rt_list_remove(&listmsg->list);
  76. #ifdef RT_CAN_USING_HDR
  77. rt_list_remove(&listmsg->hdrlist);
  78. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  79. {
  80. listmsg->owner->msgs--;
  81. }
  82. listmsg->owner = RT_NULL;
  83. #endif /*RT_CAN_USING_HDR*/
  84. }
  85. else
  86. {
  87. /* no data, enable interrupt and break out */
  88. rt_hw_interrupt_enable(level);
  89. break;
  90. }
  91. }
  92. /* enable interrupt */
  93. rt_hw_interrupt_enable(level);
  94. if (listmsg != RT_NULL)
  95. {
  96. rt_memcpy(data, &listmsg->data, sizeof(struct rt_can_msg));
  97. level = rt_hw_interrupt_disable();
  98. rt_list_insert_before(&rx_fifo->freelist, &listmsg->list);
  99. rx_fifo->freenumbers++;
  100. RT_ASSERT(rx_fifo->freenumbers <= can->config.msgboxsz);
  101. rt_hw_interrupt_enable(level);
  102. listmsg = RT_NULL;
  103. }
  104. else
  105. {
  106. break;
  107. }
  108. data ++;
  109. msgs -= sizeof(struct rt_can_msg);
  110. }
  111. return (size - msgs);
  112. }
  113. rt_inline int _can_int_tx(struct rt_can_device *can, const struct rt_can_msg *data, int msgs)
  114. {
  115. int size;
  116. struct rt_can_tx_fifo *tx_fifo;
  117. RT_ASSERT(can != RT_NULL);
  118. size = msgs;
  119. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  120. RT_ASSERT(tx_fifo != RT_NULL);
  121. while (msgs)
  122. {
  123. rt_base_t level;
  124. rt_uint32_t no;
  125. rt_uint32_t result;
  126. struct rt_can_sndbxinx_list *tx_tosnd = RT_NULL;
  127. rt_sem_take(&(tx_fifo->sem), RT_WAITING_FOREVER);
  128. level = rt_hw_interrupt_disable();
  129. tx_tosnd = rt_list_entry(tx_fifo->freelist.next, struct rt_can_sndbxinx_list, list);
  130. RT_ASSERT(tx_tosnd != RT_NULL);
  131. rt_list_remove(&tx_tosnd->list);
  132. rt_hw_interrupt_enable(level);
  133. no = ((rt_uint32_t)tx_tosnd - (rt_uint32_t)tx_fifo->buffer) / sizeof(struct rt_can_sndbxinx_list);
  134. tx_tosnd->result = RT_CAN_SND_RESULT_WAIT;
  135. if (can->ops->sendmsg(can, data, no) != RT_EOK)
  136. {
  137. /* send failed. */
  138. level = rt_hw_interrupt_disable();
  139. rt_list_insert_before(&tx_fifo->freelist, &tx_tosnd->list);
  140. rt_hw_interrupt_enable(level);
  141. rt_sem_release(&(tx_fifo->sem));
  142. goto err_ret;
  143. }
  144. can->status.sndchange = 1;
  145. rt_completion_wait(&(tx_tosnd->completion), RT_WAITING_FOREVER);
  146. level = rt_hw_interrupt_disable();
  147. result = tx_tosnd->result;
  148. if (!rt_list_isempty(&tx_tosnd->list))
  149. {
  150. rt_list_remove(&tx_tosnd->list);
  151. }
  152. rt_list_insert_before(&tx_fifo->freelist, &tx_tosnd->list);
  153. rt_hw_interrupt_enable(level);
  154. rt_sem_release(&(tx_fifo->sem));
  155. if (result == RT_CAN_SND_RESULT_OK)
  156. {
  157. level = rt_hw_interrupt_disable();
  158. can->status.sndpkg++;
  159. rt_hw_interrupt_enable(level);
  160. data ++;
  161. msgs -= sizeof(struct rt_can_msg);
  162. if (!msgs) break;
  163. }
  164. else
  165. {
  166. err_ret:
  167. level = rt_hw_interrupt_disable();
  168. can->status.dropedsndpkg++;
  169. rt_hw_interrupt_enable(level);
  170. break;
  171. }
  172. }
  173. return (size - msgs);
  174. }
  175. rt_inline int _can_int_tx_priv(struct rt_can_device *can, const struct rt_can_msg *data, int msgs)
  176. {
  177. int size;
  178. rt_base_t level;
  179. rt_uint32_t no, result;
  180. struct rt_can_tx_fifo *tx_fifo;
  181. RT_ASSERT(can != RT_NULL);
  182. size = msgs;
  183. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  184. RT_ASSERT(tx_fifo != RT_NULL);
  185. while (msgs)
  186. {
  187. no = data->priv;
  188. if (no >= can->config.sndboxnumber)
  189. {
  190. break;
  191. }
  192. level = rt_hw_interrupt_disable();
  193. if ((tx_fifo->buffer[no].result != RT_CAN_SND_RESULT_OK))
  194. {
  195. rt_hw_interrupt_enable(level);
  196. rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER);
  197. continue;
  198. }
  199. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_WAIT;
  200. rt_hw_interrupt_enable(level);
  201. if (can->ops->sendmsg(can, data, no) != RT_EOK)
  202. {
  203. continue;
  204. }
  205. can->status.sndchange = 1;
  206. rt_completion_wait(&(tx_fifo->buffer[no].completion), RT_WAITING_FOREVER);
  207. result = tx_fifo->buffer[no].result;
  208. if (result == RT_CAN_SND_RESULT_OK)
  209. {
  210. level = rt_hw_interrupt_disable();
  211. can->status.sndpkg++;
  212. rt_hw_interrupt_enable(level);
  213. data ++;
  214. msgs -= sizeof(struct rt_can_msg);
  215. if (!msgs) break;
  216. }
  217. else
  218. {
  219. level = rt_hw_interrupt_disable();
  220. can->status.dropedsndpkg++;
  221. rt_hw_interrupt_enable(level);
  222. break;
  223. }
  224. }
  225. return (size - msgs);
  226. }
  227. static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
  228. {
  229. struct rt_can_device *can;
  230. char tmpname[16];
  231. RT_ASSERT(dev != RT_NULL);
  232. can = (struct rt_can_device *)dev;
  233. CAN_LOCK(can);
  234. /* get open flags */
  235. dev->open_flag = oflag & 0xff;
  236. if (can->can_rx == RT_NULL)
  237. {
  238. if (oflag & RT_DEVICE_FLAG_INT_RX)
  239. {
  240. int i = 0;
  241. struct rt_can_rx_fifo *rx_fifo;
  242. rx_fifo = (struct rt_can_rx_fifo *) rt_malloc(sizeof(struct rt_can_rx_fifo) +
  243. can->config.msgboxsz * sizeof(struct rt_can_msg_list));
  244. RT_ASSERT(rx_fifo != RT_NULL);
  245. rx_fifo->buffer = (struct rt_can_msg_list *)(rx_fifo + 1);
  246. rt_memset(rx_fifo->buffer, 0, can->config.msgboxsz * sizeof(struct rt_can_msg_list));
  247. rt_list_init(&rx_fifo->freelist);
  248. rt_list_init(&rx_fifo->uselist);
  249. rx_fifo->freenumbers = can->config.msgboxsz;
  250. for (i = 0; i < can->config.msgboxsz; i++)
  251. {
  252. rt_list_insert_before(&rx_fifo->freelist, &rx_fifo->buffer[i].list);
  253. #ifdef RT_CAN_USING_HDR
  254. rt_list_init(&rx_fifo->buffer[i].hdrlist);
  255. rx_fifo->buffer[i].owner = RT_NULL;
  256. #endif
  257. }
  258. can->can_rx = rx_fifo;
  259. dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
  260. /* open can rx interrupt */
  261. can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  262. }
  263. }
  264. if (can->can_tx == RT_NULL)
  265. {
  266. if (oflag & RT_DEVICE_FLAG_INT_TX)
  267. {
  268. int i = 0;
  269. struct rt_can_tx_fifo *tx_fifo;
  270. tx_fifo = (struct rt_can_tx_fifo *) rt_malloc(sizeof(struct rt_can_tx_fifo) +
  271. can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
  272. RT_ASSERT(tx_fifo != RT_NULL);
  273. tx_fifo->buffer = (struct rt_can_sndbxinx_list *)(tx_fifo + 1);
  274. rt_memset(tx_fifo->buffer, 0,
  275. can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
  276. rt_list_init(&tx_fifo->freelist);
  277. for (i = 0; i < can->config.sndboxnumber; i++)
  278. {
  279. rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
  280. rt_completion_init(&(tx_fifo->buffer[i].completion));
  281. tx_fifo->buffer[i].result = RT_CAN_SND_RESULT_OK;
  282. }
  283. rt_sprintf(tmpname, "%stl", dev->parent.name);
  284. rt_sem_init(&(tx_fifo->sem), tmpname, can->config.sndboxnumber, RT_IPC_FLAG_FIFO);
  285. can->can_tx = tx_fifo;
  286. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  287. /* open can tx interrupt */
  288. can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  289. }
  290. }
  291. can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_CAN_INT_ERR);
  292. #ifdef RT_CAN_USING_HDR
  293. if (can->hdr == RT_NULL)
  294. {
  295. int i = 0;
  296. struct rt_can_hdr *phdr;
  297. phdr = (struct rt_can_hdr *) rt_malloc(can->config.maxhdr * sizeof(struct rt_can_hdr));
  298. RT_ASSERT(phdr != RT_NULL);
  299. rt_memset(phdr, 0, can->config.maxhdr * sizeof(struct rt_can_hdr));
  300. for (i = 0; i < can->config.maxhdr; i++)
  301. {
  302. rt_list_init(&phdr[i].list);
  303. }
  304. can->hdr = phdr;
  305. }
  306. #endif
  307. if (!can->timerinitflag)
  308. {
  309. can->timerinitflag = 1;
  310. rt_timer_start(&can->timer);
  311. }
  312. CAN_UNLOCK(can);
  313. return RT_EOK;
  314. }
  315. static rt_err_t rt_can_close(struct rt_device *dev)
  316. {
  317. struct rt_can_device *can;
  318. RT_ASSERT(dev != RT_NULL);
  319. can = (struct rt_can_device *)dev;
  320. CAN_LOCK(can);
  321. /* this device has more reference count */
  322. if (dev->ref_count > 1)
  323. {
  324. CAN_UNLOCK(can);
  325. return RT_EOK;
  326. }
  327. if (can->timerinitflag)
  328. {
  329. can->timerinitflag = 0;
  330. rt_timer_stop(&can->timer);
  331. }
  332. can->status_indicate.ind = RT_NULL;
  333. can->status_indicate.args = RT_NULL;
  334. #ifdef RT_CAN_USING_HDR
  335. if (can->hdr != RT_NULL)
  336. {
  337. rt_free(can->hdr);
  338. can->hdr = RT_NULL;
  339. }
  340. #endif
  341. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  342. {
  343. struct rt_can_rx_fifo *rx_fifo;
  344. rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
  345. RT_ASSERT(rx_fifo != RT_NULL);
  346. rt_free(rx_fifo);
  347. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  348. can->can_rx = RT_NULL;
  349. /* clear can rx interrupt */
  350. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  351. }
  352. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  353. {
  354. struct rt_can_tx_fifo *tx_fifo;
  355. tx_fifo = (struct rt_can_tx_fifo *)can->can_tx;
  356. RT_ASSERT(tx_fifo != RT_NULL);
  357. rt_sem_detach(&(tx_fifo->sem));
  358. rt_free(tx_fifo);
  359. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  360. can->can_tx = RT_NULL;
  361. /* clear can tx interrupt */
  362. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  363. }
  364. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_CAN_INT_ERR);
  365. CAN_UNLOCK(can);
  366. return RT_EOK;
  367. }
  368. static rt_ssize_t rt_can_read(struct rt_device *dev,
  369. rt_off_t pos,
  370. void *buffer,
  371. rt_size_t size)
  372. {
  373. struct rt_can_device *can;
  374. RT_ASSERT(dev != RT_NULL);
  375. if (size == 0) return 0;
  376. can = (struct rt_can_device *)dev;
  377. if ((dev->open_flag & RT_DEVICE_FLAG_INT_RX) && (dev->ref_count > 0))
  378. {
  379. return _can_int_rx(can, buffer, size);
  380. }
  381. return 0;
  382. }
  383. static rt_ssize_t rt_can_write(struct rt_device *dev,
  384. rt_off_t pos,
  385. const void *buffer,
  386. rt_size_t size)
  387. {
  388. struct rt_can_device *can;
  389. RT_ASSERT(dev != RT_NULL);
  390. if (size == 0) return 0;
  391. can = (struct rt_can_device *)dev;
  392. if ((dev->open_flag & RT_DEVICE_FLAG_INT_TX) && (dev->ref_count > 0))
  393. {
  394. if (can->config.privmode)
  395. {
  396. return _can_int_tx_priv(can, buffer, size);
  397. }
  398. else
  399. {
  400. return _can_int_tx(can, buffer, size);
  401. }
  402. }
  403. return 0;
  404. }
  405. static rt_err_t rt_can_control(struct rt_device *dev,
  406. int cmd,
  407. void *args)
  408. {
  409. struct rt_can_device *can;
  410. rt_err_t res;
  411. res = RT_EOK;
  412. RT_ASSERT(dev != RT_NULL);
  413. can = (struct rt_can_device *)dev;
  414. switch (cmd)
  415. {
  416. case RT_DEVICE_CTRL_SUSPEND:
  417. /* suspend device */
  418. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  419. break;
  420. case RT_DEVICE_CTRL_RESUME:
  421. /* resume device */
  422. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  423. break;
  424. case RT_DEVICE_CTRL_CONFIG:
  425. /* configure device */
  426. res = can->ops->configure(can, (struct can_configure *)args);
  427. break;
  428. case RT_CAN_CMD_SET_PRIV:
  429. /* configure device */
  430. if ((rt_uint32_t)args != can->config.privmode)
  431. {
  432. int i;
  433. rt_base_t level;
  434. struct rt_can_tx_fifo *tx_fifo;
  435. res = can->ops->control(can, cmd, args);
  436. if (res != RT_EOK) return res;
  437. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  438. if (can->config.privmode)
  439. {
  440. for (i = 0; i < can->config.sndboxnumber; i++)
  441. {
  442. level = rt_hw_interrupt_disable();
  443. if(rt_list_isempty(&tx_fifo->buffer[i].list))
  444. {
  445. rt_sem_release(&(tx_fifo->sem));
  446. }
  447. else
  448. {
  449. rt_list_remove(&tx_fifo->buffer[i].list);
  450. }
  451. rt_hw_interrupt_enable(level);
  452. }
  453. }
  454. else
  455. {
  456. for (i = 0; i < can->config.sndboxnumber; i++)
  457. {
  458. level = rt_hw_interrupt_disable();
  459. if (tx_fifo->buffer[i].result == RT_CAN_SND_RESULT_OK)
  460. {
  461. rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
  462. }
  463. rt_hw_interrupt_enable(level);
  464. }
  465. }
  466. }
  467. break;
  468. case RT_CAN_CMD_SET_STATUS_IND:
  469. can->status_indicate.ind = ((rt_can_status_ind_type_t)args)->ind;
  470. can->status_indicate.args = ((rt_can_status_ind_type_t)args)->args;
  471. break;
  472. #ifdef RT_CAN_USING_HDR
  473. case RT_CAN_CMD_SET_FILTER:
  474. res = can->ops->control(can, cmd, args);
  475. if (res != RT_EOK || can->hdr == RT_NULL)
  476. {
  477. return res;
  478. }
  479. struct rt_can_filter_config *pfilter;
  480. struct rt_can_filter_item *pitem;
  481. rt_uint32_t count;
  482. rt_base_t level;
  483. pfilter = (struct rt_can_filter_config *)args;
  484. RT_ASSERT(pfilter);
  485. count = pfilter->count;
  486. pitem = pfilter->items;
  487. if (pfilter->actived)
  488. {
  489. while (count)
  490. {
  491. if (pitem->hdr_bank >= can->config.maxhdr || pitem->hdr_bank < 0)
  492. {
  493. count--;
  494. pitem++;
  495. continue;
  496. }
  497. level = rt_hw_interrupt_disable();
  498. if (!can->hdr[pitem->hdr_bank].connected)
  499. {
  500. rt_hw_interrupt_enable(level);
  501. rt_memcpy(&can->hdr[pitem->hdr_bank].filter, pitem,
  502. sizeof(struct rt_can_filter_item));
  503. level = rt_hw_interrupt_disable();
  504. can->hdr[pitem->hdr_bank].connected = 1;
  505. can->hdr[pitem->hdr_bank].msgs = 0;
  506. rt_list_init(&can->hdr[pitem->hdr_bank].list);
  507. }
  508. rt_hw_interrupt_enable(level);
  509. count--;
  510. pitem++;
  511. }
  512. }
  513. else
  514. {
  515. while (count)
  516. {
  517. if (pitem->hdr_bank >= can->config.maxhdr || pitem->hdr_bank < 0)
  518. {
  519. count--;
  520. pitem++;
  521. continue;
  522. }
  523. level = rt_hw_interrupt_disable();
  524. if (can->hdr[pitem->hdr_bank].connected)
  525. {
  526. can->hdr[pitem->hdr_bank].connected = 0;
  527. can->hdr[pitem->hdr_bank].msgs = 0;
  528. if (!rt_list_isempty(&can->hdr[pitem->hdr_bank].list))
  529. {
  530. rt_list_remove(can->hdr[pitem->hdr_bank].list.next);
  531. }
  532. rt_hw_interrupt_enable(level);
  533. rt_memset(&can->hdr[pitem->hdr_bank].filter, 0,
  534. sizeof(struct rt_can_filter_item));
  535. }
  536. else
  537. {
  538. rt_hw_interrupt_enable(level);
  539. }
  540. count--;
  541. pitem++;
  542. }
  543. }
  544. break;
  545. #endif /*RT_CAN_USING_HDR*/
  546. #ifdef RT_CAN_USING_BUS_HOOK
  547. case RT_CAN_CMD_SET_BUS_HOOK:
  548. can->bus_hook = (rt_can_bus_hook) args;
  549. break;
  550. #endif /*RT_CAN_USING_BUS_HOOK*/
  551. default :
  552. /* control device */
  553. if (can->ops->control != RT_NULL)
  554. {
  555. res = can->ops->control(can, cmd, args);
  556. }
  557. else
  558. {
  559. res = -RT_ENOSYS;
  560. }
  561. break;
  562. }
  563. return res;
  564. }
  565. /*
  566. * can timer
  567. */
  568. static void cantimeout(void *arg)
  569. {
  570. rt_can_t can;
  571. can = (rt_can_t)arg;
  572. RT_ASSERT(can);
  573. rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
  574. if (can->status_indicate.ind != RT_NULL)
  575. {
  576. can->status_indicate.ind(can, can->status_indicate.args);
  577. }
  578. #ifdef RT_CAN_USING_BUS_HOOK
  579. if(can->bus_hook)
  580. {
  581. can->bus_hook(can);
  582. }
  583. #endif /*RT_CAN_USING_BUS_HOOK*/
  584. if (can->timerinitflag == 1)
  585. {
  586. can->timerinitflag = 0xFF;
  587. }
  588. }
  589. #ifdef RT_USING_DEVICE_OPS
  590. const static struct rt_device_ops can_device_ops =
  591. {
  592. rt_can_init,
  593. rt_can_open,
  594. rt_can_close,
  595. rt_can_read,
  596. rt_can_write,
  597. rt_can_control
  598. };
  599. #endif
  600. /*
  601. * can register
  602. */
  603. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  604. const char *name,
  605. const struct rt_can_ops *ops,
  606. void *data)
  607. {
  608. struct rt_device *device;
  609. RT_ASSERT(can != RT_NULL);
  610. device = &(can->parent);
  611. device->type = RT_Device_Class_CAN;
  612. device->rx_indicate = RT_NULL;
  613. device->tx_complete = RT_NULL;
  614. #ifdef RT_CAN_USING_HDR
  615. can->hdr = RT_NULL;
  616. #endif
  617. can->can_rx = RT_NULL;
  618. can->can_tx = RT_NULL;
  619. rt_mutex_init(&(can->lock), "can", RT_IPC_FLAG_PRIO);
  620. #ifdef RT_CAN_USING_BUS_HOOK
  621. can->bus_hook = RT_NULL;
  622. #endif /*RT_CAN_USING_BUS_HOOK*/
  623. #ifdef RT_USING_DEVICE_OPS
  624. device->ops = &can_device_ops;
  625. #else
  626. device->init = rt_can_init;
  627. device->open = rt_can_open;
  628. device->close = rt_can_close;
  629. device->read = rt_can_read;
  630. device->write = rt_can_write;
  631. device->control = rt_can_control;
  632. #endif
  633. can->ops = ops;
  634. can->status_indicate.ind = RT_NULL;
  635. can->status_indicate.args = RT_NULL;
  636. rt_memset(&can->status, 0, sizeof(can->status));
  637. device->user_data = data;
  638. can->timerinitflag = 0;
  639. rt_timer_init(&can->timer,
  640. name,
  641. cantimeout,
  642. (void *)can,
  643. can->config.ticks,
  644. RT_TIMER_FLAG_PERIODIC);
  645. /* register a character device */
  646. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  647. }
  648. /* ISR for can interrupt */
  649. void rt_hw_can_isr(struct rt_can_device *can, int event)
  650. {
  651. switch (event & 0xff)
  652. {
  653. case RT_CAN_EVENT_RXOF_IND:
  654. {
  655. rt_base_t level;
  656. level = rt_hw_interrupt_disable();
  657. can->status.dropedrcvpkg++;
  658. rt_hw_interrupt_enable(level);
  659. }
  660. case RT_CAN_EVENT_RX_IND:
  661. {
  662. struct rt_can_msg tmpmsg;
  663. struct rt_can_rx_fifo *rx_fifo;
  664. struct rt_can_msg_list *listmsg = RT_NULL;
  665. #ifdef RT_CAN_USING_HDR
  666. rt_int8_t hdr;
  667. #endif
  668. int ch = -1;
  669. rt_base_t level;
  670. rt_uint32_t no;
  671. rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
  672. RT_ASSERT(rx_fifo != RT_NULL);
  673. /* interrupt mode receive */
  674. RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
  675. no = event >> 8;
  676. ch = can->ops->recvmsg(can, &tmpmsg, no);
  677. if (ch == -1) break;
  678. /* disable interrupt */
  679. level = rt_hw_interrupt_disable();
  680. can->status.rcvpkg++;
  681. can->status.rcvchange = 1;
  682. if (!rt_list_isempty(&rx_fifo->freelist))
  683. {
  684. listmsg = rt_list_entry(rx_fifo->freelist.next, struct rt_can_msg_list, list);
  685. rt_list_remove(&listmsg->list);
  686. #ifdef RT_CAN_USING_HDR
  687. rt_list_remove(&listmsg->hdrlist);
  688. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  689. {
  690. listmsg->owner->msgs--;
  691. }
  692. listmsg->owner = RT_NULL;
  693. #endif /*RT_CAN_USING_HDR*/
  694. RT_ASSERT(rx_fifo->freenumbers > 0);
  695. rx_fifo->freenumbers--;
  696. }
  697. else if (!rt_list_isempty(&rx_fifo->uselist))
  698. {
  699. listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
  700. can->status.dropedrcvpkg++;
  701. rt_list_remove(&listmsg->list);
  702. #ifdef RT_CAN_USING_HDR
  703. rt_list_remove(&listmsg->hdrlist);
  704. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  705. {
  706. listmsg->owner->msgs--;
  707. }
  708. listmsg->owner = RT_NULL;
  709. #endif
  710. }
  711. /* enable interrupt */
  712. rt_hw_interrupt_enable(level);
  713. if (listmsg != RT_NULL)
  714. {
  715. rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg));
  716. level = rt_hw_interrupt_disable();
  717. rt_list_insert_before(&rx_fifo->uselist, &listmsg->list);
  718. #ifdef RT_CAN_USING_HDR
  719. hdr = tmpmsg.hdr_index;
  720. if (can->hdr != RT_NULL)
  721. {
  722. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  723. if (can->hdr[hdr].connected)
  724. {
  725. rt_list_insert_before(&can->hdr[hdr].list, &listmsg->hdrlist);
  726. listmsg->owner = &can->hdr[hdr];
  727. can->hdr[hdr].msgs++;
  728. }
  729. }
  730. #endif
  731. rt_hw_interrupt_enable(level);
  732. }
  733. /* invoke callback */
  734. #ifdef RT_CAN_USING_HDR
  735. if (can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind)
  736. {
  737. rt_size_t rx_length;
  738. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  739. level = rt_hw_interrupt_disable();
  740. rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg);
  741. rt_hw_interrupt_enable(level);
  742. if (rx_length)
  743. {
  744. can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length);
  745. }
  746. }
  747. else
  748. #endif
  749. {
  750. if (can->parent.rx_indicate != RT_NULL)
  751. {
  752. rt_size_t rx_length;
  753. level = rt_hw_interrupt_disable();
  754. /* get rx length */
  755. rx_length = rt_list_len(&rx_fifo->uselist)* sizeof(struct rt_can_msg);
  756. rt_hw_interrupt_enable(level);
  757. if (rx_length)
  758. {
  759. can->parent.rx_indicate(&can->parent, rx_length);
  760. }
  761. }
  762. }
  763. break;
  764. }
  765. case RT_CAN_EVENT_TX_DONE:
  766. case RT_CAN_EVENT_TX_FAIL:
  767. {
  768. struct rt_can_tx_fifo *tx_fifo;
  769. rt_uint32_t no;
  770. no = event >> 8;
  771. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  772. RT_ASSERT(tx_fifo != RT_NULL);
  773. if ((event & 0xff) == RT_CAN_EVENT_TX_DONE)
  774. {
  775. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_OK;
  776. }
  777. else
  778. {
  779. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_ERR;
  780. }
  781. rt_completion_done(&(tx_fifo->buffer[no].completion));
  782. break;
  783. }
  784. }
  785. }
  786. #ifdef RT_USING_FINSH
  787. #include <finsh.h>
  788. int cmd_canstat(int argc, void **argv)
  789. {
  790. static const char *ErrCode[] =
  791. {
  792. "No Error!",
  793. "Warning !",
  794. "Passive !",
  795. "Bus Off !"
  796. };
  797. if (argc >= 2)
  798. {
  799. struct rt_can_status status;
  800. rt_device_t candev = rt_device_find(argv[1]);
  801. if (!candev)
  802. {
  803. rt_kprintf(" Can't find can device %s\n", argv[1]);
  804. return -1;
  805. }
  806. rt_kprintf(" Found can device: %s...", argv[1]);
  807. rt_device_control(candev, RT_CAN_CMD_GET_STATUS, &status);
  808. rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.",
  809. status.rcverrcnt, status.snderrcnt);
  810. rt_kprintf("\n Bit..pad..error..count: %010ld. Format...error....count: %010ld",
  811. status.bitpaderrcnt, status.formaterrcnt);
  812. rt_kprintf("\n Ack.......error..count: %010ld. Bit......error....count: %010ld.",
  813. status.ackerrcnt, status.biterrcnt);
  814. rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ",
  815. status.crcerrcnt, status.errcode);
  816. switch (status.errcode)
  817. {
  818. case 0:
  819. rt_kprintf("%s.", ErrCode[0]);
  820. break;
  821. case 1:
  822. rt_kprintf("%s.", ErrCode[1]);
  823. break;
  824. case 2:
  825. case 3:
  826. rt_kprintf("%s.", ErrCode[2]);
  827. break;
  828. case 4:
  829. case 5:
  830. case 6:
  831. case 7:
  832. rt_kprintf("%s.", ErrCode[3]);
  833. break;
  834. }
  835. rt_kprintf("\n Total.receive.packages: %010ld. Dropped.receive.packages: %010ld.",
  836. status.rcvpkg, status.dropedrcvpkg);
  837. rt_kprintf("\n Total..send...packages: %010ld. Dropped...send..packages: %010ld.\n",
  838. status.sndpkg + status.dropedsndpkg, status.dropedsndpkg);
  839. }
  840. else
  841. {
  842. rt_kprintf(" Invalid Call %s\n", argv[0]);
  843. rt_kprintf(" Please using %s cannamex .Here canname is driver name and x is candrive number.\n", argv[0]);
  844. }
  845. return 0;
  846. }
  847. MSH_CMD_EXPORT_ALIAS(cmd_canstat, canstat, stat can device status);
  848. #endif