drv_serial.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_SERIAL_H_
  13. #define _DRV_SERIAL_H_
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include <stdint.h>
  18. #include <stddef.h>
  19. #include <stdbool.h>
  20. #include <sys/queue.h>
  21. #include <osi_compiler.h>
  22. typedef struct drv_serial drvSerial_t;
  23. typedef enum
  24. {
  25. DRV_SERIAL_FLAG_TXBUF_UNCACHED = (1 << 0), ///< TX buffer is uncached
  26. } drvSerialFlag_t;
  27. typedef enum
  28. {
  29. DRV_SERIAL_EVENT_RX_ARRIVED = (1 << 0), ///< received new data
  30. DRV_SERIAL_EVENT_RX_OVERFLOW = (1 << 1), ///< rx overflow
  31. DRV_SERIAL_EVENT_TX_COMPLETE = (1 << 2), ///< all data had been sent
  32. DRV_SERIAL_EVENT_BROKEN = (1 << 3), ///< serial broken
  33. DRV_SERIAL_EVENT_READY = (1 << 4), ///< serial ready
  34. DRV_SERIAL_EVENT_OPEN = (1 << 5), ///< open by host
  35. DRV_SERIAL_EVENT_CLOSE = (1 << 6), ///< close by host
  36. } drvSerialEvent_t;
  37. /**
  38. * @brief function type seraial event callback
  39. */
  40. typedef void (*drvSerialEventCB_t)(drvSerialEvent_t event, unsigned long p);
  41. /**
  42. * @brief type to convey serial config
  43. */
  44. typedef struct drv_serial_config
  45. {
  46. uint32_t tx_buf_size; ///< tx buffer size
  47. uint32_t rx_buf_size; ///< rx buffer size
  48. uint32_t tx_dma_size; ///< tx dma buffer size, 0 for autoset
  49. uint32_t rx_dma_size; ///< rx dma buffer size, 0 for autoset
  50. drvSerialEvent_t event_mask; ///< event mask
  51. unsigned long param; ///< caller context
  52. drvSerialEventCB_t event_cb; ///< event callback, it won't be called in ISR
  53. } drvSerialCfg_t;
  54. /**
  55. * @brief check the serial is ready or not from serial name
  56. * @param name the serial name
  57. * @return
  58. * - true ready
  59. * - false not ready
  60. */
  61. bool drvSerialCheckReadyByName(uint32_t name);
  62. /**
  63. * @brief acquire and hold a serial by port name
  64. *
  65. * @param name the port name
  66. * @param cfg the serial config
  67. * @return
  68. * - serial pointer
  69. * - NULL on failed
  70. * - no serial matches the name
  71. * - serial is in use, even by myself
  72. */
  73. drvSerial_t *drvSerialAcquire(uint32_t name, const drvSerialCfg_t *cfg);
  74. /**
  75. * @brief check the serial is ready or not
  76. * @param serial the serial
  77. * @return
  78. * - true ready
  79. * - false not ready
  80. */
  81. bool drvSerialCheckReady(drvSerial_t *serial);
  82. /**
  83. * @brief release the serial
  84. *
  85. * @param serial the serial
  86. */
  87. void drvSerialRelease(drvSerial_t *serial);
  88. /**
  89. * @brief open a serial
  90. *
  91. * @param serial the serial
  92. * @param
  93. * - (-1) fail
  94. * - 0 success
  95. */
  96. bool drvSerialOpen(drvSerial_t *serial);
  97. /**
  98. * @brief close a serial
  99. *
  100. * @param serial the serial
  101. */
  102. void drvSerialClose(drvSerial_t *serial);
  103. /**
  104. * @brief write to the serial
  105. *
  106. * @param serial the serial
  107. * @param data data address
  108. * @param size data buffer size
  109. * @return
  110. * - (-1) fail
  111. * - (positive) the bytes of data actually writen
  112. */
  113. int drvSerialWrite(drvSerial_t *serial, const void *data, size_t size);
  114. /**
  115. * @brief write to serial directly (do not cache data to software fifo)
  116. *
  117. * @param serial the serial
  118. * @param data the data buffer, max be 4 bytes aligned
  119. * @param size data buffer size
  120. * @param to_ms timeout in ms
  121. * @param flags write special flags
  122. * @return true if success else false
  123. */
  124. bool drvSerialWriteDirect(drvSerial_t *serial, const void *data, size_t size, size_t to_ms, uint32_t flags);
  125. /**
  126. * @brief wait read bytes available
  127. *
  128. * @param serial the serial
  129. * @param timeout wait timeout in milliseconds
  130. * @return
  131. * - true on success
  132. * - false on timeout
  133. */
  134. bool drvSerialWaitReadAvail(drvSerial_t *serial, unsigned timeout);
  135. /**
  136. * @brief get how many data avail to be read
  137. *
  138. * @param serial the serial
  139. * @return
  140. * - (-1) fail
  141. * - other avail size in bytes
  142. */
  143. int drvSerialReadAvail(drvSerial_t *serial);
  144. /**
  145. * @brief get how many data can be writen
  146. *
  147. * @param serial the serial
  148. * @return
  149. * - (-1) fail
  150. * - other avail size in bytes
  151. */
  152. int drvSerialWriteAvail(drvSerial_t *serial);
  153. /**
  154. * @brief wait write space available
  155. *
  156. * @param serial the serial
  157. * @param timeout wait timeout in milliseconds
  158. * @return
  159. * - true on success
  160. * - false on timeout
  161. */
  162. bool drvSerialWaitWriteAvail(drvSerial_t *serial, unsigned timeout);
  163. /**
  164. * @brief wait write finish
  165. * @param serial the serial
  166. * @param timeout wait timeout in milliseconds
  167. * @return
  168. * - ture success
  169. * - false timeout
  170. */
  171. bool drvSerialWaitWriteFinish(drvSerial_t *serial, unsigned timeout);
  172. /**
  173. * @brief read data from the serial
  174. *
  175. * @param serial the serial
  176. * @param buffer the buffer to read data
  177. * @param size the size of the buffer
  178. * @return
  179. * - (-1) fail
  180. * - (positive) the bytes of data actually read
  181. */
  182. int drvSerialRead(drvSerial_t *serial, void *buffer, size_t size);
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif // _DRV_SERIAL_H_