news 2026/6/15 6:37:44

Freertos手把手教STM32CubeMx设置STM32F4芯片DMA发送ADC数据(二)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Freertos手把手教STM32CubeMx设置STM32F4芯片DMA发送ADC数据(二)

本期目标

  • 理清本工程系统框架
  • 弄懂CubeMx配置相关原理及设置的背后含义
  • 对DMA以及ADC相关的重要API接口使用详解
  • 梳理代码设计流程

前置文章链接:

Freertos手把手教STM32CubeMx设置STM32F4芯片DMA发送ADC数据(一)-CSDN博客

在上一章节完成了对框架的初步探索以及对CubeMx的配置

在freertos的task中对buffer进行了一些测试

下面继续上一章的内容继续完成本次目标

2.启动DMA传输并进入中断

此处是使用中断的方式, 意为当数据采集完毕后会使用产生中断

本篇使用的是下面的方式

(##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA()) (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE() (+++) Configure and enable two DMA streams stream for managing data transfer from peripheral to memory (output stream) (+++) Associate the initialized DMA handle to the CRYP DMA handle using __HAL_LINKDMA() (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the two DMA Streams. The output stream should have higher priority than the input stream.

在启动ADC传输以后 , 会把数据放到DR寄存器里 , 如果是中断模式 , 会产生一个中断去取走DR寄存器里面的值 , 而是产生一个DMA request ,然后启动对应的DMA通道 , 然后把DR寄存器里面的值搬运到指定的地址(buffer1/buffer2)上

HAL_ADC_Start_DMA()

最主要的就是知晓这个函数怎么用

/** * @brief Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral * @param hadc pointer to a ADC_HandleTypeDef structure that contains * the configuration information for the specified ADC. * @param pData The destination Buffer address. * @param Length The length of data to be transferred from ADC peripheral to memory. * @retval HAL status */

点击函数跳转定义,可以看到这个函数的简介

“启动DMA request 在每次传输完ADC后 , 并且启动ADC外设”

HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)

第一个形参是ADC的句柄

于ADC.c文件里面有定义

所以我们要使用的时候就要在所使用的文件里extern引用

/* USER CODE BEGIN Variables */ extern ADC_HandleTypeDef hadc1; extern DMA_HandleTypeDef hdma_adc1; /* USER CODE END Variables */

第二个形参是DMA将要将数据搬运到的地址 , 就是我们前面定义的buffer1/2

第三个形参是要从ADC采集多少次数据

HAL_ADC_Start_DMA(&hadc1, buffer1, BUFFER_SIZE); HAL_ADC_Start_DMA(&hadc1, buffer2, BUFFER_SIZE);

void StartDefaultTask(void *argument) { /* USER CODE BEGIN StartDefaultTask */ buffer1 = (uint32_t *)malloc((sizeof(uint32_t)* BUFFER_SIZE)); buffer2 = (uint32_t *)malloc((sizeof(uint32_t)* BUFFER_SIZE)); if(NULL == buffer1) { printf("buffer1 malloc failed \r\n"); } if(NULL == buffer2) { printf("buffer2 malloc failed \r\n"); return; } printf("buffer1 , buffer2 malloc success\r\n "); memset(buffer1, 0xff , (sizeof(uint32_t)* BUFFER_SIZE)); memset(buffer2, 0xff , (sizeof(uint32_t)* BUFFER_SIZE)); printf("Unit test ADC + DMA\r\n "); HAL_ADC_Start_DMA(&hadc1, buffer1, BUFFER_SIZE); HAL_ADC_Start_DMA(&hadc1, buffer2, BUFFER_SIZE); /* Infinite loop */ for(;;) { printf("hello world \r\n"); printf("buffer1 data = [%d] \r\n" , buffer1[0]); printf("buffer2 data = [%d] \r\n" , buffer2[0]); osDelay(1000); } /* USER CODE END StartDefaultTask */ }

为了防止ADC调用出错 , 应当接收函数的返回值 , 以观察函数是否异常

HAL_StatusTypeDef ret1 = HAL_OK; HAL_StatusTypeDef ret2 = HAL_OK; ret1 = HAL_ADC_Start_DMA(&hadc1, buffer1, BUFFER_SIZE); ret2 = HAL_ADC_Start_DMA(&hadc1, buffer2, BUFFER_SIZE); if(HAL_OK != ret1) { printf("HAL_ADC1 call failed "); } if(HAL_OK != ret2) { printf("HAL_ADC2 call failed "); }

至此我们的API :HAL_ADC_Start_DMA()就调用成功了

HAL_ADC_ConvCpltCallback()

当DMA输出到Buffer1后 ,触发DMA中断(中断做的第一件事: 发送消息队列或任务通知(邮箱)给线程A) 。 (涉及函数:xTaskNotifyFromISR或xQueueGenericSendFromISR)

(+) At The end of data transfer by HAL_ADC_ConvCpltCallback() function is executed and user can add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
__weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { /* Prevent unused argument(s) compilation warning */ UNUSED(hadc); /* NOTE : This function Should not be modified, when the callback is needed, the HAL_ADC_ConvCpltCallback could be implemented in the user file */ }

将此函数复制到freertos文件调用

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { /* Prevent unused argument(s) compilation warning */ UNUSED(hadc); printf("buffer1 data = [%d] \r\n" , buffer1[0]); /* NOTE : This function Should not be modified, when the callback is needed, the HAL_ADC_ConvCpltCallback could be implemented in the user file */ }

打印一串数据到串口,验证是否可行

可以看到函数被运行了

HAL_ADC_ErrorCallback()

同样的 , 要设置一个验错机制

(+) In case of transfer Error, HAL_ADC_ErrorCallback() function is executed and user can add his own code by customization of function pointer HAL_ADC_ErrorCallback
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc) { /* Prevent unused argument(s) compilation warning */ UNUSED(hadc); /* NOTE : This function Should not be modified, when the callback is needed, the HAL_ADC_ErrorCallback could be implemented in the user file */ printf("ADC trasfer error \r\n"); }

下一章节:

Freertos手把手教STM32CubeMx设置STM32F4芯片DMA发送ADC数据(三)-CSDN博客

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/13 21:06:00

WSL2 中 pynput 无法捕获按键输入?

视频链接:https://www.bilibili.com/video/BV1vCmiB1ENV/?vd_source5ba34935b7845cd15c65ef62c64ba82f 你是否遇到过在 WSL2 中,pynput 无响应,无法捕获键盘的输入? WSL2 本质是 Linux 内核子系统,无法直接访问 Win…

作者头像 李华
网站建设 2026/6/14 20:41:28

毕业论文选题平台Top10榜单及本科生选题指南

10大论文选题工具核心对比 排名 工具名称 核心功能 效率评分 适用场景 1 aicheck 智能选题大纲生成 ★★★★★ 完全无头绪时的选题生成 2 aibiye 选题优化可行性分析 ★★★★☆ 已有初步方向的优化调整 3 知网 学术资源库选题参考 ★★★★☆ 专业领域深度…

作者头像 李华
网站建设 2026/6/15 0:14:09

分库分表详细讲解及技术选型

为什么需要分库分表 为什么要分库 分库主要解决的是并发量过⼤的问题,因为并发量⼀旦上升了,那么数据库就可能成为系统的瓶颈,因为数据库的连接数量是有上限的,虽然你可以进⾏调整,但并不是⽆限调整的。所以&#xff0…

作者头像 李华
网站建设 2026/6/15 6:05:19

Applite革命性体验:告别命令行,拥抱macOS软件管理的智能时代

Applite革命性体验:告别命令行,拥抱macOS软件管理的智能时代 【免费下载链接】Applite User-friendly GUI macOS application for Homebrew Casks 项目地址: https://gitcode.com/gh_mirrors/ap/Applite 你是否曾因繁琐的Homebrew命令而头疼&…

作者头像 李华
网站建设 2026/6/12 6:46:20

Performance-Fish如何解决《环世界》后期性能瓶颈?

Performance-Fish如何解决《环世界》后期性能瓶颈? 【免费下载链接】Performance-Fish Performance Mod for RimWorld 项目地址: https://gitcode.com/gh_mirrors/pe/Performance-Fish 当你的殖民地人口突破百人,建筑遍布地图,游戏帧率…

作者头像 李华