我查看了大多的文章,发现动不动就改CUBEMX生成的代码,觉得他们很傻逼,而且说的都是废话
STM32CUBEMX配置设置
用的串口一定要使能中断!!
加上DMA就好了
printf和scanf
官方代码其实已经附带了例子,本次使用的是DMA。
/*
* 串口重新定向
*/
#if (defined(__GNUC__) && !defined(__CC_ARM))
/* 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)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART2 and Loop until the end of transmission */
while (HAL_OK != HAL_UART_Transmit_DMA(&LOG_huart, (uint8_t *) &ch, 1))
{
;
}
return ch;
}
/**
* @brief Retargets the C library scanf function to the USART.
* @param None
* @retval None
*/
GETCHAR_PROTOTYPE
{
/* Place your implementation of fgetc here */
/* e.g. readwrite a character to the USART2 and Loop until the end of transmission */
uint8_t ch = 0;
while (HAL_OK != HAL_UART_Receive_DMA(&LOG_huart, (uint8_t *)&ch, 1))
{
;
}
return ch;
}
发送这里可以使用
HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
接收这里,我发现轮询和中断好像有点问题,只有DMA可以。
我使用时候把printf重新宏定义了一下
#include "stdio.h"
#define log(...) printf(__VA_ARGS__)
使用时候就可以使用自己的函数,不想开printf时候改掉宏定义就好了
DMA发送和接收
HAL_UART_Receive_DMA 和 HAL_UART_Receive_IT基本一致,接收完后才会进入中断。
代码表现出来为
uint8_t uart_rxbuf1[BUFFSIZE] = {0};//数据缓冲区,最大为BUFFSIZE
//存放指针和计数
typedef struct uart_pointer
{
volatile uint8_t *front;
volatile uint16_t counter;
}uartTypeDef;
uartTypeDef uart_rx1;
void DEBUG_USART1_Read(void)
{
memset(&rx_buf,0,BUFFSIZE);
if (((uart_rx1.front - uart_rxbuf1 ) >= BUFFSIZE)&&((uart_rxbuf1 - uart_rx1.front) > 0))
{
//uart1接收数据指针指向uart1数据缓冲区
uart_rx1.front = uart_rxbuf1;
uart_rx1.counter = 0;
HAL_UART_Receive_DMA(&DEBUG_huart1,(uint8_t*)uart_rx1.front,1);
}
else
{
HAL_UART_AbortReceive(&DEBUG_huart1);
LEN = uart_rx1.counter;
log("LEN %d\n",LEN);
}
if (0 != LEN)
{
memcpy((char *)rx_buf,(char *)uart_rxbuf1, LEN);
log("rx_buf : %s\n",rx_buf);
}
//uart1接收数据指针指向uart1数据缓冲区
uart_rx1.front = uart_rxbuf1;
uart_rx1.counter = 0;
HAL_UART_Receive_DMA(&DEBUG_huart1,(uint8_t*)uart_rx1.front,1);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: trasfer complete*/
if( DEBUG_USART1 == (UartHandle->Instance) )
{
uart_rx1.counter = (uart_rx1.counter + 1)%BUFFSIZE ;
uart_rx1.front = uart_rxbuf1 + uart_rx1.counter;
HAL_UART_Receive_DMA(UartHandle, (uint8_t*)(uart_rx1.front),1);
}
}
就是接收数据缓冲区开始接收,数据超过最大时从最开始的开始存放,我们只要保证不超过最大就好了
我是直接在读取函数中把数据指向缓冲区的,就是没初始化就初始化一下,初始化过了,就直接读取。
DMA发送就更简单了,与printf一致。