CH592_Makefile_Template/sdk/StdPeriphDriver/CH59x_uart3.c
2023-12-21 22:44:09 +07:00

152 lines
3.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************** (C) COPYRIGHT *******************************
* File Name : CH59x_uart3.c
* Author : WCH
* Version : V1.2
* Date : 2021/11/17
* Description
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "CH59x_common.h"
/*********************************************************************
* @fn UART3_DefInit
*
* @brief 串口默认初始化配置
*
* @param none
*
* @return none
*/
void UART3_DefInit(void)
{
UART3_BaudRateCfg(115200);
R8_UART3_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO打开触发点4字节
R8_UART3_LCR = RB_LCR_WORD_SZ;
R8_UART3_IER = RB_IER_TXD_EN;
R8_UART3_DIV = 1;
}
/*********************************************************************
* @fn UART3_BaudRateCfg
*
* @brief 串口波特率配置
*
* @param baudrate - 波特率
*
* @return none
*/
void UART3_BaudRateCfg(uint32_t baudrate)
{
uint32_t x;
x = 10 * GetSysClock() / 8 / baudrate;
x = (x + 5) / 10;
R16_UART3_DL = (uint16_t)x;
}
/*********************************************************************
* @fn UART3_ByteTrigCfg
*
* @brief 串口字节触发中断配置
*
* @param b - 触发字节数 refer to UARTByteTRIGTypeDef
*
* @return none
*/
void UART3_ByteTrigCfg(UARTByteTRIGTypeDef b)
{
R8_UART3_FCR = (R8_UART3_FCR & ~RB_FCR_FIFO_TRIG) | (b << 6);
}
/*********************************************************************
* @fn UART3_INTCfg
*
* @brief 串口中断配置
*
* @param s - 中断控制状态,是否使能相应中断
* @param i - 中断类型
* RB_IER_MODEM_CHG - 调制解调器输入状态变化中断使能位(仅 UART0 支持)
* RB_IER_LINE_STAT - 接收线路状态中断
* RB_IER_THR_EMPTY - 发送保持寄存器空中断
* RB_IER_RECV_RDY - 接收数据中断
*
* @return none
*/
void UART3_INTCfg(FunctionalState s, uint8_t i)
{
if(s)
{
R8_UART3_IER |= i;
R8_UART3_MCR |= RB_MCR_INT_OE;
}
else
{
R8_UART3_IER &= ~i;
}
}
/*********************************************************************
* @fn UART3_Reset
*
* @brief 串口软件复位
*
* @param none
*
* @return none
*/
void UART3_Reset(void)
{
R8_UART3_IER = RB_IER_RESET;
}
/*********************************************************************
* @fn UART3_SendString
*
* @brief 串口多字节发送
*
* @param buf - 待发送的数据内容首地址
* @param l - 待发送的数据长度
*
* @return none
*/
void UART3_SendString(uint8_t *buf, uint16_t l)
{
uint16_t len = l;
while(len)
{
if(R8_UART3_TFC != UART_FIFO_SIZE)
{
R8_UART3_THR = *buf++;
len--;
}
}
}
/*********************************************************************
* @fn UART3_RecvString
*
* @brief 串口读取多字节
*
* @param buf - 读取数据存放缓存区首地址
*
* @return 读取数据长度
*/
uint16_t UART3_RecvString(uint8_t *buf)
{
uint16_t len = 0;
while(R8_UART3_RFC)
{
*buf++ = R8_UART3_RBR;
len++;
}
return (len);
}