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