Recent

Author Topic: Embedded AVR - read strings from Flash  (Read 3319 times)

Timm Thaler

  • New member
  • *
  • Posts: 7
Embedded AVR - read strings from Flash
« on: September 25, 2017, 04:13:01 am »
Programming AVR controllers with Freepascal I try to read strings from flash.

In ASM I put the z-pointer to the string address in flash and read by LPM instruction. In AVR-GCC I use PSTR("MyString") and pgm_read_byte(address) to tell the compiler to read from flash.

In Freepascal I tried with
Code: Text  [Select][+][-]
  1.   buffer : array[0..16] of char;
  2.   buffer := pchar('MyStringMyString');
  3.   for i := 0 to 15 do
  4.     sendchar(buffer[i]);

This runs, I can put strings to the buffer und send the chars.

But the strings are not read from flash but from ram:

Code: ASM  [Select][+][-]
  1.         ldi     r19,lo8(_sGH_DISPLAYs_Ld3)
  2.         ldi     r18,hi8(_sGH_DISPLAYs_Ld3)  <= get ram address
  3.         ldi     r26,17  <= count chars
  4.         mov     r20,r26
  5.         mov     r30,r19
  6.         mov     r31,r18  <= ram address to z-pointer
  7.         ldi     r18,lo8(U_sGH_DEFINE_ss_BUFFER)
  8.         ldi     r19,hi8(U_sGH_DEFINE_ss_BUFFER) <= buffer address
  9.         movw    r26,r18  <= buffer address to x-pointer
  10. .Lj33:
  11.         ld      r0,Z+  <= read from ram
  12.         st      X+,r0  <= write to buffer
  13.         dec     r20
  14.         brne    .Lj33

So I checked the complete hexfile by disassembling and found:

Code: ASM  [Select][+][-]
  1.   72:   a0 e0           ldi     r26, 0x00       ; 0
  2.   74:   b1 e0           ldi     r27, 0x01       ; 1
  3.   76:   d1 e0           ldi     r29, 0x01       ; 1
  4.   78:   ee ee           ldi     r30, 0xEE       ; 238  <= z-pointer to flash address, strings start here
  5.   7a:   f8 e0           ldi     r31, 0x08       ; 8
  6.   7c:   02 c0           rjmp    .+4             ;  0x82
  7.   7e:   05 90           lpm     r0, Z+  <= read chars from flash
  8.   80:   0d 92           st      X+, r0  <= store chars in ram
  9.   82:   a6 34           cpi     r26, 0x46       ; 70
  10.   84:   bd 07           cpc     r27, r29
  11.   86:   d9 f7           brne    .-10            ;  0x7e

So at first all string are copied from flash to ram and then single strings are copied from ram to buffer (also ram). As the ram in the AVR controllers is limited this is no good idea.

Is it possible to tell the compiler to copy strings from flash to buffer directly? Or can I access flash data without copying it to ram?

Sorry, there is not much information about programming AVR controllers with Freepascal out there.
« Last Edit: September 25, 2017, 04:21:14 am by Timm Thaler »

Timm Thaler

  • New member
  • *
  • Posts: 7
Re: Embedded AVR - read strings from Flash
« Reply #1 on: September 25, 2017, 04:16:20 am »
I also tried with strpcopy but this seems not to be available for embedded AVR.

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Embedded AVR - read strings from Flash
« Reply #2 on: September 28, 2017, 03:30:00 pm »
I assume you are referring to the use of __flash or PROGMEM attribute in e.g. GCC?  I suspect that is not yet possible in FPC.

 

TinyPortal © 2005-2018