can.c 27 KB

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