Motion_EC_HC32_archived/Libraries/HC32L073_StdPeriph_Driver/inc/uart.h

275 lines
11 KiB
C
Raw Normal View History

2022-01-10 16:20:39 +08:00
/*
* @Description:
* @Date: 2022-01-06 16:25:34
* @LastEditors: CK.Zh
* @LastEditTime: 2022-01-07 12:28:05
* @FilePath: /rt-thread/bsp/hc32l073/Libraries/HC32L073_StdPeriph_Driver/inc/uart.h
*/
/******************************************************************************
* Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/*****************************************************************************/
/** \file uart.h
**
** Headerfile for UART functions
**
**
** History:
** - 2017-05-10 Cathy First Version
**
*****************************************************************************/
#ifndef __UART_H__
#define __UART_H__
/*****************************************************************************
* Include files
*****************************************************************************/
#include "ddl.h"
#include "hc32l07x.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
******************************************************************************
** \defgroup UartGroup Universal Asynchronous Receiver/Transmitter (UART)
**
******************************************************************************/
//@{
/******************************************************************************/
/* Global pre-processor symbols/macros ('#define') */
/******************************************************************************/
/******************************************************************************
* Global type definitions
******************************************************************************/
/**
******************************************************************************
**\brief uart
******************************************************************************/
/**
******************************************************************************
** \brief uart/
******************************************************************************/
typedef enum en_uart_mmdorck
{
UartMskDataOrAddr = 0u, ///<多机模式时通过读写SBUF[8]决定帧为数据帧或地址帧
UartMskEven = 0x4u, ///<非多机模式偶校验
UartMskOdd = 0x8u, ///<非多机模式奇校验
}en_uart_mmdorck_t;
/**
******************************************************************************
** \brief uart
******************************************************************************/
typedef struct stc_uart_multimode
{
uint8_t u8SlaveAddr; ///<从机地址
uint8_t u8SaddEn; ///<从及地址掩码
}stc_uart_multimode_t;
/**
******************************************************************************
** \brief uart
******************************************************************************/
typedef enum en_uart_mode
{
UartMskMode0 = 0x00u, ///<模式0
UartMskMode1 = 0x40u, ///<模式1
UartMskMode2 = 0x80u, ///<模式2
UartMskMode3 = 0xc0u, ///<模式3
} en_uart_mode_t;
/**
******************************************************************************
** \brief uart stop
******************************************************************************/
typedef enum en_uart_stop
{
UartMsk1bit = 0x0000u, ///<1位停止位
UartMsk1_5bit = 0x4000u, ///<1.5位停止位
UartMsk2bit = 0x8000u, ///<2位停止位
} en_uart_stop_t;
/**
******************************************************************************
** \brief uart 使
******************************************************************************/
typedef enum en_uart_func
{
UartRenFunc = 4u, ///<0-TX; ///<1-非mode0模式代表RX&TX ,mode0模式代表RX;
UartDmaRxFunc = 16u, ///<DMA接收功能
UartDmaTxFunc = 17u, ///<DMA发送功能
UartRtsFunc = 18u, ///<硬件流RTS功能
UartCtsFunc = 19u, ///<硬件流CTS功能
UartHdFunc = 22u, ///<单线半双工功能
}en_uart_func_t;
/**
******************************************************************************
** \brief uart使
******************************************************************************/
typedef enum en_uart_irq_sel
{
UartRxIrq = 0u, ///<接收中断使能
UartTxIrq = 1u, ///<发送中断使能
UartTxEIrq = 8u, ///<TX空中断使能
UartPEIrq = 13u, ///<奇偶校验中断使能
UartCtsIrq = 20u, ///<CTS信号翻转中断使能
UartFEIrq = 21u, ///<帧错误中断使能
}en_uart_irq_sel_t;
/**
******************************************************************************
** \brief uart
******************************************************************************/
typedef struct stc_uart_irq_cb
{
func_ptr_t pfnTxIrqCb; ///<发送中断服务函数
func_ptr_t pfnRxFEIrqCb; ///<接收帧错误中断服务函数
func_ptr_t pfnRxIrqCb; ///<接收中断服务函数
func_ptr_t pfnCtsIrqCb; ///<CTS信号翻转中断服务函数
func_ptr_t pfnPEIrqCb; ///<奇偶校验错误中断服务函数
}stc_uart_irq_cb_t;
/**
******************************************************************************
** \brief uart
******************************************************************************/
typedef enum en_uart_status
{
UartRC = 0u, ///<接收数据完成标记
UartTC = 1u, ///<发送数据完成标记
UartFE = 2u, ///<帧错误标记
UartTxe = 3u, ///<TXbuff空标记
UartPE = 4u, ///<奇偶校验错误标记
UartCtsIf = 5u, ///<CTS中断标记
UartCts = 6u, ///<CTS信号标记
}en_uart_status_t;
/**
******************************************************************************
** \brief uart
******************************************************************************/
typedef enum en_uart_clkdiv
{
UartMsk16Or32Div = 0u, ///<模式0无效模式1/3为16分频模式2为32分频
UartMsk8Or16Div = 0x200u, ///<模式0无效模式1/3为8分频模式2为16分频
}en_uart_clkdiv_t;
/**
******************************************************************************
** \brief uart Mode1Mode3
******************************************************************************/
typedef struct stc_uart_baud
{
en_uart_clkdiv_t enClkDiv; ///<采样分频
uint32_t u32Pclk; ///<pclk
uint32_t u32Baud; ///<波特率
} stc_uart_baud_t;
/**
******************************************************************************
** \uart
******************************************************************************/
typedef struct stc_uart_cfg
{
en_uart_mode_t enRunMode; ///<四种模式配置
en_uart_mmdorck_t enMmdorCk; ///<校验模式
en_uart_stop_t enStopBit; ///<停止位长度
stc_uart_baud_t stcBaud; ///<Mode1/3波特率配置
stc_uart_multimode_t* pstcMultiMode; ///<多主机模式配置
stc_uart_irq_cb_t* pstcIrqCb; ///<中断服务函数
boolean_t bTouchNvic; ///<NVIC中断使能
} stc_uart_cfg_t;
//UART初始化
en_result_t Uart_Init(M0P_UART_TypeDef* UARTx, stc_uart_cfg_t* pstcCfg);
///< UART 单线模式使能/禁止
void Uart_HdModeEnable(M0P_UART_TypeDef* UARTx);
void Uart_HdModeDisable(M0P_UART_TypeDef* UARTx);
//UART模块多机模式设置函数
en_result_t Uart_SetMultiMode(M0P_UART_TypeDef* UARTx,stc_uart_multimode_t* pstcMultiCfg);
//TB8数据设置
void Uart_SetTb8(M0P_UART_TypeDef* UARTx, boolean_t bTB8Value);
//RB8数据获取
boolean_t Uart_GetRb8(M0P_UART_TypeDef* UARTx);
//中断相关设置函数
en_result_t Uart_EnableIrq(M0P_UART_TypeDef* UARTx, en_uart_irq_sel_t enIrqSel);
en_result_t Uart_DisableIrq(M0P_UART_TypeDef* UARTx, en_uart_irq_sel_t enIrqSel);
//功能使能和禁止
en_result_t Uart_EnableFunc(M0P_UART_TypeDef* UARTx, en_uart_func_t enFunc);
en_result_t Uart_DisableFunc(M0P_UART_TypeDef* UARTx, en_uart_func_t enFunc);
//状态位的获取和清除
uint8_t Uart_GetIsr(M0P_UART_TypeDef* UARTx);
boolean_t Uart_GetStatus(M0P_UART_TypeDef* UARTx,en_uart_status_t enStatus);
en_result_t Uart_ClrIsr(M0P_UART_TypeDef* UARTx);
en_result_t Uart_ClrStatus(M0P_UART_TypeDef* UARTx,en_uart_status_t enStatus);
//数据收发操作
///< 数据查询模式发送
en_result_t Uart_SendDataPoll(M0P_UART_TypeDef* UARTx, uint8_t u8Data);
///< 数据中断模式发送
en_result_t Uart_SendDataIt(M0P_UART_TypeDef* UARTx, uint8_t u8Data);
uint8_t Uart_ReceiveData(M0P_UART_TypeDef* UARTx);
//@} // UartGroup
#ifdef __cplusplus
#endif
#endif /* __UART_H__ */
/******************************************************************************
* EOF (not truncated)
*****************************************************************************/