drv_usart.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  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. /******************************************************************************
  17. * @file drv_usart.h
  18. * @brief header file for usart driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_USART_H_
  23. #define _CSI_USART_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <drv_common.h>
  28. /// definition for usart handle.
  29. typedef void *usart_handle_t;
  30. /****** USART specific error codes *****/
  31. typedef enum {
  32. EDRV_USART_MODE = (EDRV_SPECIFIC + 1), ///< Specified Mode not supported
  33. EDRV_USART_BAUDRATE, ///< Specified baudrate not supported
  34. EDRV_USART_DATA_BITS, ///< Specified number of Data bits not supported
  35. EDRV_USART_PARITY, ///< Specified Parity not supported
  36. EDRV_USART_STOP_BITS, ///< Specified number of Stop bits not supported
  37. EDRV_USART_FLOW_CONTROL, ///< Specified Flow Control not supported
  38. EDRV_USART_CPOL, ///< Specified Clock Polarity not supported
  39. EDRV_USART_CPHA ///< Specified Clock Phase not supported
  40. } drv_usart_error_e;
  41. /*----- USART Control Codes: Mode -----*/
  42. typedef enum {
  43. USART_MODE_ASYNCHRONOUS = 0, ///< USART (Asynchronous)
  44. USART_MODE_SYNCHRONOUS_MASTER , ///< Synchronous Master
  45. USART_MODE_SYNCHRONOUS_SLAVE , ///< Synchronous Slave (external clock signal)
  46. USART_MODE_SINGLE_WIRE ///< USART Single-wire (half-duplex)
  47. } usart_mode_e;
  48. /*----- USART Control Codes: Mode Parameters: Data Bits -----*/
  49. typedef enum {
  50. USART_DATA_BITS_5 = 0, ///< 5 Data bits
  51. USART_DATA_BITS_6 , ///< 6 Data bit
  52. USART_DATA_BITS_7 , ///< 7 Data bits
  53. USART_DATA_BITS_8 , ///< 8 Data bits (default)
  54. USART_DATA_BITS_9 ///< 9 Data bits
  55. } usart_data_bits_e;
  56. /*----- USART Control Codes: Mode Parameters: Parity -----*/
  57. typedef enum {
  58. USART_PARITY_NONE = 0, ///< No Parity (default)
  59. USART_PARITY_EVEN , ///< Even Parity
  60. USART_PARITY_ODD , ///< Odd Parity
  61. USART_PARITY_1 , ///< Parity forced to 1
  62. USART_PARITY_0 ///< Parity forced to 0
  63. } usart_parity_e;
  64. /*----- USART Control Codes: Mode Parameters: Stop Bits -----*/
  65. typedef enum {
  66. USART_STOP_BITS_1 = 0, ///< 1 Stop bit (default)
  67. USART_STOP_BITS_2 , ///< 2 Stop bits
  68. USART_STOP_BITS_1_5 , ///< 1.5 Stop bits
  69. USART_STOP_BITS_0_5 ///< 0.5 Stop bits
  70. } usart_stop_bits_e;
  71. /*----- USART Control Codes: Mode Parameters: Clock Polarity (Synchronous mode) -----*/
  72. typedef enum {
  73. USART_CPOL0 = 0, ///< CPOL = 0 (default)
  74. USART_CPOL1 ///< CPOL = 1
  75. } usart_cpol_e;
  76. /*----- USART Control Codes: Mode Parameters: Clock Phase (Synchronous mode) -----*/
  77. typedef enum {
  78. USART_CPHA0 = 0, ///< CPHA = 0 (default)
  79. USART_CPHA1 ///< CPHA = 1
  80. } usart_cpha_e;
  81. /*----- USART Control Codes: flush data type-----*/
  82. typedef enum {
  83. USART_FLUSH_WRITE,
  84. USART_FLUSH_READ
  85. } usart_flush_type_e;
  86. /**
  87. \brief USART Status
  88. */
  89. typedef struct {
  90. uint32_t tx_busy : 1; ///< Transmitter busy flag
  91. uint32_t rx_busy : 1; ///< Receiver busy flag
  92. uint32_t tx_underflow : 1; ///< Transmit data underflow detected (cleared on start of next send operation)(Synchronous Slave)
  93. uint32_t rx_overflow : 1; ///< Receive data overflow detected (cleared on start of next receive operation)
  94. uint32_t rx_break : 1; ///< Break detected on receive (cleared on start of next receive operation)
  95. uint32_t rx_framing_error : 1; ///< Framing error detected on receive (cleared on start of next receive operation)
  96. uint32_t rx_parity_error : 1; ///< Parity error detected on receive (cleared on start of next receive operation)
  97. } usart_status_t;
  98. /****** USART Event *****/
  99. typedef enum {
  100. USART_EVENT_SEND_COMPLETE = 0, ///< Send completed; however USART may still transmit data
  101. USART_EVENT_RECEIVE_COMPLETE = 1, ///< Receive completed
  102. USART_EVENT_RECEIVED = 2, ///< Receiving data
  103. USART_EVENT_TRANSFER_COMPLETE = 3, ///< Transfer completed
  104. USART_EVENT_TX_COMPLETE = 4, ///< Transmit completed (optional)
  105. USART_EVENT_TX_UNDERFLOW = 5, ///< Transmit data not available (Synchronous Slave)
  106. USART_EVENT_RX_OVERFLOW = 6, ///< Receive data overflow
  107. USART_EVENT_RX_TIMEOUT = 7, ///< Receive character timeout (optional)
  108. USART_EVENT_RX_BREAK = 8, ///< Break detected on receive
  109. USART_EVENT_RX_FRAMING_ERROR = 9, ///< Framing error detected on receive
  110. USART_EVENT_RX_PARITY_ERROR = 10, ///< Parity error detected on receive
  111. } usart_event_e;
  112. typedef void (*usart_event_cb_t)(usart_event_e event, void *cb_arg); ///< Pointer to \ref usart_event_cb_t : USART Event call back.
  113. /**
  114. \brief USART Driver Capabilities.
  115. */
  116. typedef struct {
  117. uint32_t asynchronous : 1; ///< supports USART (Asynchronous) mode
  118. uint32_t synchronous_master : 1; ///< supports Synchronous Master mode
  119. uint32_t synchronous_slave : 1; ///< supports Synchronous Slave mode
  120. uint32_t single_wire : 1; ///< supports USART Single-wire mode
  121. uint32_t event_tx_complete : 1; ///< Transmit completed event
  122. uint32_t event_rx_timeout : 1; ///< Signal receive character timeout event
  123. } usart_capabilities_t;
  124. /**
  125. \brief Initialize USART Interface. 1. Initializes the resources needed for the USART interface 2.registers event callback function
  126. \param[in] tx usart pin of tx
  127. \param[in] rx usart pin of rx
  128. \param[in] cb_event Pointer to \ref usart_event_cb_t
  129. \param[in] cb_arg argument for cb_event
  130. \return return usart handle if success
  131. */
  132. usart_handle_t csi_usart_initialize(pin_t tx, pin_t rx, usart_event_cb_t cb_event, void *cb_arg);
  133. /**
  134. \brief De-initialize USART Interface. stops operation and releases the software resources used by the interface
  135. \param[in] handle usart handle to operate.
  136. \return error code
  137. */
  138. int32_t csi_usart_uninitialize(usart_handle_t handle);
  139. /**
  140. \brief Get driver capabilities.
  141. \param[in] handle usart handle to operate.
  142. \return \ref usart_capabilities_t
  143. */
  144. usart_capabilities_t csi_usart_get_capabilities(usart_handle_t handle);
  145. /**
  146. \brief config usart mode.
  147. \param[in] handle usart handle to operate.
  148. \param[in] sysclk system clock.
  149. \param[in] baud configured baud .
  150. \param[in] mode \ref usart_mode_e .
  151. \param[in] parity \ref usart_parity_e .
  152. \param[in] stopbits \ref usart_stop_bits_e .
  153. \param[in] bits \ref usart_data_bits_e .
  154. \return error code
  155. */
  156. int32_t csi_usart_config(usart_handle_t handle,
  157. uint32_t sysclk,
  158. uint32_t baud,
  159. usart_mode_e mode,
  160. usart_parity_e parity,
  161. usart_stop_bits_e stopbits,
  162. usart_data_bits_e bits);
  163. /**
  164. \brief config usart default tx value. used in synchronous mode
  165. \param[in] handle usart handle to operate.
  166. \param[in] value default tx value
  167. \return error code
  168. */
  169. int32_t csi_usart_set_default_tx_value(usart_handle_t handle, uint32_t value);
  170. /**
  171. \brief Start sending data to USART transmitter,(received data is ignored).
  172. This function is non-blocking,\ref usart_event_e is signaled when operation completes or error happens.
  173. \ref csi_usart_get_status can indicates operation status.
  174. \param[in] handle usart handle to operate.
  175. \param[in] data Pointer to buffer with data to send to USART transmitter. data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
  176. \param[in] num Number of data items to send
  177. \return error code
  178. */
  179. int32_t csi_usart_send(usart_handle_t handle, const void *data, uint32_t num/*,bool asynch*/);
  180. /**
  181. \brief Abort Send data to USART transmitter
  182. \param[in] handle usart handle to operate.
  183. \return error code
  184. */
  185. int32_t csi_usart_abort_send(usart_handle_t handle);
  186. /**
  187. \brief Start receiving data from USART receiver.transmits the default value as specified by csi_usart_set_default_tx_value. \n
  188. This function is non-blocking,\ref usart_event_e is signaled when operation completes or error happens.
  189. \ref csi_usart_get_status can indicates operation status.
  190. \param[in] handle usart handle to operate.
  191. \param[out] data Pointer to buffer for data to receive from USART receiver.data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
  192. \param[in] num Number of data items to receive
  193. \return error code
  194. */
  195. int32_t csi_usart_receive(usart_handle_t handle, void *data, uint32_t num/*,bool asynch*/);
  196. /**
  197. \brief query data from UART receiver FIFO.
  198. \param[in] handle usart handle to operate.
  199. \param[out] data Pointer to buffer for data to receive from UART receiver
  200. \param[in] num Number of data items to receive
  201. \return receive fifo data num
  202. */
  203. int32_t csi_usart_receive_query(usart_handle_t handle, void *data, uint32_t num/*,bool asynch*/);
  204. /**
  205. \brief Abort Receive data from USART receiver
  206. \param[in] handle usart handle to operate.
  207. \return error code
  208. */
  209. int32_t csi_usart_abort_receive(usart_handle_t handle);
  210. /**
  211. \brief Start synchronously sends data to the USART transmitter and receives data from the USART receiver. used in synchronous mode
  212. This function is non-blocking,\ref usart_event_e is signaled when operation completes or error happens.
  213. \ref csi_usart_get_status can indicates operation status.
  214. \param[in] handle usart handle to operate.
  215. \param[in] data_out Pointer to buffer with data to send to USART transmitter.data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
  216. \param[out] data_in Pointer to buffer for data to receive from USART receiver.data_type is : uint8_t for 5..8 data bits, uint16_t for 9 data bits
  217. \param[in] num Number of data items to transfer
  218. \return error code
  219. */
  220. int32_t csi_usart_transfer(usart_handle_t handle, const void *data_out, void *data_in, uint32_t num/*,bool asynch*/);
  221. /**
  222. \brief abort sending/receiving data to/from USART transmitter/receiver.
  223. \param[in] handle usart handle to operate.
  224. \return error code
  225. */
  226. int32_t csi_usart_abort_transfer(usart_handle_t handle);
  227. /**
  228. \brief Get USART status.
  229. \param[in] handle usart handle to operate.
  230. \return USART status \ref usart_status_t
  231. */
  232. usart_status_t csi_usart_get_status(usart_handle_t handle);
  233. /**
  234. \brief flush receive/send data.
  235. \param[in] handle usart handle to operate.
  236. \param[in] type \ref usart_flush_type_e .
  237. \return error code
  238. */
  239. int32_t csi_usart_flush(usart_handle_t handle, usart_flush_type_e type);
  240. //add function
  241. int32_t dw_usart_getchar(usart_handle_t handle, uint8_t *ch);
  242. int32_t dw_usart_getchar_no_poll(usart_handle_t handle, uint8_t *ch);
  243. int32_t dw_usart_putchar(usart_handle_t handle, uint8_t ch);
  244. int32_t dw_usart_set_int_flag(usart_handle_t handle,uint32_t flag);
  245. int32_t dw_usart_clr_int_flag(usart_handle_t handle,uint32_t flag);
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #endif /* _CSI_USART_H_ */