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

150 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_uart2.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 UART2_DefInit
*
* @brief <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϳ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param none
*
* @return none
*/
void UART2_DefInit(void)
{
UART2_BaudRateCfg(115200);
R8_UART2_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_UART2_LCR = RB_LCR_WORD_SZ;
R8_UART2_IER = RB_IER_TXD_EN;
R8_UART2_DIV = 1;
}
/*********************************************************************
* @fn UART2_BaudRateCfg
*
* @brief <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param baudrate - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return none
*/
void UART2_BaudRateCfg(uint32_t baudrate)
{
uint32_t x;
x = 10 * GetSysClock() / 8 / baudrate;
x = (x + 5) / 10;
R16_UART2_DL = (uint16_t)x;
}
/*********************************************************************
* @fn UART2_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 UART2_ByteTrigCfg(UARTByteTRIGTypeDef b)
{
R8_UART2_FCR = (R8_UART2_FCR & ~RB_FCR_FIFO_TRIG) | (b << 6);
}
/*********************************************************************
* @fn UART2_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 UART2_INTCfg(FunctionalState s, uint8_t i)
{
if(s)
{
R8_UART2_IER |= i;
R8_UART2_MCR |= RB_MCR_INT_OE;
}
else
{
R8_UART2_IER &= ~i;
}
}
/*********************************************************************
* @fn UART2_Reset
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
*
* @param none
*
* @return none
*/
void UART2_Reset(void)
{
R8_UART2_IER = RB_IER_RESET;
}
/*********************************************************************
* @fn UART2_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 UART2_SendString(uint8_t *buf, uint16_t l)
{
uint16_t len = l;
while(len)
{
if(R8_UART2_TFC != UART_FIFO_SIZE)
{
R8_UART2_THR = *buf++;
len--;
}
}
}
/*********************************************************************
* @fn UART2_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 UART2_RecvString(uint8_t *buf)
{
uint16_t len = 0;
while(R8_UART2_RFC)
{
*buf++ = R8_UART2_RBR;
len++;
}
return (len);
}