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

ESP32 在Platform Arduino平台驱动外部PSAM,进行内存管理

一,基本介绍

        本文中主要介绍ESP32、ESP32S3系列单片机,基于Vscode Platform Arduino和Arduino框架下如何使用外部PSAM,以及必要的API调用函数进行内存分配和管理。

        使用前提是开发板有外部PSRAM。

二,平台配置

2.1 Arduino平台

2.2 Platform IO

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
;串口波特率
monitor_speed = 115200
;串口下载速度
upload_speed = 921600
;CPU的运行速度240Mhz
board_build.f_cpu = 240000000
;根据自己的开发板,选择分区表
board_build.partitions = default_16MB.csv
;外部PSRAM使用qio或者opi
board_build.arduino.memory_type = qio_opi
;通过该宏定义,启动外部的PSRAM
build_flags = -DBOARD_HAS_PSRAM
;外部Flash的大小
board_upload.flash_size = 16MB

三,内存测试代码

测试单片机为ESP32S3N16R8,具有16MB Flash 和8MB PSAM


#include <Arduino.h>
#include <esp_heap_caps.h>void setup() {Serial.begin(115200);//查看ESP32堆的总共大小Serial.printf("ESP32 total Heap size :%d bytes\n",ESP.getHeapSize());//查看ESP32堆的可用大小Serial.printf("ESP32 free  Heap size :%d bytes\n",ESP.getFreeHeap());//查看ESP32的Flash的大小Serial.printf("Flash size: %d bytes\n", ESP.getFlashChipSize());//查看ESP32的内部和外部RAM的总共大小Serial.printf("Deafult total size: %d bytes\n", heap_caps_get_total_size(MALLOC_CAP_DEFAULT));//查看ESP32的内部和外部RAM的可用大小Serial.printf("Deafult free size: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));//查看ESP32的内部RAM总共大小Serial.printf("Internal total size: %d bytes\n", heap_caps_get_total_size(MALLOC_CAP_INTERNAL));//查看ESP32的内部RAM可用大小Serial.printf("Internal free size: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));//查看ESP32的外部RAM的可用大小Serial.printf("PSRAM total size: %d bytes\n", heap_caps_get_total_size(MALLOC_CAP_SPIRAM));//查看ESP32的外部RAM的可用大小Serial.printf("PSRAM free size: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
}void loop() {delay(10); 
}

串口输出结果

四,内存申请与释放代码

测试单片机为ESP32S3N16R8

 //从芯片内部申请PSARAM,大小为1000字节,数据类型为char型char* str1=(char *)heap_caps_malloc(1000,MALLOC_CAP_INTERNAL);  //从芯片外部申请PSARAM,大小为1000字节,数据类型为char型char* str1=(char *)heap_caps_malloc(1000,MALLOC_CAP_SPIRAM);  //内存释放heap_caps_free(str); 

#include <Arduino.h>
#include <esp_heap_caps.h>void setup() {Serial.begin(115200);Serial.printf("内部RAM可使用大小: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));char* str=(char *)heap_caps_malloc(1000,MALLOC_CAP_INTERNAL);sprintf(str,"hello world! I am a handsome boy!");Serial.println(str);Serial.printf("内部RAM可使用大小: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));Serial.printf("外部RAM可使用大小: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));char* str1=(char *)heap_caps_malloc(1000,MALLOC_CAP_SPIRAM);sprintf(str1,"hello world! I am a pretty girl!");Serial.println(str1);Serial.printf("外部RAM可使用大小: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));Serial.println("释放内存!");heap_caps_free(str); heap_caps_free(str1); Serial.printf("内部RAM可使用大小: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));Serial.printf("外部RAM可使用大小: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));}void loop() {delay(10); 
}

串口输出结果

http://www.xdnf.cn/news/3525.html

相关文章:

  • 数字智慧方案5846丨智慧广场整体解决方案(91页PPT)(文末有下载方式)
  • mindyolo填坑
  • 应用接入Stripe支付实战【2025版+配置+服务端+客户端+生产级+架构图+代码】
  • 表管理(约束)实验
  • C语言与指针3——基本数据类型
  • Learning vtkjs之TubeFilter
  • TMI投稿指南(四):投稿相关网址
  • 【Linux】Linux基础命令
  • 27.电源和地的单点串并联接线隐患及对EMC的影响分析
  • 数字智慧方案6206丨智慧园区大数据整体解决方案(45页PPT)(文末有下载方式)
  • HDLBIT-程序(Procedures)
  • 【原创开发】无印去水印[特殊字符]短视频去水印工具[特殊字符]支持一键批量解析
  • CloudCompare 中的 KDTree详解
  • 设计模式简述(十六)门面模式
  • DeepSeek构建非农预测模型:量化关税滞后效应与非线性经济冲击传导
  • cPanel 的 Let’s Encrypt™ 插件
  • 平台介绍-开放API接口-鉴权
  • 【C到Java的深度跃迁:从指针到对象,从过程到生态】第五模块·生态征服篇 —— 第二十章 项目实战:从C系统到Java架构的蜕变
  • MATLAB滤波工具箱演示——自定义维度、滤波方法的例程演示与绘图、数据输出
  • 详细说明StandardCopyOption.REPLACE_EXISTING参数的作用和使用方法
  • 虚幻引擎 IK Retargeter 编辑器界面解析
  • 上位机知识篇---PSRAM和RAM
  • 从零开始讲DDR(9)——AXI 接口MIG 使用(2)
  • n8n 键盘快捷键和控制键
  • 基于YOLOV5的目标检测识别
  • Expected SARSA算法详解:python 从零实现
  • 输入输出(python)
  • BBR 之 ProbeRTT 新改
  • DeepSeek-R1模型蒸馏
  • SALOME源码分析: ParaVis