Naze32 clone with Frysky receiver
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

serial_api.h 9.0KB

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