0 CUBEMX配置
1 串口发送
1.1 阻塞发送
/**
* @brief Sends an amount of data in blocking mode.
* @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
* the sent data is handled as a set of u16. In this case, Size must indicate the number
* of u16 provided through pData.
* @param huart Pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @param pData Pointer to data buffer (u8 or u16 data elements).
* @param Size Amount of data elements (u8 or u16) to be sent
* @param Timeout Timeout duration
* @retval HAL status
*/
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
#include "string.h"
char data[] = "hello world";
uint16_t size = strlen(data);
HAL_UART_Transmit(&huart1, (uint8_t *)data, size, HAL_MAX_DELAY);
1.2 DMA发送
/**
* @brief Sends an amount of data in DMA mode.
* @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
* the sent data is handled as a set of u16. In this case, Size must indicate the number
* of u16 provided through pData.
* @param huart Pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @param pData Pointer to data buffer (u8 or u16 data elements).
* @param Size Amount of data elements (u8 or u16) to be sent
* @retval HAL status
*/
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
#include "string.h"
char data[] = "hello world";
uint16_t size = strlen(data);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)data, size);
1.3 串口printf
阻塞方式实现串口printf
#include "string.h"
#include "stdarg.h"
#include "stdio.h"
void usart_printf(UART_HandleTypeDef USARTx, char *fmt,...);
void usart_printf(UART_HandleTypeDef USARTx, char *fmt,...)
{
unsigned char UsartPrintfBuf[296];
va_list ap;
unsigned char *pStr = UsartPrintfBuf;
va_start(ap, fmt);
vsnprintf((char *)UsartPrintfBuf, sizeof(UsartPrintfBuf), fmt, ap);
va_end(ap);
while(*pStr != NULL)
{
HAL_UART_Transmit (&USARTx ,(uint8_t *)pStr++,1,HAL_MAX_DELAY );
}
}
2 串口接收
2.1 串口空闲接收中断 + DMA
#include "string.h"
#define usart1_rx_buff_size (512)
// 串口结构体
typedef struct
{
uint8_t data; // 接收到的操作数
uint8_t rx_buff[usart1_rx_buff_size]; // 接收缓冲
void (*analysis)(uint8_t *pData, uint16_t Size); // 串口接收数据解析
}usart_struct;
usart_struct usart1;
void usart1_analysis(uint8_t *pData, uint16_t Size)
{
switch(pData[0])
{
case 0x01:
usart1.data = 100;
break;
case 0x02:
usart1.data = 200;
break;
}
}
void usart_init(void)
{
usart1.analysis =usart1_analysis;
}
// 串口接收回调函数
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef * huart, uint16_t Size)
{
if(huart->Instance == USART1)
{
usart1.analysis((uint8_t*)usart1.rx_buff, Size);
memset(usart1.rx_buff, 0, Size);
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, usart1.rx_buff, usart1_rx_buff_size);
}
}
// 串口接收错误回调函数
void HAL_UART_ErrorCallback(UART_HandleTypeDef * huart)
{
if(huart->Instance == USART1)
{
memset(usart1.rx_buff, 0, usart1_rx_buff_size);
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, usart1.rx_buff, usart1_rx_buff_size);
}
}
usart_init(); / 初始化串口结构体
// 开启串口空闲中断+DMA
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, usart1.rx_buff, usart1_rx_buff_size);
// 根据操作数进行操作
if(usart1.data)
{
usart_printf(huart1, "%d", usart1.data);
usart1.data = 0;
}