can.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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_tx;
  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. #ifdef RT_CAN_USING_BUS_HOOK
  552. case RT_CAN_CMD_SET_BUS_HOOK:
  553. can->bus_hook = (rt_can_bus_hook) args;
  554. break;
  555. #endif /*RT_CAN_USING_BUS_HOOK*/
  556. default :
  557. /* control device */
  558. if (can->ops->control != RT_NULL)
  559. {
  560. can->ops->control(can, cmd, args);
  561. }
  562. break;
  563. }
  564. return RT_EOK;
  565. }
  566. /*
  567. * can timer
  568. */
  569. static void cantimeout(void *arg)
  570. {
  571. rt_can_t can = (rt_can_t)arg;
  572. rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
  573. if (can->status_indicate.ind != RT_NULL)
  574. {
  575. can->status_indicate.ind(can, can->status_indicate.args);
  576. }
  577. #ifdef RT_CAN_USING_BUS_HOOK
  578. if(can->bus_hook)
  579. {
  580. can->bus_hook(can);
  581. }
  582. #endif /*RT_CAN_USING_BUS_HOOK*/
  583. if (can->timerinitflag == 1)
  584. {
  585. can->timerinitflag = 0xFF;
  586. }
  587. }
  588. /*
  589. * can register
  590. */
  591. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  592. const char *name,
  593. const struct rt_can_ops *ops,
  594. void *data)
  595. {
  596. struct rt_device *device;
  597. RT_ASSERT(can != RT_NULL);
  598. device = &(can->parent);
  599. device->type = RT_Device_Class_CAN;
  600. device->rx_indicate = RT_NULL;
  601. device->tx_complete = RT_NULL;
  602. #ifdef RT_CAN_USING_HDR
  603. can->hdr = RT_NULL;
  604. #endif
  605. can->can_rx = RT_NULL;
  606. can->can_tx = RT_NULL;
  607. rt_mutex_init(&(can->lock), "can", RT_IPC_FLAG_PRIO);
  608. #ifdef RT_CAN_USING_BUS_HOOK
  609. can->bus_hook = RT_NULL;
  610. #endif /*RT_CAN_USING_BUS_HOOK*/
  611. device->init = rt_can_init;
  612. device->open = rt_can_open;
  613. device->close = rt_can_close;
  614. device->read = rt_can_read;
  615. device->write = rt_can_write;
  616. device->control = rt_can_control;
  617. can->ops = ops;
  618. can->status_indicate.ind = RT_NULL;
  619. can->status_indicate.args = RT_NULL;
  620. rt_memset(&can->status, 0, sizeof(can->status));
  621. device->user_data = data;
  622. can->timerinitflag = 0;
  623. rt_timer_init(&can->timer,
  624. name,
  625. cantimeout,
  626. (void *)can,
  627. can->config.ticks,
  628. RT_TIMER_FLAG_PERIODIC);
  629. /* register a character device */
  630. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  631. }
  632. /* ISR for can interrupt */
  633. void rt_hw_can_isr(struct rt_can_device *can, int event)
  634. {
  635. switch (event & 0xff)
  636. {
  637. case RT_CAN_EVENT_RXOF_IND:
  638. {
  639. rt_base_t level;
  640. level = rt_hw_interrupt_disable();
  641. can->status.dropedrcvpkg++;
  642. rt_hw_interrupt_enable(level);
  643. }
  644. case RT_CAN_EVENT_RX_IND:
  645. {
  646. struct rt_can_msg tmpmsg;
  647. struct rt_can_rx_fifo *rx_fifo;
  648. struct rt_can_msg_list *listmsg = RT_NULL;
  649. #ifdef RT_CAN_USING_HDR
  650. rt_int32_t hdr;
  651. #endif
  652. int ch = -1;
  653. rt_base_t level;
  654. rt_uint32_t no;
  655. rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
  656. RT_ASSERT(rx_fifo != RT_NULL);
  657. /* interrupt mode receive */
  658. RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
  659. no = event >> 8;
  660. ch = can->ops->recvmsg(can, &tmpmsg, no);
  661. if (ch == -1) break;
  662. /* disable interrupt */
  663. level = rt_hw_interrupt_disable();
  664. can->status.rcvpkg++;
  665. can->status.rcvchange = 1;
  666. if (!rt_list_isempty(&rx_fifo->freelist))
  667. {
  668. listmsg = rt_list_entry(rx_fifo->freelist.next, struct rt_can_msg_list, list);
  669. rt_list_remove(&listmsg->list);
  670. #ifdef RT_CAN_USING_HDR
  671. rt_list_remove(&listmsg->hdrlist);
  672. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  673. {
  674. listmsg->owner->msgs--;
  675. }
  676. listmsg->owner = RT_NULL;
  677. #endif /*RT_CAN_USING_HDR*/
  678. RT_ASSERT(rx_fifo->freenumbers > 0);
  679. rx_fifo->freenumbers--;
  680. }
  681. else if (!rt_list_isempty(&rx_fifo->uselist))
  682. {
  683. listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
  684. can->status.dropedrcvpkg++;
  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
  694. }
  695. /* enable interrupt */
  696. rt_hw_interrupt_enable(level);
  697. if (listmsg != RT_NULL)
  698. {
  699. rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg));
  700. level = rt_hw_interrupt_disable();
  701. rt_list_insert_before(&rx_fifo->uselist, &listmsg->list);
  702. #ifdef RT_CAN_USING_HDR
  703. hdr = tmpmsg.hdr;
  704. if (can->hdr != RT_NULL)
  705. {
  706. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  707. if (can->hdr[hdr].connected)
  708. {
  709. rt_list_insert_before(&can->hdr[hdr].list, &listmsg->hdrlist);
  710. listmsg->owner = &can->hdr[hdr];
  711. can->hdr[hdr].msgs++;
  712. }
  713. }
  714. #endif
  715. rt_hw_interrupt_enable(level);
  716. }
  717. /* invoke callback */
  718. #ifdef RT_CAN_USING_HDR
  719. if (can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind)
  720. {
  721. rt_size_t rx_length;
  722. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  723. level = rt_hw_interrupt_disable();
  724. rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg);
  725. rt_hw_interrupt_enable(level);
  726. can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length);
  727. }
  728. else
  729. #endif
  730. {
  731. if (can->parent.rx_indicate != RT_NULL)
  732. {
  733. rt_size_t rx_length;
  734. level = rt_hw_interrupt_disable();
  735. /* get rx length */
  736. rx_length = rx_fifo->freenumbers * sizeof(struct rt_can_msg);
  737. rt_hw_interrupt_enable(level);
  738. can->parent.rx_indicate(&can->parent, rx_length);
  739. }
  740. }
  741. break;
  742. }
  743. case RT_CAN_EVENT_TX_DONE:
  744. case RT_CAN_EVENT_TX_FAIL:
  745. {
  746. struct rt_can_tx_fifo *tx_fifo;
  747. rt_uint32_t no;
  748. no = event >> 8;
  749. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  750. RT_ASSERT(tx_fifo != RT_NULL);
  751. if ((event & 0xff) == RT_CAN_EVENT_TX_DONE)
  752. {
  753. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_OK;
  754. }
  755. else
  756. {
  757. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_ERR;
  758. }
  759. rt_completion_done(&(tx_fifo->buffer[no].completion));
  760. break;
  761. }
  762. }
  763. }
  764. #ifdef RT_USING_FINSH
  765. #include <finsh.h>
  766. int cmd_canstat(int argc, void **argv)
  767. {
  768. static const char *ErrCode[] =
  769. {
  770. "No Error!",
  771. "Warning !",
  772. "Passive !",
  773. "Bus Off !"
  774. };
  775. if (argc >= 2)
  776. {
  777. struct rt_can_status status;
  778. rt_device_t candev = rt_device_find(argv[1]);
  779. if (!candev)
  780. {
  781. rt_kprintf(" Can't find can device %s\n", argv[1]);
  782. return -1;
  783. }
  784. rt_kprintf(" Finded can device: %s...", argv[1]);
  785. rt_device_control(candev, RT_CAN_CMD_GET_STATUS, &status);
  786. rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.",
  787. status.rcverrcnt, status.snderrcnt);
  788. rt_kprintf("\n Bit..pad..error..count: %010ld. Format...error....count: %010ld",
  789. status.bitpaderrcnt, status.formaterrcnt);
  790. rt_kprintf("\n Ack.......error..count: %010ld. Bit......error....count: %010ld.",
  791. status.ackerrcnt, status.biterrcnt);
  792. rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ",
  793. status.crcerrcnt, status.errcode);
  794. switch (status.errcode)
  795. {
  796. case 0:
  797. rt_kprintf("%s.", ErrCode[0]);
  798. break;
  799. case 1:
  800. rt_kprintf("%s.", ErrCode[1]);
  801. break;
  802. case 2:
  803. case 3:
  804. rt_kprintf("%s.", ErrCode[2]);
  805. break;
  806. case 4:
  807. case 5:
  808. case 6:
  809. case 7:
  810. rt_kprintf("%s.", ErrCode[3]);
  811. break;
  812. }
  813. rt_kprintf("\n Total.receive.packages: %010ld. Droped.receive.packages: %010ld.",
  814. status.rcvpkg, status.dropedrcvpkg);
  815. rt_kprintf("\n Total..send...packages: %010ld. Droped...send..packages: %010ld.\n",
  816. status.sndpkg + status.dropedsndpkg, status.dropedsndpkg);
  817. }
  818. else
  819. {
  820. rt_kprintf(" Invalid Call %s\n", argv[0]);
  821. rt_kprintf(" Please using %s cannamex .Here canname is driver name and x is candrive number.\n", argv[0]);
  822. }
  823. return 0;
  824. }
  825. FINSH_FUNCTION_EXPORT_ALIAS(cmd_canstat, __cmd_canstat, Stat Can Device Status.);
  826. #endif