esp32-S3模塊內(nèi)部的存儲分為ROM,RAM,SPRAM,RTC內(nèi)存,F(xiàn)LASH,種類很多,幾乎可以不使用外接存儲器的情況下,可以進行很多業(yè)務場景,十分有用?,F(xiàn)在我們逐一講解一下他們的作用和使用方法。
一、ROM
384 KB 內(nèi)部 ROM,
作用:ESP32技術手冊明確說明:Internal ROM 是只讀存儲器,不可編程。Internal ROM 中存放有一些系統(tǒng)底層軟件的 ROM 代碼(程序指令和一些只讀數(shù)據(jù))。程序無法修改,暫不討論。
關于ROM注意的是,全局的const變量和字符串常量通常會存放在只讀數(shù)據(jù)區(qū)(.rodata),有一些硬件芯片會將這個數(shù)據(jù)區(qū)放在ROM里面,但是ESP32將數(shù)據(jù)區(qū)放置在RAM,區(qū)分清楚就好。
二、RAM
種類:片內(nèi)SRAM +片外PSRAM
(一)片內(nèi)SRAM:
大小:512 KB
關系:片內(nèi)SRAM = IRAM (192KB ) + DRAM( 328KB )
IRAM :
作用:存儲關鍵代碼。
1.中斷處理程序。注冊中斷處理程序時使用了 ESP_INTR_FLAG_IRAM,則中斷處理程序必須要放入 IRAM。
2.可將一些時序關鍵代碼放入 IRAM,以減少從 flash 中加載代碼造成的相關損失。
3.以上兩點為鏈接器自主操作,官方也提供接口,可以通過程序來聲明函數(shù),可以將 IRAM_ATTR 宏用作屬性,直接將程序存儲在這部分空間。(非芯片核心開發(fā)人員不推薦使用,容易觸發(fā)芯片系統(tǒng)安全機制)
void IRAM_ATTR gpio_isr_handler(void* arg)
{
const static DRAM_ATTR uint8_t INDEX_DATA[] = { 45, 33, 12, 0 };
const static char *MSG = DRAM_STR("I am a string stored in RAM");
}
函數(shù)中的字符串或常量可能沒有自動放入 RAM 中,這時可以使用 DRAM_ATTR 屬性進行標記,或者也可以使用鏈接器腳本方法將它們自動放入 RAM 中。
DRAM:
作用兩個:
- 非常量靜態(tài)數(shù)據(jù)(.data 段)和零初始化數(shù)據(jù)(.bss 段)由鏈接器放入內(nèi)部 SRAM 作為數(shù)據(jù)存儲。
這部分空間的使用是由鏈接器在對項目代碼編譯鏈接時,根據(jù)程序內(nèi)容自動使用??臻g大小隨程序內(nèi)部使用情況擴張。 - 官方也提供接口,可以通過程序聲明變量,直接使用這部分空間??梢詫?__NOINIT_ATTR 宏用作屬性,從而將數(shù)據(jù)放入 .noinit 部分。放入該部分的值在啟動時不會被初始化,在軟件重啟后也會保持值不變。
__NOINIT_ATTR uint32_t noinit_data;
- 此區(qū)域中的剩余空間可在程序運行時用作堆,也就是內(nèi)部堆。
查看內(nèi)部堆大小的函數(shù):
printf("sp_get_free_internal_heap_size = %ld\n\r", esp_get_free_internal_heap_size());
一般打印完,發(fā)現(xiàn)內(nèi)部堆大小只有200多KB,因為實際的內(nèi)部堆大小只有RAM中的DRAM中的一部分,其最主要的作用是負責程序的運行空間,例如一般創(chuàng)建線程時聲明的任務棧大小所需的空間就是直接從內(nèi)部堆里面分配出去的。創(chuàng)建20k大小的線程,對應的內(nèi)部堆就減少20k空間。
xTaskCreate((TaskFunction_t )task,"task", 20*1024, (void* )NULL,20, (TaskHandle_t* )&ITask_Handler);
(二)片外PSRAM:
具體大小可根據(jù)使用業(yè)務進行配置,通常:2-8M;
作用:提供更多空間,方便數(shù)據(jù)的存儲和使用。
配置使用:
1.開啟:使能Support for external,SPI-connected RAM
2.根據(jù)實際外置的PSRAM類型來選擇:Quad 或者Octal
3.選擇程序申請堆的API接口:可以選擇2或者3。
(上圖為2.9.1版ESP-IDE,部分舊版編輯器將此項類型ESPS3特殊設置或者ESP系統(tǒng)設置里面)
選型2:使用heap_caps_malloc()申請堆,靈活配置申請。
char *data=(char *) heap_caps_malloc(1024*sizeof(char), MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM);
配置項的意義:
/**
* @brief Flags to indicate the capabilities of the various memory systems
*/
#define MALLOC_CAP_EXEC (1<<0) ///< Memory must be able to run executable code
#define MALLOC_CAP_32BIT (1<<1) ///< Memory must allow for aligned 32-bit data accesses
#define MALLOC_CAP_8BIT (1<<2) ///< Memory must allow for 8/16/...-bit data accesses
#define MALLOC_CAP_DMA (1<<3) ///< Memory must be able to accessed by DMA
#define MALLOC_CAP_PID2 (1<<4) ///< Memory must be mapped to PID2 memory space (PIDs are not currently used)
#define MALLOC_CAP_PID3 (1<<5) ///< Memory must be mapped to PID3 memory space (PIDs are not currently used)
#define MALLOC_CAP_PID4 (1<<6) ///< Memory must be mapped to PID4 memory space (PIDs are not currently used)
#define MALLOC_CAP_PID5 (1<<7) ///< Memory must be mapped to PID5 memory space (PIDs are not currently used)
#define MALLOC_CAP_PID6 (1<<8) ///< Memory must be mapped to PID6 memory space (PIDs are not currently used)
#define MALLOC_CAP_PID7 (1<<9) ///< Memory must be mapped to PID7 memory space (PIDs are not currently used)
#define MALLOC_CAP_SPIRAM (1<<10) ///< Memory must be in SPI RAM
#define MALLOC_CAP_INTERNAL (1<<11) ///< Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off
#define MALLOC_CAP_DEFAULT (1<<12) ///< Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(), calloc()) call
#define MALLOC_CAP_IRAM_8BIT (1<<13) ///< Memory must be in IRAM and allow unaligned access
#define MALLOC_CAP_RETENTION (1<<14) ///< Memory must be able to accessed by retention DMA
#define MALLOC_CAP_RTCRAM (1<<15) ///< Memory must be in RTC fast memory
#define MALLOC_CAP_INVALID (1<<31) ///< Memory can't be used / list end marker
選型3:使用malloc方式申請最直接,默認使用malloc時向外部推申請空間。
char *data=(char *)malloc(1024*sizeof(char));
另外,也可以通過API接口查看外部堆的剩余大?。?/p>
printf(".esp_get_free_heap_size = %d\n\r", esp_get_free_heap_size());
這次分享到處結(jié)束,下次分享給大家講講Cache、片上的flash和RTC存儲器用法,覺得有幫助可以點個贊,謝謝大家。
參考:文章來源:http://www.zghlxwxcb.cn/news/detail-554770.html
https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32s3/api-guides/memory-types.html#iram-ram
https://www.espressif.com.cn/sites/default/files/documentation/esp32-s3_technical_reference_manual_cn.pdf
https://blog.csdn.net/qq_41741344/article/details/116380816文章來源地址http://www.zghlxwxcb.cn/news/detail-554770.html
到了這里,關于esp32-S3專題二:內(nèi)存1之RAM使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!