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 
53 #ifndef __UART_H__
54 #define __UART_H__
55 /*****************************************************************************
56  * Include files
57  *****************************************************************************/
58 #include "hc32l196_ddl.h"
59 
60 
61 #ifdef __cplusplus
62 extern "C"
63 {
64 #endif
65 
66 /**
67  ******************************************************************************
68  ** \defgroup UartGroup Universal Asynchronous Receiver/Transmitter (UART)
69  **
70  ******************************************************************************/
71 //@{
72 
73 /******************************************************************************/
74 /* Global pre-processor symbols/macros ('#define')                            */
75 /******************************************************************************/
76 
77 /******************************************************************************
78  * Global type definitions
79  ******************************************************************************/
80  /**
81  ******************************************************************************
82  **\brief uart通道选择
83  ******************************************************************************/
84 
85 /**
86  ******************************************************************************
87  ** \brief uart多机模式地址帧/数据帧或者奇偶校验
88  ******************************************************************************/
89 typedef enum en_uart_mmdorck
90 {
91     UartMskDataOrAddr = 0u,     ///<多机模式时,通过读写SBUF[8]决定帧为数据帧或地址帧
92     UartMskEven       = 0x4u,   ///<非多机模式偶校验
93     UartMskOdd        = 0x8u,   ///<非多机模式奇校验
94 }en_uart_mmdorck_t;
95 
96 /**
97  ******************************************************************************
98  ** \brief uart多机模式及从机地址和地址掩码配置
99  ******************************************************************************/
100 typedef struct stc_uart_multimode
101 {
102     uint8_t             u8SlaveAddr;    ///<从机地址
103     uint8_t             u8SaddEn;       ///<从及地址掩码
104 }stc_uart_multimode_t;
105 
106 /**
107  ******************************************************************************
108  ** \brief uart 四种工作模式选择
109  ******************************************************************************/
110 
111 typedef enum en_uart_mode
112 {
113     UartMskMode0 = 0x00u, ///<模式0
114     UartMskMode1 = 0x40u, ///<模式1
115     UartMskMode2 = 0x80u, ///<模式2
116     UartMskMode3 = 0xc0u, ///<模式3
117 } en_uart_mode_t;
118 /**
119  ******************************************************************************
120  ** \brief uart stop长度选择
121  ******************************************************************************/
122 
123 typedef enum en_uart_stop
124 {
125     UartMsk1bit   = 0x0000u, ///<1位停止位
126     UartMsk1_5bit = 0x4000u, ///<1.5位停止位
127     UartMsk2bit   = 0x8000u, ///<2位停止位
128 } en_uart_stop_t;
129 /**
130  ******************************************************************************
131  ** \brief uart 功能使能
132  ******************************************************************************/
133 typedef enum en_uart_func
134 {
135     UartRenFunc    = 4u,    ///<0-TX; ///<1-非mode0模式代表RX&TX ,mode0模式代表RX;
136     UartDmaRxFunc  = 16u,   ///<DMA接收功能
137     UartDmaTxFunc  = 17u,   ///<DMA发送功能
138     UartRtsFunc    = 18u,   ///<硬件流RTS功能
139     UartCtsFunc    = 19u,   ///<硬件流CTS功能
140     UartHdFunc     = 22u,   ///<单线半双工功能
141 }en_uart_func_t;
142 /**
143  ******************************************************************************
144  ** \brief uart中断使能控制
145  ******************************************************************************/
146 typedef enum en_uart_irq_sel
147 {
148     UartRxIrq  = 0u,    ///<接收中断使能
149     UartTxIrq  = 1u,    ///<发送中断使能
150     UartTxEIrq = 8u,    ///<TX空中断使能
151     UartPEIrq  = 13u,   ///<奇偶校验中断使能
152     UartCtsIrq = 20u,   ///<CTS信号翻转中断使能
153     UartFEIrq  = 21u,   ///<帧错误中断使能
154 }en_uart_irq_sel_t;
155 
156 /**
157  ******************************************************************************
158  ** \brief uart 状态标志位
159  ******************************************************************************/
160 typedef enum en_uart_status
161 {
162     UartRC    = 0u,  ///<接收数据完成标记
163     UartTC    = 1u,  ///<发送数据完成标记
164     UartFE    = 2u,  ///<帧错误标记
165     UartTxe   = 3u,  ///<TXbuff空标记
166     UartPE    = 4u,  ///<奇偶校验错误标记
167     UartCtsIf = 5u,  ///<CTS中断标记
168     UartCts   = 6u,  ///<CTS信号标记
169 }en_uart_status_t;
170 
171 /**
172  ******************************************************************************
173  ** \brief uart 通道采样分频配置
174  ******************************************************************************/
175 typedef enum en_uart_clkdiv
176 {
177     UartMsk16Or32Div = 0u,      ///<模式0无效,模式1/3为16分频,模式2为32分频
178     UartMsk8Or16Div  = 0x200u,  ///<模式0无效,模式1/3为8分频,模式2为16分频
179 }en_uart_clkdiv_t;
180 /**
181  ******************************************************************************
182  ** \brief uart 通道Mode1和Mode3波特率计算参数
183  ******************************************************************************/
184 typedef struct stc_uart_baud
185 {
186     en_uart_clkdiv_t     enClkDiv;      ///<采样分频
187     uint32_t             u32Pclk;       ///<pclk
188     uint32_t             u32Baud;       ///<波特率
189 } stc_uart_baud_t;
190 /**
191  ******************************************************************************
192  ** \uart 总体配置
193  ******************************************************************************/
194 
195 typedef struct stc_uart_cfg
196 {
197     en_uart_mode_t       enRunMode;     ///<四种模式配置
198     en_uart_mmdorck_t    enMmdorCk;     ///<校验模式
199     en_uart_stop_t       enStopBit;     ///<停止位长度
200     stc_uart_baud_t      stcBaud;       ///<Mode1/3波特率配置
201 } stc_uart_cfg_t;
202 
203 //UART初始化
204 en_result_t Uart_Init(M0P_UART_TypeDef* UARTx, stc_uart_cfg_t* pstcCfg);
205 
206 ///< UART 单线模式使能/禁止
207 void Uart_HdModeEnable(M0P_UART_TypeDef* UARTx);
208 void Uart_HdModeDisable(M0P_UART_TypeDef* UARTx);
209 
210 //UART模块多机模式设置函数
211 en_result_t Uart_SetMultiMode(M0P_UART_TypeDef* UARTx,stc_uart_multimode_t* pstcMultiCfg);
212 
213 //TB8数据设置
214 void Uart_SetTb8(M0P_UART_TypeDef* UARTx, boolean_t bTB8Value);
215 //RB8数据获取
216 boolean_t Uart_GetRb8(M0P_UART_TypeDef* UARTx);
217 
218 //中断相关设置函数
219 en_result_t Uart_EnableIrq(M0P_UART_TypeDef* UARTx, en_uart_irq_sel_t enIrqSel);
220 en_result_t Uart_DisableIrq(M0P_UART_TypeDef* UARTx, en_uart_irq_sel_t enIrqSel);
221 
222 //功能使能和禁止
223 en_result_t Uart_EnableFunc(M0P_UART_TypeDef* UARTx, en_uart_func_t enFunc);
224 en_result_t Uart_DisableFunc(M0P_UART_TypeDef* UARTx, en_uart_func_t enFunc);
225 
226 //状态位的获取和清除
227 uint8_t Uart_GetIsr(M0P_UART_TypeDef* UARTx);
228 boolean_t Uart_GetStatus(M0P_UART_TypeDef* UARTx,en_uart_status_t enStatus);
229 en_result_t Uart_ClrIsr(M0P_UART_TypeDef* UARTx);
230 en_result_t Uart_ClrStatus(M0P_UART_TypeDef* UARTx,en_uart_status_t enStatus);
231 
232 //数据收发操作
233 ///< 数据查询模式发送
234 en_result_t Uart_SendDataPoll(M0P_UART_TypeDef* UARTx, uint8_t u8Data);
235 ///< 数据中断模式发送
236 en_result_t Uart_SendDataIt(M0P_UART_TypeDef* UARTx, uint8_t u8Data);
237 uint8_t Uart_ReceiveData(M0P_UART_TypeDef* UARTx);
238 
239 //@} // UartGroup
240 
241 #ifdef __cplusplus
242 #endif
243 
244 #endif /* __UART_H__ */
245 /******************************************************************************
246  * EOF (not truncated)
247  *****************************************************************************/
248 
249 
250 
251