开发板:NUCLEO-F072RB
串口练习:
1.发送完判断。
2.接收完按断。
---------------------------------------------------------------
1.实现发送完成判断。按键触发中断发送一段字符串并闪烁LED,发送完成后停止闪烁。
#include "main.h"
#include "usart.h"
#include "gpio.h"
#define COUNTOF(__BUFFER__) ( sizeof(__BUFFER__) / sizeof(*__BUFFER__) )
#define TXBUFFERSIZE (COUNTOF(aTxBuffer) - 1)
#define RXBUFFERSIZE 100
uint8_t aTxBuffer[] = "--- hello chao 1223366666666666666666666666669996 ---\r\n";
uint8_t aRxBuffer[RXBUFFERSIZE];
__IO ITStatus UartReady = RESET;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxBuffer, TXBUFFERSIZE);
while (1)
{
if(UartReady == SET)
{
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
}
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_13)
{
UartReady = SET;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxBuffer, TXBUFFERSIZE);
}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
UartReady = RESET;
}
}
2.实现接收字符判断。按下按键开始接收5个字符并且闪烁LED,接收完成后停止闪烁。
#include "main.h"
#include "usart.h"
#include "gpio.h"
#define COUNTOF(__BUFFER__) ( sizeof(__BUFFER__) / sizeof(*__BUFFER__) )
#define TXBUFFERSIZE (COUNTOF(aTxBuffer) - 1)
#define RXBUFFERSIZE 100
uint8_t aTxBuffer[] = "--- hello chao 1223366666666666666666666666669996 ---\r\n";
uint8_t aRxBuffer[RXBUFFERSIZE];
__IO ITStatus UartReady = RESET;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxBuffer, TXBUFFERSIZE);
while (1)
{
if(UartReady == SET)
{
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
}
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_13)
{
UartReady = SET;
//接收5个字符
HAL_UART_Receive_IT(&huart1, (uint8_t*)aRxBuffer, 5);
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
UartReady = RESET;
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
}
}
---------------
【碰到一个串口DMA问题】:
问题:
串口DMA发送不了数据。
原因:
cubeMX生成代码时候,不知道怎么,先生成MX_DMA_Init()导致DMA后使能。(DMA要先使能,再初始化,优先级可以后设置)
-----------------------------------------
【碰到一个问题】:
HAL_UART_Transmit_DMA连续发送,pc串口小工具接收字符串不全的问题
extern UART_HandleTypeDef huart1;
uint8_t hello_msg[] = "program begin\r\n";
uint8_t msg_1[] = "BBBBB\r\nAAAAA\r\n";
int main()
{
//.....
//省略初始化
//....
HAL_UART_Transmit_DMA(&huart1, hello_msg, sizeof(hello_msg));
while(huart1.gState != HAL_UART_STATE_READY);
HAL_UART_Transmit_DMA(&huart1, msg_1, sizeof(msg_1));
while(1)
{
}
}
两个串口小工具,接收到的不一样,左边的一直没问题。
我TM...右边这东西有bug....
这问题你说怎么debug,还要我用两个小工具试了
-------------------------------
【串口DMA收发】:
HAL_UART_Transmit_DMA(&huart1, hello_msg, sizeof(hello_msg));
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
while(huart1.gState != HAL_UART_STATE_READY);
HAL_UART_Transmit_DMA(&huart1, msg_1, sizeof(msg_1));
HAL_UART_Receive_DMA(&huart1, (uint8_t*)aRxBuffer, 10);
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
直接CubeMx生成的
好处是可以异步,不用等待,不用cpu
------------------------
【串口DMA不定长发送】:
这个我就不自己想了,网上的学习下,实现一遍。
2019年3月13日-HAL库UART使用DMA收发(二)_y_softwave的博客-CSDN博客
STM32 HAL库 DMA接收数据丢失_白冬雷的博客-CSDN博客_串口dma接收丢数据
代码:
extern UART_HandleTypeDef huart1;
uint8_t hello_msg[] = "program begin\r\n";
uint8_t msg_1[] = "BBBBB\r\nAAAAA\r\n";
uint8_t aRxBuffer[30];
extern uint8_t aRxCount;
extern uint8_t aRxBuffer_Save[1024];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
HAL_UART_Transmit_DMA(&huart1, hello_msg, sizeof(hello_msg));
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
while(huart1.gState != HAL_UART_STATE_READY);
HAL_UART_Transmit_DMA(&huart1, msg_1, sizeof(msg_1));
HAL_UART_Receive_DMA(&huart1, (uint8_t*)aRxBuffer, 10);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
while (1)
{
if(aRxCount > 0)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_UART_Transmit_DMA(&huart1, aRxBuffer_Save, aRxCount);
aRxCount = 0;
HAL_Delay(100);
}
}
}
//stm32f0xx_it.c
uint8_t aRxCount=0;
uint8_t aRxBuffer_Save[1024];
extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern UART_HandleTypeDef huart1;
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
HAL_UART_IDLECallBack(&huart1);
}
void HAL_UART_IDLECallBack(UART_HandleTypeDef *huart)
{
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE)!= RESET)
{
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
HAL_UART_DMAStop(&huart1);
aRxCount = USART_BUF_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
HAL_UART_Receive_DMA(&huart1,aRxBuffer_Save, USART_BUF_SIZE);
}
}
-----------------
接下来是,数据长度大于DNA接收长度的问题练习,感受一下