ESP8266串口部分
#include "stm32f10x_rcc.h"
#include "Hal_Usart/hal_uart.h"
#ifdef __GNUC__
// With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar()
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief 串口GPIO初始化
* @param none
* @return none
*/
void uartGpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#if defined (Open_UART1)
USART1_GPIO_Cmd(USART1_GPIO_CLK, ENABLE);
USART1_AFIO_Cmd(USART1_AFIO_CLK, ENABLE);
USART1_CLK_Cmd(USART1_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = USART1_TxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_GPIO_PORT, &GPIO_InitStructure);
#endif
#if defined (Open_UART2)
USART2_GPIO_Cmd(USART2_GPIO_CLK, ENABLE);
USART2_CLK_Cmd(USART2_CLK, ENABLE);
USART2_AFIO_Cmd(USART2_AFIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = USART2_TxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART2_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART2_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART2_GPIO_PORT, &GPIO_InitStructure);
#endif
#if defined (Open_UART3)
USART3_GPIO_Cmd(USART3_GPIO_CLK, ENABLE);
USART3_CLK_Cmd(USART3_CLK, ENABLE);
USART3_AFIO_Cmd(USART3_AFIO_CLK, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_USART3,ENABLE);
GPIO_InitStructure.GPIO_Pin = USART3_TxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART3_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART3_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART3_GPIO_PORT, &GPIO_InitStructure);
#endif
}
/**
* @brief 串口配置初始化
* @param none
* @return none
*/
void uartConfig(void)
{
USART_InitTypeDef USART_InitStructure;
#if defined (Open_UART1)
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);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC); /*清空发送完成标志,Transmission Complete flag */
#endif
#if defined (Open_UART2)
USART_InitStructure.USART_BaudRate = 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);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC); /*清空发送完成标志,Transmission Complete flag */
#endif
#if defined (Open_UART3)
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(USART3, &USART_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART_Cmd(USART3, ENABLE);
USART_ClearFlag(USART3, USART_FLAG_TC); /*清空发送完成标志,Transmission Complete flag */
#endif
}
/**
* @brief 串口NVIC初始化
* @param none
* @return none
*/
void nvicConfiguration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#if defined (Open_UART1)
/*使能串口中断,并设置优先级*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
#if defined (Open_UART2)
/*使能串口中断,并设置优先级*/
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
#if defined (Open_UART3)
/*使能串口中断,并设置优先级*/
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
}
/**
* @brief UART1写操作
*
* @param data : 数据
*
* @return : none
*/
void uart1SendData(uint8_t data)
{
USART_SendData(USART1,data);
//Loop until the end of transmission
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
/**
* @brief 串口初始化函数
* @param none
* @return none
*/
void uartxInit(void)
{
uartGpioInit();
uartConfig();
nvicConfiguration();
}
/**
* @brief printf打印重定向
* @param none
* @return none
*/
PUTCHAR_PROTOTYPE
{
//Place your implementation of fputc here , e.g. write a character to the USART
USART_SendData(USART1,(u8)ch);
//Loop until the end of transmission
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
|