SerialBase.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 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_SERIALBASE_H
  17. #define MBED_SERIALBASE_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
  20. #include "Callback.h"
  21. #include "serial_api.h"
  22. #include "mbed_toolchain.h"
  23. #include "platform/NonCopyable.h"
  24. #if DEVICE_SERIAL_ASYNCH
  25. #include "CThunk.h"
  26. #include "dma_api.h"
  27. #endif
  28. namespace mbed {
  29. /** \addtogroup drivers */
  30. /** A base class for serial port implementations
  31. * Can't be instantiated directly (use Serial or RawSerial)
  32. *
  33. * @note Synchronization level: Set by subclass
  34. * @ingroup drivers
  35. */
  36. class SerialBase : private NonCopyable<SerialBase> {
  37. public:
  38. /** Set the baud rate of the serial port
  39. *
  40. * @param baudrate The baudrate of the serial port (default = 9600).
  41. */
  42. void baud(int baudrate);
  43. enum Parity {
  44. None = 0,
  45. Odd,
  46. Even,
  47. Forced1,
  48. Forced0
  49. };
  50. enum IrqType {
  51. RxIrq = 0,
  52. TxIrq,
  53. IrqCnt
  54. };
  55. enum Flow {
  56. Disabled = 0,
  57. RTS,
  58. CTS,
  59. RTSCTS
  60. };
  61. /** Set the transmission format used by the serial port
  62. *
  63. * @param bits The number of bits in a word (5-8; default = 8)
  64. * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
  65. * @param stop_bits The number of stop bits (1 or 2; default = 1)
  66. */
  67. void format(int bits = 8, Parity parity = SerialBase::None, int stop_bits = 1);
  68. /** Determine if there is a character available to read
  69. *
  70. * @returns
  71. * 1 if there is a character available to read,
  72. * 0 otherwise
  73. */
  74. int readable();
  75. /** Determine if there is space available to write a character
  76. *
  77. * @returns
  78. * 1 if there is space to write a character,
  79. * 0 otherwise
  80. */
  81. int writeable();
  82. /** Attach a function to call whenever a serial interrupt is generated
  83. *
  84. * @param func A pointer to a void function, or 0 to set as none
  85. * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
  86. */
  87. void attach(Callback<void()> func, IrqType type = RxIrq);
  88. /** Attach a member function to call whenever a serial interrupt is generated
  89. *
  90. * @param obj pointer to the object to call the member function on
  91. * @param method pointer to the member function to be called
  92. * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
  93. * @deprecated
  94. * The attach function does not support cv-qualifiers. Replaced by
  95. * attach(callback(obj, method), type).
  96. */
  97. template<typename T>
  98. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  99. "The attach function does not support cv-qualifiers. Replaced by "
  100. "attach(callback(obj, method), type).")
  101. void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
  102. {
  103. attach(callback(obj, method), type);
  104. }
  105. /** Attach a member function to call whenever a serial interrupt is generated
  106. *
  107. * @param obj pointer to the object to call the member function on
  108. * @param method pointer to the member function to be called
  109. * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
  110. * @deprecated
  111. * The attach function does not support cv-qualifiers. Replaced by
  112. * attach(callback(obj, method), type).
  113. */
  114. template<typename T>
  115. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  116. "The attach function does not support cv-qualifiers. Replaced by "
  117. "attach(callback(obj, method), type).")
  118. void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
  119. {
  120. attach(callback(obj, method), type);
  121. }
  122. /** Generate a break condition on the serial line
  123. */
  124. void send_break();
  125. protected:
  126. /** Acquire exclusive access to this serial port
  127. */
  128. virtual void lock(void);
  129. /** Release exclusive access to this serial port
  130. */
  131. virtual void unlock(void);
  132. public:
  133. #if DEVICE_SERIAL_FC
  134. /** Set the flow control type on the serial port
  135. *
  136. * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
  137. * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
  138. * @param flow2 the second flow control pin (CTS for RTSCTS)
  139. */
  140. void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
  141. #endif
  142. static void _irq_handler(uint32_t id, SerialIrq irq_type);
  143. #if DEVICE_SERIAL_ASYNCH
  144. /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
  145. *
  146. * This function locks the deep sleep until any event has occurred
  147. *
  148. * @param buffer The buffer where received data will be stored
  149. * @param length The buffer length in bytes
  150. * @param callback The event callback function
  151. * @param event The logical OR of TX events
  152. */
  153. int write(const uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE);
  154. /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
  155. *
  156. * This function locks the deep sleep until any event has occurred
  157. *
  158. * @param buffer The buffer where received data will be stored
  159. * @param length The buffer length in bytes
  160. * @param callback The event callback function
  161. * @param event The logical OR of TX events
  162. */
  163. int write(const uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE);
  164. /** Abort the on-going write transfer
  165. */
  166. void abort_write();
  167. /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
  168. *
  169. * This function locks the deep sleep until any event has occurred
  170. *
  171. * @param buffer The buffer where received data will be stored
  172. * @param length The buffer length in bytes
  173. * @param callback The event callback function
  174. * @param event The logical OR of RX events
  175. * @param char_match The matching character
  176. */
  177. int read(uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
  178. /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
  179. *
  180. * This function locks the deep sleep until any event has occurred
  181. *
  182. * @param buffer The buffer where received data will be stored
  183. * @param length The buffer length in bytes
  184. * @param callback The event callback function
  185. * @param event The logical OR of RX events
  186. * @param char_match The matching character
  187. */
  188. int read(uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
  189. /** Abort the on-going read transfer
  190. */
  191. void abort_read();
  192. /** Configure DMA usage suggestion for non-blocking TX transfers
  193. *
  194. * @param usage The usage DMA hint for peripheral
  195. * @return Zero if the usage was set, -1 if a transaction is on-going
  196. */
  197. int set_dma_usage_tx(DMAUsage usage);
  198. /** Configure DMA usage suggestion for non-blocking RX transfers
  199. *
  200. * @param usage The usage DMA hint for peripheral
  201. * @return Zero if the usage was set, -1 if a transaction is on-going
  202. */
  203. int set_dma_usage_rx(DMAUsage usage);
  204. protected:
  205. void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match);
  206. void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event);
  207. void interrupt_handler_asynch(void);
  208. #endif
  209. protected:
  210. SerialBase(PinName tx, PinName rx, int baud);
  211. virtual ~SerialBase();
  212. int _base_getc();
  213. int _base_putc(int c);
  214. #if DEVICE_SERIAL_ASYNCH
  215. CThunk<SerialBase> _thunk_irq;
  216. DMAUsage _tx_usage;
  217. DMAUsage _rx_usage;
  218. event_callback_t _tx_callback;
  219. event_callback_t _rx_callback;
  220. #endif
  221. serial_t _serial;
  222. Callback<void()> _irq[IrqCnt];
  223. int _baud;
  224. };
  225. } // namespace mbed
  226. #endif
  227. #endif