lpuart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /******************************************************************************
  2. * Copyright (C) 2017, Xiaohua Semiconductor Co.,Ltd All rights reserved.
  3. *
  4. * This software is owned and published by:
  5. * Xiaohua Semiconductor Co.,Ltd ("XHSC").
  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 XHSC
  11. * components. This software is licensed by XHSC to be adapted only
  12. * for use in systems utilizing XHSC components. XHSC shall not be
  13. * responsible for misuse or illegal use of this software for devices not
  14. * supported herein. XHSC 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. * XHSC 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. * XHSC 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 lpuart.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. #ifdef __cplusplus
  59. extern "C"
  60. {
  61. #endif
  62. /**
  63. ******************************************************************************
  64. ** \defgroup LPUartGroup Universal Asynchronous Receiver/Transmitter (LPUART)
  65. **
  66. ******************************************************************************/
  67. //@{
  68. /******************************************************************************/
  69. /* Global pre-processor symbols/macros ('#define') */
  70. /******************************************************************************/
  71. /******************************************************************************
  72. * Global type definitions
  73. ******************************************************************************/
  74. /**
  75. ******************************************************************************
  76. **\brief LPuart通道选择
  77. ******************************************************************************/
  78. /**
  79. ******************************************************************************
  80. ** \brief lpuart 的sclk时钟源选择
  81. ******************************************************************************/
  82. typedef enum en_lpuart_sclk_sel
  83. {
  84. LPUartMskPclk = 0u<<11, ///<pclk
  85. LPUartMskXtl = 2u<<11, ///<外部低速晶振
  86. LPUartMskRcl = 3u<<11, ///<内部低速晶振
  87. } en_lpuart_sclksel_t;
  88. /**
  89. ******************************************************************************
  90. ** \brief lpuart多机模式地址帧/数据帧或者奇偶校验
  91. ******************************************************************************/
  92. typedef enum en_lpuart_mmdorck
  93. {
  94. LPUartDataOrAddr = 0u, ///<多机模式时,通过读写SBUF[8]决定帧为数据帧或地址帧
  95. LPUartEven = 0x4u, ///<非多机模式偶校验
  96. LPUartOdd = 0x8u, ///<非多机模式奇校验
  97. }en_lpuart_mmdorck_t;
  98. /**
  99. ******************************************************************************
  100. ** \brief lpuart多机模式及从机地址和地址掩码配置
  101. ******************************************************************************/
  102. typedef struct stc_lpuart_multimode
  103. {
  104. uint8_t u8SlaveAddr; ///<从机地址
  105. uint8_t u8SaddEn; ///<从及地址掩码
  106. }stc_lpuart_multimode_t;
  107. /**
  108. ******************************************************************************
  109. ** \brief lpuart 四种工作模式选择
  110. ******************************************************************************/
  111. typedef enum en_lpuart_mode
  112. {
  113. LPUartMskMode0 = 0x00u, ///<模式0
  114. LPUartMskMode1 = 0x40u, ///<模式1
  115. LPUartMskMode2 = 0x80u, ///<模式2
  116. LPUartMskMode3 = 0xc0u, ///<模式3
  117. } en_lpuart_mode_t;
  118. /**
  119. ******************************************************************************
  120. ** \brief lpuart stop长度选择
  121. ******************************************************************************/
  122. typedef enum en_lpuart_stop
  123. {
  124. LPUart1bit = 0x0000u, ///<1位停止位
  125. LPUart1_5bit = 0x4000u, ///<1.5位停止位
  126. LPUart2bit = 0x8000u, ///<2位停止位
  127. } en_lpuart_stop_t;
  128. /**
  129. ******************************************************************************
  130. ** \brief lpuart 功能使能
  131. ******************************************************************************/
  132. typedef enum en_lpuart_func
  133. {
  134. LPUartRenFunc = 4u, ///<0-TX; ///<1-非mode0模式代表RX&TX ,mode0模式代表RX;
  135. LPUartDmaRxFunc = 16u, ///<DMA接收功能
  136. LPUartDmaTxFunc = 17u, ///<DMA发送功能
  137. LPUartRtsFunc = 18u, ///<硬件流RTS功能
  138. LPUartCtsFunc = 19u, ///<硬件流CTS功能
  139. LPUartHdFunc = 22u, ///<单线半双工功能
  140. }en_lpuart_func_t;
  141. /**
  142. ******************************************************************************
  143. ** \brief lpuart中断使能控制
  144. ******************************************************************************/
  145. typedef enum en_lpuart_irq_sel
  146. {
  147. LPUartRxIrq = 0u, ///<接收中断使能
  148. LPUartTxIrq = 1u, ///<发送中断使能
  149. LPUartTxEIrq = 8u, ///<TX空中断使能
  150. LPUartPEIrq = 13u, ///<奇偶校验中断使能
  151. LPUartCtsIrq = 20u, ///<CTS信号翻转中断使能
  152. LPUartFEIrq = 21u, ///<帧错误中断使能
  153. }en_lpuart_irq_sel_t;
  154. /**
  155. ******************************************************************************
  156. ** \brief lpuart 状态标志位
  157. ******************************************************************************/
  158. typedef enum en_lpuart_status
  159. {
  160. LPUartRC = 0u, ///<接收数据完成标记
  161. LPUartTC = 1u, ///<发送数据完成标记
  162. LPUartFE = 2u, ///<帧错误标记
  163. LPUartTxe = 3u, ///<TXbuff空标记
  164. LPUartPE = 4u, ///<奇偶校验错误标记
  165. LPUartCtsIf = 5u, ///<CTS中断标记
  166. LPUartCts = 6u, ///<CTS信号标记
  167. }en_lpuart_status_t;
  168. /**
  169. ******************************************************************************
  170. ** \brief lpuart 通道采样分频配置
  171. ******************************************************************************/
  172. typedef enum en_lpuart_clkdiv
  173. {
  174. LPUartMsk16Or32Div = 0u, ///<模式0无效,模式1/3为16分频,模式2为32分频
  175. LPUartMsk8Or16Div = 0x200u, ///<模式0无效,模式1/3为8分频,模式2为16分频
  176. LPUartMsk4Or8Div = 0x400u, ///<模式0无效,模式1/3为4分频,模式2为8分频
  177. }en_lpuart_clkdiv_t;
  178. /**
  179. ******************************************************************************
  180. ** \brief lpuart 通道Mode1和Mode3波特率计算参数
  181. ******************************************************************************/
  182. typedef struct stc_lpuart_baud
  183. {
  184. en_lpuart_sclksel_t enSclkSel; ///<传输时钟源选择
  185. en_lpuart_clkdiv_t enSclkDiv; ///<采样分频选择
  186. uint32_t u32Sclk; ///<sclk
  187. uint32_t u32Baud; ///< 波特率
  188. } stc_lpuart_baud_t;
  189. /**
  190. ******************************************************************************
  191. ** \lpuart 总体配置
  192. ******************************************************************************/
  193. typedef struct stc_lpuart_cfg
  194. {
  195. en_lpuart_mode_t enRunMode; ///<四种模式配置
  196. en_lpuart_mmdorck_t enMmdorCk; ///<校验模式
  197. en_lpuart_stop_t enStopBit; ///<停止位长度
  198. stc_lpuart_baud_t stcBaud; ///<Mode1/3波特率配置
  199. } stc_lpuart_cfg_t;
  200. // 总初始化处理
  201. en_result_t LPUart_Init(M0P_LPUART_TypeDef* LPUARTx, stc_lpuart_cfg_t* pstcCfg);
  202. //TB8数据设置
  203. void LPUart_SetTb8(M0P_LPUART_TypeDef* LPUARTx, boolean_t bTB8Value);
  204. //数据寄存器bit8位获取
  205. boolean_t LPUart_GetRb8(M0P_LPUART_TypeDef* LPUARTx);
  206. //中断相关设置函数使能和禁止
  207. en_result_t LPUart_EnableIrq(M0P_LPUART_TypeDef* LPUARTx, en_lpuart_irq_sel_t enIrqSel);
  208. en_result_t LPUart_DisableIrq(M0P_LPUART_TypeDef* LPUARTx, en_lpuart_irq_sel_t enIrqSel);
  209. //特殊功能使能和禁止
  210. en_result_t LPUart_EnableFunc(M0P_LPUART_TypeDef* LPUARTx, en_lpuart_func_t enFunc);
  211. en_result_t LPUart_DisableFunc(M0P_LPUART_TypeDef* LPUARTx, en_lpuart_func_t enFunc);
  212. //状态位获取函数
  213. boolean_t LPUart_GetStatus(M0P_LPUART_TypeDef* LPUARTx,en_lpuart_status_t enStatus);
  214. //状态位的清除
  215. en_result_t LPUart_ClrStatus(M0P_LPUART_TypeDef* LPUARTx,en_lpuart_status_t enStatus);
  216. //整个状态寄存器获取
  217. uint8_t LPUart_GetIsr(M0P_LPUART_TypeDef* LPUARTx);
  218. //整个状态寄存器清除
  219. en_result_t LPUart_ClrIsr(M0P_LPUART_TypeDef* LPUARTx);
  220. //数据查询方式的发送
  221. en_result_t LPUart_SendData(M0P_LPUART_TypeDef* LPUARTx, uint8_t u8Data);
  222. en_result_t LPUart_SendDataTimeOut(M0P_LPUART_TypeDef* LPUARTx, uint8_t u8Data, uint32_t u32TimeOut);
  223. //数据中断方式的发送
  224. en_result_t LPUart_SendDataIt(M0P_LPUART_TypeDef* LPUARTx, uint8_t u8Data);
  225. //数据接收
  226. uint8_t LPUart_ReceiveData(M0P_LPUART_TypeDef* LPUARTx);
  227. //LPUARTx通道号,enClk 时钟源选项
  228. en_result_t LPUart_SelSclk(M0P_LPUART_TypeDef* LPUARTx, en_lpuart_sclksel_t enSclk);
  229. //LPUART通道多主机模式配置
  230. en_result_t LPUart_SetMultiMode(M0P_LPUART_TypeDef* LPUARTx, stc_lpuart_multimode_t* pstcMultiCfg);
  231. //LPUART通道多主机模式从机地址配置函数
  232. en_result_t LPUart_SetSaddr(M0P_LPUART_TypeDef* LPUARTx,uint8_t u8Addr);
  233. //@} // LPUartGroup
  234. #ifdef __cplusplus
  235. #endif
  236. #endif /* __UART_H__ */
  237. /******************************************************************************
  238. * EOF (not truncated)
  239. *****************************************************************************/