About expansion to RAM1

Shun

Member
HI all,

The error occurs in compilation project.
擷取.JPG

How to expansion to RAM1 ?

What is the difference between RAM1 and RAM2? Can they be used with each other?

Thanks.
 
I assume you've already read this ?
And after reading that seeing the RAM 1: padding:27192 for this build says if 6KB of code moved to FLASHMEM that would free up 32KB of variable space in RAM1, that would overcome the shown 4.4K deficit - at least until the code grows. Also if there is const DATA that could be kept in flash with PROGMEM - and if that might be too slow when the shared data cache gets exhausted - it could be dynamically moved to RAM2 for reference perhaps.

300KB of code might offer some decent savings for lesser used code executed from Flash.

Also RAM2 has 500KB of RAM available that (when not covered by the 32KB data cache) will execute at 1/4 the speed of RAM1 but over twice the RAM resource currently in use for lesser used data that doesn't need to run at full processor speed.
 
As far as I know, both RAM1 and RAM2 are used to store variables. The program code is stored in FLASH.
Is this as I understand it?
 
And after reading that seeing the RAM 1: padding:27192 for this build says if 6KB of code moved to FLASHMEM that would free up 32KB of variable space in RAM1, that would overcome the shown 4.4K deficit - at least until the code grows. Also if there is const DATA that could be kept in flash with PROGMEM - and if that might be too slow when the shared data cache gets exhausted - it could be dynamically moved to RAM2 for reference perhaps.

300KB of code might offer some decent savings for lesser used code executed from Flash.

Also RAM2 has 500KB of RAM available that (when not covered by the 32KB data cache) will execute at 1/4 the speed of RAM1 but over twice the RAM resource currently in use for lesser used data that doesn't need to run at full processor speed.
What is the content of padding ?
 
As far as I know, both RAM1 and RAM2 are used to store variables. The program code is stored in FLASH.
Is this as I understand it?
No. During startup all code that is not marked as FLASHMEM will be copied into RAM1 for maximum performance. Any code that does not have a type is treated as FASTRUN and copied to RAM1
 
What is the content of padding ?
Padding is abandoned space left unused as the CODE/ITCM and DATA/DTCM split is on a 32KB boundary. After the code space is allocated, the data is started on the next 32KB boundary and the 'padding' between is left unused.
But RAM2 just save variables right?
RAM2 cannot execute code and cannot have initialized data. It is available for dynamic data/variable allocation, and using DMAMEM will properly allocate usable 'names' for variable or arrays as expected - but not be given any initial value.

With these notes referring back to https://www.pjrc.com/store/teensy41.html#memory should help with use and understanding.

And as @thebigg notes in p#7 - the stack will build in RAM1 and enough free space must be reserved to allow proper function.
 
The solution is to add FLASHMEM to some of your code, so it doesn't use RAM1, and/or to add DMAMEM to some of your arrays so they are allocated in RAM2 rather than RAM1.

RAM1 is faster than RAM2, and *much* faster than flash. But both RAM2 and flash are cached, and the cache is essentially the same speed as RAM1. For many common uses, the cache makes up most of the performance difference, though there are some exceptions where caches doesn't help much (FIR filters larger with impulse response much longer than the cache are a prime example).

So you would normally choose large functions that aren't speed critical or only run at startup to use FLASHMEM. Likewise, large arrays used as buffers are usually a prime candidate for RAM2. If the buffer needs to be initialized, add code in setup() to clear it, as RAM2 isn't initialized at startup.
 
Not adding anything that hasn't already been said but trying to put it in more learner friendly language:

RAM 1: Holds your code. Any constants. Any global variables and objects. Basically everything that isn't dynamically allocated. The amount listed as free is what is left over and is available for the stack.
The stack is used for variables local to a function and to track where to return to when a function ends. It grows and shrinks as needed as the program runs. If you run out of space for the stack the program crashes. Normally a few kB is enough for the stack but it does depend on what you are doing, some programs have large local variables and so need a lot more stack space.
RAM1 memory is split between code and variables, you have to draw a line telling the system where this split it. Padding is how much memory is lost because you can only set the split to a multiple of 32 k

RAM 2: Used for anything created using new() or malloc()

RAM 1 is the fastest which is why it is used as default.
If you use the macro DMAMEM before a variable it will tell the system to instead put it in RAM 2. (A bit slower, also make sure you set a default, the system won't automatically set it to 0 for you)
If you use the macro FLASHMEM before a constant or function it will tell the system to leave it in the flash. (A lot slower)

While these options are slower the teensy is fast enough that a lot of the time the performance hit doesn't matter and isn't noticeable.

So if you are out of memory in RAM 1 either move some of your larger variables / constants to RAM 2 using the DMAMEM macro or leave some of your larger less used functions in flash using the FLASHMEM macro. Generally anything in your startup code isn't going to be performance critical and is safe to move to flashmem.

One other other option is to change the optimisation option from fast to smallest code. This will significantly reduce the amount of RAM1 used by your code. There is a performance hit for doing this but it will be significantly less than leaving frequently used code in flash memory.
 
It‘s successful to use FLASHMEM to move the startup code to FLASH

If I'm not mistaken. Let's sum up.
Teensy provides three built-in storage spaces: FLASH RAM1 RAM2
RAM1:It can store function and variables. RAM1 has the fastest execution speed. Static allocation keyword is FASTRUN which is also defaulted.
RAM2:It can store variables. These variables can't be initialized your program must write their initial values, if needed. Static allocation keyword is DMAMEM .
FLASH: It can store function and read-only variables. FLASH is slower than RAM1 should be used on startup code and other functions where speed is not important. Static allocation keyword is FLASHMEM .
 
Back
Top