can.c 27 KB

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