Re: [avr-libc-dev] PSTR() in static initialisers
Joerg Wunsch schrieb: As Georg-Johann Lay wrote: If that static is not function-static, then __flash can be used: Thanks for that, Johann. I tried to come up with a __flash-based implementation myself, but didn't get it to work. Maybe your "recipe" would be worth an entry into the avr-libc FAQ? Dunno if that's actually specified or just works by accident. What's really strange is that it doesn't work for locals, and there is no technical reason for that. https://gcc.gnu.org/PR84163 Maybe it had just been overlooked when ASes were added to gcc, as the first AS targets didn't go as far as avr (iirc they didn't even need statics as it was all about using specific instructions for memory-mapped I/O). FWIF there's also a mention at https://www.mikrocontroller.net/articles/AVR-GCC-Tutorial#Flash_mit_flash_und_Embedded-C Johann ___ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev
Re: [avr-libc-dev] PSTR() in static initialisers
As Georg-Johann Lay wrote: > If that static is not function-static, then __flash can be used: Thanks for that, Johann. I tried to come up with a __flash-based implementation myself, but didn't get it to work. Maybe your "recipe" would be worth an entry into the avr-libc FAQ? -- cheers, Joerg .-.-. --... ...-- -.. . DL8DTL http://www.sax.de/~joerg/ Never trust an operating system you don't have sources for. ;-) ___ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev
Re: [avr-libc-dev] PSTR() in static initialisers
On 31.01.2018 21:15, Paul "LeoNerd" Evans wrote: On Wed, 31 Jan 2018 21:04:55 +0100 Joerg Wunsch wrote: As Paul "LeoNerd" Evans wrote: Does anyone have a suggestion on how I can have a string pointer to a flash-stored string in a static initialiser? This only works by allocating each of the string into a variable (well, constant actually) in flash by itself, and then mention their names in the struct initializer. Hrm; that's upsettingly annoying. Is there perhaps a way this can be convinced into working? I generally dislike the untidiness of having to define lots of things above the main array definition, just so I can pull them in by name into it, and use that name nowhere else. Inline things would be nicer there. If that static is not function-static, then __flash can be used: #if !defined __FLASH || (defined __AVR_PM_BASE_ADDRESS__) // For devices that see flash in RAM address space, resort to vanilla C. #define __flash // empty #endif // have __flash #define FSTR(X) ((const __flash char[]) { X } ) const __flash char* const __flash animals[] = { FSTR ("mite"), FSTR ("slug"), FSTR ("sloth") }; const __flash char flash[] = "flash"; const __flash char *pram_to_flash = FSTR ("pet"); const __flash char * const __flash pflash_to_flash = FSTR ("cat"); const __flash char* ani (int i) { return animals[i]; } Notice that in contrast to progmem, __flash is a qualifier and *not* an attribute. In particular, it matters where it occurs in a declaration, e.g. whether left or right of a "*". gcc will generate accesses as desired, no need for pgm_read macros or inline asm. However gcc rejects FSTR for function-static so that there is no one-fits-all solution: const __flash char* get_pet (void) { static const __flash char *pet = FSTR ("pet"); return pet; } In function 'get_pet': error: compound literal qualified by address-space qualifier return FSTR ("pet"); ^~ Johann ___ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev
Re: [avr-libc-dev] PSTR() in static initialisers
As Paul "LeoNerd" Evans wrote: > Hrm; that's upsettingly annoying. Is there perhaps a way this can be > convinced into working? I could not find a way. You want to initialize a flash pointer, and for that to work, you need an (array) object already in flash. A string literal defaults to end up in RAM. -- cheers, Joerg .-.-. --... ...-- -.. . DL8DTL http://www.sax.de/~joerg/ Never trust an operating system you don't have sources for. ;-) ___ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev
Re: [avr-libc-dev] PSTR() in static initialisers
On Wed, 31 Jan 2018 21:04:55 +0100 Joerg Wunsch wrote: > As Paul "LeoNerd" Evans wrote: > > > Does anyone have a suggestion on how I can have a string pointer to > > a flash-stored string in a static initialiser? > > This only works by allocating each of the string into a variable > (well, constant actually) in flash by itself, and then mention their > names in the struct initializer. Hrm; that's upsettingly annoying. Is there perhaps a way this can be convinced into working? I generally dislike the untidiness of having to define lots of things above the main array definition, just so I can pull them in by name into it, and use that name nowhere else. Inline things would be nicer there. -- Paul "LeoNerd" Evans leon...@leonerd.org.uk | https://metacpan.org/author/PEVANS http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/ pgpqh6gJQEPEF.pgp Description: OpenPGP digital signature ___ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev
Re: [avr-libc-dev] PSTR() in static initialisers
As Paul "LeoNerd" Evans wrote: > Does anyone have a suggestion on how I can have a string pointer to a > flash-stored string in a static initialiser? This only works by allocating each of the string into a variable (well, constant actually) in flash by itself, and then mention their names in the struct initializer. -- cheers, Joerg .-.-. --... ...-- -.. . DL8DTL http://www.sax.de/~joerg/ Never trust an operating system you don't have sources for. ;-) ___ AVR-libc-dev mailing list AVR-libc-dev@nongnu.org https://lists.nongnu.org/mailman/listinfo/avr-libc-dev