esp32 无法申请PSRAM

霜霜code
Posts: 2
Joined: Mon Nov 04, 2024 9:13 am

esp32 无法申请PSRAM

Postby 霜霜code » Mon Nov 04, 2024 9:30 am

if (esp_spiram_test()){
mySerial.println("esp_spiram_test() ok");
}
//成功返回testok
heap_caps_get_free_size(MALLOC_CAP_8BIT)
heap_caps_get_free_size(MALLOC_CAP_SPIRAM)
//两个函数都能同时获取到 PSram的空间大小
//但在通过函数申请psram芯片所提供的空间时候,发生重启
//目前看来 不管在代码何处执行申请芯片所提供的代码空间时,都会导致代码重启
(uint8_t *)heap_caps_calloc(pipeiwidth * pipeiheight, sizeof(uint8_t), MALLOC_CAP_SPIRAM);



再次测试 使用如下测试代码
#include <Arduino.h>
#include <esp_system.h>
#include <esp_spiram.h>


// 假设要分配的内存大小为1024字节
#define MEMORY_SIZE 1024

// 串口初始化(使用Serial对象)
void initSerial() {
Serial.begin(115200);
while (!Serial) {
// 等待串口连接
}
}

void setup() {
initSerial();

// 测试PSRAM
if (esp_spiram_test()) {
Serial.println("esp_spiram_test() ok");
} else {
Serial.println("PSRAM test failed!");
while (1); // 停止执行,如果PSRAM测试失败
}
delay(3000);
size_t max_free_size = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
printf("Maximum free heap size (internal RAM): %zu bytes\n", max_free_size);
// 获取并输出PSRAM大小
int psram_size = esp_spiram_get_size();
Serial.printf("PSRAM size: %d bytes\n", psram_size);

// 输出内部堆、SPIRAM等内存区域的空闲大小
Serial.println("Internal free size (8-bit):");
Serial.printf("%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT));
Serial.println("SPIRAM free size:");
Serial.printf("%d\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
Serial.println("Default free size:");
Serial.printf("%d\n", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));

// 输出复位原因
esp_reset_reason_t reset_reason = esp_reset_reason();
Serial.println("Reset reason:");
switch (reset_reason) {
// ... (复位原因的输出代码与之前相同)
}

// 使用heap_caps_calloc从PSRAM中分配内存
uint8_t* psramMemory = (uint8_t*)heap_caps_calloc(MEMORY_SIZE, sizeof(uint8_t), MALLOC_CAP_SPIRAM);
if (psramMemory == NULL) {
Serial.println("Failed to allocate memory from SPIRAM!");
return;
}

// 初始化内存内容
for (int i = 0; i < MEMORY_SIZE; i++) {
psramMemory = 'A' + (i % 26); // 循环写入A-Z
}

// 输出分配内存后的SPIRAM空闲大小
Serial.printf("SPIRAM free size after allocation: %d\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));

// 通过串口输出内存内容的一部分
Serial.print("Memory content: ");
for (int i = 0; i < 64 && i < MEMORY_SIZE; i++) {
Serial.print((char)psramMemory);
}
Serial.println();

// 释放内存
heap_caps_free(psramMemory); // 使用heap_caps_free来释放由heap_caps_calloc分配的内存

// 输出释放内存后的SPIRAM空闲大小
Serial.printf("SPIRAM free size after release: %d\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
}

void loop() {

}


出现以下情况

[2024-11-01 22:49:56.369]
RX:esp_spiram_test() ok

[2024-11-01 22:49:59.586]
RX:Maximum free heap size (internal RAM): 340996 bytes
PSRAM size: 8388608 bytes
Internal free size (8-bit):
4470923
SPIRAM free size:
4192123
Default free size:
4470923
Reset reason:
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.

Core 1 register dump:
PC : 0x4008c0ac PS : 0x00060a33 A0 : 0x8008ddc0 A1 : 0x3ffb21a0
A2 : 0xaaaaaaaa A3 : 0xb33fffff A4 : 0x0000cdcd A5 : 0x00060a23
A6 : 0x00060a20 A7 : 0x0000abab A8 : 0x0000abab A9 : 0xffffffff
A10 : 0x00000001 A11 : 0x00000000 A12 : 0x00000000 A13 : 0x00000000
A14 : 0x6b2aaaaa A15 : 0x003fffff SAR : 0x00000004 EXCCAUSE: 0x0000001d
EXCVADDR: 0xaaaaaaaa LBEG : 0x40087695 LEND : 0x400876a5 LCOUNT : 0xffffffff


Backtrace: 0x4008c0a9:0x3ffb21a0 0x4008ddbd:0x3ffb21e0 0x4008df25:0x3ffb2200 0x4008392c:0x3ffb2220 0x40083b5b:0x3ffb2250 0x400d1585:0x3ffb2270 0x400d35b6:0x3ffb2290




ELF file SHA256: 4d5f31689b7db5de

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4


再次测试
手写如下程序
bool esp_spiram_test1()
{
volatile int *spiram=(volatile int*)SOC_EXTRAM_DATA_LOW;
size_t p;
size_t s=heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
int errct=0;
int initial_err=-1;
for (p=0; p<(s/sizeof(int)); p+=8) {
spiram[p]=p^0xAAAAAAAA;
}
for (p=0; p<(s/sizeof(int)); p+=8) {
if (spiram[p]!=(p^0xAAAAAAAA)) {
errct++;
if (errct==1) initial_err=p*4;
}
}
if (errct) {
Serial.printf( "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, s/32, initial_err+SOC_EXTRAM_DATA_LOW);
return false;
} else {
Serial.printf( "SPI SRAM memory test OK");
return true;
}
}
可以执行 但是
一旦进行malloc申请 都会导致重启 报错内容和上文相同

Who is online

Users browsing this forum: No registered users and 30 guests