cancel
Showing results for 
Search instead for 
Did you mean: 

Strings in RAM and Flash and transfer them to function

sholojda
Associate III

Hi

How can I put strings in fash memory in gcc compiler so that there is no copy in both memories. In 8bit AVR we need to use PROGMEM . How it is in STM32 gcc?

The secong thing. If i have for example such a function void MyFunction( char *Stri(ng); which accepts pointers to string and if i call this function with this parameter MyFunction("String to Pass"); Where this string beeing a parameter of this function will be placed? In Falsh, in RAM or mybe it will be two copies in Ram and Flash?

5 REPLIES 5
Nikita91
Lead II

ARM cortex and AVR have different architecture.

AVR is Harvard: separate memory for data and instructions.

ARM Cortex is von Neumann : only one memory.

So to put a string in Flash you must use the const qualifier:

const char str [] = "abc" ;

If you use :

char str2 [] = "xyz" ;

then the string will be in Flash and copied to RAM by the startup code, before the call to main().

If you use MyFunction("String to Pass"); then the string is const and placed in Flash.

Beware: if you use

MyFunction (str) ;

the compiler will warn about const qualifier, if the function declaration is:

MyFunction(char *) ;

So you must declare:

MyFunction (const char *) ;

Have a nice day.

sholojda
Associate III

Thank you for detailed explanation.

As I understood i can not have one function suport two kind of strings? I mean: do i need to have one function to work with const strings and one with those in ram?

Nikita91
Lead II

You can cast the 'const char *' to 'char *. Not very clean but doable.

MyFunction ((char *) str) ;

But MyFunction cannot modify the string, as it is in Flash.

sholojda
Associate III

Tank you very much for help 😀

RMcCa
Senior II

Welcome to the wonderful world of 32bit ARMs. One of the nice things about stm32s is the flat 32bit ​(4 GB) address space which is, not coincidentally i believe, the same size as the data bus width. The nice thing about this is the ease of using pointers in C as a pointer and uint_32 are essentially the same thing, just used differently by the compiler. There is nothing "unclean" about casting from a const uint_8 * to a uint_8 * as they not really different (except not being able to write directly to flash). It is a formality to keep the compiler quiet. It's incredibly flexible and powerful, a true paradise for a traditional C programmer.