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