ch32v003fun icon indicating copy to clipboard operation
ch32v003fun copied to clipboard

Solution for using strings in flash instead of RAM

Open karstengit opened this issue 2 years ago • 5 comments
trafficstars

This is a question or feature request and not a bug.

I just started to build my first project with a CH32V003F4P6 that uses an 4x10 character LCD display. So it is needed to handle strings with text of course. (The missing strcat function is simply added to the source.) When you are working with much strings you are running out of RAM at a certain point.

Is there a possibility to do something like the F() macro in Arduino or or the PSTR() macro in the avr-gcc? At this time I could not find out how this macros have been implemented - only that it has to do with PROGMEM.

I could find in .../hardware/arduino/avr/cores/arduino/WString.h

class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

and in .../hardware/tools/avr/avr/include/avr/pgmspace.h

    Used to declare a static pointer to a string in program space. */
# define PSTR(s) ((const PROGMEM char *)(s))

that can be read by

    Read a byte from the program space with a 16-bit (near) address. 
    \note The address is a byte address.
    The address is in the program space. */

#define pgm_read_byte_near(address_short) __LPM((uint16_t)(address_short))

Has someone already an solution for this compiler for it?

karstengit avatar Aug 08 '23 15:08 karstengit

Isn't it just as simple as making the strings const and they will end up in the text segment instead of data ?

char foo1[]="ABCDEFGHIJABCD"; vs const char foo2[]="ABCDEFGHIJABCD";

mengstr avatar Aug 08 '23 15:08 mengstr

Hmm - thanks - this must be tested. At this time the data section is given out with 0 after compilation.

karstengit avatar Aug 08 '23 15:08 karstengit

The reason for AVR having to do special things to get strings into the flash is because it's a Harvard architecture where the data and code memories are totally separated. There's a special instruction to read from code memory into a register.

The RISCV is Von Neumann where code and data (and in this case also IO/Peripherals) shares a single memory address area. So you don't need to jump through hoops to get the desired RAM savings.

mengstr avatar Aug 08 '23 15:08 mengstr

Thank you for the explanation!

karstengit avatar Aug 08 '23 17:08 karstengit

ticket can be closed.

mrx23dot avatar Oct 07 '23 18:10 mrx23dot