当前位置: 首页 > news >正文

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博客

http://www.cnnetsun.cn/news/53850.html

相关文章:

  • JVM G1 和 CMS 详解与对比
  • 实战解析:2PC与Saga分布式事务的完全避坑指南
  • Lumafly模组管理器:重构空洞骑士模组生态的专业解决方案
  • 12.14 - 搜索旋转排序数组 判断两个结构体是否相等
  • WaveTools鸣潮120帧解锁与游戏性能优化全攻略
  • 三步学会百度网盘极速下载:告别龟速的终极方案
  • 5大实用技巧:用Calibre-Douban插件智能管理电子书元数据
  • 飞书文档批量导出终极指南:一键解决文档迁移难题
  • Source Han Serif思源宋体:免费开源中文字体专业应用指南
  • DOM Element:深入理解与操作
  • 深度解析 Flutter 路由管理:从原生路由到 AutoRoute 的优雅升级与性能优化
  • Turnitin系统查英文AI率多少为正常?报告显示星号*%怎么办?
  • 暖通净化空调恒温恒湿项目:PLC 与触摸屏上位机程序探秘
  • 第30章 Shell 正则表达式实战:精准匹配字符串、日志与配置项
  • 音视频学习(七十二):视频压缩:分块与预处理
  • AMD Ryzen性能调优:快速掌握处理器调试工具的使用技巧
  • 深蓝词库转换:轻松打通全平台输入法数据壁垒
  • (新卷,200分)- 最小传输时延Ⅱ(Java JS Python)
  • OpenHarmony AI人脸识别与手势控制系统开发指南
  • 新一代空间感知驱动的军工仓库与硐室透明化管控技术研究
  • Sketch MeaXure插件:设计师必备的智能标注工具
  • 强化学习Q-learning求最优策略
  • 你对电脑上的【Fn】熟悉多少
  • 计及N-k安全约束的含光热电站电力系统优化调度模型【IEEE14节点、118节点】附Matlab代码
  • 计及需求响应的粒子群算法求解风能、光伏、柴油机、储能容量优化配置附Matlab代码
  • conda使用详细指南
  • 豆包与DeepSeek底层大模型的深度解析:技术架构、设计理念与生态分野
  • Linux系统中的socket激活:先创建监听端口,后启动程序
  • 从零解决pyproject.toml构建失败的实战指南
  • Redis Lua脚本入门:从零写出你的第一个原子操作