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

108 lines
3.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 "bsp.h"
//串口接收缓存区
Usart5_Fram_Record_Struct Usart5_Fram_Record_Structs;
u8 Usart5_TX_BUF[520]; //发送缓冲
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时钟
//NVIC_Configuration();
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); //清除发送完成标志位
}
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);
}
void UART5_IRQHandler(void)
{
u8 ucCh;
if(USART_GetITStatus( UART5, USART_IT_RXNE ) != RESET )
{
ucCh = USART_ReceiveData( UART5 );
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
}
}
//串口5,printf 函数
//确保一次发送数据不超过Usart5_MAX_SEND_LEN字节
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]);
}
}