UARTSerial.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2017 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_UARTSERIAL_H
  17. #define MBED_UARTSERIAL_H
  18. #include "platform/platform.h"
  19. #if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
  20. #include "FileHandle.h"
  21. #include "SerialBase.h"
  22. #include "InterruptIn.h"
  23. #include "PlatformMutex.h"
  24. #include "serial_api.h"
  25. #include "CircularBuffer.h"
  26. #include "platform/NonCopyable.h"
  27. #ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE
  28. #define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256
  29. #endif
  30. #ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE
  31. #define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256
  32. #endif
  33. namespace mbed {
  34. /** \addtogroup drivers */
  35. /** Class providing buffered UART communication functionality using separate circular buffer for send and receive channels
  36. *
  37. * @ingroup drivers
  38. */
  39. class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UARTSerial> {
  40. public:
  41. /** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
  42. * @param tx Transmit pin
  43. * @param rx Receive pin
  44. * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
  45. */
  46. UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
  47. virtual ~UARTSerial();
  48. /** Equivalent to POSIX poll(). Derived from FileHandle.
  49. * Provides a mechanism to multiplex input/output over a set of file handles.
  50. */
  51. virtual short poll(short events) const;
  52. /* Resolve ambiguities versus our private SerialBase
  53. * (for writable, spelling differs, but just in case)
  54. */
  55. using FileHandle::readable;
  56. using FileHandle::writable;
  57. /** Write the contents of a buffer to a file
  58. *
  59. * Follows POSIX semantics:
  60. *
  61. * * if blocking, block until all data is written
  62. * * if no data can be written, and non-blocking set, return -EAGAIN
  63. * * if some data can be written, and non-blocking set, write partial
  64. *
  65. * @param buffer The buffer to write from
  66. * @param length The number of bytes to write
  67. * @return The number of bytes written, negative error on failure
  68. */
  69. virtual ssize_t write(const void *buffer, size_t length);
  70. /** Read the contents of a file into a buffer
  71. *
  72. * Follows POSIX semantics:
  73. *
  74. * * if no data is available, and non-blocking set return -EAGAIN
  75. * * if no data is available, and blocking set, wait until data is available
  76. * * If any data is available, call returns immediately
  77. *
  78. * @param buffer The buffer to read in to
  79. * @param length The number of bytes to read
  80. * @return The number of bytes read, 0 at end of file, negative error on failure
  81. */
  82. virtual ssize_t read(void *buffer, size_t length);
  83. /** Close a file
  84. *
  85. * @return 0 on success, negative error code on failure
  86. */
  87. virtual int close();
  88. /** Check if the file in an interactive terminal device
  89. *
  90. * @return True if the file is a terminal
  91. * @return False if the file is not a terminal
  92. * @return Negative error code on failure
  93. */
  94. virtual int isatty();
  95. /** Move the file position to a given offset from from a given location
  96. *
  97. * Not valid for a device type FileHandle like UARTSerial.
  98. * In case of UARTSerial, returns ESPIPE
  99. *
  100. * @param offset The offset from whence to move to
  101. * @param whence The start of where to seek
  102. * SEEK_SET to start from beginning of file,
  103. * SEEK_CUR to start from current position in file,
  104. * SEEK_END to start from end of file
  105. * @return The new offset of the file, negative error code on failure
  106. */
  107. virtual off_t seek(off_t offset, int whence);
  108. /** Flush any buffers associated with the file
  109. *
  110. * @return 0 on success, negative error code on failure
  111. */
  112. virtual int sync();
  113. /** Set blocking or non-blocking mode
  114. * The default is blocking.
  115. *
  116. * @param blocking true for blocking mode, false for non-blocking mode.
  117. */
  118. virtual int set_blocking(bool blocking)
  119. {
  120. _blocking = blocking;
  121. return 0;
  122. }
  123. /** Check current blocking or non-blocking mode for file operations.
  124. *
  125. * @return true for blocking mode, false for non-blocking mode.
  126. */
  127. virtual bool is_blocking() const
  128. {
  129. return _blocking;
  130. }
  131. /** Register a callback on state change of the file.
  132. *
  133. * The specified callback will be called on state changes such as when
  134. * the file can be written to or read from.
  135. *
  136. * The callback may be called in an interrupt context and should not
  137. * perform expensive operations.
  138. *
  139. * Note! This is not intended as an attach-like asynchronous api, but rather
  140. * as a building block for constructing such functionality.
  141. *
  142. * The exact timing of when the registered function
  143. * is called is not guaranteed and susceptible to change. It should be used
  144. * as a cue to make read/write/poll calls to find the current state.
  145. *
  146. * @param func Function to call on state change
  147. */
  148. virtual void sigio(Callback<void()> func);
  149. /** Setup interrupt handler for DCD line
  150. *
  151. * If DCD line is connected, an IRQ handler will be setup.
  152. * Does nothing if DCD is NC, i.e., not connected.
  153. *
  154. * @param dcd_pin Pin-name for DCD
  155. * @param active_high a boolean set to true if DCD polarity is active low
  156. */
  157. void set_data_carrier_detect(PinName dcd_pin, bool active_high = false);
  158. /** Set the baud rate
  159. *
  160. * @param baud The baud rate
  161. */
  162. void set_baud(int baud);
  163. // Expose private SerialBase::Parity as UARTSerial::Parity
  164. using SerialBase::Parity;
  165. // In C++11, we wouldn't need to also have using directives for each value
  166. using SerialBase::None;
  167. using SerialBase::Odd;
  168. using SerialBase::Even;
  169. using SerialBase::Forced1;
  170. using SerialBase::Forced0;
  171. /** Set the transmission format used by the serial port
  172. *
  173. * @param bits The number of bits in a word (5-8; default = 8)
  174. * @param parity The parity used (None, Odd, Even, Forced1, Forced0; default = None)
  175. * @param stop_bits The number of stop bits (1 or 2; default = 1)
  176. */
  177. void set_format(int bits = 8, Parity parity = UARTSerial::None, int stop_bits = 1);
  178. #if DEVICE_SERIAL_FC
  179. // For now use the base enum - but in future we may have extra options
  180. // such as XON/XOFF or manual GPIO RTSCTS.
  181. using SerialBase::Flow;
  182. // In C++11, we wouldn't need to also have using directives for each value
  183. using SerialBase::Disabled;
  184. using SerialBase::RTS;
  185. using SerialBase::CTS;
  186. using SerialBase::RTSCTS;
  187. /** Set the flow control type on the serial port
  188. *
  189. * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
  190. * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
  191. * @param flow2 the second flow control pin (CTS for RTSCTS)
  192. */
  193. void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
  194. #endif
  195. private:
  196. void wait_ms(uint32_t millisec);
  197. /** SerialBase lock override */
  198. virtual void lock(void);
  199. /** SerialBase unlock override */
  200. virtual void unlock(void);
  201. /** Acquire mutex */
  202. virtual void api_lock(void);
  203. /** Release mutex */
  204. virtual void api_unlock(void);
  205. /** Software serial buffers
  206. * By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json
  207. */
  208. CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf;
  209. CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf;
  210. PlatformMutex _mutex;
  211. Callback<void()> _sigio_cb;
  212. bool _blocking;
  213. bool _tx_irq_enabled;
  214. bool _rx_irq_enabled;
  215. InterruptIn *_dcd_irq;
  216. /** Device Hanged up
  217. * Determines if the device hanged up on us.
  218. *
  219. * @return True, if hanged up
  220. */
  221. bool hup() const;
  222. /** ISRs for serial
  223. * Routines to handle interrupts on serial pins.
  224. * Copies data into Circular Buffer.
  225. * Reports the state change to File handle.
  226. */
  227. void tx_irq(void);
  228. void rx_irq(void);
  229. void wake(void);
  230. void dcd_irq(void);
  231. };
  232. } //namespace mbed
  233. #endif //(DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
  234. #endif //MBED_UARTSERIAL_H