can.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. }
  280. if (can->can_tx == RT_NULL)
  281. {
  282. if (oflag & RT_DEVICE_FLAG_INT_TX)
  283. {
  284. int i = 0;
  285. struct rt_can_tx_fifo *tx_fifo;
  286. tx_fifo = (struct rt_can_tx_fifo *) rt_malloc(sizeof(struct rt_can_tx_fifo) +
  287. can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
  288. RT_ASSERT(tx_fifo != RT_NULL);
  289. tx_fifo->buffer = (struct rt_can_sndbxinx_list *)(tx_fifo + 1);
  290. rt_memset(tx_fifo->buffer, 0,
  291. can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
  292. rt_list_init(&tx_fifo->freelist);
  293. for (i = 0; i < can->config.sndboxnumber; i++)
  294. {
  295. rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
  296. rt_completion_init(&(tx_fifo->buffer[i].completion));
  297. tx_fifo->buffer[i].result = RT_CAN_SND_RESULT_OK;
  298. }
  299. rt_completion_init(&(tx_fifo->completion));
  300. can->can_tx = tx_fifo;
  301. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  302. /* configure low level device */
  303. can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  304. }
  305. }
  306. can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_CAN_INT_ERR);
  307. #ifdef RT_CAN_USING_HDR
  308. if (can->hdr == RT_NULL)
  309. {
  310. int i = 0;
  311. struct rt_can_hdr *phdr;
  312. phdr = (struct rt_can_hdr *) rt_malloc(can->config.maxhdr * sizeof(struct rt_can_hdr));
  313. RT_ASSERT(phdr != RT_NULL);
  314. rt_memset(phdr, 0, can->config.maxhdr * sizeof(struct rt_can_hdr));
  315. for (i = 0; i < can->config.maxhdr; i++)
  316. {
  317. rt_list_init(&phdr[i].list);
  318. }
  319. can->hdr = phdr;
  320. }
  321. #endif
  322. if (!can->timerinitflag)
  323. {
  324. can->timerinitflag = 1;
  325. rt_timer_start(&can->timer);
  326. }
  327. CAN_UNLOCK(can);
  328. return RT_EOK;
  329. }
  330. static rt_err_t rt_can_close(struct rt_device *dev)
  331. {
  332. struct rt_can_device *can;
  333. RT_ASSERT(dev != RT_NULL);
  334. can = (struct rt_can_device *)dev;
  335. CAN_LOCK(can);
  336. /* this device has more reference count */
  337. if (dev->ref_count > 1)
  338. {
  339. CAN_UNLOCK(can);
  340. return RT_EOK;
  341. }
  342. if (can->timerinitflag)
  343. {
  344. can->timerinitflag = 0;
  345. rt_timer_stop(&can->timer);
  346. }
  347. can->status_indicate.ind = RT_NULL;
  348. can->status_indicate.args = RT_NULL;
  349. #ifdef RT_CAN_USING_HDR
  350. if (can->hdr != RT_NULL)
  351. {
  352. rt_free(can->hdr);
  353. can->hdr = RT_NULL;
  354. }
  355. #endif
  356. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  357. {
  358. struct rt_can_rx_fifo *rx_fifo;
  359. rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
  360. RT_ASSERT(rx_fifo != RT_NULL);
  361. rt_free(rx_fifo);
  362. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  363. /* configure low level device */
  364. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  365. }
  366. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  367. {
  368. struct rt_can_tx_fifo *tx_fifo;
  369. tx_fifo = (struct rt_can_tx_fifo *)can->can_rx;
  370. RT_ASSERT(tx_fifo != RT_NULL);
  371. rt_free(tx_fifo);
  372. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  373. /* configure low level device */
  374. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  375. }
  376. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_CAN_INT_ERR);
  377. CAN_UNLOCK(can);
  378. return RT_EOK;
  379. }
  380. static rt_size_t rt_can_read(struct rt_device *dev,
  381. rt_off_t pos,
  382. void *buffer,
  383. rt_size_t size)
  384. {
  385. struct rt_can_device *can;
  386. RT_ASSERT(dev != RT_NULL);
  387. if (size == 0) return 0;
  388. can = (struct rt_can_device *)dev;
  389. if ((dev->open_flag & RT_DEVICE_FLAG_INT_RX) && (dev->ref_count > 0))
  390. {
  391. return _can_int_rx(can, buffer, size);
  392. }
  393. return 0;
  394. }
  395. static rt_size_t rt_can_write(struct rt_device *dev,
  396. rt_off_t pos,
  397. const void *buffer,
  398. rt_size_t size)
  399. {
  400. struct rt_can_device *can;
  401. RT_ASSERT(dev != RT_NULL);
  402. if (size == 0) return 0;
  403. can = (struct rt_can_device *)dev;
  404. if ((dev->open_flag & RT_DEVICE_FLAG_INT_TX) && (dev->ref_count > 0))
  405. {
  406. if (can->config.privmode)
  407. {
  408. return _can_int_tx_priv(can, buffer, size);
  409. }
  410. else
  411. {
  412. return _can_int_tx(can, buffer, size);
  413. }
  414. }
  415. return 0;
  416. }
  417. static rt_err_t rt_can_control(struct rt_device *dev,
  418. rt_uint8_t cmd,
  419. void *args)
  420. {
  421. struct rt_can_device *can;
  422. rt_err_t res;
  423. RT_ASSERT(dev != RT_NULL);
  424. can = (struct rt_can_device *)dev;
  425. switch (cmd)
  426. {
  427. case RT_DEVICE_CTRL_SUSPEND:
  428. /* suspend device */
  429. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  430. break;
  431. case RT_DEVICE_CTRL_RESUME:
  432. /* resume device */
  433. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  434. break;
  435. case RT_DEVICE_CTRL_CONFIG:
  436. /* configure device */
  437. can->ops->configure(can, (struct can_configure *)args);
  438. break;
  439. case RT_CAN_CMD_SET_PRIV:
  440. /* configure device */
  441. if ((rt_uint32_t)args != can->config.privmode)
  442. {
  443. int i;
  444. rt_base_t level;
  445. struct rt_can_tx_fifo *tx_fifo;
  446. res = can->ops->control(can, cmd, args);
  447. if (res != RT_EOK) return res;
  448. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  449. if (can->config.privmode)
  450. {
  451. rt_completion_done(&(tx_fifo->completion));
  452. level = rt_hw_interrupt_disable();
  453. for (i = 0; i < can->config.sndboxnumber; i++)
  454. {
  455. rt_list_remove(&tx_fifo->buffer[i].list);
  456. }
  457. rt_hw_interrupt_enable(level);
  458. }
  459. else
  460. {
  461. for (i = 0; i < can->config.sndboxnumber; i++)
  462. {
  463. level = rt_hw_interrupt_disable();
  464. if (tx_fifo->buffer[i].result == RT_CAN_SND_RESULT_OK)
  465. {
  466. rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
  467. }
  468. rt_hw_interrupt_enable(level);
  469. }
  470. }
  471. return RT_EOK;
  472. }
  473. break;
  474. case RT_CAN_CMD_SET_STATUS_IND:
  475. can->status_indicate.ind = ((rt_can_status_ind_type_t)args)->ind;
  476. can->status_indicate.args = ((rt_can_status_ind_type_t)args)->args;
  477. break;
  478. #ifdef RT_CAN_USING_HDR
  479. case RT_CAN_CMD_SET_FILTER:
  480. res = can->ops->control(can, cmd, args);
  481. if (res != RT_EOK || can->hdr == RT_NULL)
  482. {
  483. return res;
  484. }
  485. {
  486. struct rt_can_filter_config *pfilter;
  487. struct rt_can_filter_item *pitem;
  488. rt_uint32_t count;
  489. rt_base_t level;
  490. pfilter = (struct rt_can_filter_config *)args;
  491. count = pfilter->count;
  492. pitem = pfilter->items;
  493. if (pfilter->actived)
  494. {
  495. while (count)
  496. {
  497. if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
  498. {
  499. count--;
  500. pitem++;
  501. continue;
  502. }
  503. level = rt_hw_interrupt_disable();
  504. if (!can->hdr[pitem->hdr].connected)
  505. {
  506. rt_memcpy(&can->hdr[pitem->hdr].filter, pitem,
  507. sizeof(struct rt_can_filter_item));
  508. can->hdr[pitem->hdr].connected = 1;
  509. can->hdr[pitem->hdr].msgs = 0;
  510. rt_list_init(&can->hdr[pitem->hdr].list);
  511. }
  512. rt_hw_interrupt_enable(level);
  513. count--;
  514. pitem++;
  515. }
  516. }
  517. else
  518. {
  519. while (count)
  520. {
  521. if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
  522. {
  523. count--;
  524. pitem++;
  525. continue;
  526. }
  527. level = rt_hw_interrupt_disable();
  528. if (can->hdr[pitem->hdr].connected)
  529. {
  530. rt_memset(&can->hdr[pitem->hdr].filter, 0,
  531. sizeof(struct rt_can_filter_item));
  532. can->hdr[pitem->hdr].connected = 0;
  533. can->hdr[pitem->hdr].msgs = 0;
  534. if (!rt_list_isempty(&can->hdr[pitem->hdr].list))
  535. {
  536. rt_list_remove(can->hdr[pitem->hdr].list.next);
  537. }
  538. }
  539. rt_hw_interrupt_enable(level);
  540. count--;
  541. pitem++;
  542. }
  543. }
  544. }
  545. break;
  546. #endif /*RT_CAN_USING_HDR*/
  547. default :
  548. /* control device */
  549. if (can->ops->control != RT_NULL)
  550. {
  551. can->ops->control(can, cmd, args);
  552. }
  553. break;
  554. }
  555. return RT_EOK;
  556. }
  557. /*
  558. * can timer
  559. */
  560. static void cantimeout(void *arg)
  561. {
  562. rt_can_t can = (rt_can_t)arg;
  563. rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
  564. if (can->timerinitflag == 1)
  565. {
  566. can->timerinitflag = 0xFF;
  567. }
  568. if (can->status_indicate.ind != RT_NULL)
  569. {
  570. can->status_indicate.ind(can, can->status_indicate.args);
  571. }
  572. }
  573. /*
  574. * can register
  575. */
  576. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  577. const char *name,
  578. const struct rt_can_ops *ops,
  579. void *data)
  580. {
  581. struct rt_device *device;
  582. RT_ASSERT(can != RT_NULL);
  583. device = &(can->parent);
  584. device->type = RT_Device_Class_CAN;
  585. device->rx_indicate = RT_NULL;
  586. device->tx_complete = RT_NULL;
  587. #ifdef RT_CAN_USING_HDR
  588. can->hdr = RT_NULL;
  589. #endif
  590. can->can_rx = RT_NULL;
  591. can->can_tx = RT_NULL;
  592. rt_mutex_init(&(can->lock), "can", RT_IPC_FLAG_PRIO);
  593. device->init = rt_can_init;
  594. device->open = rt_can_open;
  595. device->close = rt_can_close;
  596. device->read = rt_can_read;
  597. device->write = rt_can_write;
  598. device->control = rt_can_control;
  599. can->ops = ops;
  600. can->status_indicate.ind = RT_NULL;
  601. can->status_indicate.args = RT_NULL;
  602. rt_memset(&can->status, 0, sizeof(can->status));
  603. device->user_data = data;
  604. can->timerinitflag = 0;
  605. rt_timer_init(&can->timer,
  606. name,
  607. cantimeout,
  608. (void *)can,
  609. can->config.ticks,
  610. RT_TIMER_FLAG_PERIODIC);
  611. /* register a character device */
  612. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  613. }
  614. /* ISR for can interrupt */
  615. void rt_hw_can_isr(struct rt_can_device *can, int event)
  616. {
  617. switch (event & 0xff)
  618. {
  619. case RT_CAN_EVENT_RXOF_IND:
  620. {
  621. rt_base_t level;
  622. level = rt_hw_interrupt_disable();
  623. can->status.dropedrcvpkg++;
  624. rt_hw_interrupt_enable(level);
  625. }
  626. case RT_CAN_EVENT_RX_IND:
  627. {
  628. struct rt_can_msg tmpmsg;
  629. struct rt_can_rx_fifo *rx_fifo;
  630. struct rt_can_msg_list *listmsg = RT_NULL;
  631. #ifdef RT_CAN_USING_HDR
  632. rt_int32_t hdr;
  633. #endif
  634. int ch = -1;
  635. rt_base_t level;
  636. rt_uint32_t no;
  637. rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
  638. RT_ASSERT(rx_fifo != RT_NULL);
  639. /* interrupt mode receive */
  640. RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
  641. no = event >> 8;
  642. ch = can->ops->recvmsg(can, &tmpmsg, no);
  643. if (ch == -1) break;
  644. /* disable interrupt */
  645. level = rt_hw_interrupt_disable();
  646. can->status.rcvpkg++;
  647. can->status.rcvchange = 1;
  648. if (!rt_list_isempty(&rx_fifo->freelist))
  649. {
  650. listmsg = rt_list_entry(rx_fifo->freelist.next, struct rt_can_msg_list, list);
  651. rt_list_remove(&listmsg->list);
  652. #ifdef RT_CAN_USING_HDR
  653. rt_list_remove(&listmsg->hdrlist);
  654. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  655. {
  656. listmsg->owner->msgs--;
  657. }
  658. listmsg->owner = RT_NULL;
  659. #endif /*RT_CAN_USING_HDR*/
  660. RT_ASSERT(rx_fifo->freenumbers > 0);
  661. rx_fifo->freenumbers--;
  662. }
  663. else if (!rt_list_isempty(&rx_fifo->uselist))
  664. {
  665. listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
  666. can->status.dropedrcvpkg++;
  667. rt_list_remove(&listmsg->list);
  668. #ifdef RT_CAN_USING_HDR
  669. rt_list_remove(&listmsg->hdrlist);
  670. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  671. {
  672. listmsg->owner->msgs--;
  673. }
  674. listmsg->owner = RT_NULL;
  675. #endif
  676. }
  677. /* enable interrupt */
  678. rt_hw_interrupt_enable(level);
  679. if (listmsg != RT_NULL)
  680. {
  681. rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg));
  682. level = rt_hw_interrupt_disable();
  683. rt_list_insert_before(&rx_fifo->uselist, &listmsg->list);
  684. #ifdef RT_CAN_USING_HDR
  685. hdr = tmpmsg.hdr;
  686. if (can->hdr != RT_NULL)
  687. {
  688. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  689. if (can->hdr[hdr].connected)
  690. {
  691. rt_list_insert_before(&can->hdr[hdr].list, &listmsg->hdrlist);
  692. listmsg->owner = &can->hdr[hdr];
  693. can->hdr[hdr].msgs++;
  694. }
  695. }
  696. #endif
  697. rt_hw_interrupt_enable(level);
  698. }
  699. /* invoke callback */
  700. #ifdef RT_CAN_USING_HDR
  701. if (can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind)
  702. {
  703. rt_size_t rx_length;
  704. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  705. level = rt_hw_interrupt_disable();
  706. rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg);
  707. rt_hw_interrupt_enable(level);
  708. can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length);
  709. }
  710. else
  711. #endif
  712. {
  713. if (can->parent.rx_indicate != RT_NULL)
  714. {
  715. rt_size_t rx_length;
  716. level = rt_hw_interrupt_disable();
  717. /* get rx length */
  718. rx_length = rx_fifo->freenumbers * sizeof(struct rt_can_msg);
  719. rt_hw_interrupt_enable(level);
  720. can->parent.rx_indicate(&can->parent, rx_length);
  721. }
  722. }
  723. break;
  724. }
  725. case RT_CAN_EVENT_TX_DONE:
  726. case RT_CAN_EVENT_TX_FAIL:
  727. {
  728. struct rt_can_tx_fifo *tx_fifo;
  729. rt_uint32_t no;
  730. no = event >> 8;
  731. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  732. RT_ASSERT(tx_fifo != RT_NULL);
  733. if ((event & 0xff) == RT_CAN_EVENT_TX_DONE)
  734. {
  735. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_OK;
  736. }
  737. else
  738. {
  739. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_ERR;
  740. }
  741. rt_completion_done(&(tx_fifo->buffer[no].completion));
  742. break;
  743. }
  744. }
  745. }
  746. #ifdef RT_USING_FINSH
  747. #include <finsh.h>
  748. int cmd_canstat(int argc, void **argv)
  749. {
  750. static const char *ErrCode[] =
  751. {
  752. "No Error!",
  753. "Warning !",
  754. "Passive !",
  755. "Bus Off !"
  756. };
  757. if (argc >= 2)
  758. {
  759. struct rt_can_status status;
  760. rt_device_t candev = rt_device_find(argv[1]);
  761. if (!candev)
  762. {
  763. rt_kprintf(" Can't find can device %s\n", argv[1]);
  764. return -1;
  765. }
  766. rt_kprintf(" Finded can device: %s...", argv[1]);
  767. rt_device_control(candev, RT_CAN_CMD_GET_STATUS, &status);
  768. rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.",
  769. status.rcverrcnt, status.snderrcnt);
  770. rt_kprintf("\n Bit..pad..error..count: %010ld. Format...error....count: %010ld",
  771. status.bitpaderrcnt, status.formaterrcnt);
  772. rt_kprintf("\n Ack.......error..count: %010ld. Bit......error....count: %010ld.",
  773. status.ackerrcnt, status.biterrcnt);
  774. rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ",
  775. status.crcerrcnt, status.errcode);
  776. switch (status.errcode)
  777. {
  778. case 0:
  779. rt_kprintf("%s.", ErrCode[0]);
  780. break;
  781. case 1:
  782. rt_kprintf("%s.", ErrCode[1]);
  783. break;
  784. case 2:
  785. case 3:
  786. rt_kprintf("%s.", ErrCode[2]);
  787. break;
  788. case 4:
  789. case 5:
  790. case 6:
  791. case 7:
  792. rt_kprintf("%s.", ErrCode[3]);
  793. break;
  794. }
  795. rt_kprintf("\n Total.receive.packages: %010ld. Droped.receive.packages: %010ld.",
  796. status.rcvpkg, status.dropedrcvpkg);
  797. rt_kprintf("\n Total..send...packages: %010ld. Droped...send..packages: %010ld.\n",
  798. status.sndpkg + status.dropedsndpkg, status.dropedsndpkg);
  799. }
  800. else
  801. {
  802. rt_kprintf(" Invalid Call %s\n", argv[0]);
  803. rt_kprintf(" Please using %s cannamex .Here canname is driver name and x is candrive number.\n", argv[0]);
  804. }
  805. return 0;
  806. }
  807. FINSH_FUNCTION_EXPORT_ALIAS(cmd_canstat, __cmd_canstat, Stat Can Device Status.);
  808. #endif