can.c 27 KB

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