lpuart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 LPUART functions
  46. **
  47. **
  48. ** History:
  49. ** - 2017-05-10 Cathy First Version
  50. **
  51. *****************************************************************************/
  52. #ifndef __LPUART_H__
  53. #define __LPUART_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 LPUartGroup Universal Asynchronous Receiver/Transmitter (LPUART)
  66. **
  67. ******************************************************************************/
  68. //@{
  69. /******************************************************************************/
  70. /* Global pre-processor symbols/macros ('#define') */
  71. /******************************************************************************/
  72. /******************************************************************************
  73. * Global type definitions
  74. ******************************************************************************/
  75. /**
  76. ******************************************************************************
  77. **\brief LPuart通道选择
  78. ******************************************************************************/
  79. typedef enum en_lpuart_channel
  80. {
  81. LPUART0 = 0u, ///<串口2
  82. LPUART1 = 1u, ///<串口3
  83. }en_lpuart_channel_t;
  84. /**
  85. ******************************************************************************
  86. ** \brief lpuart 的sclk时钟源选择
  87. ******************************************************************************/
  88. typedef enum en_lpuart_sclk_sel
  89. {
  90. LPUart_Pclk = 0u, ///<pclk
  91. LPUart_Pclk_1 = 1u, ///<pclk
  92. LPUart_Xtl = 2u, ///<外部低速晶振
  93. LPUart_Rcl = 3u, ///<内部低速晶振
  94. } en_lpuart_sclksel_t;
  95. /**
  96. ******************************************************************************
  97. ** \brief lpuart多主机模式地址帧/数据帧或者奇偶校验
  98. ******************************************************************************/
  99. typedef enum en_lpuart_mmdorck
  100. {
  101. LPUartDataOrAddr = 0u, ///<多主机模式时,通过读写SBUF[8]决定帧为数据帧或地址帧
  102. LPUartEven = 1u, ///<非多主机模式偶校验
  103. LPUartOdd = 2u, ///<非多主机模式奇校验
  104. }en_lpuart_mmdorck_t;
  105. /**
  106. ******************************************************************************
  107. ** \brief lpuart多主机模式配置
  108. ******************************************************************************/
  109. typedef enum en_lpuart_multimode
  110. {
  111. LPUartNormal = 0u, ///<正常工作模式
  112. LPUartMulti = 1u, ///<多主机工作模式
  113. }en_lpuart_multimode_t;
  114. /**
  115. ******************************************************************************
  116. ** \brief lpuart多主机模式及从机地址和地址掩码配置
  117. ******************************************************************************/
  118. typedef struct stc_lpuart_multimode
  119. {
  120. en_lpuart_multimode_t enMulti_mode; ///<多主机模式配置
  121. uint8_t u8SlaveAddr; ///<从机地址
  122. uint8_t u8SaddEn; ///<从及地址掩码
  123. }stc_lpuart_multimode_t;
  124. /**
  125. ******************************************************************************
  126. ** \brief lpuart 四种工作模式选择
  127. ******************************************************************************/
  128. typedef enum en_lpuart_mode
  129. {
  130. LPUartMode0 = 0u, ///<模式0
  131. LPUartMode1 = 1u, ///<模式1
  132. LPUartMode2 = 2u, ///<模式2
  133. LPUartMode3 = 3u, ///<模式3
  134. } en_lpuart_mode_t;
  135. /**
  136. ******************************************************************************
  137. ** \brief lpuart stop长度选择
  138. ******************************************************************************/
  139. typedef enum en_lpuart_stop
  140. {
  141. LPUart1bit = 0u, ///<1位停止位
  142. LPUart15bit = 1u, ///<1.5位停止位
  143. LPUart2bit = 2u, ///<2位停止位
  144. } en_lpuart_stop_t;
  145. /**
  146. ******************************************************************************
  147. ** \brief lpuart 功能使能
  148. ******************************************************************************/
  149. typedef enum en_lpuart_func
  150. {
  151. LPUartTx = 0u, ///<mode0模式代表TX
  152. LPUartRx = 1u, ///<非mode0模式代表RX and TX ,mode0模式代表RX
  153. LPUartDmaTx = 3u, ///<DMA发送使能
  154. LPUartDmaRx = 4u, ///<DMA接收使能
  155. LPUartCtsRts = 5u, ///<硬件流使能
  156. }en_lpuart_func_t;
  157. /**
  158. ******************************************************************************
  159. ** \brief lpuart中断使能控制
  160. ******************************************************************************/
  161. typedef enum en_lpuart_irq_sel
  162. {
  163. LPUartTxIrq = 0u, ///<发送中断使能
  164. LPUartRxIrq = 1u, ///<接收中断使能
  165. LPUartFEIrq = 3u, ///<帧错误中断使能
  166. LPUartCtsIrq = 4u, ///<CTS信号翻转中断使能
  167. LPUartPEIrq = 5u, ///<奇偶校验中断使能
  168. LPUartTxEIrq = 6u, ///<TX空中断使能
  169. }en_lpuart_irq_sel_t;
  170. /**
  171. ******************************************************************************
  172. ** \brief lpuart发送接收中断处理函数接口
  173. ******************************************************************************/
  174. typedef struct stc_lpuart_irq_cb
  175. {
  176. func_ptr_t pfnTxIrqCb; ///<发送中断服务函数
  177. func_ptr_t pfnRxFEIrqCb; ///<接收帧错误中断服务函数
  178. func_ptr_t pfnRxIrqCb; ///<接收中断服务函数
  179. func_ptr_t pfnCtsIrqCb; ///<CTS信号翻转中断服务函数
  180. func_ptr_t pfnPEIrqCb; ///<奇偶校验错误中断服务函数
  181. }stc_lpuart_irq_cb_t;
  182. /**
  183. ******************************************************************************
  184. ** \brief lpuart 状态标志位
  185. ******************************************************************************/
  186. typedef enum en_lpuart_status
  187. {
  188. LPUartCts = 0u, ///<CTS信号标记
  189. LPUartRC = 1u, ///<接收数据完成标记
  190. LPUartTC = 2u, ///<发送数据完成标记
  191. LPUartPE = 3u, ///<奇偶校验错误标记
  192. LPUartFE = 4u, ///<帧错误标记
  193. LPUartCtsIf = 5u, ///CTS中断标志
  194. LPUartTxe = 6u, ///TXbuff空标记
  195. }en_lpuart_status_t;
  196. /**
  197. ******************************************************************************
  198. ** \brief lpuart 通道地址及中断函数地址结构
  199. ******************************************************************************/
  200. typedef struct stc_lpuart_instance_data
  201. {
  202. uint32_t u32Idx; ///< 通道号
  203. M0P_LPUART_TypeDef *pstcInstance; ///< 通道寄存器地址
  204. stc_lpuart_irq_cb_t stcLPUartInternIrqCb; ///< 通道中断服务函数
  205. } stc_lpuart_instance_data_t;
  206. /**
  207. ******************************************************************************
  208. ** \brief lpuart 通道波特率配置
  209. ******************************************************************************/
  210. typedef enum en_lpuart_clkdiv
  211. {
  212. LPUart16Or32Div = 0u,///<模式0无效,模式1/3为16分频,模式2为32分频
  213. LPUart8Or16Div = 1u,///<模式0无效,模式1/3为8分频,模式2为16分频
  214. LPUart4Or8Div = 2u,///<模式0无效,模式1/3为4分频,模式2为8分频
  215. }en_lpuart_clkdiv_t;
  216. /**
  217. ******************************************************************************
  218. ** \brief lpuart 时钟相关配置
  219. ******************************************************************************/
  220. typedef struct stc_lpuart_sclk_sel
  221. {
  222. en_lpuart_clkdiv_t enSclk_Prs; ///<分频选择
  223. en_lpuart_sclksel_t enSclk_sel; ///<传输时钟选择
  224. }stc_lpuart_sclk_sel_t;
  225. /**
  226. ******************************************************************************
  227. ** \brief lpuart 通道波特率计算参数
  228. ******************************************************************************/
  229. typedef struct stc_lpuart_baud
  230. {
  231. uint32_t u32Sclk; ///<sclk
  232. en_lpuart_mode_t enRunMode; ///< 四种模式配置
  233. uint32_t u32Baud; ///< 波特率
  234. } stc_lpuart_baud_t;
  235. /**
  236. ******************************************************************************
  237. ** \lpuart 总体配置
  238. ******************************************************************************/
  239. typedef struct stc_lpuart_config
  240. {
  241. en_lpuart_mode_t enRunMode; ///< 四种模式配置
  242. stc_lpuart_sclk_sel_t* pstcLpuart_clk; ///<时钟源配置
  243. en_lpuart_stop_t enStopBit; ///<停止位长度
  244. stc_lpuart_multimode_t* pstcMultiMode; ///<多主机模式配置
  245. stc_lpuart_irq_cb_t* pstcIrqCb; ///<中断服务函数
  246. boolean_t bTouchNvic; ///<NVIC中断使能
  247. } stc_lpuart_config_t;
  248. //中断相关设置函数
  249. en_result_t LPUart_EnableIrq(uint8_t u8Idx,
  250. en_lpuart_irq_sel_t enIrqSel);
  251. en_result_t LPUart_DisableIrq(uint8_t u8Idx,
  252. en_lpuart_irq_sel_t enIrqSel);
  253. // 总初始化处理
  254. en_result_t LPUart_Init(uint8_t u8Idx,
  255. stc_lpuart_config_t* pstcConfig);
  256. //LPUART模块工作模式设置函数
  257. en_result_t LPUart_SetMode(uint8_t u8Idx,en_lpuart_mode_t enMode);
  258. //LPUART模块工作模式设置函数
  259. en_result_t LPUart_SetMultiMode(uint8_t u8Idx,stc_lpuart_multimode_t* pstcMultiConfig);
  260. //LPUART通道多主机模式发送数据/地址帧或者奇偶校验配置TB8
  261. en_result_t LPUart_SetMMDOrCk(uint8_t u8Idx,en_lpuart_mmdorck_t enTb8);
  262. //从机地址配置
  263. en_result_t LPUart_SetSaddr(uint8_t u8Idx,uint8_t u8Addr);
  264. //从机地址掩码配置
  265. en_result_t LPUart_SetSaddrEn(uint8_t u8Idx,uint8_t u8Addren);
  266. //停止位配置
  267. en_result_t LPUart_SetStopBit(uint8_t u8Idx,uint8_t u8Len);
  268. //数据寄存器bit8位获取
  269. boolean_t LPUart_GetRb8(uint8_t u8Idx);
  270. //时钟源选择
  271. en_result_t LPUart_SelSclk(uint8_t u8Idx,en_lpuart_sclksel_t enClk);
  272. //时钟源时钟获取
  273. uint32_t LPUart_GetSclk(uint8_t u8Idx);
  274. //采样分频设置
  275. en_result_t LPUart_SetClkDiv(uint8_t u8Idx,en_lpuart_clkdiv_t enClkDiv);
  276. //波特率设置值计算
  277. uint16_t LPUart_CalScnt(uint8_t u8Idx,stc_lpuart_baud_t *pstcBaud);
  278. //波特率设置
  279. en_result_t LPUart_SetBaud(uint8_t u8Idx,uint16_t u16Scnt);
  280. //获取波特率
  281. uint32_t LPUart_GetBaud(uint8_t u8Idx,uint8_t u8Mode,uint32_t u32Pclk);
  282. //功能使能和禁止
  283. en_result_t LPUart_EnableFunc(uint8_t u8Idx, en_lpuart_func_t enFunc);
  284. en_result_t LPUart_DisableFunc(uint8_t u8Idx, en_lpuart_func_t enFunc);
  285. //某个状态bit获取函数
  286. boolean_t LPUart_GetStatus(uint8_t u8Idx,en_lpuart_status_t enStatus);
  287. //某个状态bit状态位的清除
  288. en_result_t LPUart_ClrStatus(uint8_t u8Idx,en_lpuart_status_t enStatus);
  289. //整个状态寄存器获取
  290. uint8_t LPUart_GetIsr(uint8_t u8Idx);
  291. //整个状态寄存器清除
  292. en_result_t LPUart_ClrIsr(uint8_t u8Idx);
  293. //数据查询方式的发送操作
  294. en_result_t LPUart_SendData(uint8_t u8Idx, uint8_t u8Data);
  295. //数据查询方式的接收操作
  296. uint8_t LPUart_ReceiveData(uint8_t u8Idx);
  297. //@} // LPUartGroup
  298. #ifdef __cplusplus
  299. #endif
  300. #endif /* __UART_H__ */
  301. /******************************************************************************
  302. * EOF (not truncated)
  303. *****************************************************************************/