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