How RAM and Flash actually work?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

How RAM and Flash actually work?

1,371 次查看
dimitrissideris
Contributor III

I am trying to understand the procedure memory handling in LPC mcus.

I am working with an LPCxpresso 4367.

I wanted to store a buffer in flash I tested this code:

#include "board.h"

#define FLASHA_BASE_ADDR 0x1A000000
static int *fb  = (int *)FLASHA_BASE_ADDR;

int main(void)
{
    SystemCoreClockUpdate();
    Board_Init();
    int arsize = 10;

    for (int i = 0; i < arsize; i++)
        {
        fb[i] = 0xA;
      }

    while (1) {
        __WFI();
    }
}

And after adding -print-memory-usage linker option i get this result:


MFlashA512:        7984 B       512 KB      1.52%
RamLoc32:          352 B        32 KB         1.07%

I went through normally without declaring specific address to fb buffer and i tested this code:

#include "board.h"

#define FLASHA_BASE_ADDR 0x1A000000

int main(void)
{
    SystemCoreClockUpdate();
    Board_Init();
    int arsize = 10;
    int fb[arsize];

    for (int i = 0; i < arsize; i++)
        {
        fb[i] = 0xA;
      }


    while (1) {
        __WFI();
    }
}


MFlashA512:        8044 B       512 KB     1.53%
RamLoc32:          348 B        32 KB       1.06%

Can some explain what is going on?

When i put fb in flash, flash occupied space is less.

When i put fb in ram, ram occupied space is less.

4 回复数

990 次查看
converse
Senior Contributor V

Your  2nd example place fb on the stack, so it is 'allocated ' at run time .

in fact your 1st example doesn't place fb in flash either... suggest you look up the meaning of static in C

0 项奖励
回复

990 次查看
dimitrissideris
Contributor III

Did run again the examples without static. Same results

example1:

MFlashA512:        7984 B       512 KB      1.52%
RamLoc32:          352 B        32 KB      1.07%

example2:

MFlashA512:        8044 B       512 KB      1.53%
RamLoc32:          348 B        32 KB      1.06%

As far as example2 is concerned, ok it is allocated on run time. But why flashA has more space occupied?

For example1 the question is if i can store this way a variable or a buffer in Flash memory

0 项奖励
回复

990 次查看
Carlos_Mendoza
NXP Employee
NXP Employee

Hi Dimitris,

In addition to Con Verse information, I think that the following post could also help you understand how data is stored in Flash and RAM:

text, data and bss: Code and Data Size Explained | MCU on Eclipse 


Hope it helps!

Best Regards,
Carlos Mendoza
Technical Support Engineer

0 项奖励
回复

990 次查看
converse
Senior Contributor V

In C "static" defines the scope of a variable - it says nothing about where it is stored. When applied to a variable defined in a module, the variable is only visible within that module. Look up "C storage classes" for more information.

Flash is like ROM - you cannot write to it. So you cannot place a buffer in flash and expect it to work. Flash is for storing stuff that doesn't (or very rarely) changes. Flash can be *programmed* in blocks (sectors, or pages) by using a special algorithm, depending on whose flash is used - in NXP this is achieved by using the IAP calls as described in the User Manual.  For more info, see Flash memory - Wikipedia.