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

一,基本介绍

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

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

二,平台配置

2.1 Arduino平台

2.2 Platform IO

cpp 复制代码
; 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

cpp 复制代码
#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

cpp 复制代码
 //从芯片内部申请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); 
cpp 复制代码
#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); 
}

串口输出结果

相关推荐
许白掰1 小时前
Linux入门篇学习——Linux 编写第一个自己的命令
linux·运维·数据库·嵌入式硬件·学习
liuluyang5301 小时前
linux 4.14 kernel屏蔽arm arch timer的方法
嵌入式硬件·arm·arch_timer·coretime
书山有路勤为径~2 小时前
3 STM32单片机-delay延时驱动
stm32·单片机
C语言小火车2 小时前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针
凤年徐2 小时前
【数据结构】时间复杂度和空间复杂度
c语言·数据结构·c++·笔记·算法
鑫宇吖2 小时前
Polyspace作为MISRA-C合规性检查工具,其检查规则会根据目标C语言标准(C90或C99)动态调整限值要求。
c语言·嵌入式·c99·c90·polyspace·misra-c合规性检查
吃货界的硬件攻城狮3 小时前
【显示模块】嵌入式显示与触摸屏技术理论
stm32·单片机·嵌入式硬件·学习
钮钴禄·爱因斯晨3 小时前
C语言 | 函数核心机制深度解构:从底层架构到工程化实践
c语言·开发语言·数据结构
爱学习的小邓同学4 小时前
数据结构 --- 队列
c语言·数据结构
啟明起鸣10 小时前
【网络编程】简易的 p2p 模型,实现两台虚拟机之间的简单点对点通信,并以小见大观察 TCP 协议的具体运行
c语言·网络·tcp/ip·p2p