2022-03-26 20:51:40 +08:00

149 lines
3.3 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 : CH58x_uart1.c
* Author : WCH
* Version : V1.2
* Date : 2021/11/17
* Description
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH58x_common.h"
/*********************************************************************
* @fn UART1_DefInit
*
* @brief <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϳ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param none
*
* @return none
*/
void UART1_DefInit(void)
{
UART1_BaudRateCfg(115200);
R8_UART1_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO<46>򿪣<EFBFBD><F2BFAAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD>ֽ<EFBFBD>
R8_UART1_LCR = RB_LCR_WORD_SZ;
R8_UART1_IER = RB_IER_TXD_EN;
R8_UART1_DIV = 1;
}
/*********************************************************************
* @fn UART1_BaudRateCfg
*
* @brief <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param baudrate - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return none
*/
void UART1_BaudRateCfg(uint32_t baudrate)
{
uint32_t x;
x = 10 * GetSysClock() / 8 / baudrate;
x = (x + 5) / 10;
R16_UART1_DL = (uint16_t)x;
}
/*********************************************************************
* @fn UART1_ByteTrigCfg
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD>ֽڴ<D6BD><DAB4><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
*
* @param b - <20><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD> refer to UARTByteTRIGTypeDef
*
* @return none
*/
void UART1_ByteTrigCfg(UARTByteTRIGTypeDef b)
{
R8_UART1_FCR = (R8_UART1_FCR & ~RB_FCR_FIFO_TRIG) | (b << 6);
}
/*********************************************************************
* @fn UART1_INTCfg
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
*
* @param s - <20>жϿ<D0B6><CFBF><EFBFBD>״̬<D7B4><CCAC><EFBFBD>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>Ӧ<EFBFBD>ж<EFBFBD>
* @param i - <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>
* RB_IER_MODEM_CHG - <20><><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><EFBFBD>ж<EFBFBD>ʹ<EFBFBD><CAB9>λ<EFBFBD><CEBB><EFBFBD><EFBFBD> UART0 ֧<>֣<EFBFBD>
* RB_IER_LINE_STAT - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·״̬<D7B4>ж<EFBFBD>
* RB_IER_THR_EMPTY - <20><><EFBFBD>ͱ<EFBFBD><CDB1>ּĴ<D6BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
* RB_IER_RECV_RDY - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
*
* @return none
*/
void UART1_INTCfg(FunctionalState s, uint8_t i)
{
if(s)
{
R8_UART1_IER |= i;
R8_UART1_MCR |= RB_MCR_INT_OE;
}
else
{
R8_UART1_IER &= ~i;
}
}
/*********************************************************************
* @fn UART1_Reset
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
*
* @param none
*
* @return none
*/
void UART1_Reset(void)
{
R8_UART1_IER = RB_IER_RESET;
}
/*********************************************************************
* @fn UART1_SendString
*
* @brief <20><><EFBFBD>ڶ<EFBFBD><DAB6>ֽڷ<D6BD><DAB7><EFBFBD>
*
* @param buf - <20><><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
* @param l - <20><><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
*
* @return none
*/
void UART1_SendString(uint8_t *buf, uint16_t l)
{
uint16_t len = l;
while(len)
{
if(R8_UART1_TFC != UART_FIFO_SIZE)
{
R8_UART1_THR = *buf++;
len--;
}
}
}
/*********************************************************************
* @fn UART1_RecvString
*
* @brief <20><><EFBFBD>ڶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ֽ<EFBFBD>
*
* @param buf - <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݴ<EFBFBD><DDB4>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
*
* @return <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
*/
uint16_t UART1_RecvString(uint8_t *buf)
{
uint16_t len = 0;
while(R8_UART1_RFC)
{
*buf++ = R8_UART1_RBR;
len++;
}
return (len);
}