can.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /****************************************************************************
  2. * drivers/can.c
  3. *
  4. * Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <semaphore.h>
  45. #include <fcntl.h>
  46. #include <assert.h>
  47. #include <errno.h>
  48. #include <debug.h>
  49. #include <nuttx/fs/fs.h>
  50. #include <nuttx/arch.h>
  51. #include <nuttx/can.h>
  52. #include <arch/irq.h>
  53. #ifdef CONFIG_CAN
  54. /****************************************************************************
  55. * Pre-processor Definitions
  56. ****************************************************************************/
  57. /* Debug ********************************************************************/
  58. /* Non-standard debug that may be enabled just for testing CAN */
  59. #ifdef CONFIG_DEBUG_CAN
  60. # define candbg dbg
  61. # define canvdbg vdbg
  62. # define canlldbg lldbg
  63. # define canllvdbg llvdbg
  64. #else
  65. # define candbg(x...)
  66. # define canvdbg(x...)
  67. # define canlldbg(x...)
  68. # define canllvdbg(x...)
  69. #endif
  70. /* Timing Definitions *******************************************************/
  71. #define HALF_SECOND_MSEC 500
  72. #define HALF_SECOND_USEC 500000L
  73. /****************************************************************************
  74. * Private Type Definitions
  75. ****************************************************************************/
  76. /****************************************************************************
  77. * Private Function Prototypes
  78. ****************************************************************************/
  79. static int can_open(FAR struct file *filep);
  80. static int can_close(FAR struct file *filep);
  81. static ssize_t can_read(FAR struct file *filep, FAR char *buffer, size_t buflen);
  82. static int can_xmit(FAR struct can_dev_s *dev);
  83. static ssize_t can_write(FAR struct file *filep, FAR const char *buffer, size_t buflen);
  84. static inline ssize_t can_rtrread(FAR struct can_dev_s *dev, FAR struct canioctl_rtr_s *rtr);
  85. static int can_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  86. /****************************************************************************
  87. * Private Data
  88. ****************************************************************************/
  89. static const struct file_operations g_canops =
  90. {
  91. can_open, /* open */
  92. can_close, /* close */
  93. can_read, /* read */
  94. can_write, /* write */
  95. 0, /* seek */
  96. can_ioctl /* ioctl */
  97. #ifndef CONFIG_DISABLE_POLL
  98. , 0 /* poll */
  99. #endif
  100. };
  101. /****************************************************************************
  102. * Private Functions
  103. ****************************************************************************/
  104. /************************************************************************************
  105. * Name: can_open
  106. *
  107. * Description:
  108. * This function is called whenever the CAN device is opened.
  109. *
  110. ************************************************************************************/
  111. static int can_open(FAR struct file *filep)
  112. {
  113. FAR struct inode *inode = filep->f_inode;
  114. FAR struct can_dev_s *dev = inode->i_private;
  115. uint8_t tmp;
  116. int ret = OK;
  117. canvdbg("ocount: %d\n", dev->cd_ocount);
  118. /* If the port is the middle of closing, wait until the close is finished */
  119. if (sem_wait(&dev->cd_closesem) != OK)
  120. {
  121. ret = -errno;
  122. }
  123. else
  124. {
  125. /* Increment the count of references to the device. If this the first
  126. * time that the driver has been opened for this device, then initialize
  127. * the device.
  128. */
  129. tmp = dev->cd_ocount + 1;
  130. if (tmp == 0)
  131. {
  132. /* More than 255 opens; uint8_t overflows to zero */
  133. ret = -EMFILE;
  134. }
  135. else
  136. {
  137. /* Check if this is the first time that the driver has been opened. */
  138. if (tmp == 1)
  139. {
  140. /* Yes.. perform one time hardware initialization. */
  141. irqstate_t flags = irqsave();
  142. ret = dev_setup(dev);
  143. if (ret == OK)
  144. {
  145. /* Mark the FIFOs empty */
  146. dev->cd_xmit.tx_head = 0;
  147. dev->cd_xmit.tx_queue = 0;
  148. dev->cd_xmit.tx_tail = 0;
  149. dev->cd_recv.rx_head = 0;
  150. dev->cd_recv.rx_tail = 0;
  151. /* Finally, Enable the CAN RX interrupt */
  152. dev_rxint(dev, true);
  153. /* Save the new open count on success */
  154. dev->cd_ocount = tmp;
  155. }
  156. irqrestore(flags);
  157. }
  158. }
  159. sem_post(&dev->cd_closesem);
  160. }
  161. return ret;
  162. }
  163. /************************************************************************************
  164. * Name: can_close
  165. *
  166. * Description:
  167. * This routine is called when the CAN device is closed.
  168. * It waits for the last remaining data to be sent.
  169. *
  170. ************************************************************************************/
  171. static int can_close(FAR struct file *filep)
  172. {
  173. FAR struct inode *inode = filep->f_inode;
  174. FAR struct can_dev_s *dev = inode->i_private;
  175. irqstate_t flags;
  176. int ret = OK;
  177. canvdbg("ocount: %d\n", dev->cd_ocount);
  178. if (sem_wait(&dev->cd_closesem) != OK)
  179. {
  180. ret = -errno;
  181. }
  182. else
  183. {
  184. /* Decrement the references to the driver. If the reference count will
  185. * decrement to 0, then uninitialize the driver.
  186. */
  187. if (dev->cd_ocount > 1)
  188. {
  189. dev->cd_ocount--;
  190. sem_post(&dev->cd_closesem);
  191. }
  192. else
  193. {
  194. /* There are no more references to the port */
  195. dev->cd_ocount = 0;
  196. /* Stop accepting input */
  197. dev_rxint(dev, false);
  198. /* Now we wait for the transmit FIFO to clear */
  199. while (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail)
  200. {
  201. #ifndef CONFIG_DISABLE_SIGNALS
  202. usleep(HALF_SECOND_USEC);
  203. #else
  204. up_mdelay(HALF_SECOND_MSEC);
  205. #endif
  206. }
  207. /* And wait for the TX hardware FIFO to drain */
  208. while (!dev_txempty(dev))
  209. {
  210. #ifndef CONFIG_DISABLE_SIGNALS
  211. usleep(HALF_SECOND_USEC);
  212. #else
  213. up_mdelay(HALF_SECOND_MSEC);
  214. #endif
  215. }
  216. /* Free the IRQ and disable the CAN device */
  217. flags = irqsave(); /* Disable interrupts */
  218. dev_shutdown(dev); /* Disable the CAN */
  219. irqrestore(flags);
  220. sem_post(&dev->cd_closesem);
  221. }
  222. }
  223. return ret;
  224. }
  225. /************************************************************************************
  226. * Name: can_read
  227. *
  228. * Description:
  229. * Read standard CAN messages
  230. *
  231. ************************************************************************************/
  232. static ssize_t can_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
  233. {
  234. FAR struct inode *inode = filep->f_inode;
  235. FAR struct can_dev_s *dev = inode->i_private;
  236. size_t nread;
  237. irqstate_t flags;
  238. int ret = 0;
  239. canvdbg("buflen: %d\n", buflen);
  240. /* The caller must provide enough memory to catch the smallest possible message
  241. * This is not a system error condition, but we won't permit it, Hence we return 0.
  242. */
  243. if (buflen >= CAN_MSGLEN(0))
  244. {
  245. /* Interrupts must be disabled while accessing the cd_recv FIFO */
  246. flags = irqsave();
  247. while (dev->cd_recv.rx_head == dev->cd_recv.rx_tail)
  248. {
  249. /* The receive FIFO is empty -- was non-blocking mode selected? */
  250. if (filep->f_oflags & O_NONBLOCK)
  251. {
  252. ret = -EAGAIN;
  253. goto return_with_irqdisabled;
  254. }
  255. /* Wait for a message to be received */
  256. ret = sem_wait(&dev->cd_recv.rx_sem);
  257. if (ret < 0)
  258. {
  259. ret = -errno;
  260. goto return_with_irqdisabled;
  261. }
  262. }
  263. /* The cd_recv FIFO is not empty. Copy all buffered data that will fit
  264. * in the user buffer.
  265. */
  266. nread = 0;
  267. do
  268. {
  269. /* Will the next message in the FIFO fit into the user buffer? */
  270. FAR struct can_msg_s *msg = &dev->cd_recv.rx_buffer[dev->cd_recv.rx_head];
  271. int msglen = CAN_MSGLEN(msg->cm_hdr.ch_dlc);
  272. if (nread + msglen > buflen)
  273. {
  274. break;
  275. }
  276. /* Copy the message to the user buffer */
  277. memcpy(&buffer[nread], msg, msglen);
  278. nread += msglen;
  279. /* Increment the head of the circular message buffer */
  280. if (++dev->cd_recv.rx_head >= CONFIG_CAN_FIFOSIZE)
  281. {
  282. dev->cd_recv.rx_head = 0;
  283. }
  284. }
  285. while (dev->cd_recv.rx_head != dev->cd_recv.rx_tail);
  286. /* All on the messages have bee transferred. Return the number of bytes
  287. * that were read.
  288. */
  289. ret = nread;
  290. return_with_irqdisabled:
  291. irqrestore(flags);
  292. }
  293. return ret;
  294. }
  295. /************************************************************************************
  296. * Name: can_xmit
  297. *
  298. * Description:
  299. * Send the message at the head of the cd_xmit FIFO
  300. *
  301. * Assumptions:
  302. * Called with interrupts disabled
  303. *
  304. ************************************************************************************/
  305. static int can_xmit(FAR struct can_dev_s *dev)
  306. {
  307. int tmpndx;
  308. int ret = -EBUSY;
  309. canllvdbg("xmit head: %d queue: %d tail: %d\n",
  310. dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue, dev->cd_xmit.tx_tail);
  311. /* If there is nothing to send, then just disable interrupts and return */
  312. if (dev->cd_xmit.tx_head == dev->cd_xmit.tx_tail)
  313. {
  314. DEBUGASSERT(dev->cd_xmit.tx_queue == dev->cd_xmit.tx_head);
  315. dev_txint(dev, false);
  316. return -EIO;
  317. }
  318. /* Check if we have already queued all of the data in the TX fifo.
  319. *
  320. * tx_tail: Incremented in can_write each time a message is queued in the FIFO
  321. * tx_head: Incremented in can_txdone each time a message completes
  322. * tx_queue: Incremented each time that a message is sent to the hardware.
  323. *
  324. * Logically (ignoring buffer wrap-around): tx_head <= tx_queue <= tx_tail
  325. * tx_head == tx_queue == tx_tail means that the FIFO is empty
  326. * tx_head < tx_queue == tx_tail means that all data has been queued, but
  327. * we are still waiting for transmissions to complete.
  328. */
  329. while (dev->cd_xmit.tx_queue != dev->cd_xmit.tx_tail && dev_txready(dev))
  330. {
  331. /* No.. The fifo should not be empty in this case */
  332. DEBUGASSERT(dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail);
  333. /* Increment the FIFO queue index before sending (because dev_send()
  334. * might call can_txdone().
  335. */
  336. tmpndx = dev->cd_xmit.tx_queue;
  337. if (++dev->cd_xmit.tx_queue >= CONFIG_CAN_FIFOSIZE)
  338. {
  339. dev->cd_xmit.tx_queue = 0;
  340. }
  341. /* Send the next message at the FIFO queue index */
  342. ret = dev_send(dev, &dev->cd_xmit.tx_buffer[tmpndx]);
  343. if (ret != OK)
  344. {
  345. candbg("dev_send failed: %d\n", ret);
  346. break;
  347. }
  348. }
  349. /* Make sure that TX interrupts are enabled */
  350. dev_txint(dev, true);
  351. return ret;
  352. }
  353. /************************************************************************************
  354. * Name: can_write
  355. ************************************************************************************/
  356. static ssize_t can_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
  357. {
  358. FAR struct inode *inode = filep->f_inode;
  359. FAR struct can_dev_s *dev = inode->i_private;
  360. FAR struct can_txfifo_s *fifo = &dev->cd_xmit;
  361. FAR struct can_msg_s *msg;
  362. bool inactive;
  363. ssize_t nsent = 0;
  364. irqstate_t flags;
  365. int nexttail;
  366. int msglen;
  367. int ret = 0;
  368. canvdbg("buflen: %d\n", buflen);
  369. /* Interrupts must disabled throughout the following */
  370. flags = irqsave();
  371. /* Check if the TX is inactive when we started. In certain race conditionas, there
  372. * may be a pending interrupt to kick things back off, but we will here that there
  373. * is not. That the hardware is IDLE and will need to be kick-started.
  374. */
  375. inactive = dev_txempty(dev);
  376. /* Add the messages to the FIFO. Ignore any trailing messages that are
  377. * shorter than the minimum.
  378. */
  379. while ((buflen - nsent) >= CAN_MSGLEN(0))
  380. {
  381. /* Check if adding this new message would over-run the drivers ability to enqueue
  382. * xmit data.
  383. */
  384. nexttail = fifo->tx_tail + 1;
  385. if (nexttail >= CONFIG_CAN_FIFOSIZE)
  386. {
  387. nexttail = 0;
  388. }
  389. /* If the XMIT fifo becomes full, then wait for space to become available */
  390. while (nexttail == fifo->tx_head)
  391. {
  392. /* The transmit FIFO is full -- was non-blocking mode selected? */
  393. if (filep->f_oflags & O_NONBLOCK)
  394. {
  395. if (nsent == 0)
  396. {
  397. ret = -EAGAIN;
  398. }
  399. else
  400. {
  401. ret = nsent;
  402. }
  403. goto return_with_irqdisabled;
  404. }
  405. /* If the TX hardware was inactive when we started, then we will have
  406. * start the XMIT sequence generate the TX done interrrupts needed
  407. * to clear the FIFO.
  408. */
  409. if (inactive)
  410. {
  411. can_xmit(dev);
  412. }
  413. /* Wait for a message to be sent */
  414. do
  415. {
  416. DEBUGASSERT(dev->cd_ntxwaiters < 255);
  417. dev->cd_ntxwaiters++;
  418. ret = sem_wait(&fifo->tx_sem);
  419. dev->cd_ntxwaiters--;
  420. if (ret < 0 && errno != EINTR)
  421. {
  422. ret = -errno;
  423. goto return_with_irqdisabled;
  424. }
  425. }
  426. while (ret < 0);
  427. /* Re-check the FIFO state */
  428. inactive = dev_txempty(dev);
  429. }
  430. /* We get here if there is space at the end of the FIFO. Add the new
  431. * CAN message at the tail of the FIFO.
  432. */
  433. msg = (FAR struct can_msg_s *)&buffer[nsent];
  434. msglen = CAN_MSGLEN(msg->cm_hdr.ch_dlc);
  435. memcpy(&fifo->tx_buffer[fifo->tx_tail], msg, msglen);
  436. /* Increment the tail of the circular buffer */
  437. fifo->tx_tail = nexttail;
  438. /* Increment the number of bytes that were sent */
  439. nsent += msglen;
  440. }
  441. /* We get here after all messages have been added to the FIFO. Check if
  442. * we need to kick of the XMIT sequence.
  443. */
  444. if (inactive)
  445. {
  446. can_xmit(dev);
  447. }
  448. /* Return the number of bytes that were sent */
  449. ret = nsent;
  450. return_with_irqdisabled:
  451. irqrestore(flags);
  452. return ret;
  453. }
  454. /************************************************************************************
  455. * Name: can_rtrread
  456. *
  457. * Description:
  458. * Read RTR messages. The RTR message is a special message -- it is an outgoing
  459. * message that says "Please re-transmit the message with the same identifier as
  460. * this message. So the RTR read is really a send-wait-receive operation.
  461. *
  462. ************************************************************************************/
  463. static inline ssize_t can_rtrread(FAR struct can_dev_s *dev, FAR struct canioctl_rtr_s *rtr)
  464. {
  465. FAR struct can_rtrwait_s *wait = NULL;
  466. irqstate_t flags;
  467. int i;
  468. int ret = -ENOMEM;
  469. /* Disable interrupts through this operation */
  470. flags = irqsave();
  471. /* Find an avaiable slot in the pending RTR list */
  472. for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++)
  473. {
  474. FAR struct can_rtrwait_s *tmp = &dev->cd_rtr[i];
  475. if (!rtr->ci_msg)
  476. {
  477. tmp->cr_id = rtr->ci_id;
  478. tmp->cr_msg = rtr->ci_msg;
  479. dev->cd_npendrtr++;
  480. wait = tmp;
  481. break;
  482. }
  483. }
  484. if (wait)
  485. {
  486. /* Send the remote transmission request */
  487. ret = dev_remoterequest(dev, wait->cr_id);
  488. if (ret == OK)
  489. {
  490. /* Then wait for the response */
  491. ret = sem_wait(&wait->cr_sem);
  492. }
  493. }
  494. irqrestore(flags);
  495. return ret;
  496. }
  497. /************************************************************************************
  498. * Name: can_ioctl
  499. ************************************************************************************/
  500. static int can_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  501. {
  502. FAR struct inode *inode = filep->f_inode;
  503. FAR struct can_dev_s *dev = inode->i_private;
  504. int ret = OK;
  505. canvdbg("cmd: %d arg: %ld\n", cmd, arg);
  506. /* Handle built-in ioctl commands */
  507. switch (cmd)
  508. {
  509. /* CANIOCTL_RTR: Send the remote transmission request and wait for the response.
  510. * Argument is a reference to struct canioctl_rtr_s (casting to uintptr_t first
  511. * eliminates complaints on some architectures where the sizeof long is different
  512. * from the size of a pointer).
  513. */
  514. case CANIOCTL_RTR:
  515. ret = can_rtrread(dev, (struct canioctl_rtr_s*)((uintptr_t)arg));
  516. break;
  517. /* Not a "built-in" ioctl command.. perhaps it is unique to this device driver */
  518. default:
  519. ret = dev_ioctl(dev, cmd, arg);
  520. break;
  521. }
  522. return ret;
  523. }
  524. /****************************************************************************
  525. * Public Functions
  526. ****************************************************************************/
  527. /************************************************************************************
  528. * Name: can_register
  529. *
  530. * Description:
  531. * Register serial console and serial ports.
  532. *
  533. ************************************************************************************/
  534. int can_register(FAR const char *path, FAR struct can_dev_s *dev)
  535. {
  536. int i;
  537. /* Initialize the CAN device structure */
  538. dev->cd_ocount = 0;
  539. sem_init(&dev->cd_xmit.tx_sem, 0, 0);
  540. sem_init(&dev->cd_recv.rx_sem, 0, 0);
  541. sem_init(&dev->cd_closesem, 0, 1);
  542. for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++)
  543. {
  544. sem_init(&dev->cd_rtr[i].cr_sem, 0, 0);
  545. dev->cd_rtr[i].cr_msg = NULL;
  546. dev->cd_npendrtr--;
  547. }
  548. /* Initialize/reset the CAN hardware */
  549. dev_reset(dev);
  550. /* Register the CAN device */
  551. canvdbg("Registering %s\n", path);
  552. return register_driver(path, &g_canops, 0666, dev);
  553. }
  554. /************************************************************************************
  555. * Name: can_receive
  556. *
  557. * Description:
  558. * Called from the CAN interrupt handler when new read data is available
  559. *
  560. * Parameters:
  561. * dev - CAN driver state structure
  562. * hdr - CAN message header
  563. * data - CAN message data (if DLC > 0)
  564. *
  565. * Assumptions:
  566. * CAN interrupts are disabled.
  567. *
  568. ************************************************************************************/
  569. int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, FAR uint8_t *data)
  570. {
  571. FAR struct can_rxfifo_s *fifo = &dev->cd_recv;
  572. FAR uint8_t *dest;
  573. int nexttail;
  574. int err = -ENOMEM;
  575. int i;
  576. canllvdbg("ID: %d DLC: %d\n", hdr->ch_id, hdr->ch_dlc);
  577. /* Check if adding this new message would over-run the drivers ability to enqueue
  578. * read data.
  579. */
  580. nexttail = fifo->rx_tail + 1;
  581. if (nexttail >= CONFIG_CAN_FIFOSIZE)
  582. {
  583. nexttail = 0;
  584. }
  585. /* First, check if this response matches any RTR response that we may be waiting for */
  586. if (dev->cd_npendrtr > 0)
  587. {
  588. /* There are pending RTR requests -- search the lists of requests
  589. * and see any any matches this new message.
  590. */
  591. for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++)
  592. {
  593. FAR struct can_rtrwait_s *rtr = &dev->cd_rtr[i];
  594. FAR struct can_msg_s *msg = rtr->cr_msg;
  595. /* Check if the entry is valid and if the ID matches. A valid entry has
  596. * a non-NULL receiving address
  597. */
  598. if (msg && hdr->ch_id == rtr->cr_id)
  599. {
  600. /* We have the response... copy the data to the user's buffer */
  601. memcpy(&msg->cm_hdr, hdr, sizeof(struct can_hdr_s));
  602. for (i = 0, dest = msg->cm_data; i < hdr->ch_dlc; i++)
  603. {
  604. *dest++ = *data++;
  605. }
  606. /* Mark the entry unused */
  607. rtr->cr_msg = NULL;
  608. /* And restart the waiting thread */
  609. sem_post(&rtr->cr_sem);
  610. }
  611. }
  612. }
  613. /* Refuse the new data if the FIFO is full */
  614. if (nexttail != fifo->rx_head)
  615. {
  616. /* Add the new, decoded CAN message at the tail of the FIFO */
  617. memcpy(&fifo->rx_buffer[fifo->rx_tail].cm_hdr, hdr, sizeof(struct can_hdr_s));
  618. for (i = 0, dest = fifo->rx_buffer[fifo->rx_tail].cm_data; i < hdr->ch_dlc; i++)
  619. {
  620. *dest++ = *data++;
  621. }
  622. /* Increment the tail of the circular buffer */
  623. fifo->rx_tail = nexttail;
  624. /* The increment the counting semaphore. The maximum value should be
  625. * CONFIG_CAN_FIFOSIZE -- one possible count for each allocated message buffer.
  626. */
  627. sem_post(&fifo->rx_sem);
  628. err = OK;
  629. }
  630. return err;
  631. }
  632. /************************************************************************************
  633. * Name: can_txdone
  634. *
  635. * Description:
  636. * Called from the CAN interrupt handler at the completion of a send operation.
  637. *
  638. * Parameters:
  639. * dev - The specific CAN device
  640. * hdr - The 16-bit CAN header
  641. * data - An array contain the CAN data.
  642. *
  643. * Return:
  644. * OK on success; a negated errno on failure.
  645. *
  646. ************************************************************************************/
  647. int can_txdone(FAR struct can_dev_s *dev)
  648. {
  649. int ret = -ENOENT;
  650. canllvdbg("xmit head: %d queue: %d tail: %d\n",
  651. dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue, dev->cd_xmit.tx_tail);
  652. /* Verify that the xmit FIFO is not empty */
  653. if (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail)
  654. {
  655. DEBUGASSERT(dev->cd_xmit.tx_head != dev->cd_xmit.tx_queue);
  656. /* Remove the message at the head of the xmit FIFO */
  657. if (++dev->cd_xmit.tx_head >= CONFIG_CAN_FIFOSIZE)
  658. {
  659. dev->cd_xmit.tx_head = 0;
  660. }
  661. /* Send the next message in the FIFO */
  662. ret = can_xmit(dev);
  663. /* Are there any threads waiting for space in the TX FIFO? */
  664. if (ret == OK && dev->cd_ntxwaiters > 0)
  665. {
  666. /* Yes.. Inform them that new xmit space is available */
  667. ret = sem_post(&dev->cd_xmit.tx_sem);
  668. }
  669. }
  670. return ret;
  671. }
  672. #endif /* CONFIG_CAN */