Determine chip's flash memory size at compile time

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Determine chip's flash memory size at compile time

3,333 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Fri Nov 04 13:20:36 MST 2011
Hi!
I need to determine chip's memory size at the compile time to know how much of that flash memory I can use to store user's data (we can use different chips in production and it's bad thing to edit sources for each of them). Is there special "built-in" symbol defined to check it and set constants in program with appropriate values ("size of available flash") ?
Sorry if this is stupid question and thx in advance :)
0 Kudos
Reply
15 Replies

3,296 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Tue Nov 08 00:14:03 MST 2011
Thank you!
I'm inattentive, of course for 32K parts this var is MFLash32 and for 512K part which I have this is MFlash512
0 Kudos
Reply

3,296 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Mon Nov 07 04:57:00 MST 2011
There is no current way of determining this automatically, from code. The problem is that the name of the symbol for the top of flash is not consistent. It is Mflash32 for 32k parts and Mflash512 for 512k parts, etc.

I suggest that you use the BuildMacros
http://support.code-red-tech.com/CodeRedWiki/BuildMacros

and then conditionally compile your code to define the flash size for that part.
0 Kudos
Reply

3,296 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Mon Nov 07 01:54:26 MST 2011
atomicdog, thank you, I tried your project, but I'm using LPC1768 and I changed settings for it: CMSIS (includes and lib path), MCU type and startup file from empty LPC17 project.
This is what I get:
**** Build of configuration Debug for project ld_var_test ****

make all 
Building file: ../src/cr_startup_lpc176x.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_CMSIS=CMSISv2p00_LPC11Uxx -I"/home/flysnake/Documents/Projects/LPC4Workspace/CMSISv2p00_LPC17xx/inc" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m3 -mthumb -MMD -MP -MF"src/cr_startup_lpc176x.d" -MT"src/cr_startup_lpc176x.d" -o"src/cr_startup_lpc176x.o" "../src/cr_startup_lpc176x.c"
./src/main.o:(.rodata.flash+0x0): undefined reference to `__top_MFlash32'
Finished building: ../src/cr_startup_lpc176x.c
 
Building file: ../src/main.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_CMSIS=CMSISv2p00_LPC11Uxx -I"/home/flysnake/Documents/Projects/LPC4Workspace/CMSISv2p00_LPC17xx/inc" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m3 -mthumb -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.c"
collect2: ld returned 1 exit status
make: *** [ld_var_test.axf] Error 1
Finished building: ../src/main.c
Looks like the same error: undefined reference to `__top_MFlash32'
__top_MFlash512 is present in map file:

Quote:
                0x00080000                __top_MFlash512 = 0x80000

Here is my test 'vanilla' project for LPC1768. Required CMSISv2p00_LPC17xx in workspace
0 Kudos
Reply

3,296 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Sun Nov 06 16:06:49 MST 2011
Flysnake, please post your project so we can see what you are doing wrong.
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by atomicdog on Sun Nov 06 11:30:39 MST 2011
Maybe there is a difference in the default project setting for Linux and Windows?
I attached a test (CMSIS-2/RedLib/Semihost) project zip. I'm not sure if Linux/LPCxpresso will import a project from windows/LPCxpresso  though. This feature has been around for a long time so the version number shouldn't matter.
Is the __top_MFlash32 symbol in the disassembly or map file?
#include <stdio.h>
#include <stdint.h>

extern size_t __top_MFlash32;
const size_t flash = (size_t)&__top_MFlash32;

int main(void) {
    
  printf("Hello World: %d\n",flash);
    
    // Enter an infinite loop, just incrementing a counter
    volatile static int i = 0 ;
    while(1) {
        i++ ;
    }
    return 0 ;
}
**** Build of configuration Debug for project ld_var_test ****

make all 
Building file: ../src/cr_startup_lpc11u.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_CMSIS=CMSISv2p00_LPC11Uxx -I"C:\Users\John\Documents\LPCXpresso_4.0.5_123\workspace\CMSISv2p00_LPC11Uxx\inc" -O0 -Os -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m0 -mthumb -MMD -MP -MF"src/cr_startup_lpc11u.d" -MT"src/cr_startup_lpc11u.d" -o"src/cr_startup_lpc11u.o" "../src/cr_startup_lpc11u.c"
Finished building: ../src/cr_startup_lpc11u.c
 
Building file: ../src/main.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_CMSIS=CMSISv2p00_LPC11Uxx -I"C:\Users\John\Documents\LPCXpresso_4.0.5_123\workspace\CMSISv2p00_LPC11Uxx\inc" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m0 -mthumb -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.c"
Finished building: ../src/main.c
 
Building target: ld_var_test.axf
Invoking: MCU Linker
arm-none-eabi-gcc -nostdlib -L"C:\Users\John\Documents\LPCXpresso_4.0.5_123\workspace\CMSISv2p00_LPC11Uxx\Debug" -Xlinker -Map=ld_var_test.map -Xlinker --gc-sections -mcpu=cortex-m0 -mthumb -T "ld_var_test_Debug.ld" -o"ld_var_test.axf"  ./src/cr_startup_lpc11u.o ./src/main.o   -lCMSISv2p00_LPC11Uxx
Finished building target: ld_var_test.axf
 
make --no-print-directory post-build
Performing post-build steps
arm-none-eabi-size ld_var_test.axf; # arm-none-eabi-objcopy -O binary ld_var_test.axf ld_var_test.bin ; checksum -p LPC11U14_201 -d ld_var_test.bin;
   text       data        bss        dec        hex    filename
  12136          0        340      12476       30bc    ld_var_test.axf
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Sun Nov 06 05:31:42 MST 2011
./src/main.o:(.rodata.flash+0x0): undefined reference to `__top_MFlash32'
collect2: ld returned 1 exit status
I tried to create such new empty project too. CMSIS 2.00 and Redlib (semihost), no additional settings.

Quote:
/opt/lpcxpresso_4.0.6_131/lpcxpresso/tools/arm-none-eabi/bin/ld --version
GNU ld (Red Suite 2010Q4 by Code Red) 2.20.51.20100809


Quote:
/opt/lpcxpresso_4.0.6_131/lpcxpresso/tools/arm-none-eabi/bin/gcc --version
gcc (Red Suite 2010Q4 by Code Red) 4.5.1

0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by atomicdog on Sat Nov 05 15:07:46 MST 2011

Quote: FlySnake
Yes, it's there. Since LPCXpresso generate ld scripts on-the-fly I tried to disable "Manage linker script" and add these generated scripts manually - nothing changed.


I tried it on a basic/default test program and it works fine. I didn't change any linker options. what error do you get when you use the extern declaration?

Quote: FlySnake
But, 1 question: should the compiler see linker's variable at all?

It's a feature of the GNU linker (ld). All the compiler sees is the extern declaration and so it then relies on the linker to find that symbol.

extern size_t __top_MFlash32;
const size_t flash = (size_t)&__top_MFlash32;

int main(void) {
    
    printf("Hello World: %d\n",flash);
    
    //uintptr_t *ptr = &__top_MFlash32;
    // Enter an infinite loop, just incrementing a counter
    volatile static int i = 0 ;
    while(1) {
        i++ ;
    }
    return 0 ;
}
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Sat Nov 05 13:58:52 MST 2011
Yes, it's there. Since LPCXpresso generate ld scripts on-the-fly I tried to disable "Manage linker script" and add these generated scripts manually - nothing changed. But, 1 question: should the compiler see linker's variable at all?
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by atomicdog on Sat Nov 05 13:10:41 MST 2011

Quote: FlySnake
It still doesn't work. Switching the library type (semihost/nohost) doesn't help.

Moreover I cannot find where this variable defined (recursive search text in lpcxpreso install dir)


It's defined in the *[I]_mem.ld[/I] linker script.
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Sat Nov 05 12:51:14 MST 2011
It still doesn't work. Switching the library type (semihost/nohost) doesn't help.

Quote:
undefined reference to `__top_MFlash32'


Moreover I cannot find where this variable defined (recursive search text in lpcxpreso install dir)
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by atomicdog on Sat Nov 05 10:39:31 MST 2011

Quote: FlySnake
It doesn't work


Sorry, I forget you need to declare it also.

extern uint32_t __top_MFlash32;

const uint32_t top = (uint32_t)&__top_MFlash32;
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Sat Nov 05 10:17:21 MST 2011
It doesn't work :(

Quote:
error: '__top_MFlash32' undeclared (first use in this function)

0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by atomicdog on Fri Nov 04 19:09:22 MST 2011
I believe you can use the linker variable [B][I]__top_MFlash32[/I][/B] in your code by taking its address.

&[B][I]__top_MFlash32[/I][/B]
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by FlySnake on Fri Nov 04 14:29:56 MST 2011

Quote: Zero
Are we talking about Flash/RAM consumption?
http://support.code-red-tech.com/CodeRedWiki/FlashRamSize


No, I want something like this:
#define FLASH512

...
 
#if defined (FLASH512)
const uint8_t max_flash_sector=29; // 29 sectors available
#elif defined (FLASH256)
const uint8_t max_flash_sector=21; // 21 sector available
...
...
but without #define FLASH512 and use some other symbol from build options. I can just forget about these manually added macros 3 months later when switch to another chip
0 Kudos
Reply

3,295 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri Nov 04 13:52:15 MST 2011

Quote: FlySnake
...chip's memory size at the compile time

  Are we talking about Flash/RAM consumption?
http://support.code-red-tech.com/CodeRedWiki/FlashRamSize
0 Kudos
Reply