serial_api.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /** \addtogroup hal */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 2006-2013 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MBED_SERIAL_API_H
  19. #define MBED_SERIAL_API_H
  20. #include "device.h"
  21. #include "hal/buffer.h"
  22. #include "hal/dma_api.h"
  23. #if DEVICE_SERIAL
  24. #define SERIAL_EVENT_TX_SHIFT (2)
  25. #define SERIAL_EVENT_RX_SHIFT (8)
  26. #define SERIAL_EVENT_TX_MASK (0x00FC)
  27. #define SERIAL_EVENT_RX_MASK (0x3F00)
  28. #define SERIAL_EVENT_ERROR (1 << 1)
  29. /**
  30. * @defgroup SerialTXEvents Serial TX Events Macros
  31. *
  32. * @{
  33. */
  34. #define SERIAL_EVENT_TX_COMPLETE (1 << (SERIAL_EVENT_TX_SHIFT + 0))
  35. #define SERIAL_EVENT_TX_ALL (SERIAL_EVENT_TX_COMPLETE)
  36. /**@}*/
  37. /**
  38. * @defgroup SerialRXEvents Serial RX Events Macros
  39. *
  40. * @{
  41. */
  42. #define SERIAL_EVENT_RX_COMPLETE (1 << (SERIAL_EVENT_RX_SHIFT + 0))
  43. #define SERIAL_EVENT_RX_OVERRUN_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 1))
  44. #define SERIAL_EVENT_RX_FRAMING_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 2))
  45. #define SERIAL_EVENT_RX_PARITY_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 3))
  46. #define SERIAL_EVENT_RX_OVERFLOW (1 << (SERIAL_EVENT_RX_SHIFT + 4))
  47. #define SERIAL_EVENT_RX_CHARACTER_MATCH (1 << (SERIAL_EVENT_RX_SHIFT + 5))
  48. #define SERIAL_EVENT_RX_ALL (SERIAL_EVENT_RX_OVERFLOW | SERIAL_EVENT_RX_PARITY_ERROR | \
  49. SERIAL_EVENT_RX_FRAMING_ERROR | SERIAL_EVENT_RX_OVERRUN_ERROR | \
  50. SERIAL_EVENT_RX_COMPLETE | SERIAL_EVENT_RX_CHARACTER_MATCH)
  51. /**@}*/
  52. #define SERIAL_RESERVED_CHAR_MATCH (255)
  53. typedef enum {
  54. ParityNone = 0,
  55. ParityOdd = 1,
  56. ParityEven = 2,
  57. ParityForced1 = 3,
  58. ParityForced0 = 4
  59. } SerialParity;
  60. typedef enum {
  61. RxIrq,
  62. TxIrq
  63. } SerialIrq;
  64. typedef enum {
  65. FlowControlNone,
  66. FlowControlRTS,
  67. FlowControlCTS,
  68. FlowControlRTSCTS
  69. } FlowControl;
  70. typedef void (*uart_irq_handler)(uint32_t id, SerialIrq event);
  71. #if DEVICE_SERIAL_ASYNCH
  72. /** Asynch serial HAL structure
  73. */
  74. typedef struct {
  75. struct serial_s serial; /**< Target specific serial structure */
  76. struct buffer_s tx_buff; /**< TX buffer */
  77. struct buffer_s rx_buff; /**< RX buffer */
  78. uint8_t char_match; /**< Character to be matched */
  79. uint8_t char_found; /**< State of the matched character */
  80. } serial_t;
  81. #else
  82. /** Non-asynch serial HAL structure
  83. */
  84. typedef struct serial_s serial_t;
  85. #endif
  86. #ifdef __cplusplus
  87. extern "C" {
  88. #endif
  89. /**
  90. * \defgroup hal_GeneralSerial Serial Configuration Functions
  91. * @{
  92. */
  93. /** Initialize the serial peripheral. It sets the default parameters for serial
  94. * peripheral, and configures its specifieds pins.
  95. *
  96. * @param obj The serial object
  97. * @param tx The TX pin name
  98. * @param rx The RX pin name
  99. */
  100. void serial_init(serial_t *obj, PinName tx, PinName rx);
  101. /** Release the serial peripheral, not currently invoked. It requires further
  102. * resource management.
  103. *
  104. * @param obj The serial object
  105. */
  106. void serial_free(serial_t *obj);
  107. /** Configure the baud rate
  108. *
  109. * @param obj The serial object
  110. * @param baudrate The baud rate to be configured
  111. */
  112. void serial_baud(serial_t *obj, int baudrate);
  113. /** Configure the format. Set the number of bits, parity and the number of stop bits
  114. *
  115. * @param obj The serial object
  116. * @param data_bits The number of data bits
  117. * @param parity The parity
  118. * @param stop_bits The number of stop bits
  119. */
  120. void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
  121. /** The serial interrupt handler registration
  122. *
  123. * @param obj The serial object
  124. * @param handler The interrupt handler which will be invoked when the interrupt fires
  125. * @param id The SerialBase object
  126. */
  127. void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id);
  128. /** Configure serial interrupt. This function is used for word-approach
  129. *
  130. * @param obj The serial object
  131. * @param irq The serial IRQ type (RX or TX)
  132. * @param enable Set to non-zero to enable events, or zero to disable them
  133. */
  134. void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable);
  135. /** Get character. This is a blocking call, waiting for a character
  136. *
  137. * @param obj The serial object
  138. */
  139. int serial_getc(serial_t *obj);
  140. /** Send a character. This is a blocking call, waiting for a peripheral to be available
  141. * for writing
  142. *
  143. * @param obj The serial object
  144. * @param c The character to be sent
  145. */
  146. void serial_putc(serial_t *obj, int c);
  147. /** Check if the serial peripheral is readable
  148. *
  149. * @param obj The serial object
  150. * @return Non-zero value if a character can be read, 0 if nothing to read
  151. */
  152. int serial_readable(serial_t *obj);
  153. /** Check if the serial peripheral is writable
  154. *
  155. * @param obj The serial object
  156. * @return Non-zero value if a character can be written, 0 otherwise.
  157. */
  158. int serial_writable(serial_t *obj);
  159. /** Clear the serial peripheral
  160. *
  161. * @param obj The serial object
  162. */
  163. void serial_clear(serial_t *obj);
  164. /** Set the break
  165. *
  166. * @param obj The serial object
  167. */
  168. void serial_break_set(serial_t *obj);
  169. /** Clear the break
  170. *
  171. * @param obj The serial object
  172. */
  173. void serial_break_clear(serial_t *obj);
  174. /** Configure the TX pin for UART function.
  175. *
  176. * @param tx The pin name used for TX
  177. */
  178. void serial_pinout_tx(PinName tx);
  179. /** Configure the serial for the flow control. It sets flow control in the hardware
  180. * if a serial peripheral supports it, otherwise software emulation is used.
  181. *
  182. * @param obj The serial object
  183. * @param type The type of the flow control. Look at the available FlowControl types.
  184. * @param rxflow The TX pin name
  185. * @param txflow The RX pin name
  186. */
  187. void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow);
  188. #if DEVICE_SERIAL_ASYNCH
  189. /**@}*/
  190. /**
  191. * \defgroup hal_AsynchSerial Asynchronous Serial Hardware Abstraction Layer
  192. * @{
  193. */
  194. /** Begin asynchronous TX transfer. The used buffer is specified in the serial object,
  195. * tx_buff
  196. *
  197. * @param obj The serial object
  198. * @param tx The transmit buffer
  199. * @param tx_length The number of bytes to transmit
  200. * @param tx_width Deprecated argument
  201. * @param handler The serial handler
  202. * @param event The logical OR of events to be registered
  203. * @param hint A suggestion for how to use DMA with this transfer
  204. * @return Returns number of data transfered, otherwise returns 0
  205. */
  206. int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint);
  207. /** Begin asynchronous RX transfer (enable interrupt for data collecting)
  208. * The used buffer is specified in the serial object - rx_buff
  209. *
  210. * @param obj The serial object
  211. * @param rx The receive buffer
  212. * @param rx_length The number of bytes to receive
  213. * @param rx_width Deprecated argument
  214. * @param handler The serial handler
  215. * @param event The logical OR of events to be registered
  216. * @param handler The serial handler
  217. * @param char_match A character in range 0-254 to be matched
  218. * @param hint A suggestion for how to use DMA with this transfer
  219. */
  220. void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint);
  221. /** Attempts to determine if the serial peripheral is already in use for TX
  222. *
  223. * @param obj The serial object
  224. * @return Non-zero if the RX transaction is ongoing, 0 otherwise
  225. */
  226. uint8_t serial_tx_active(serial_t *obj);
  227. /** Attempts to determine if the serial peripheral is already in use for RX
  228. *
  229. * @param obj The serial object
  230. * @return Non-zero if the RX transaction is ongoing, 0 otherwise
  231. */
  232. uint8_t serial_rx_active(serial_t *obj);
  233. /** The asynchronous TX and RX handler.
  234. *
  235. * @param obj The serial object
  236. * @return Returns event flags if an RX transfer termination condition was met; otherwise returns 0
  237. */
  238. int serial_irq_handler_asynch(serial_t *obj);
  239. /** Abort the ongoing TX transaction. It disables the enabled interupt for TX and
  240. * flushes the TX hardware buffer if TX FIFO is used
  241. *
  242. * @param obj The serial object
  243. */
  244. void serial_tx_abort_asynch(serial_t *obj);
  245. /** Abort the ongoing RX transaction. It disables the enabled interrupt for RX and
  246. * flushes the RX hardware buffer if RX FIFO is used
  247. *
  248. * @param obj The serial object
  249. */
  250. void serial_rx_abort_asynch(serial_t *obj);
  251. /**@}*/
  252. #endif
  253. #ifdef __cplusplus
  254. }
  255. #endif
  256. #endif
  257. #endif
  258. /** @}*/