简体   繁体   中英

ESP32 IDF. Will this table be placed in the SPI FLASH and read from it or it will be loaded to the RAM

Question to the ESP32 specialists.

example:

const uint8_t * const data[] = {
    (const uint8_t[]){0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0x87, 0x01, 0x00, 0x95, 0xF7, 0xff, 0xff},
    (const uint8_t[]){0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0xFF, 0x02, 0x00, 0x0E, 0x61, 0xff, 0xff},
    (const uint8_t[]){0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x68, 0xff, 0xff},
    (const uint8_t[]){0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x07, 0x1F, 0x9E, 0xff, 0xff},
    (const uint8_t[]){0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x16, 0x74, 0xff, 0xff},
    (const uint8_t[]){0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x31, 0xff, 0xff},
};

My questions: Is SPI flash mapped into the address space? If yes, will this array (and compound literals) be placed in the FLASH and not copied to the RAM on startup?

Yes.

From the ESP-IDF Programming Guide :

DROM (data stored in flash)

By default, constant data is placed by the linker into a region mapped to the MMU flash cache. This is the same as the IROM (code executed from flash) section, but is for read-only data not executable code.

The only constant data not placed into this memory type by default are literal constants which are embedded by the compiler into application code. These are placed as the surrounding function's executable instructions.

The DRAM_ATTR attribute can be used to force constants from DROM into the DRAM (Data RAM) section (see above).

Using the const modifier in C/C++ code will tell the linker it's safe to leave the data in flash. The ESP32 compiler and linker are smart enough to do this automatically, so there's no need for macros like _F or PROGMEM that Arduino uses.

You can confirm this by building a simple program that declares a very large initialized array. The program will need to do something to the array so that the array doesn't get optimized out. Run it once with the array declared const and have it output the free heap space. Run it again with the array not declared const . The second time you should see a smaller amount of heap space available, with the array using the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM