AVR - Strings direkt aus Flash lesen

Mathias
Beiträge: 6160
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Mathias »

Ich habe nochmals einen Anlauf mit folgendem Code probiert.

Code: Alles auswählen

program Project1;
var
    es: ShortString = 'Ich liebe Lazarus  !'; section '.eeprom';
//  es: ShortString = 'Ich liebe Lazarus !';

begin
  repeat
    PORTB := byte(es[2]);
  until False;
end.       
Kompiliert wird es anstandslos, aber avrdude hat Mühe es hoch zu laden.

Code: Alles auswählen

Projekt kompilieren, OS: embedded, CPU: avr, Ziel: Project1.elf: Erfolg, Hinweise: 2
Hint: Start of reading config file /home/tux/fpcupdeluxe_avr5/fpc/bin/x86_64-linux/fpc.cfg
Hint: End of reading config file /home/tux/fpcupdeluxe_avr5/fpc/bin/x86_64-linux/fpc.cfg
Projekt: Ausführen des Befehls nach: Exit code 1

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "Project1.hex"
avrdude: ERROR: address 0x810010 out of range at line 13 of Project1.hex
avrdude: read from file 'Project1.hex' failed

avrdude done.  Thank you.
avrdude wird folgendermassen ausgerufen:

Code: Alles auswählen

avrdude -patmega328p -cusbasp -D -Uflash:w:Project1.hex:i
-D Hat keinen Einfluss.

Nehme ich das section '.eeprom'; raus, wird anstandslos hochgeladen.


Nachtrag:
Durch dieses Problem hier: viewtopic.php?f=9&t=12985&p=115077#p115077 habe ich noch folgende Infos vom Compiler.
Probiert am ATtiny2313.
Mit .eeprom:

Code: Alles auswählen

Error: /home/tux/fpcupdeluxe_avr25/cross/bin/avr-embedded/avr-ld: /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/AVR/ATtiny2313/EEPROM_String_Test/Project1.elf section `.eeprom' will not fit in region `eeprom'
Ohne .eeprom:

Code: Alles auswählen

Error: /home/tux/fpcupdeluxe_avr25/cross/bin/avr-embedded/avr-ld: /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/AVR/ATtiny2313/EEPROM_String_Test/Project1.elf section `.data' will not fit in region `data'
Etwas ungültiges:

Code: Alles auswählen

Error: /home/tux/fpcupdeluxe_avr25/cross/bin/avr-embedded/avr-ld: /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/AVR/ATtiny2313/EEPROM_String_Test/Project1.elf section `.test' will not fit in region `data'
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Timm Thaler
Beiträge: 1224
Registriert: So 20. Mär 2016, 22:14
OS, Lazarus, FPC: Win7-64bit Laz1.9.0 FPC3.1.1 für Win, RPi, AVR embedded
CPU-Target: Raspberry Pi 3

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Timm Thaler »

Wie sieht denn der erzeugte ASM-Code aus? (Compiler-Option -l und dann die *.s-Dateien ansehen)

Wäre ja schön, wenn der Compiler Zugriff auf Eeprom und Flash kann, aber dazu müßte er erkennen, dass er das unterschiedlich behandeln muss. Technisch geht das aber, der GCC kann das schon lange.

Mathias
Beiträge: 6160
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Mathias »

Timm Thaler hat geschrieben:
Mi 10. Jun 2020, 22:13
Wie sieht denn der erzeugte ASM-Code aus? (Compiler-Option -l und dann die *.s-Dateien ansehen.
Meinst du beim Versuch mit dem Atmega328 ?
Beim Attiny2313 hat ja schon der Compiler versagt.
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Timm Thaler
Beiträge: 1224
Registriert: So 20. Mär 2016, 22:14
OS, Lazarus, FPC: Win7-64bit Laz1.9.0 FPC3.1.1 für Win, RPi, AVR embedded
CPU-Target: Raspberry Pi 3

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Timm Thaler »

Die Frage zum ATtiny2313 hattest Du vor ein paar Monaten schonmal selbst beantwortet:

Man muss den Shortstring auf die reale Stringlänge begrenzen. Sonst nimmt der Compiler 255 als Standard - und die passen weder in den EEPROM noch in den Datenbereich des SRAM. (Der Datenbereich ist kleiner als der reale RAM, weil noch Stack davon abgeht.)

Bei mir sieht so eine Definition dann so aus:

Code: Alles auswählen

  Smenu11 : string[32] = 'Set Radio ---sec' +
                         'Ch ---  Ad -----'; section '.progmem';
  Smenu12 : string[32] = 'Set Zeit   --:--' +
                         '                '; section '.progmem';
Allerdings speichere ich fixe Strings im Flash, weil a) der Zugriff darauf einfacher ist b) genug davon da ist und c) ich die während der Laufzeit nicht ändere.

Timm Thaler
Beiträge: 1224
Registriert: So 20. Mär 2016, 22:14
OS, Lazarus, FPC: Win7-64bit Laz1.9.0 FPC3.1.1 für Win, RPi, AVR embedded
CPU-Target: Raspberry Pi 3

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Timm Thaler »

Dass hier kopiert einen ShortString aus dem Flash in den Ram. An der ersten Pos im String steht die Länge. Ein String mit [32] hat also 32 Zeichen, aber braucht 33 Byte Speicher.

Code: Alles auswählen

// String aus Flash in Buffer schreiben

procedure copy_pgm_string(psrc, pto : pchar); assembler; nostackframe;
// Rein: r22, r23, r24, r25 Pointer
label
  loop;
asm
  push r31  // Register sichern
  push r30
  push r27
  push r26
  push r16
  push r0

  mov r26, r22  // Pointer Ziel Sram
  mov r27, r23
  mov r30, r24   // Pointer Quelle Flash
  mov r31, r25
  lpm r0, Z+  // Länge holen
  st X+, r0  // Länge in Sram
  mov r16, r0  // Länge in Schleifenzähler
  loop:
  lpm r0, Z+  // Zeichen holen
  st X+, r0  // Zeichen in Sram
  dec r16
  brne loop

  pop r0  // Register wiederherstellen
  pop r16
  pop r26
  pop r27
  pop r30
  pop r31
end[];         
Der Aufruf erfolgt mit Übergabe der Adressen von Quelle und Ziel:

Code: Alles auswählen

  dispbuf : string [32];  // Displaybuffer 32 Zeichen
...
  copy_pgm_string(@Smenu11, @dispbuf);

Mathias
Beiträge: 6160
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Mathias »

Wie sieht denn der erzeugte ASM-Code aus? (Compiler-Option -l und dann die *.s-Dateien ansehen.
Ich wollte dies probieren, aber irgendwie hat der Parameter "-al" keinen Einfluss, es wird keine *.s erzeugt. :roll:
Hat das einen Zusammenhang, da ich eine sehr neue Trunk verwende ?

Vor längerer Zeit konnte ich nach ASM-Dateien erzeugen.

Nachtrag:
Jetzt habe ich es gefunden, die Datei ist unter "/tmp".

Im Anhang die *.s und eine Datei, welche die Differenz zeigt.
Wie schon mal gesagt, die Version mit .eeprom, kann ich mit AVRdude nicht hochladen.
Dateianhänge
asm.zip
(2.52 KiB) 165-mal heruntergeladen
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Mathias
Beiträge: 6160
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Mathias »

Ich bin per Zufall auf folgende FPC-Option gestossen: -k-Ttext=0x00080000
Wen ich dort den Wert 0x0000000 eintrage, bekomme ich folgende Fehlermeldung:

Code: Alles auswählen

...
Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: address 0x368 of /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.text' is not within region `flash'
Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.data' will not fit in region `flash'
Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: address 0x368 of /n4800/DATEN/Programmierung/Lazarus/Tutorials/Embedded/ARM/Arduino_DUE/Blink_Pin13/Project1.elf section `.text' is not within region `flash'
Error: /home/tux/fpcupdeluxe_stm32/cross/bin/arm-embedded/arm-none-eabi-ld: region `flash' overflowed by -785452 bytes
Project1.pas(49,0) Error: Error while linking
Dort stehen auch .xxx Werte. Könnte das evtl. mit "section" zusammenhängen ?

Das Ganze nur zur Info, vielleicht kann man diesen "-k...." Parameter für die Strings im EEPROM gebrauchen.
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Mathias
Beiträge: 6160
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Mathias »

Könnte das dies sein, was Timm ursprünglich gesucht hatte ?

https://github.com/ccrause/freepascal/w ... -FPC#usage
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Socke
Lazarusforum e. V.
Beiträge: 3158
Registriert: Di 22. Jul 2008, 19:27
OS, Lazarus, FPC: Lazarus: SVN; FPC: svn; Win 10/Linux/Raspbian/openSUSE
CPU-Target: 32bit x86 armhf
Wohnort: Köln
Kontaktdaten:

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Socke »

Mathias hat geschrieben:
So 20. Jun 2021, 16:43
Könnte das dies sein, was Timm ursprünglich gesucht hatte ?

https://github.com/ccrause/freepascal/w ... -FPC#usage
Vermutlich ja. Leider wird in dem Link nicht davon gesprochen, welche FPC-Version/-Revision genutzt wird. Die Wikiseite enthält erst seit März den Abschnitt zum impliziten Lesen in einer Zuweisung. In der vorherigen Version von Januar war es noch nicht enthalten. Als dieser Thread 2017 gestartet wurde, gab es diesen Wiki-Beitrag noch gar nicht.
MfG Socke
Ein Gedicht braucht keinen Reim//Ich pack’ hier trotzdem einen rein

PascalDragon
Beiträge: 825
Registriert: Mi 3. Jun 2020, 07:18
OS, Lazarus, FPC: L 2.0.8, FPC Trunk, OS Win/Linux
CPU-Target: Aarch64 bis Z80 ;)
Wohnort: München

Re: AVR - Strings direkt aus Flash lesen

Beitrag von PascalDragon »

Socke hat geschrieben:
Mo 21. Jun 2021, 07:51
Mathias hat geschrieben:
So 20. Jun 2021, 16:43
Könnte das dies sein, was Timm ursprünglich gesucht hatte ?

https://github.com/ccrause/freepascal/w ... -FPC#usage
Vermutlich ja. Leider wird in dem Link nicht davon gesprochen, welche FPC-Version/-Revision genutzt wird.
Es muss auch nicht davon sprechen, da das Feature in dem Repository ja entwickelt wird. Da kannst du auch sehen wie aktuell der Basiscode ist. Aktuell steht da "This branch is 52 commits ahead, 19 commits behind graemeg:master.". Also ist der Code nur 19 Commits hinter dem aktuellen Commit des GitHub Mirrors von FPC (der höchstens ne Viertel Stunde hinter dem SVN Trunk ist).
FPC Compiler Entwickler

Mathias
Beiträge: 6160
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Mathias »

Dank dieser Maillist bin ich zu dem github von ccrause gestossen.
Compiler support for the data memory sections .progmem (flash memory) and .eeprom was added. The compiler will generate the required low level instructions to access the respective memory section automatically.  The support covers both classic AVR architecture and the newer unified memory architecture. Refer to attached patches for details.

The section support is combined with type checking to ensure consistent and predictable behaviour.  The rules are (hopefully) designed according to the Pascal language spirit.  The following rules and behaviour have been implemented:
- A variable, constant or type can be associated with a section.  If the section name is not recognized the current behaviour is followed.
- Read/write access to .eeprom space is supported, but only read access for the .progmem space is provided (due to the paged erase/write semantics).
- The .progmem access is strictly enforced at declaration time, so only true constants, string literals and typed constants (if writeableconst is off) are allowed to be in .progmem space.  Any typed const variables could also be allowed if required, since another access check is done in the code generator step to catch write operations to .progmem.
- A local switch literalStringsInProgmem can be used to indicate storage of string literals in code in .progmem space.
- Parameters to functions/procedures can also refer to sections, on the callee side the appropriate access code will be generated.
- Type checking of parameters is extended to also compare section types.
- Some build-in conversions from a sectioned type to a regular type is provided.  This is used when a sectioned type is passed to a procedure with a regular parameter type (if a conversion is possible). This conversion doesn't apply when passing pointers or references.
- A local switch convertSectionedStringsToTemp can be specified to preconvert sectioned shortstrings to regular temporary shortstrings before further processing.  This reduces the dependence of section specific helper routines and may lead to smaller executable size in certain cases.  When this switch is active, the only time a sectioned shortstring will not be converted is when it is passed as a parameter to a procedure with a matching sectioned parameter type.
- Section information is added to symbols stored in ppu files so that sectioned types work across unit boundaries.
- Helper code has been added to the AVR controller units to provide the low level access in the include files sectionhelpersh.inc and sectionhelpers.inc.
  * These include files provide low level EEPROM access and shortstring conversion and handling helpers for section specific data.
  * Added extra I/O registers and bit definitions required for EEPROM simulation to avrsim unit.  Simulation of EEPROM access requires an updated version of fp-avrsim.

More information and simple examples/syntax can be viewed here: https://github.com/ccrause/freepascal/w ... -FPC#usage
Test cases can be viewed here: https://github.com/ccrause/freepascal/t ... s/test/avr

Review, comments, suggestions and ready for submission discussions would be appreciated.

Best wishes,
Christo
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Timm Thaler
Beiträge: 1224
Registriert: So 20. Mär 2016, 22:14
OS, Lazarus, FPC: Win7-64bit Laz1.9.0 FPC3.1.1 für Win, RPi, AVR embedded
CPU-Target: Raspberry Pi 3

Re: AVR - Strings direkt aus Flash lesen

Beitrag von Timm Thaler »

Okay. Werd mal die aktuelle Version holen und schauen, wie das vom Compiler umgesetzt wird.

PascalDragon
Beiträge: 825
Registriert: Mi 3. Jun 2020, 07:18
OS, Lazarus, FPC: L 2.0.8, FPC Trunk, OS Win/Linux
CPU-Target: Aarch64 bis Z80 ;)
Wohnort: München

Re: AVR - Strings direkt aus Flash lesen

Beitrag von PascalDragon »

Timm Thaler hat geschrieben:
Di 22. Jun 2021, 12:18
Okay. Werd mal die aktuelle Version holen und schauen, wie das vom Compiler umgesetzt wird.
Nur um eventueller Enttäuschung/Verwirrung vorzubeugen: du hast hoffentlich gesehen, dass dies ein Feature eines Drittentwicklers ist, der dies in seinem Fork des Repositories entwickelt hat und das Feature in der Mailing Liste quasi als Request for Comment vorgestellt hat? Du wirst also im offiziellen Trunk noch nichts dazu finden.
FPC Compiler Entwickler

Antworten