84 lines
2.5 KiB
C
84 lines
2.5 KiB
C
#include "Serial.h"
|
|
#include "Delay.h" // Get_Time_ms 在这里,所以我们包含它
|
|
#include <string.h>
|
|
|
|
#define RX_BUFFER_SIZE 1024 // ← 增大到1024字节,支持完整HTTP响应
|
|
char RxBuffer[RX_BUFFER_SIZE];
|
|
uint16_t RxCounter = 0;
|
|
|
|
// 初始化ESP8266所用的串口 (根据连接图.md: USART1 PA9/PA10)
|
|
void Serial_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
|
|
|
|
// PA9: USART1_TX
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
// PA10: USART1_RX
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.USART_BaudRate = 115200;
|
|
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(USART1, &USART_InitStructure);
|
|
|
|
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 优先级低于K210
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
USART_Cmd(USART1, ENABLE);
|
|
}
|
|
|
|
// 发送字符串
|
|
void Serial_SendString(const char* String)
|
|
{
|
|
for (uint32_t i = 0; String[i] != '\0'; i++)
|
|
{
|
|
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
|
|
USART_SendData(USART1, String[i]);
|
|
}
|
|
}
|
|
|
|
// 获取接收缓冲区
|
|
char* Serial_GetRxBuffer(void)
|
|
{
|
|
return RxBuffer;
|
|
}
|
|
|
|
// 清空接收缓冲区
|
|
void Serial_ClearRxBuffer(void)
|
|
{
|
|
memset(RxBuffer, 0, RX_BUFFER_SIZE);
|
|
RxCounter = 0;
|
|
}
|
|
|
|
// USART1中断服务函数 (ESP8266)
|
|
void USART1_IRQHandler(void)
|
|
{
|
|
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
|
|
{
|
|
uint8_t RxData = USART_ReceiveData(USART1);
|
|
if (RxCounter < RX_BUFFER_SIZE - 1)
|
|
{
|
|
RxBuffer[RxCounter++] = RxData;
|
|
}
|
|
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
|
|
}
|
|
}
|