can.c 28 KB

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