我是采用DMA空闲中断来接收不定长的数据长度,仅设置了DMA串口接收,没有DMA发送设置。
#include "usart.h"
#include "gd32f450i_eval.h"
#include "dataprocess_app.h"
#include "stdio.h"
#include "string.h"
/*
* 结构体实例初始化
*/
//T_COMOptr COM0 ={
// .isReFinish = FALSE,
// .renum = 0,
// .ComInit = Usart0_Init,
// .SendByte = Usart0_Send,
// .SendBuf = Usart0_Sendbuf,
//};
T_COMOptr COM1 ={
.isReFinish = FALSE,
.renum = 0,
.ComInit = Usart1_Init,
.SendByte = Usart1_Send,
.SendBuf = Usart1_Sendbuf,
};
T_COMOptr COM2 ={
.isReFinish = FALSE,
.renum = 0,
.ComInit = Usart2_Init,
.SendByte = Usart2_Send,
.SendBuf = Usart2_Sendbuf,
};
T_COMOptr COM3 ={
.isReFinish = FALSE,
.renum = 0,
.ComInit = Uart3_Init,
.SendByte = Uart3_Send,
.SendBuf = Uart3_Sendbuf,
};
T_COMOptr COM5 ={
.isReFinish = FALSE,
.renum = 0,
.ComInit = Usart5_Init,
.SendByte = Usart5_Send,
.SendBuf = Usart5_Sendbuf,
};
T_COMOptr COM7 ={
.isReFinish = FALSE,
.renum = 0,
.ComInit = Uart7_Init,
.SendByte = Uart7_Send,
.SendBuf = Uart7_Sendbuf,
};
#if 1
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(UART3, (uint8_t)ch);
while(RESET == usart_flag_get(UART3, USART_FLAG_TBE));
return ch;
}
#endif
/*
* 支持printf函数,而不需要选择use MicroLIB
*/
#if 0
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
/*
* 名称: _sys_exit函数
* 功能: 定义_sys_exit()以避免使用半主机模式
* 返回: 无
*/
void _sys_exit(int x)
{
x = x;
}
/*
* 名称: fputc函数
* 功能: 重定义fputc函数
* 返回: ch
*/
int fputc(int ch, FILE *f)
{
while(RESET == usart_flag_get(UART7, USART_FLAG_TBE));//循环发送,直到发送完毕
usart_data_transmit(UART7, (uint8_t) ch);
return ch;
}
#endif
/*
* 名称: Usart0_Send函数
* 功能: 串口0发送
* 返回: 无
*/
static void Usart0_Send(u8 dat)
{
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE));
usart_data_transmit(USART0, (uint8_t) dat);
}
/*
* 名称: Usart0_Sendbuf函数
* 功能: 串口0发送
* 返回: 无
*/
static void Usart0_Sendbuf(u8 *pdata,u8 num)
{
u8 i;
for(i = 0;i < num;i++){
Usart0_Send(*(pdata+i));
}
}
/*
* 名称: Usart1_Send函数
* 功能: 串口1发送
* 返回: 无
*/
static void Usart1_Send(u8 dat)
{
while (RESET == usart_flag_get(USART1, USART_FLAG_TBE));
usart_data_transmit(USART1, (uint8_t) dat);
}
/*
* 名称: Usart1_Sendbuf函数
* 功能: 串口1发送
* 返回: 无
*/
static void Usart1_Sendbuf(u8 *pdata,u8 num)
{
u8 i;
for(i = 0;i < num;i++){
Usart1_Send(*(pdata+i));
}
}
/*
* 名称: Usart2_Send函数
* 功能: 串口2发送
* 返回: 无
*/
static void Usart2_Send(u8 dat)
{
while (RESET == usart_flag_get(USART2, USART_FLAG_TBE));
usart_data_transmit(USART2, (uint8_t) dat);
}
/*
* 名称: Usart2_Sendbuf函数
* 功能: 串口2发送
* 返回: 无
*/
static void Usart2_Sendbuf(u8 *pdata,u8 num)
{
u8 i;
for(i = 0;i < num;i++){
Usart2_Send(*(pdata+i));
}
}
/*
* 名称: Uart3_Send函数
* 功能: 串口3发送
* 返回: 无
*/
static void Uart3_Send(u8 dat)
{
while (RESET == usart_flag_get(UART3, USART_FLAG_TBE));
usart_data_transmit(UART3, (uint8_t) dat);
}
/*
* 名称: Uart3_Sendbuf函数
* 功能: 串口3发送
* 返回: 无
*/
static void Uart3_Sendbuf(u8 *pdata,u8 num)
{
u8 i;
for(i = 0;i < num;i++){
Uart3_Send(*(pdata+i));
}
}
/*
* 名称: Usart5_Send函数
* 功能: 串口5发送
* 返回: 无
*/
static void Usart5_Send(u8 dat)
{
while (RESET == usart_flag_get(USART5, USART_FLAG_TBE));
usart_data_transmit(USART5, (uint8_t) dat);
}
/*
* 名称: Usart5_Sendbuf函数
* 功能: 串口5发送
* 返回: 无
*/
static void Usart5_Sendbuf(u8 *pdata,u8 num)
{
u8 i;
for(i = 0;i < num;i++){
Usart5_Send(*(pdata+i));
}
}
/*
* 名称: Uart7_Send函数
* 功能: 串口7发送
* 返回: 无
*/
void Uart7_Send(u8 dat)
{
while (RESET == usart_flag_get(UART7, USART_FLAG_TBE));
usart_data_transmit(UART7, (uint8_t) dat);
}
/*
* 名称: Uart7_Sendbuf函数
* 功能: 串口7发送
* 返回: 无
*/
void Uart7_Sendbuf(u8 *pdata,u8 num)
{
u8 i;
for(i = 0;i < num;i++){
Uart7_Send(*(pdata+i));
}
}
// /*
// * 名称: Usart0_Init
// * 功能: 用于串口0的初始化
// * 参数: bound:波特率
// * 返回: 无
// */
//static void Usart0_Init(u32 bound)
//{
// /* USART0 GPIO configure */
// rcu_periph_clock_enable(RCU_GPIOA); //enable GPIOA clock
// rcu_periph_clock_enable(RCU_USART0); //enable USART0 clock
// gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9); //connect port to USARTx_Tx
//