Page 1 of 1

use (swap) static IRAM for static DRAM

Posted: Wed Apr 18, 2018 2:11 pm
by davdav
Hi everybody,

If I ask "make size" for my project I have the following:

Code: Select all

Total sizes:
 DRAM .data size:   16444 bytes
 DRAM .bss  size:   74192 bytes
Used static DRAM:   90636 bytes (  24564 available, 78.7% used)
Used static IRAM:   85692 bytes (  45380 available, 65.4% used)
      Flash code:  747670 bytes
    Flash rodata:  198844 bytes
Total image size:~1048650 bytes (.bin may be padded larger)
I would like to use some of the static IRAM for DRAM. How can I do this? Or, is there a pragma for the compiler to inform that a variable (not a function) should be allocated in IRAM segment?

Thanks

Re: use (swap) static IRAM for static DRAM

Posted: Thu Apr 19, 2018 12:25 am
by kolban
If we look at table 3 in the ESP32 Technical Reference Manual we see that there appears to be 128K pre-allocated to instruction ram (IRAM) between address range 0x4008 0000 and 0x4009 FFFF ... known as SRAM 0. If I were to guess, I'd say that this 128K is fixed for the purpose of instruction RAM with no obvious options to make it available in the DRAM address space.

Re: use (swap) static IRAM for static DRAM

Posted: Thu Apr 19, 2018 5:42 am
by ESP_Angus
Hi dav,

The IRAM_ATTR attribute which places code into IRAM can also be used to place static variables in IRAM:
http://esp-idf.readthedocs.io/en/latest ... uction-ram

You can also dynamically allocate from IRAM by using function calls like heap_caps_malloc(MALLOC_CAP_32BIT, size). More details here:
http://esp-idf.readthedocs.io/en/latest ... ecial-uses

(Note that all accesses to/from data in IRAM has to be 32-bit aligned word reads/writes or your program will crash with a fatal exception.)

[SOLVED] Re: use (swap) static IRAM for static DRAM

Posted: Thu Apr 19, 2018 10:17 am
by davdav
Thanks @Kolban and @ESP_Angus.


Static allocation with IRAM_ATTR works perfectly.

I need to figure out how to use dynamic allocation in particular the 32-bit access part in case I have "non-32 bit variable"

Anyway the question is solved.

Thanks

Posted: Thu Apr 19, 2018 2:38 pm
by davdav
Sorry @ESP_Angus, probably I didn't understand last part of your post
(Note that all accesses to/from data in IRAM has to be 32-bit aligned word reads/writes or your program will crash with a fatal exception.)
Is it referred only to dynamic allocation or is it true also for static variables?

I have a variable defined as:

Code: Select all

IRAM_ATTR MyType_struct Config;
where "MyType_struct" is a structure containg char, int, ecc...

Code: Select all

typedef struct {
    	uint8_t	active;
    	char	master[16];
    	char	slave2[16];
    	char	slave3[16];
    	uint16_t slave_tag;
    }MyType_struct ;
As soon as I access the variable like

Code: Select all

 memset(Config.master, 0x0, sizeof(Config.master));
I have a crash (I guess as you wrote)

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadStoreError)
How can I access with 32-bit aligned?

Thanks

Re: use (swap) static IRAM for static DRAM

Posted: Thu Apr 19, 2018 2:51 pm
by kolban
My loose thinking is that you should think of each memory location as having an address ... for example:

A0 A1 A2 A3 A4 .....

For the IRAM data, you can only read and write data at addresses that are evenly dividable by 4 for example ...

0 4 8 12 16 20 ...

If I understand correctly, data on the instruction bus is read in units of 32bits ... 32 bits = 4 bytes. So "logically" instead of thinking of the memory as units of 8 bits, we "could" think of the memory as units of 32bits but still addressed by bytes.

So...
Unit 0 of IRAM can be found at address 0
Unit 1 of IRAM can be found at address 4
Unit 2 of IRAM can be found at address 8
...

If we ask IRAM to retrieve the unit of data found at address 1, 2, or 3 ... this has no meaning ... and an exception is thrown.

(my words are loose here).

Re: use (swap) static IRAM for static DRAM

Posted: Fri Apr 20, 2018 9:03 am
by davdav
Thanks @Kolban, your explanation is correct and I understand that a 32-bit access means that I can only "read 4 bytes at a time".

My problem is "operative": how to do that?

Just to let it simple: If I have a char array

Code: Select all

IRAM_ATTR char Test[10];
How can I write/access it?

-Should I take care to pad it (in practice declare the variable as multiple of 4)?

Code: Select all

IRAM_ATTR char Test[12];
-Should I declare it as an array of uint32_t (waste of memory)?

Code: Select all

IRAM_ATTR uint32_t Test[10];
In practice, is there an example to understand how to manage this case? I have searched on all esp-idf components folder and the only use of IRAM_ATTR with static variable is here:

Code: Select all

\esp\esp-idf\components\driver\test\test_spi_master.c

IRAM_ATTR  static uint32_t data_iram[320];
but it doesn't help so much. Also google didn't give any result.

Can someone provide an example of use a static variable placed in IRAM segment?