当前位置:网站首页>Stm32 usart+dma usage based on Hal Library
Stm32 usart+dma usage based on Hal Library
2022-06-29 08:03:00 【Lighting master ~】
Development board : Point atom Apollo
Create a project
Can make USART
Turn on DMA

Configuration interrupt

After the above configuration , Click generate project
One 、 receive 、 Send data of specified length
Designated reception 、 The length of the data sent is 1
Define a receive buffer
char tx_buffer[10];
Write interrupt callback function
When receiving interrupt is triggered , Pass the received data through DMA Send out
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == USART1) {
// Function to use DMA The way of
HAL_UART_Transmit_DMA(&huart1,tx_buffer,1);
}
}
When the transmission interrupt is triggered , Use DMA Take over
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == USART1) {
Function to use DMA The way of
HAL_UART_Receive_DMA(&huart1,tx_buffer,1);
}
}
Enable receive interrupt
stay main.c in

compile 、 Download it to see the effect
Two 、 receive 、 Send variable length data
When receiving variable length , You need to determine USART When is idle , When an idle interrupt is triggered , Prove that all data has been received at this time , need
The functions used are
HAL_UART_DMAStop(UART_HandleTypeDef *huart); // stop it DMA transmission
__HAL_DMA_GET_COUNTER(__HANDLE__); // obtain DMA How much data was transmitted
__HAL_UART_CLEAR_IDLEFLAG(__HANDLE__); // Clear idle flag
HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size); // Use DMA By sending
HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size); // Use DMA To receive
Enable idle interrupt
stay MX_USART1_UART_Init() Function to add
/* USER CODE BEGIN USART1_Init 2 */
// Enable idle interrupt
__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);
/* USER CODE END USART1_Init 2 */
Define a receive buffer
/* USER CODE BEGIN 0 */
#define RXSIZE 1024 /
uint8_t rx_buffer[RXSIZE] = {
0};
uint16_t RxNum = 0;
/* USER CODE END 0 */
To write USART1 interrupt
/** * @brief This function handles USART1 global interrupt. */
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
// Determine the idle flag
if (__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE) == SET) {
// Clear idle interrupt flag
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
// stop it DMA
HAL_UART_DMAStop(&huart1);
// How long data has been received
RxNum = RXSIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
// Send the received data out again
HAL_UART_Transmit_DMA(&huart1,rx_buffer,RxNum);
}
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
Write interrupt callback function
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == USART1) {
RxNum = 0;
// Empty a buffer
memset(rx_buffer,0,RXSIZE);
// To enable to receive
HAL_UART_Receive_DMA(&huart1,rx_buffer,RXSIZE);
}
}
Enable receive interrupt
stay main.c in
If you do not add this sentence, you cannot enter the interrupt 
compile 、 Download it to see the effect
边栏推荐
- 产品安全 - 小漏洞导致大问题
- 【6G】算力网络技术白皮书整理
- 【量化投资系统】问题记录及解决方法
- 【深度之眼吴恩达机器学习作业班第四期】逻辑回归编程实现
- Should product managers learn from ink knife or Axure?
- 【深度之眼吴恩达机器学习作业班第四期】Regularization正则化总结
- RobotFramework学习笔记:Robot Framework和BrowserLibrary(PlayWright)简介
- 在colaboratory上云端使用GPU训练(以YOLOv5举例)
- 101. symmetric binary tree (recursive and iterative methods)
- C # import CSV into MySQL database
猜你喜欢
![[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming](/img/da/08f23cfe7f8759b73576eb0638fec5.png)
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming

产品经理应该学习墨刀还是Axure?

ROS2中的行为树 BehaviorTree
![[industrial control old horse] detailed design of PLC six way responder system](/img/9c/8bfe336bb95a028a4fb8130941ff81.png)
[industrial control old horse] detailed design of PLC six way responder system

手撕二叉搜索树(Binary Search Tree)

C实战——高配版贪吃蛇游戏设计

Should product managers learn from ink knife or Axure?

AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation

C#Mqtt订阅消息

SQL Server 开启cdc
随机推荐
Improvement direction of programming ability
Compiling principle: the king's way
基础知识 - 语法标准(ANSI C、ISO C、GNU C)
[eye of depth wuenda machine learning homework class phase IV] summary of logistic regression
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
Code:: blocks code formatting shortcuts
PostgreSQL installation: the database cluster initialization failed, stack hbuilder installation
AI deep dive of Huawei cloud
js:Array. Reduce cumulative calculation and array consolidation
C actual combat - high configuration version of Snake game design
Line features & surface features of vSLAM features
搭建jenkins环境并自动关联打包好的工程jar进行自动发布
Vulnhub's dc6 target
【kerberos】kerberos 认证浅析
Audio and video development cases 99 lectures - Contents
C compiler - implicit function declaration
JS to implement a detailed scheme for lazy loading of pictures (it can be used after being imported)
498. diagonal traversal (simulation)
SQL Server 开启cdc
qtcreator设置字符集