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