串口2配置bsp_usart.c
#include "bsp_usart2.h"
#include <stdarg.h>
/*
* 函数名:USART2_Config
* 描述 :USART2 GPIO 配置,工作模式配置
* 输入 :无
* 输出 : 无
* 调用 :外部调用
*/
void USART2_Config(void)
{ printf(&quot;\r\nusart2_config\r\n&quot;);
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* config USART2 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* USART2 GPIO config */
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART2 mode config */
USART_InitStructure.USART_BaudRate = 9600; //GPS模块默认使用波特率:9600
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(USART2, &USART_InitStructure);
// Channel Interrupt ENABLE
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2, ENABLE);
}
#if 1
//中断缓存串口数据
#define UART_BUFF_SIZE 255
volatile uint8_t uart_p = 0;
uint8_t uart_buff[UART_BUFF_SIZE];
void bsp_USART2_IRQHandler(void)
{
printf(&quot;\r\nHere1.......................\r\n&quot;);
if(uart_p<UART_BUFF_SIZE)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
uart_buff[uart_p] = USART_ReceiveData(USART2);
uart_p++;
}
}
}
//获取接收到的数据和长度
char *get_rebuff(uint8_t *len)
{
*len = uart_p;
return (char *)&uart_buff;
}
void clean_rebuff(void)
{
uart_p = 0;
}
#endif |