can.c 27 KB

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