can.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. 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_free(tx_fifo);
  357. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  358. can->can_tx = RT_NULL;
  359. /* clear can tx interrupt */
  360. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  361. }
  362. can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_CAN_INT_ERR);
  363. CAN_UNLOCK(can);
  364. return RT_EOK;
  365. }
  366. static rt_size_t rt_can_read(struct rt_device *dev,
  367. rt_off_t pos,
  368. void *buffer,
  369. rt_size_t size)
  370. {
  371. struct rt_can_device *can;
  372. RT_ASSERT(dev != RT_NULL);
  373. if (size == 0) return 0;
  374. can = (struct rt_can_device *)dev;
  375. if ((dev->open_flag & RT_DEVICE_FLAG_INT_RX) && (dev->ref_count > 0))
  376. {
  377. return _can_int_rx(can, buffer, size);
  378. }
  379. return 0;
  380. }
  381. static rt_size_t rt_can_write(struct rt_device *dev,
  382. rt_off_t pos,
  383. const void *buffer,
  384. rt_size_t size)
  385. {
  386. struct rt_can_device *can;
  387. RT_ASSERT(dev != RT_NULL);
  388. if (size == 0) return 0;
  389. can = (struct rt_can_device *)dev;
  390. if ((dev->open_flag & RT_DEVICE_FLAG_INT_TX) && (dev->ref_count > 0))
  391. {
  392. if (can->config.privmode)
  393. {
  394. return _can_int_tx_priv(can, buffer, size);
  395. }
  396. else
  397. {
  398. return _can_int_tx(can, buffer, size);
  399. }
  400. }
  401. return 0;
  402. }
  403. static rt_err_t rt_can_control(struct rt_device *dev,
  404. int cmd,
  405. void *args)
  406. {
  407. struct rt_can_device *can;
  408. rt_err_t res;
  409. res = RT_EOK;
  410. RT_ASSERT(dev != RT_NULL);
  411. can = (struct rt_can_device *)dev;
  412. switch (cmd)
  413. {
  414. case RT_DEVICE_CTRL_SUSPEND:
  415. /* suspend device */
  416. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  417. break;
  418. case RT_DEVICE_CTRL_RESUME:
  419. /* resume device */
  420. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  421. break;
  422. case RT_DEVICE_CTRL_CONFIG:
  423. /* configure device */
  424. res = can->ops->configure(can, (struct can_configure *)args);
  425. break;
  426. case RT_CAN_CMD_SET_PRIV:
  427. /* configure device */
  428. if ((rt_uint32_t)args != can->config.privmode)
  429. {
  430. int i;
  431. rt_base_t level;
  432. struct rt_can_tx_fifo *tx_fifo;
  433. res = can->ops->control(can, cmd, args);
  434. if (res != RT_EOK) return res;
  435. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  436. if (can->config.privmode)
  437. {
  438. for (i = 0; i < can->config.sndboxnumber; i++)
  439. {
  440. level = rt_hw_interrupt_disable();
  441. if(rt_list_isempty(&tx_fifo->buffer[i].list))
  442. {
  443. rt_sem_release(&(tx_fifo->sem));
  444. }
  445. else
  446. {
  447. rt_list_remove(&tx_fifo->buffer[i].list);
  448. }
  449. rt_hw_interrupt_enable(level);
  450. }
  451. }
  452. else
  453. {
  454. for (i = 0; i < can->config.sndboxnumber; i++)
  455. {
  456. level = rt_hw_interrupt_disable();
  457. if (tx_fifo->buffer[i].result == RT_CAN_SND_RESULT_OK)
  458. {
  459. rt_list_insert_before(&tx_fifo->freelist, &tx_fifo->buffer[i].list);
  460. }
  461. rt_hw_interrupt_enable(level);
  462. }
  463. }
  464. }
  465. break;
  466. case RT_CAN_CMD_SET_STATUS_IND:
  467. can->status_indicate.ind = ((rt_can_status_ind_type_t)args)->ind;
  468. can->status_indicate.args = ((rt_can_status_ind_type_t)args)->args;
  469. break;
  470. #ifdef RT_CAN_USING_HDR
  471. case RT_CAN_CMD_SET_FILTER:
  472. res = can->ops->control(can, cmd, args);
  473. if (res != RT_EOK || can->hdr == RT_NULL)
  474. {
  475. return res;
  476. }
  477. struct rt_can_filter_config *pfilter;
  478. struct rt_can_filter_item *pitem;
  479. rt_uint32_t count;
  480. rt_base_t level;
  481. pfilter = (struct rt_can_filter_config *)args;
  482. RT_ASSERT(pfilter);
  483. count = pfilter->count;
  484. pitem = pfilter->items;
  485. if (pfilter->actived)
  486. {
  487. while (count)
  488. {
  489. if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
  490. {
  491. count--;
  492. pitem++;
  493. continue;
  494. }
  495. level = rt_hw_interrupt_disable();
  496. if (!can->hdr[pitem->hdr].connected)
  497. {
  498. rt_hw_interrupt_enable(level);
  499. rt_memcpy(&can->hdr[pitem->hdr].filter, pitem,
  500. sizeof(struct rt_can_filter_item));
  501. level = rt_hw_interrupt_disable();
  502. can->hdr[pitem->hdr].connected = 1;
  503. can->hdr[pitem->hdr].msgs = 0;
  504. rt_list_init(&can->hdr[pitem->hdr].list);
  505. }
  506. rt_hw_interrupt_enable(level);
  507. count--;
  508. pitem++;
  509. }
  510. }
  511. else
  512. {
  513. while (count)
  514. {
  515. if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
  516. {
  517. count--;
  518. pitem++;
  519. continue;
  520. }
  521. level = rt_hw_interrupt_disable();
  522. if (can->hdr[pitem->hdr].connected)
  523. {
  524. can->hdr[pitem->hdr].connected = 0;
  525. can->hdr[pitem->hdr].msgs = 0;
  526. if (!rt_list_isempty(&can->hdr[pitem->hdr].list))
  527. {
  528. rt_list_remove(can->hdr[pitem->hdr].list.next);
  529. }
  530. rt_hw_interrupt_enable(level);
  531. rt_memset(&can->hdr[pitem->hdr].filter, 0,
  532. sizeof(struct rt_can_filter_item));
  533. }
  534. else
  535. {
  536. rt_hw_interrupt_enable(level);
  537. }
  538. count--;
  539. pitem++;
  540. }
  541. }
  542. break;
  543. #endif /*RT_CAN_USING_HDR*/
  544. #ifdef RT_CAN_USING_BUS_HOOK
  545. case RT_CAN_CMD_SET_BUS_HOOK:
  546. can->bus_hook = (rt_can_bus_hook) args;
  547. break;
  548. #endif /*RT_CAN_USING_BUS_HOOK*/
  549. default :
  550. /* control device */
  551. if (can->ops->control != RT_NULL)
  552. {
  553. res = can->ops->control(can, cmd, args);
  554. }
  555. else
  556. {
  557. res = -RT_ENOSYS;
  558. }
  559. break;
  560. }
  561. return res;
  562. }
  563. /*
  564. * can timer
  565. */
  566. static void cantimeout(void *arg)
  567. {
  568. rt_can_t can;
  569. can = (rt_can_t)arg;
  570. RT_ASSERT(can);
  571. rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
  572. if (can->status_indicate.ind != RT_NULL)
  573. {
  574. can->status_indicate.ind(can, can->status_indicate.args);
  575. }
  576. #ifdef RT_CAN_USING_BUS_HOOK
  577. if(can->bus_hook)
  578. {
  579. can->bus_hook(can);
  580. }
  581. #endif /*RT_CAN_USING_BUS_HOOK*/
  582. if (can->timerinitflag == 1)
  583. {
  584. can->timerinitflag = 0xFF;
  585. }
  586. }
  587. #ifdef RT_USING_DEVICE_OPS
  588. const static struct rt_device_ops can_device_ops =
  589. {
  590. rt_can_init,
  591. rt_can_open,
  592. rt_can_close,
  593. rt_can_read,
  594. rt_can_write,
  595. rt_can_control
  596. };
  597. #endif
  598. /*
  599. * can register
  600. */
  601. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  602. const char *name,
  603. const struct rt_can_ops *ops,
  604. void *data)
  605. {
  606. struct rt_device *device;
  607. RT_ASSERT(can != RT_NULL);
  608. device = &(can->parent);
  609. device->type = RT_Device_Class_CAN;
  610. device->rx_indicate = RT_NULL;
  611. device->tx_complete = RT_NULL;
  612. #ifdef RT_CAN_USING_HDR
  613. can->hdr = RT_NULL;
  614. #endif
  615. can->can_rx = RT_NULL;
  616. can->can_tx = RT_NULL;
  617. rt_mutex_init(&(can->lock), "can", RT_IPC_FLAG_PRIO);
  618. #ifdef RT_CAN_USING_BUS_HOOK
  619. can->bus_hook = RT_NULL;
  620. #endif /*RT_CAN_USING_BUS_HOOK*/
  621. #ifdef RT_USING_DEVICE_OPS
  622. device->ops = &can_device_ops;
  623. #else
  624. device->init = rt_can_init;
  625. device->open = rt_can_open;
  626. device->close = rt_can_close;
  627. device->read = rt_can_read;
  628. device->write = rt_can_write;
  629. device->control = rt_can_control;
  630. #endif
  631. can->ops = ops;
  632. can->status_indicate.ind = RT_NULL;
  633. can->status_indicate.args = RT_NULL;
  634. rt_memset(&can->status, 0, sizeof(can->status));
  635. device->user_data = data;
  636. can->timerinitflag = 0;
  637. rt_timer_init(&can->timer,
  638. name,
  639. cantimeout,
  640. (void *)can,
  641. can->config.ticks,
  642. RT_TIMER_FLAG_PERIODIC);
  643. /* register a character device */
  644. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  645. }
  646. /* ISR for can interrupt */
  647. void rt_hw_can_isr(struct rt_can_device *can, int event)
  648. {
  649. switch (event & 0xff)
  650. {
  651. case RT_CAN_EVENT_RXOF_IND:
  652. {
  653. rt_base_t level;
  654. level = rt_hw_interrupt_disable();
  655. can->status.dropedrcvpkg++;
  656. rt_hw_interrupt_enable(level);
  657. }
  658. case RT_CAN_EVENT_RX_IND:
  659. {
  660. struct rt_can_msg tmpmsg;
  661. struct rt_can_rx_fifo *rx_fifo;
  662. struct rt_can_msg_list *listmsg = RT_NULL;
  663. #ifdef RT_CAN_USING_HDR
  664. rt_int8_t hdr;
  665. #endif
  666. int ch = -1;
  667. rt_base_t level;
  668. rt_uint32_t no;
  669. rx_fifo = (struct rt_can_rx_fifo *)can->can_rx;
  670. RT_ASSERT(rx_fifo != RT_NULL);
  671. /* interrupt mode receive */
  672. RT_ASSERT(can->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
  673. no = event >> 8;
  674. ch = can->ops->recvmsg(can, &tmpmsg, no);
  675. if (ch == -1) break;
  676. /* disable interrupt */
  677. level = rt_hw_interrupt_disable();
  678. can->status.rcvpkg++;
  679. can->status.rcvchange = 1;
  680. if (!rt_list_isempty(&rx_fifo->freelist))
  681. {
  682. listmsg = rt_list_entry(rx_fifo->freelist.next, struct rt_can_msg_list, list);
  683. rt_list_remove(&listmsg->list);
  684. #ifdef RT_CAN_USING_HDR
  685. rt_list_remove(&listmsg->hdrlist);
  686. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  687. {
  688. listmsg->owner->msgs--;
  689. }
  690. listmsg->owner = RT_NULL;
  691. #endif /*RT_CAN_USING_HDR*/
  692. RT_ASSERT(rx_fifo->freenumbers > 0);
  693. rx_fifo->freenumbers--;
  694. }
  695. else if (!rt_list_isempty(&rx_fifo->uselist))
  696. {
  697. listmsg = rt_list_entry(rx_fifo->uselist.next, struct rt_can_msg_list, list);
  698. can->status.dropedrcvpkg++;
  699. rt_list_remove(&listmsg->list);
  700. #ifdef RT_CAN_USING_HDR
  701. rt_list_remove(&listmsg->hdrlist);
  702. if (listmsg->owner != RT_NULL && listmsg->owner->msgs)
  703. {
  704. listmsg->owner->msgs--;
  705. }
  706. listmsg->owner = RT_NULL;
  707. #endif
  708. }
  709. /* enable interrupt */
  710. rt_hw_interrupt_enable(level);
  711. if (listmsg != RT_NULL)
  712. {
  713. rt_memcpy(&listmsg->data, &tmpmsg, sizeof(struct rt_can_msg));
  714. level = rt_hw_interrupt_disable();
  715. rt_list_insert_before(&rx_fifo->uselist, &listmsg->list);
  716. #ifdef RT_CAN_USING_HDR
  717. hdr = tmpmsg.hdr;
  718. if (can->hdr != RT_NULL)
  719. {
  720. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  721. if (can->hdr[hdr].connected)
  722. {
  723. rt_list_insert_before(&can->hdr[hdr].list, &listmsg->hdrlist);
  724. listmsg->owner = &can->hdr[hdr];
  725. can->hdr[hdr].msgs++;
  726. }
  727. }
  728. #endif
  729. rt_hw_interrupt_enable(level);
  730. }
  731. /* invoke callback */
  732. #ifdef RT_CAN_USING_HDR
  733. if (can->hdr != RT_NULL && can->hdr[hdr].connected && can->hdr[hdr].filter.ind)
  734. {
  735. rt_size_t rx_length;
  736. RT_ASSERT(hdr < can->config.maxhdr && hdr >= 0);
  737. level = rt_hw_interrupt_disable();
  738. rx_length = can->hdr[hdr].msgs * sizeof(struct rt_can_msg);
  739. rt_hw_interrupt_enable(level);
  740. can->hdr[hdr].filter.ind(&can->parent, can->hdr[hdr].filter.args, hdr, rx_length);
  741. }
  742. else
  743. #endif
  744. {
  745. if (can->parent.rx_indicate != RT_NULL)
  746. {
  747. rt_size_t rx_length;
  748. level = rt_hw_interrupt_disable();
  749. /* get rx length */
  750. rx_length = rt_list_len(&rx_fifo->uselist)* sizeof(struct rt_can_msg);
  751. rt_hw_interrupt_enable(level);
  752. can->parent.rx_indicate(&can->parent, rx_length);
  753. }
  754. }
  755. break;
  756. }
  757. case RT_CAN_EVENT_TX_DONE:
  758. case RT_CAN_EVENT_TX_FAIL:
  759. {
  760. struct rt_can_tx_fifo *tx_fifo;
  761. rt_uint32_t no;
  762. no = event >> 8;
  763. tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
  764. RT_ASSERT(tx_fifo != RT_NULL);
  765. if ((event & 0xff) == RT_CAN_EVENT_TX_DONE)
  766. {
  767. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_OK;
  768. }
  769. else
  770. {
  771. tx_fifo->buffer[no].result = RT_CAN_SND_RESULT_ERR;
  772. }
  773. rt_completion_done(&(tx_fifo->buffer[no].completion));
  774. break;
  775. }
  776. }
  777. }
  778. #ifdef RT_USING_FINSH
  779. #include <finsh.h>
  780. int cmd_canstat(int argc, void **argv)
  781. {
  782. static const char *ErrCode[] =
  783. {
  784. "No Error!",
  785. "Warning !",
  786. "Passive !",
  787. "Bus Off !"
  788. };
  789. if (argc >= 2)
  790. {
  791. struct rt_can_status status;
  792. rt_device_t candev = rt_device_find(argv[1]);
  793. if (!candev)
  794. {
  795. rt_kprintf(" Can't find can device %s\n", argv[1]);
  796. return -1;
  797. }
  798. rt_kprintf(" Finded can device: %s...", argv[1]);
  799. rt_device_control(candev, RT_CAN_CMD_GET_STATUS, &status);
  800. rt_kprintf("\n Receive...error..count: %010ld. Send.....error....count: %010ld.",
  801. status.rcverrcnt, status.snderrcnt);
  802. rt_kprintf("\n Bit..pad..error..count: %010ld. Format...error....count: %010ld",
  803. status.bitpaderrcnt, status.formaterrcnt);
  804. rt_kprintf("\n Ack.......error..count: %010ld. Bit......error....count: %010ld.",
  805. status.ackerrcnt, status.biterrcnt);
  806. rt_kprintf("\n CRC.......error..count: %010ld. Error.code.[%010ld]: ",
  807. status.crcerrcnt, status.errcode);
  808. switch (status.errcode)
  809. {
  810. case 0:
  811. rt_kprintf("%s.", ErrCode[0]);
  812. break;
  813. case 1:
  814. rt_kprintf("%s.", ErrCode[1]);
  815. break;
  816. case 2:
  817. case 3:
  818. rt_kprintf("%s.", ErrCode[2]);
  819. break;
  820. case 4:
  821. case 5:
  822. case 6:
  823. case 7:
  824. rt_kprintf("%s.", ErrCode[3]);
  825. break;
  826. }
  827. rt_kprintf("\n Total.receive.packages: %010ld. Droped.receive.packages: %010ld.",
  828. status.rcvpkg, status.dropedrcvpkg);
  829. rt_kprintf("\n Total..send...packages: %010ld. Droped...send..packages: %010ld.\n",
  830. status.sndpkg + status.dropedsndpkg, status.dropedsndpkg);
  831. }
  832. else
  833. {
  834. rt_kprintf(" Invalid Call %s\n", argv[0]);
  835. rt_kprintf(" Please using %s cannamex .Here canname is driver name and x is candrive number.\n", argv[0]);
  836. }
  837. return 0;
  838. }
  839. FINSH_FUNCTION_EXPORT_ALIAS(cmd_canstat, __cmd_canstat, Stat Can Device Status.);
  840. #endif