Files
SM-1000M/RTX5_20220316/Driver/usart/usart5.c
2026-04-23 10:50:18 +08:00

206 lines
6.5 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.

/*
*****************************************************************
头文件
*****************************************************************
*/
#include "usart5.h"
/*
************************************************************************************
变量定义
************************************************************************************
*/
Usart5_Fram_Record_Struct Usart5_Fram_Record_Structs;
u8 Usart5_TX_BUF[520]; //发送缓冲
/*
*********************************************************************************************************
* 函 数 名: USART5_Configuration()
* 功能说明: 串口5配置
* 输 入 波特率baund
* 输 出 :无
*********************************************************************************************************
*/
void Usart5_Configuration(u32 baund)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //使能Usart5
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOD, ENABLE); //GPIOc时钟
USART_DeInit(UART5); //复位串口1
//USART1_TX PC12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//USART1_RX PD2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
USART_InitStructure.USART_BaudRate = baund;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(UART5,&USART_InitStructure);
USART_Cmd(UART5,ENABLE);
USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);//开启USART1的接收中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
USART_ClearFlag(UART5,USART_FLAG_TC); //清除发送完成标志位
}
/*
*********************************************************************************************************
* 函 数 名: Usart5_ReceiveBuff_Clear()
* 功能说明: 串口5清空缓冲区
* 输 入 :无
* 输 出 :无
*********************************************************************************************************
*/
void Usart5_ReceiveBuff_Clear(void)
{
Usart5_Fram_Record_Structs.InfBit.FramLength=0;
Usart5_Fram_Record_Structs.InfBit.FramFinishFlag=0;
memset(Usart5_Fram_Record_Structs.Usart5_RX_BUF,0,Usart5_MAX_RECV_LEN);
memset(Usart5_TX_BUF,0,200);
}
/*
*********************************************************************************************************
* 函 数 名: UART5_IRQHandler()
* 功能说明: 串口5中断函数
* 输 入 :无
* 输 出 :无
*********************************************************************************************************
*/
void UART5_IRQHandler(void)
{
u8 ucCh;
if(USART_GetITStatus( UART5, USART_IT_RXNE ) != RESET )
{
ucCh = USART_ReceiveData( UART5 );
// 打印接收的数据
//u5_printf("Received data: %02X\n", ucCh);
if(Usart5_Fram_Record_Structs .InfBit .FramLength < ( Usart5_MAX_RECV_LEN - 1 ) )
{
Usart5_Fram_Record_Structs .Usart5_RX_BUF[ Usart5_Fram_Record_Structs .InfBit .FramLength ++ ] = ucCh;
}
}
if( USART_GetITStatus( UART5, USART_IT_IDLE ) == SET ) //如果总线空闲
{
Usart5_Fram_Record_Structs .InfBit .FramFinishFlag = 1;
ucCh = USART_ReceiveData( UART5 ); //由软件序列清除中断标志位先读USART_SR,然后读USART_DR
}
}
/*
*********************************************************************************************************
* 函 数 名: u5_printf()
* 功能说明: 串口5输出
* 输 入
* 输 出
*********************************************************************************************************
*/
void u5_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)Usart5_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)Usart5_TX_BUF); //此次发送数据的长度
for(j=0;j<i;j++) //循环发送数据
{
while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕
USART_SendData(UART5,Usart5_TX_BUF[j]);
}
}
/**
* 函 数:串口发送一个字节
* 参 数Byte 要发送的一个字节
* 返 回 值:无
*/
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(UART5, Byte); //将字节数据写入数据寄存器写入后USART自动生成时序波形
while (USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET); //等待发送完成
/*下次写入数据寄存器会自动清除发送完成标志位,故此循环后,无需清除标志位*/
}
/**
* 函 数:串口发送一个数组
* 参 数Array 要发送数组的首地址
* 参 数Length 要发送数组的长度
* 返 回 值:无
*/
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
uint16_t i;
for (i = 0; i < Length; i ++) //遍历数组
{
Serial_SendByte(Array[i]); //依次调用Serial_SendByte发送每个字节数据
}
}
/**
* 函 数:串口发送一个字符串
* 参 数String 要发送字符串的首地址
* 返 回 值:无
*/
void Serial_SendString(char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i ++)//遍历字符数组(字符串),遇到字符串结束标志位后停止
{
Serial_SendByte(String[i]); //依次调用Serial_SendByte发送每个字节数据
}
}
/**
* 函 数自己封装的prinf函数
* 参 数format 格式化字符串
* 参 数:... 可变的参数列表
* 返 回 值:无
*/
void Serial_Printf(char *format, ...)
{
char String[100]; //定义字符数组
va_list arg; //定义可变参数列表数据类型的变量arg
va_start(arg, format); //从format开始接收参数列表到arg变量
vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中
va_end(arg); //结束变量arg
Serial_SendString(String); //串口发送字符数组(字符串)
}