can.c 27 KB

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