Files
player/Project/Src/Drive/Source/usart2.c
2025-07-05 19:47:28 +08:00

171 lines
4.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 "usart2.h"
#include "stm32f4xx.h"
#include "buff.h"
/*----------------------USART配置宏 ------------------------*/
#define USART_BaudRate_ 115200
#if 0
#define USART_TX_PIN GPIO_Pin_9
#define USART_TX_PORT GPIOA
#define USART_TX_CLK RCC_AHB1Periph_GPIOA
#define USART_TX_PinSource GPIO_PinSource9
#define USART_RX_PIN GPIO_Pin_10
#define USART_RX_PORT GPIOA
#define USART_RX_CLK RCC_AHB1Periph_GPIOA
#define USART_RX_PinSource GPIO_PinSource10
#define GPIO_AF_USART GPIO_AF_USART1
#define USART USART1
#define USART_IRQn USART1_IRQn
#define USART_CLK_ON() RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
#define USART_CLK_OFF() RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE)
#define USART_IRQHandler USART1_IRQHandler
#else
#define USART_TX_PIN GPIO_Pin_5
#define USART_TX_PORT GPIOD
#define USART_TX_CLK RCC_AHB1Periph_GPIOD
#define USART_TX_PinSource GPIO_PinSource5
#define USART_RX_PIN GPIO_Pin_6
#define USART_RX_PORT GPIOD
#define USART_RX_CLK RCC_AHB1Periph_GPIOD
#define USART_RX_PinSource GPIO_PinSource6
#define GPIO_AF_USART GPIO_AF_USART2
#define USART USART2
#define USART_IRQn USART2_IRQn
#define USART_CLK_ON() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE)
#define USART_CLK_OFF() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE)
#define USART_IRQHandler USART2_IRQHandler
#endif
static data_buff g_recv;
static void (*g_recv_cb)(uint8_t d);
// 函数usart IO口初始化
//
static void USART_GPIO_Config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd ( USART_TX_CLK|USART_RX_CLK, ENABLE); //IO口时钟配置
//IO配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //速度等级
//初始化 TX 引脚
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN;
GPIO_Init(USART_TX_PORT, &GPIO_InitStructure);
//初始化 RX 引脚
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_Init(USART_RX_PORT, &GPIO_InitStructure);
//IO复用复用到USART
GPIO_PinAFConfig(USART_TX_PORT,USART_TX_PinSource,GPIO_AF_USART);
GPIO_PinAFConfig(USART_RX_PORT,USART_RX_PinSource,GPIO_AF_USART);
}
// 函数USART 口初始化
//
void usart2_init(void)
{
USART_InitTypeDef USART_InitStructure;
USART_CLK_ON();
// IO口初始化
USART_GPIO_Config();
// 配置串口各项参数
USART_InitStructure.USART_BaudRate = USART_BaudRate_; //波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送和接收模式
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 不使用硬件流控制
USART_Init(USART,&USART_InitStructure); //初始化串口1
USART_Cmd(USART,ENABLE); //使能串口1
buff_init(&g_recv,1024,1,0xff,0xfc);
/* 打开接收中断 */
USART_ITConfig(USART,USART_IT_RXNE,ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=USART_IRQn; //定时器中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void usart2_deinit(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=USART_IRQn; //定时器中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=DISABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART);
buff_deinit(&g_recv);
}
void usart2_set_cbcall(void (*cb)(uint8_t))
{
g_recv_cb=cb;
}
int usart2_get_byte(uint8_t *data)
{
return buff_read_byte(&g_recv,data);
}
int usart2_put_byte(uint8_t data)
{
USART_SendData(USART, data);
while (USART_GetFlagStatus(USART, USART_FLAG_TXE) == RESET);
return 0;
}
int usart2_clear(void)
{
return buff_clear(&g_recv);
}
void USART_IRQHandler (void)
{
uint8_t t=0;
if(USART->SR&USART_FLAG_RXNE)
{
t=USART->DR;
buff_save_byte(&g_recv,t);
if(g_recv_cb) g_recv_cb(t);
}
else
{
t=USART->SR;
t=USART->DR;
}
}