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); 
}

串口输出结果

相关推荐
CodeWithMe1 小时前
【C/C++】线程状态以及转换
java·c语言·c++
Ma_si3 小时前
PyLink 使用指南
网络·python·嵌入式硬件
岂是尔等觊觎4 小时前
PCB设计教程【入门篇】——电路分析基础-基本元件(二极管三极管场效应管)
经验分享·笔记·嵌入式硬件·学习·pcb工艺
梁下轻语的秋缘4 小时前
每日c/c++题 备战蓝桥杯(洛谷P4715 【深基16.例1】淘汰赛 题解)
c语言·c++·蓝桥杯
梁下轻语的秋缘4 小时前
每日c/c++题 备战蓝桥杯(洛谷P1873 EKO砍树问题详解)
c语言·c++·蓝桥杯
青春不张扬5 小时前
S32K开发环境搭建详细教程(二、添加S32K3xx SDK)
单片机
2401_859049085 小时前
Git使用
arm开发·git·stm32·单片机·mcu·算法
辰哥单片机设计5 小时前
STM32项目分享:智能家居(机智云)升级版
stm32·嵌入式硬件·智能家居
蓝心湄6 小时前
C语言-枚举
c语言·开发语言·算法
2301_800399726 小时前
STM32 USART串口通信
stm32·单片机·嵌入式硬件