hc32l196_uart.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /******************************************************************************
  2. * Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved.
  3. *
  4. * This software is owned and published by:
  5. * Huada Semiconductor Co.,Ltd ("HDSC").
  6. *
  7. * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
  8. * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
  9. *
  10. * This software contains source code for use with HDSC
  11. * components. This software is licensed by HDSC to be adapted only
  12. * for use in systems utilizing HDSC components. HDSC shall not be
  13. * responsible for misuse or illegal use of this software for devices not
  14. * supported herein. HDSC is providing this software "AS IS" and will
  15. * not be responsible for issues arising from incorrect user implementation
  16. * of the software.
  17. *
  18. * Disclaimer:
  19. * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
  20. * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
  21. * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
  22. * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
  23. * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
  24. * WARRANTY OF NONINFRINGEMENT.
  25. * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
  26. * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
  27. * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
  28. * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
  29. * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
  31. * SAVINGS OR PROFITS,
  32. * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
  34. * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
  35. * FROM, THE SOFTWARE.
  36. *
  37. * This software may be replicated in part or whole for the licensed use,
  38. * with the restriction that this Disclaimer and Copyright notice must be
  39. * included with each copy of this software, whether used in part or whole,
  40. * at all times.
  41. */
  42. /*****************************************************************************/
  43. /** \file uart.h
  44. **
  45. ** Headerfile for UART functions
  46. **
  47. **
  48. ** History:
  49. ** - 2017-05-10 Cathy First Version
  50. **
  51. *****************************************************************************/
  52. #ifndef __UART_H__
  53. #define __UART_H__
  54. /*****************************************************************************
  55. * Include files
  56. *****************************************************************************/
  57. #include "hc32l196_ddl.h"
  58. #ifdef __cplusplus
  59. extern "C"
  60. {
  61. #endif
  62. /**
  63. ******************************************************************************
  64. ** \defgroup UartGroup Universal Asynchronous Receiver/Transmitter (UART)
  65. **
  66. ******************************************************************************/
  67. //@{
  68. /******************************************************************************/
  69. /* Global pre-processor symbols/macros ('#define') */
  70. /******************************************************************************/
  71. /******************************************************************************
  72. * Global type definitions
  73. ******************************************************************************/
  74. /**
  75. ******************************************************************************
  76. **\brief uart通道选择
  77. ******************************************************************************/
  78. /**
  79. ******************************************************************************
  80. ** \brief uart多机模式地址帧/数据帧或者奇偶校验
  81. ******************************************************************************/
  82. typedef enum en_uart_mmdorck
  83. {
  84. UartMskDataOrAddr = 0u, ///<多机模式时,通过读写SBUF[8]决定帧为数据帧或地址帧
  85. UartMskEven = 0x4u, ///<非多机模式偶校验
  86. UartMskOdd = 0x8u, ///<非多机模式奇校验
  87. }en_uart_mmdorck_t;
  88. /**
  89. ******************************************************************************
  90. ** \brief uart多机模式及从机地址和地址掩码配置
  91. ******************************************************************************/
  92. typedef struct stc_uart_multimode
  93. {
  94. uint8_t u8SlaveAddr; ///<从机地址
  95. uint8_t u8SaddEn; ///<从及地址掩码
  96. }stc_uart_multimode_t;
  97. /**
  98. ******************************************************************************
  99. ** \brief uart 四种工作模式选择
  100. ******************************************************************************/
  101. typedef enum en_uart_mode
  102. {
  103. UartMskMode0 = 0x00u, ///<模式0
  104. UartMskMode1 = 0x40u, ///<模式1
  105. UartMskMode2 = 0x80u, ///<模式2
  106. UartMskMode3 = 0xc0u, ///<模式3
  107. } en_uart_mode_t;
  108. /**
  109. ******************************************************************************
  110. ** \brief uart stop长度选择
  111. ******************************************************************************/
  112. typedef enum en_uart_stop
  113. {
  114. UartMsk1bit = 0x0000u, ///<1位停止位
  115. UartMsk1_5bit = 0x4000u, ///<1.5位停止位
  116. UartMsk2bit = 0x8000u, ///<2位停止位
  117. } en_uart_stop_t;
  118. /**
  119. ******************************************************************************
  120. ** \brief uart 功能使能
  121. ******************************************************************************/
  122. typedef enum en_uart_func
  123. {
  124. UartRenFunc = 4u, ///<0-TX; ///<1-非mode0模式代表RX&TX ,mode0模式代表RX;
  125. UartDmaRxFunc = 16u, ///<DMA接收功能
  126. UartDmaTxFunc = 17u, ///<DMA发送功能
  127. UartRtsFunc = 18u, ///<硬件流RTS功能
  128. UartCtsFunc = 19u, ///<硬件流CTS功能
  129. UartHdFunc = 22u, ///<单线半双工功能
  130. }en_uart_func_t;
  131. /**
  132. ******************************************************************************
  133. ** \brief uart中断使能控制
  134. ******************************************************************************/
  135. typedef enum en_uart_irq_sel
  136. {
  137. UartRxIrq = 0u, ///<接收中断使能
  138. UartTxIrq = 1u, ///<发送中断使能
  139. UartTxEIrq = 8u, ///<TX空中断使能
  140. UartPEIrq = 13u, ///<奇偶校验中断使能
  141. UartCtsIrq = 20u, ///<CTS信号翻转中断使能
  142. UartFEIrq = 21u, ///<帧错误中断使能
  143. }en_uart_irq_sel_t;
  144. /**
  145. ******************************************************************************
  146. ** \brief uart 状态标志位
  147. ******************************************************************************/
  148. typedef enum en_uart_status
  149. {
  150. UartRC = 0u, ///<接收数据完成标记
  151. UartTC = 1u, ///<发送数据完成标记
  152. UartFE = 2u, ///<帧错误标记
  153. UartTxe = 3u, ///<TXbuff空标记
  154. UartPE = 4u, ///<奇偶校验错误标记
  155. UartCtsIf = 5u, ///<CTS中断标记
  156. UartCts = 6u, ///<CTS信号标记
  157. }en_uart_status_t;
  158. /**
  159. ******************************************************************************
  160. ** \brief uart 通道采样分频配置
  161. ******************************************************************************/
  162. typedef enum en_uart_clkdiv
  163. {
  164. UartMsk16Or32Div = 0u, ///<模式0无效,模式1/3为16分频,模式2为32分频
  165. UartMsk8Or16Div = 0x200u, ///<模式0无效,模式1/3为8分频,模式2为16分频
  166. }en_uart_clkdiv_t;
  167. /**
  168. ******************************************************************************
  169. ** \brief uart 通道Mode1和Mode3波特率计算参数
  170. ******************************************************************************/
  171. typedef struct stc_uart_baud
  172. {
  173. en_uart_clkdiv_t enClkDiv; ///<采样分频
  174. uint32_t u32Pclk; ///<pclk
  175. uint32_t u32Baud; ///<波特率
  176. } stc_uart_baud_t;
  177. /**
  178. ******************************************************************************
  179. ** \uart 总体配置
  180. ******************************************************************************/
  181. typedef struct stc_uart_cfg
  182. {
  183. en_uart_mode_t enRunMode; ///<四种模式配置
  184. en_uart_mmdorck_t enMmdorCk; ///<校验模式
  185. en_uart_stop_t enStopBit; ///<停止位长度
  186. stc_uart_baud_t stcBaud; ///<Mode1/3波特率配置
  187. } stc_uart_cfg_t;
  188. //UART初始化
  189. en_result_t Uart_Init(M0P_UART_TypeDef* UARTx, stc_uart_cfg_t* pstcCfg);
  190. ///< UART 单线模式使能/禁止
  191. void Uart_HdModeEnable(M0P_UART_TypeDef* UARTx);
  192. void Uart_HdModeDisable(M0P_UART_TypeDef* UARTx);
  193. //UART模块多机模式设置函数
  194. en_result_t Uart_SetMultiMode(M0P_UART_TypeDef* UARTx,stc_uart_multimode_t* pstcMultiCfg);
  195. //TB8数据设置
  196. void Uart_SetTb8(M0P_UART_TypeDef* UARTx, boolean_t bTB8Value);
  197. //RB8数据获取
  198. boolean_t Uart_GetRb8(M0P_UART_TypeDef* UARTx);
  199. //中断相关设置函数
  200. en_result_t Uart_EnableIrq(M0P_UART_TypeDef* UARTx, en_uart_irq_sel_t enIrqSel);
  201. en_result_t Uart_DisableIrq(M0P_UART_TypeDef* UARTx, en_uart_irq_sel_t enIrqSel);
  202. //功能使能和禁止
  203. en_result_t Uart_EnableFunc(M0P_UART_TypeDef* UARTx, en_uart_func_t enFunc);
  204. en_result_t Uart_DisableFunc(M0P_UART_TypeDef* UARTx, en_uart_func_t enFunc);
  205. //状态位的获取和清除
  206. uint8_t Uart_GetIsr(M0P_UART_TypeDef* UARTx);
  207. boolean_t Uart_GetStatus(M0P_UART_TypeDef* UARTx,en_uart_status_t enStatus);
  208. en_result_t Uart_ClrIsr(M0P_UART_TypeDef* UARTx);
  209. en_result_t Uart_ClrStatus(M0P_UART_TypeDef* UARTx,en_uart_status_t enStatus);
  210. //数据收发操作
  211. ///< 数据查询模式发送
  212. en_result_t Uart_SendDataPoll(M0P_UART_TypeDef* UARTx, uint8_t u8Data);
  213. ///< 数据中断模式发送
  214. en_result_t Uart_SendDataIt(M0P_UART_TypeDef* UARTx, uint8_t u8Data);
  215. uint8_t Uart_ReceiveData(M0P_UART_TypeDef* UARTx);
  216. //@} // UartGroup
  217. #ifdef __cplusplus
  218. #endif
  219. #endif /* __UART_H__ */
  220. /******************************************************************************
  221. * EOF (not truncated)
  222. *****************************************************************************/