Forum > Embedded - AVR
Embedded AVR - read strings from Flash
(1/1)
Timm Thaler:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- buffer : array[0..16] of char; buffer := pchar('MyStringMyString'); for i := 0 to 15 do 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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- ldi r19,lo8(_sGH_DISPLAYs_Ld3) ldi r18,hi8(_sGH_DISPLAYs_Ld3) <= get ram address ldi r26,17 <= count chars mov r20,r26 mov r30,r19 mov r31,r18 <= ram address to z-pointer ldi r18,lo8(U_sGH_DEFINE_ss_BUFFER) ldi r19,hi8(U_sGH_DEFINE_ss_BUFFER) <= buffer address movw r26,r18 <= buffer address to x-pointer.Lj33: ld r0,Z+ <= read from ram st X+,r0 <= write to buffer dec r20 brne .Lj33
So I checked the complete hexfile by disassembling and found:
--- Code: ASM [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 72: a0 e0 ldi r26, 0x00 ; 0 74: b1 e0 ldi r27, 0x01 ; 1 76: d1 e0 ldi r29, 0x01 ; 1 78: ee ee ldi r30, 0xEE ; 238 <= z-pointer to flash address, strings start here 7a: f8 e0 ldi r31, 0x08 ; 8 7c: 02 c0 rjmp .+4 ; 0x82 7e: 05 90 lpm r0, Z+ <= read chars from flash 80: 0d 92 st X+, r0 <= store chars in ram 82: a6 34 cpi r26, 0x46 ; 70 84: bd 07 cpc r27, r29 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.
Timm Thaler:
I also tried with strpcopy but this seems not to be available for embedded AVR.
ccrause:
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.
Navigation
[0] Message Index