Files
player/Project/Src/Drive/Source/usart.c

244 lines
6.8 KiB
C
Raw Normal View History

2025-06-27 00:32:57 +08:00
/***
************************************************************************************
* @file usart.c
* @brief usart <EFBFBD>ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>غ<EFBFBD><EFBFBD><EFBFBD>
************************************************************************************
* @description
*
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>USART1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> PA9/PA10<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>USART1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ8λ<EFBFBD><EFBFBD>ֹͣλ1λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڵIJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ115200<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>usart.h<EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD>USART1_BaudRate<EFBFBD><EFBFBD>
* <EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>fputc<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>printf<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
************************************************************************************
***/
2025-06-27 00:32:57 +08:00
2025-10-10 14:58:02 +08:00
#include "usart.h"
2025-06-27 00:32:57 +08:00
#include "buff.h"
#include "libc.h"
#include "stm32f4xx.h"
2025-06-27 00:32:57 +08:00
#define MUTEX_INIT()
#define MUTEX_DELETE()
#define MUTEX_TAKE()
#define MUTEX_RELEASE()
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>usart IO<49>ڳ<EFBFBD>ʼ<EFBFBD><CABC>
2025-06-27 00:32:57 +08:00
//
void USART_GPIO_Config(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// IO<49><4F>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
RCC_AHB1PeriphClockCmd(USART1_TX_CLK | USART1_RX_CLK, ENABLE);
// IO<49><4F><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // <20><><EFBFBD><EFBFBD>ģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // <20><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // <20><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // <20>ٶȵȼ<C8B5>
// <20><>ʼ<EFBFBD><CABC> TX <09><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = USART1_TX_PIN;
GPIO_Init(USART1_TX_PORT, &GPIO_InitStructure);
// <20><>ʼ<EFBFBD><CABC> RX <20><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;
GPIO_Init(USART1_RX_PORT, &GPIO_InitStructure);
// IO<49><4F><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>õ<EFBFBD>USART1
GPIO_PinAFConfig(USART1_TX_PORT, USART1_TX_PinSource, GPIO_AF_USART1);
GPIO_PinAFConfig(USART1_RX_PORT, USART1_RX_PinSource, GPIO_AF_USART1);
2025-06-27 00:32:57 +08:00
}
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USART <20>ڳ<EFBFBD>ʼ<EFBFBD><CABC>
2025-06-27 00:32:57 +08:00
//
void Usart_Config(void) {
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// IO<49>ڳ<EFBFBD>ʼ<EFBFBD><CABC>
USART_GPIO_Config();
// <20><><EFBFBD>ô<EFBFBD><C3B4>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_BaudRate = USART1_BaudRate;
// <20><><EFBFBD><EFBFBD>λ8λ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// ֹͣλ1λ
USART_InitStructure.USART_StopBits = USART_StopBits_1;
// <20><>У<EFBFBD><D0A3>
USART_InitStructure.USART_Parity = USART_Parity_No;
// <20><><EFBFBD>ͺͽ<CDBA><CDBD><EFBFBD>ģʽ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// <20><>ʹ<EFBFBD><CAB9>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
USART_Init(USART1, &USART_InitStructure);
// ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>1
USART_Cmd(USART1, ENABLE);
2025-06-27 00:32:57 +08:00
}
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD>fputc<74><63><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
//
static int usart_putc(int c) {
// <20><><EFBFBD>͵<EFBFBD><CDB5>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
USART_SendData(USART1, (uint8_t)c);
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
return (c); // <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
2025-06-27 00:32:57 +08:00
}
static int usart_puts(const void *data, int size) {
2025-10-10 14:58:02 +08:00
// MUTEX_TAKE();
for (int i = 0; i < size; i++) {
// <20><><EFBFBD>͵<EFBFBD><CDB5>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
USART_SendData(USART1, ((uint8_t *)data)[i]);
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
2025-06-27 00:32:57 +08:00
}
2025-10-10 14:58:02 +08:00
// MUTEX_RELEASE();
2025-06-27 00:32:57 +08:00
return size;
}
static data_buff g_recv;
static int usart_getc(void) {
2025-06-27 00:32:57 +08:00
uint8_t data;
if (buff_read_byte(&g_recv, &data) == 0)
2025-06-27 00:32:57 +08:00
return data;
else
return EOF;
2025-06-27 00:32:57 +08:00
}
static int usart_open(void) {
2025-06-27 00:32:57 +08:00
MUTEX_INIT();
2025-10-10 14:58:02 +08:00
buff_init(&g_recv, 1024, 0, 0, 0);
/* <20>򿪽<EFBFBD><F2BFAABD><EFBFBD><EFBFBD>ж<EFBFBD> */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
// <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// <20><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
// <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
2025-06-27 00:32:57 +08:00
return 0;
}
static int usart_close(void) {
2025-06-27 00:32:57 +08:00
MUTEX_DELETE();
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
2025-06-27 00:32:57 +08:00
buff_deinit(&g_recv);
return 0;
}
extern_device2(usart, usart_open, usart_close, usart_putc, usart_getc,
usart_puts, NULL);
void USART1_IRQHandler(void) {
uint8_t t = 0;
if (USART1->SR & USART_FLAG_RXNE) {
t = USART1->DR;
buff_save_byte(&g_recv, t);
// if(g_recv_cb) g_recv_cb(t);
} else {
t = USART1->SR;
t = USART1->DR;
}
2025-06-27 00:32:57 +08:00
}
void USART3_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // IO<49><4F>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// IO<49><4F><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // <20><><EFBFBD><EFBFBD>ģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // <20><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // <20><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // <20>ٶȵȼ<C8B5>
// <20><>ʼ<EFBFBD><CABC> TX <09><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// <20><>ʼ<EFBFBD><CABC> RX <20><><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// IO<49><4F><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>õ<EFBFBD>USART1
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// <20><><EFBFBD>ô<EFBFBD><C3B4>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_BaudRate = 115200;
// <20><><EFBFBD><EFBFBD>λ8λ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// ֹͣλ1λ
USART_InitStructure.USART_StopBits = USART_StopBits_1;
// <20><>У<EFBFBD><D0A3>
USART_InitStructure.USART_Parity = USART_Parity_No;
// <20><><EFBFBD>ͺͽ<CDBA><CDBD><EFBFBD>ģʽ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// <20><>ʹ<EFBFBD><CAB9>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_Init(USART3, &USART_InitStructure);
// <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ͨ<EFBFBD><CDA8>
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; // <20><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQͨ<51><CDA8>ʹ<EFBFBD><CAB9>
NVIC_Init(&NVIC_InitStructure); // <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>VIC<49>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>
USART_Cmd(USART3, ENABLE); // ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>1
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
2025-06-27 00:32:57 +08:00
}
// <20><><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>
static uint8_t g_key = 0;
static uint8_t g_keyPress = 0;
static uint8_t g_keyPressed = 0;
void USART3_IRQHandler(void) {
uint8_t res = 0;
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
g_key = USART_ReceiveData(USART3);
if ((g_keyPress ^ g_key) & g_keyPress) {
// <20><><EFBFBD><EFBFBD><E2B0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
g_keyPressed = g_keyPress ^ g_key;
}
g_keyPress = g_key;
}
2025-06-27 00:32:57 +08:00
}
uint8_t USART3_GetKey(void) {
uint8_t ret = g_key;
// g_key=0;
return ret;
2025-06-27 00:32:57 +08:00
}
uint8_t USART3_GetKeyPressed(void) {
uint8_t ret = g_keyPressed;
g_keyPressed = 0;
return ret;
2025-06-27 00:32:57 +08:00
}