#include "Drv_io.h"
#include "hk32f0xx.h"
#include "Drv_TimeBase.h"
static u8 TxdFlag = 0;
#define RXD_BUFF_LEN_MAX 100
#define TXD_BUFF_LEN_MAX 300
static u16 Rxd1Length, Txd1Length;
static u8 Rxd1Buff[RXD_BUFF_LEN_MAX], Txd1Buff[TXD_BUFF_LEN_MAX];
#define TXD_RCU UART1_TXD_RCU
#define TXD_PORT UART1_TXD_PORT
#define TXD_PIN UART1_TXD_PIN
#define TXD_READ UART1_TXD_READ
#define TXD_LOW UART1_TXD_LOW
#define TXD_HIG UART1_TXD_HIG
#define RXD_RCU UART1_RXD_RCU
#define RXD_PORT UART1_RXD_PORT
#define RXD_PIN UART1_RXD_PIN
#define RXD_READ UART1_RXD_READ
#define RXD_LOW UART1_RXD_LOW
#define RXD_HIG UART1_RXD_HIG
static void drv_InitUart1(u32 baud)
{
GPIO_InitTypeDef GpioInitStruct;
USART_InitTypeDef USART_InitStructure;
//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
GpioInitStruct.GPIO_Pin = TXD_PIN | RXD_PIN;
GpioInitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GpioInitStruct.GPIO_Mode = GPIO_Mode_AF;
GpioInitStruct.GPIO_OType = GPIO_OType_PP;
GpioInitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(RXD_PORT, &GpioInitStruct);
USART_DeInit(USART1);
//USART_MSBFirstCmd(USART1, ENABLE);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //使能接收中断=
USART_ITConfig(USART1,USART_IT_ORE,ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
/* USART1 IRQ Channel configuration */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);//使能串口 1
}
void Interrup_uart1_rxd(void)
{
u8 Buff = USART1->RDR;
if(Rxd1Length < RXD_BUFF_LEN_MAX)
{
Rxd1Buff[Rxd1Length++] = Buff;
}
else
{
Rxd1Length = 0;
}
}
void Interrup_uart1_txd(void)
{
static u16 i = 0;
if(i < Txd1Length && Txd1Length < TXD_BUFF_LEN_MAX)
{
TxdFlag = 1;
USART1->TDR = Txd1Buff[i++];
}
else
{
i = 0;
TxdFlag = 0;
Txd1Length = 0;
USART_ITConfig(USART1,USART_IT_TXE ,DISABLE);
}
}
void drv_uart1_get_buff(u32 baud, u8 **TxdBuf, u8 **RxdBuf, u16 **TxdLen, u16 **RxdLen)
{
drv_InitUart1(baud);
*RxdLen = &Rxd1Length;
*TxdLen = &Txd1Length;
*RxdBuf = &Rxd1Buff[0];
*TxdBuf = &Txd1Buff[0];
}
void drv_uart1(void)
{
static u8 cnt = 0;
if(cnt == 0)
{
cnt++;
}
else if(TxdFlag == 0 && Txd1Length > 0)
{
USART_ITConfig(USART1, USART_IT_TXE ,ENABLE);
}
}
SYM32 接收 发送中断的调用,一个一个字节收RXNE或一个一个字节的发送-标志位TXE 。
于 2022-12-30 17:45:59 首次发布