uart.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "ddl.h"
  58. #include "interrupts_hc32l136.h"
  59. #ifdef __cplusplus
  60. extern "C"
  61. {
  62. #endif
  63. /**
  64. ******************************************************************************
  65. ** \defgroup UartGroup Universal Asynchronous Receiver/Transmitter (UART)
  66. **
  67. ******************************************************************************/
  68. //@{
  69. /******************************************************************************/
  70. /* Global pre-processor symbols/macros ('#define') */
  71. /******************************************************************************/
  72. /******************************************************************************
  73. * Global type definitions
  74. ******************************************************************************/
  75. /**
  76. ******************************************************************************
  77. **\brief uart通道选择
  78. ******************************************************************************/
  79. typedef enum en_uart_channel
  80. {
  81. UARTCH0 = 0u, ///<串口0
  82. UARTCH1 = 1u, ///<串口1
  83. }en_uart_channel_t;
  84. /**
  85. ******************************************************************************
  86. ** \brief uart多主机模式地址帧/数据帧或者奇偶校验
  87. ******************************************************************************/
  88. typedef enum en_uart_mmdorck
  89. {
  90. UartDataOrAddr = 0u, ///<多主机模式时,通过读写SBUF[8]决定帧为数据帧或地址帧
  91. UartEven = 1u, ///<非多主机模式偶校验
  92. UartOdd = 2u, ///<非多主机模式奇校验
  93. }en_uart_mmdorck_t;
  94. /**
  95. ******************************************************************************
  96. ** \brief uart多主机模式配置
  97. ******************************************************************************/
  98. typedef enum en_uart_multimode
  99. {
  100. UartNormal = 0u, ///<正常工作模式
  101. UartMulti = 1u, ///<多主机工作模式
  102. }en_uart_multimode_t;
  103. /**
  104. ******************************************************************************
  105. ** \brief uart多主机模式及从机地址和地址掩码配置
  106. ******************************************************************************/
  107. typedef struct stc_uart_multimode
  108. {
  109. en_uart_multimode_t enMulti_mode; ///<多主机模式配置
  110. uint8_t u8SlaveAddr; ///<从机地址
  111. uint8_t u8SaddEn; ///<从及地址掩码
  112. }stc_uart_multimode_t;
  113. /**
  114. ******************************************************************************
  115. ** \brief uart 四种工作模式选择
  116. ******************************************************************************/
  117. typedef enum en_uart_mode
  118. {
  119. UartMode0 = 0u, ///<模式0
  120. UartMode1 = 1u, ///<模式1
  121. UartMode2 = 2u, ///<模式2
  122. UartMode3 = 3u, ///<模式3
  123. } en_uart_mode_t;
  124. /**
  125. ******************************************************************************
  126. ** \brief uart stop长度选择
  127. ******************************************************************************/
  128. typedef enum en_uart_stop
  129. {
  130. Uart1bit = 0u, ///<1位停止位
  131. Uart15bit = 1u, ///<1.5位停止位
  132. Uart2bit = 2u, ///<2位停止位
  133. } en_uart_stop_t;
  134. /**
  135. ******************************************************************************
  136. ** \brief uart 功能使能
  137. ******************************************************************************/
  138. typedef enum en_uart_func
  139. {
  140. UartTx = 0u, ///<mode0模式代表TX
  141. UartRx = 1u, ///<非mode0模式代表RX and TX ,mode0模式代表RX
  142. UartDmaTx = 3u, ///<DMA发送使能
  143. UartDmaRx = 4u, ///<DMA接收使能
  144. UartCtsRts = 5u, ///<硬件流使能
  145. }en_uart_func_t;
  146. /**
  147. ******************************************************************************
  148. ** \brief uart中断使能控制
  149. ******************************************************************************/
  150. typedef enum en_uart_irq_sel
  151. {
  152. UartTxIrq = 0u, ///<发送中断使能
  153. UartRxIrq = 1u, ///<接收中断使能
  154. UartFEIrq = 3u, ///<帧错误中断使能
  155. UartCtsIrq = 4u, ///<CTS信号翻转中断使能
  156. UartPEIrq = 5u, ///<奇偶校验中断使能
  157. UartTxEIrq = 6u, ///<TX空中断使能
  158. }en_uart_irq_sel_t;
  159. /**
  160. ******************************************************************************
  161. ** \brief uart发送接收中断处理函数接口
  162. ******************************************************************************/
  163. typedef struct stc_uart_irq_cb
  164. {
  165. func_ptr_t pfnTxIrqCb; ///<发送中断服务函数
  166. func_ptr_t pfnRxFEIrqCb; ///<接收帧错误中断服务函数
  167. func_ptr_t pfnRxIrqCb; ///<接收中断服务函数
  168. func_ptr_t pfnCtsIrqCb; ///<CTS信号翻转中断服务函数
  169. func_ptr_t pfnPEIrqCb; ///<奇偶校验错误中断服务函数
  170. }stc_uart_irq_cb_t;
  171. /**
  172. ******************************************************************************
  173. ** \brief uart 状态标志位
  174. ******************************************************************************/
  175. typedef enum en_uart_status
  176. {
  177. UartCts = 0u, ///<CTS信号标记
  178. UartRC = 1u, ///<接收数据完成标记
  179. UartTC = 2u, ///<发送数据完成标记
  180. UartPE = 3u, ///<奇偶校验错误标记
  181. UartFE = 4u, ///<帧错误标记
  182. UartCtsIf = 5u, ///<CTS中断标记
  183. UartTxe = 6u, ///<TXbuff空标记
  184. }en_uart_status_t;
  185. /**
  186. ******************************************************************************
  187. ** \brief uart 通道地址及中断函数地址结构
  188. ******************************************************************************/
  189. typedef struct stc_uart_instance_data
  190. {
  191. uint32_t u32Idx; ///< 通道号
  192. M0P_UART_TypeDef *pstcInstance; ///< 通道寄存器地址
  193. stc_uart_irq_cb_t stcUartInternIrqCb; ///< 通道中断服务函数
  194. } stc_uart_instance_data_t;
  195. /**
  196. ******************************************************************************
  197. ** \brief uart 通道波特率配置
  198. ******************************************************************************/
  199. typedef enum en_uart_clkdiv
  200. {
  201. Uart16Or32Div = 0u,///<模式0无效,模式1/3为16分频,模式2为32分频
  202. Uart8Or16Div = 1u,///<模式0无效,模式1/3为8分频,模式2为16分频
  203. }en_uart_clkdiv_t;
  204. /**
  205. ******************************************************************************
  206. ** \brief uart 通道波特率计算参数
  207. ******************************************************************************/
  208. typedef struct stc_uart_baud
  209. {
  210. uint32_t u32Pclk; ///<pclk
  211. en_uart_mode_t enRunMode; ///< 四种模式配置
  212. uint32_t u32Baud; ///< 波特率
  213. } stc_uart_baud_t;
  214. /**
  215. ******************************************************************************
  216. ** \uart 总体配置
  217. ******************************************************************************/
  218. typedef struct stc_uart_config
  219. {
  220. en_uart_mode_t enRunMode; ///< 四种模式配置
  221. en_uart_stop_t enStopBit; ///<停止位长度
  222. stc_uart_multimode_t* pstcMultiMode; ///<多主机模式配置
  223. stc_uart_irq_cb_t* pstcIrqCb; ///<中断服务函数
  224. boolean_t bTouchNvic; ///<NVIC中断使能
  225. } stc_uart_config_t;
  226. //中断相关设置函数
  227. en_result_t Uart_EnableIrq(uint8_t u8Idx,
  228. en_uart_irq_sel_t enIrqSel);
  229. en_result_t Uart_DisableIrq(uint8_t u8Idx,
  230. en_uart_irq_sel_t enIrqSel);
  231. //void Uart_IrqHandler(uint8_t u8Idx);
  232. // 总初始化处理
  233. en_result_t Uart_Init(uint8_t u8Idx,
  234. stc_uart_config_t* pstcConfig);
  235. en_result_t Uart_DeInit(uint8_t u8Idx, boolean_t bTouchNvic);
  236. //UART模块工作模式设置函数
  237. en_result_t Uart_SetMode(uint8_t u8Idx,en_uart_mode_t enMode);
  238. //UART模块多主机模式设置函数
  239. en_result_t Uart_SetMultiMode(uint8_t u8Idx,stc_uart_multimode_t* pstcMultiConfig);
  240. //UART通道多主机模式发送数据/地址帧或者奇偶校验配置TB8
  241. en_result_t Uart_SetMMDOrCk(uint8_t u8Idx,en_uart_mmdorck_t enTb8);
  242. //从机地址配置
  243. en_result_t Uart_SetSaddr(uint8_t u8Idx,uint8_t u8Addr);
  244. //从机地址掩码配置
  245. en_result_t Uart_SetSaddrEn(uint8_t u8Idx,uint8_t u8Addren);
  246. //停止位长度设置
  247. en_result_t Uart_SetStopBit(uint8_t u8Idx,uint8_t u8Len);
  248. //RB8数据获取
  249. boolean_t Uart_GetRb8(uint8_t u8Idx);
  250. //采样分频设置
  251. en_result_t Uart_SetClkDiv(uint8_t u8Idx,en_uart_clkdiv_t enClkDiv);
  252. //波特率设置值计算
  253. uint16_t Uart_CalScnt(uint8_t u8Idx,stc_uart_baud_t *pstcBaud);
  254. //波特率设置
  255. en_result_t Uart_SetBaud(uint8_t u8Idx,uint16_t u16Scnt);
  256. //获取波特率
  257. uint32_t Uart_GetBaud(uint8_t u8Idx,uint8_t u8Mode,uint32_t u32Pclk);
  258. // 功能使能和禁止
  259. en_result_t Uart_EnableFunc(uint8_t u8Idx, en_uart_func_t enFunc);
  260. en_result_t Uart_DisableFunc(uint8_t u8Idx, en_uart_func_t enFunc);
  261. //状态位的获取和清除
  262. uint8_t Uart_GetIsr(uint8_t u8Idx);
  263. boolean_t Uart_GetStatus(uint8_t u8Idx,en_uart_status_t enStatus);
  264. en_result_t Uart_ClrIsr(uint8_t u8Idx);
  265. en_result_t Uart_ClrStatus(uint8_t u8Idx,en_uart_status_t enStatus);
  266. //数据查询方式的收发操作
  267. //en_result_t Uart_MultiSendFirstData(uint8_t U8Addr);
  268. en_result_t Uart_SendData(uint8_t u8Idx, uint8_t u8Data);
  269. int Uart_ReceiveData(uint8_t u8Idx);
  270. //@} // UartGroup
  271. #ifdef __cplusplus
  272. #endif
  273. #endif /* __UART_H__ */
  274. /******************************************************************************
  275. * EOF (not truncated)
  276. *****************************************************************************/