An awesome cpp code to analyze memory map of STM8

The memory sections of any microcontroller are -

RAM Memory (RAM) Sections: [ .stack – .heap – .data – .bss ]

  • .stack: This memory section is used for local variables
  • .heap: For dynamically allocated variables (not used in most embedded applications)
  • .data: For global and static initialized variables
  • .bss: For global and static uninitialized variables

Flash Memory (ROM) Sections: [ .sdata – .rodata – .text – .cstartup ]

  • .sdata: For initialized globals, the startup code copies it to .data section at system start
  • .rodata: For const variables (read-only)
  • .text: The code of your application
  • .cstartup: The startup code that initialized the system and branches to the main application

In general, any toolchain will release a memory map file which would contain all this information.

Modern MCU toolchains would give this result at compile time as a message on the window. For STVD, you can generate a memory map as follows :

No alt text provided for this image

A typical STM8 memory map would look like this -

You can find it inside the debug/release folder.

  ? ?                            --------
  ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Segments
  ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?--------
  
  
  start 00008080 end 00008080 length? ? ?0 segment .const
  start 00008083 end 000080fe length? ?123 segment .text
  start 00004000 end 00004000 length? ? ?0 segment .eeprom
  start 00000000 end 00000000 length? ? ?0 segment .bsct
  start 00000000 end 00000007 length? ? ?7 segment .ubsct
  start 00000007 end 00000007 length? ? ?0 segment .bit
  start 00000007 end 00000007 length? ? ?0 segment .share
  start 00000100 end 00000100 length? ? ?0 segment .data
  start 00000100 end 00000100 length? ? ?0 segment .bss
  start 00000000 end 000001ac length? ?428 segment .info.
  start 00000000 end 0000026d length? ?621 segment .debug
  start 00008000 end 00008080 length? ?128 segment .const
  start 00008080 end 00008083 length? ? ?3 segment .init
  
 ........        

However there are some legacy toolchains like STVD and Cosmic C compiler setup that do not provide this information to us.

If you are looking for this information and want a quick hack to analyze the memory map then your search ends here as you have struck gold. :D

All you need is a C/CPP compiler to generate an executable file.

Here is how you can install CPP compiler on windows -

Next you can copy and compile this code to generate an .exe file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#define STR_FLAG ? ?" segment "
#define STR_STACK "Stack size:"



enum
{
? ? TYPE_UNKNOWN,
? ? TYPE_RAM,
? ? TYPE_FLASH,
? ? TYPE_RAM_FLASH,
? ? TYPE_EEPROM,
TYPE_INFO,
TYPE_BIT,
TYPE_DEBUG
};



int get_len(char *pos);
int get_type(char *pos);



int main(int argc, char* argv[])
{
? ? char fn[2048] = "\0";
? ? char *buf = NULL;



? ? for(int i=1;i<argc;i++)
? ? {
? ? ? ? strcat(fn,argv[i]);
? ? }
? ? //
? ? //
? ? //
? ? FILE *fp;



? ? fp = fopen(fn,"r");
? ? if( NULL==fp )
? ? {
? ? ? ? printf("mapinfo error: Can not read \"%s\"",fn);
? ? ? ? return -1;
? ? }
fseek(fp,0,SEEK_END);
long flen = ftell(fp);
buf = (char*)malloc(flen);
if( NULL==buf )
{
printf("mapinfo error: malloc failed!");
? ? ? ? return -1;
}
? ? fseek(fp,0,SEEK_SET);
? ? fread(buf,1,flen,fp);
? ? fclose(fp);
? ? buf[flen-1]='\0';
? ? //
? ? //
? ? //
? ? char *pos = buf;
char *next = buf;
int byte_unknown = 0;
? ? int byte_flash = 0;
? ? int byte_ram = 0;
? ? int byte_ram_flash = 0;
? ? int byte_eeprom = 0;
int byte_stack = 0;
? ? for(;;)
? ? {
? ? ? ? pos = strstr(pos, STR_FLAG);
? ? ? ? if( NULL==pos )
? ? ? ? {
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? int len = get_len(pos);
? ? ? ? pos += sizeof(STR_FLAG);
next = pos;
? ? ? ? int type = get_type(pos);
? ? ? ? //
? ? ? ? //
? ? ? ? //
? ? ? ? switch( type )
? ? ? ? {
case TYPE_UNKNOWN:
byte_unknown += len;
break;
? ? ? ? case TYPE_FLASH:
? ? ? ? ? ? byte_flash += len;
? ? ? ? ? ? break;
? ? ? ? case TYPE_RAM:
? ? ? ? ? ? byte_ram += len;
? ? ? ? ? ? break;
? ? ? ? case TYPE_RAM_FLASH:
? ? ? ? ? ? byte_ram += len;
? ? ? ? ? ? byte_flash += len;
? ? ? ? ? ? break;
? ? ? ? case TYPE_EEPROM:
? ? ? ? ? ? byte_eeprom += len;
? ? ? ? ? ? break;
? ? ? ? }
? ? }
pos = strstr(next,STR_STACK);
if( pos!=NULL )
{
byte_stack = atoi(&pos[sizeof(STR_STACK)]);
}
? ? //
? ? //
? ? //
if( byte_unknown!=0 )
{
printf("\nram:%d ? flash:%d ? eeprom:%d ? stack:%d ? unknown:%d", byte_ram,byte_flash,byte_eeprom,byte_stack,byte_unknown);
}
else
{
printf("\nram:%d ? flash:%d ? eeprom:%d ? stack:%d", byte_ram,byte_flash,byte_eeprom,byte_stack);
}
? ? return 0;
}



int get_len(char *pos)
{
? ? *pos = '\0';
? ? while(*--pos!=' ')
? ? {
? ? }
? ? return atoi(pos+1);
}



#define ? ?STRCMP(str1,str2) ? ?strncmp(str1,str2,sizeof(str2)-1)
int get_type(char *pos)
{
? ? if( 0==STRCMP(pos,"const") )
? ? {
? ? ? ? return TYPE_FLASH;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"text") )
? ? {
? ? ? ? return TYPE_FLASH;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"eeprom") )
? ? {
? ? ? ? return TYPE_EEPROM;
? ? }
else
? ? if( 0==STRCMP(pos,"bsct, initialized") )
? ? {
? ? ? ? return TYPE_BIT;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"bsct, from") )
? ? {
? ? ? ? return TYPE_RAM_FLASH;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"ubsct") )
? ? {
? ? ? ? return TYPE_RAM;
? ? }
else
? ? if( 0==STRCMP(pos,"bit, initialized") )
? ? {
? ? ? ? return TYPE_RAM;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"bit, from") )
? ? {
? ? ? ? return TYPE_RAM_FLASH;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"share") )
? ? {
? ? ? ? return TYPE_RAM;
? ? }
else
? ? if( 0==STRCMP(pos,"data, initialized") )
? ? {
? ? ? ? return TYPE_RAM;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"data, from") )
? ? {
? ? ? ? return TYPE_FLASH;
? ? }
? ? else
? ? if( 0==STRCMP(pos,"bss") )
? ? {
? ? ? ? return TYPE_RAM;
? ? }
else
if( 0==STRCMP(pos,"info.") )
{
return TYPE_INFO;
}
else
if( 0==STRCMP(pos,"debug") )
{
return TYPE_DEBUG;
}
? ? else
? ? if( 0==STRCMP(pos,"init") )
? ? {
? ? ? ? return TYPE_FLASH;
? ? }
? ? return TYPE_UNKNOWN;
}

        

The output would be as follows -

PS C:\Users\user\Desktop> .\mapinfo.exe C:BLAH_BLAH_BLAH\Debug\ramtest.ma


ram:7? ?flash:254? ?eeprom:0? ?stack:10        

Thats it folks! You can now read the analysis.

要查看或添加评论,请登录

Gurajapu Raja Sumant的更多文章

社区洞察

其他会员也浏览了