mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-22 17:12:55 +08:00
150 lines
3.3 KiB
C
150 lines
3.3 KiB
C
|
/********************************** (C) COPYRIGHT *******************************
|
|||
|
* File Name : CH58x_uart0.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 UART0_DefInit
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϳ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param none
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void UART0_DefInit(void)
|
|||
|
{
|
|||
|
UART0_BaudRateCfg(115200);
|
|||
|
R8_UART0_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_UART0_LCR = RB_LCR_WORD_SZ;
|
|||
|
R8_UART0_IER = RB_IER_TXD_EN;
|
|||
|
R8_UART0_DIV = 1;
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn UART0_BaudRateCfg
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param baudrate - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void UART0_BaudRateCfg(uint32_t baudrate)
|
|||
|
{
|
|||
|
uint32_t x;
|
|||
|
|
|||
|
x = 10 * GetSysClock() / 8 / baudrate;
|
|||
|
x = (x + 5) / 10;
|
|||
|
R16_UART0_DL = (uint16_t)x;
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn UART0_ByteTrigCfg
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param b - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD> refer to UARTByteTRIGTypeDef
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void UART0_ByteTrigCfg(UARTByteTRIGTypeDef b)
|
|||
|
{
|
|||
|
R8_UART0_FCR = (R8_UART0_FCR & ~RB_FCR_FIFO_TRIG) | (b << 6);
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn UART0_INTCfg
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param s - <EFBFBD>жϿ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ж<EFBFBD>
|
|||
|
* @param i - <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_IER_MODEM_CHG - <EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>仯<EFBFBD>ж<EFBFBD>ʹ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> UART0 ֧<EFBFBD>֣<EFBFBD>
|
|||
|
* RB_IER_LINE_STAT - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·״̬<EFBFBD>ж<EFBFBD>
|
|||
|
* RB_IER_THR_EMPTY - <EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><EFBFBD>ּĴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
* RB_IER_RECV_RDY - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void UART0_INTCfg(FunctionalState s, uint8_t i)
|
|||
|
{
|
|||
|
if(s)
|
|||
|
{
|
|||
|
R8_UART0_IER |= i;
|
|||
|
R8_UART0_MCR |= RB_MCR_INT_OE;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
R8_UART0_IER &= ~i;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn UART0_Reset
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
|
|||
|
*
|
|||
|
* @param none
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void UART0_Reset(void)
|
|||
|
{
|
|||
|
R8_UART0_IER = RB_IER_RESET;
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn UART0_SendString
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD>ֽڷ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param buf - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|||
|
* @param l - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void UART0_SendString(uint8_t *buf, uint16_t l)
|
|||
|
{
|
|||
|
uint16_t len = l;
|
|||
|
|
|||
|
while(len)
|
|||
|
{
|
|||
|
if(R8_UART0_TFC != UART_FIFO_SIZE)
|
|||
|
{
|
|||
|
R8_UART0_THR = *buf++;
|
|||
|
len--;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn UART0_RecvString
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|||
|
*
|
|||
|
* @param buf - <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|||
|
*
|
|||
|
* @return <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
uint16_t UART0_RecvString(uint8_t *buf)
|
|||
|
{
|
|||
|
uint16_t len = 0;
|
|||
|
|
|||
|
while(R8_UART0_RFC)
|
|||
|
{
|
|||
|
*buf++ = R8_UART0_RBR;
|
|||
|
len++;
|
|||
|
}
|
|||
|
|
|||
|
return (len);
|
|||
|
}
|
|||
|
|