serial_io.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /************************************************************************************
  2. * drivers/serial/serial_io.c
  3. *
  4. * Copyright (C) 2007-2009, 2011, 2015, 2018 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 <semaphore.h>
  42. #include <debug.h>
  43. #include <nuttx/serial/serial.h>
  44. /************************************************************************************
  45. * Public Functions
  46. ************************************************************************************/
  47. /************************************************************************************
  48. * Name: uart_xmitchars
  49. *
  50. * Description:
  51. * This function is called from the UART interrupt handler when an interrupt
  52. * is received indicating that there is more space in the transmit FIFO. This
  53. * function will send characters from the tail of the xmit buffer while the driver
  54. * write() logic adds data to the head of the xmit buffer.
  55. *
  56. ************************************************************************************/
  57. void uart_xmitchars(FAR uart_dev_t *dev)
  58. {
  59. uint16_t nbytes = 0;
  60. /* Send while we still have data in the TX buffer & room in the fifo */
  61. while (dev->xmit.head != dev->xmit.tail && uart_txready(dev))
  62. {
  63. /* Send the next byte */
  64. uart_send(dev, dev->xmit.buffer[dev->xmit.tail]);
  65. nbytes++;
  66. /* Increment the tail index */
  67. if (++(dev->xmit.tail) >= dev->xmit.size)
  68. {
  69. dev->xmit.tail = 0;
  70. }
  71. }
  72. /* When all of the characters have been sent from the buffer disable the TX
  73. * interrupt.
  74. *
  75. * Potential bug? If nbytes == 0 && (dev->xmit.head == dev->xmit.tail) &&
  76. * dev->xmitwaiting == true, then disabling the TX interrupt will leave
  77. * the uart_write() logic waiting to TX to complete with no TX interrupts.
  78. * Can that happen?
  79. */
  80. if (dev->xmit.head == dev->xmit.tail)
  81. {
  82. uart_disabletxint(dev);
  83. }
  84. /* If any bytes were removed from the buffer, inform any waiters there there is
  85. * space available.
  86. */
  87. if (nbytes)
  88. {
  89. uart_datasent(dev);
  90. }
  91. }
  92. /************************************************************************************
  93. * Name: uart_receivechars
  94. *
  95. * Description:
  96. * This function is called from the UART interrupt handler when an interrupt
  97. * is received indicating that are bytes available in the receive fifo. This
  98. * function will add chars to head of receive buffer. Driver read() logic will
  99. * take characters from the tail of the buffer.
  100. *
  101. ************************************************************************************/
  102. void uart_recvchars(FAR uart_dev_t *dev)
  103. {
  104. FAR struct uart_buffer_s *rxbuf = &dev->recv;
  105. #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
  106. unsigned int watermark;
  107. #endif
  108. unsigned int status;
  109. int nexthead = rxbuf->head + 1;
  110. #if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGSTP)
  111. int signo = 0;
  112. #endif
  113. uint16_t nbytes = 0;
  114. if (nexthead >= rxbuf->size)
  115. {
  116. nexthead = 0;
  117. }
  118. #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
  119. /* Pre-calculate the watermark level that we will need to test against. */
  120. watermark = (CONFIG_SERIAL_IFLOWCONTROL_UPPER_WATERMARK * rxbuf->size) / 100;
  121. #endif
  122. /* Loop putting characters into the receive buffer until there are no further
  123. * characters to available.
  124. */
  125. while (uart_rxavailable(dev))
  126. {
  127. bool is_full = (nexthead == rxbuf->tail);
  128. char ch;
  129. #ifdef CONFIG_SERIAL_IFLOWCONTROL
  130. #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
  131. unsigned int nbuffered;
  132. /* How many bytes are buffered */
  133. if (rxbuf->head >= rxbuf->tail)
  134. {
  135. nbuffered = rxbuf->head - rxbuf->tail;
  136. }
  137. else
  138. {
  139. nbuffered = rxbuf->size - rxbuf->tail + rxbuf->head;
  140. }
  141. /* Is the level now above the watermark level that we need to report? */
  142. if (nbuffered >= watermark)
  143. {
  144. /* Let the lower level driver know that the watermark level has been
  145. * crossed. It will probably activate RX flow control.
  146. */
  147. if (uart_rxflowcontrol(dev, nbuffered, true))
  148. {
  149. /* Low-level driver activated RX flow control, exit loop now. */
  150. break;
  151. }
  152. }
  153. #else
  154. /* Check if RX buffer is full and allow serial low-level driver to pause
  155. * processing. This allows proper utilization of hardware flow control.
  156. */
  157. if (is_full)
  158. {
  159. if (uart_rxflowcontrol(dev, rxbuf->size, true))
  160. {
  161. /* Low-level driver activated RX flow control, exit loop now. */
  162. break;
  163. }
  164. }
  165. #endif
  166. #endif
  167. /* Get this next character from the hardware */
  168. ch = uart_receive(dev, &status);
  169. #ifdef CONFIG_TTY_SIGINT
  170. /* Is this the special character that will generate the SIGINT signal? */
  171. if (dev->pid >= 0 && ch == CONFIG_TTY_SIGINT_CHAR)
  172. {
  173. /* Yes.. note that the kill is needed and do not put the character
  174. * into the Rx buffer. It should not be read as normal data.
  175. */
  176. signo = SIGINT;
  177. }
  178. else
  179. #endif
  180. #ifdef CONFIG_TTY_SIGSTP
  181. /* Is this the special character that will generate the SIGSTP signal? */
  182. if (dev->pid >= 0 && ch == CONFIG_TTY_SIGSTP_CHAR)
  183. {
  184. #ifdef CONFIG_TTY_SIGINT
  185. /* Give precedence to SIGINT */
  186. if (signo == 0)
  187. #endif
  188. {
  189. /* Note that the kill is needed and do not put the character
  190. * into the Rx buffer. It should not be read as normal data.
  191. */
  192. signo = SIGSTP;
  193. }
  194. }
  195. else
  196. #endif
  197. /* If the RX buffer becomes full, then the serial data is discarded. This is
  198. * necessary because on most serial hardware, you must read the data in order
  199. * to clear the RX interrupt. An option on some hardware might be to simply
  200. * disable RX interrupts until the RX buffer becomes non-FULL. However, that
  201. * would probably just cause the overrun to occur in hardware (unless it has
  202. * some large internal buffering).
  203. */
  204. if (!is_full)
  205. {
  206. /* Add the character to the buffer */
  207. rxbuf->buffer[rxbuf->head] = ch;
  208. nbytes++;
  209. /* Increment the head index */
  210. rxbuf->head = nexthead;
  211. if (++nexthead >= rxbuf->size)
  212. {
  213. nexthead = 0;
  214. }
  215. }
  216. }
  217. /* If any bytes were added to the buffer, inform any waiters there is new
  218. * incoming data available.
  219. */
  220. if (nbytes)
  221. {
  222. uart_datareceived(dev);
  223. }
  224. #if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGSTP)
  225. /* Send the signal if necessary */
  226. if (signo != 0)
  227. {
  228. kill(dev->pid, signo);
  229. uart_reset_sem(dev);
  230. }
  231. #endif
  232. }