Recent

Author Topic: Store string in flash memory  (Read 865 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Store string in flash memory
« on: November 27, 2023, 12:14:49 pm »
Does anybody have a snip procedure in FPC, to store a string in the flash memory?

I need 1 procedure to read it and 1 procedure to write it.

So safe the value for the next boot of the MCU

Using the Attiny841

Greets, Wouter van Wegen, PascalByThree !

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: Store string in flash memory
« Reply #1 on: November 27, 2023, 01:29:52 pm »
Below is how I do it.
This are snippets from larger program, so it won't work out of the box, but you'll get the general idea:
Code: Pascal  [Select][+][-]
  1. const
  2.   LCD_Lines      = 4;          // broj redova na displeju
  3.   LCD_LineLength = 20;
  4.  
  5. type
  6.   String10 = String[10];
  7.   String16 = String[16];
  8.   TLCDString = String[LCD_LineLength];
  9.  
  10. const
  11.   opcija_da_ne: array [0 .. 1] of TLCDString = ('Ne', 'Da'); {$ifdef CPUAVR}section '.progmem';{$endif}
  12.   opcija_nacin_rada: array [0 .. 1] of TLCDString = ('Hladenje', 'Grijanje'); {$ifdef CPUAVR}section '.progmem';{$endif}
  13.   opcija_meni_kretanje: array [0 .. 1] of TLCDString = ('U krug', 'Fiksno'); {$ifdef CPUAVR}section '.progmem';{$endif}
  14.  
  15. function progmemByte(const address: pointer): byte; assembler; nostackframe;
  16. asm
  17.   movw ZL, r24
  18.   lpm r24, Z
  19. end;
  20.  
  21. function progmemChar(const address: pointer): char; assembler; nostackframe;
  22. asm
  23.   movw ZL, r24
  24.   lpm r24, Z
  25. end;
  26.  
  27. procedure TMenu.LoadOptionText(var AText: String; const s_progmem: TLCDString);
  28. var
  29.   len, i: byte;
  30. begin
  31.   len := progmemByte(@s_progmem[0]);
  32.   AText[0] := chr(len);
  33.   for i := 1 to len do
  34.     AText[i] := progmemChar(@s_progmem[i]);
  35. end;
  36.  
  37. // get string
  38. var sValue: TLCDString;
  39.  
  40. LoadOptionText(sValue, @opcija_nacin_rada);
  41.  

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: Store string in flash memory
« Reply #2 on: November 27, 2023, 01:44:04 pm »
Does anybody have a snip procedure in FPC, to store a string in the flash memory?

I need 1 procedure to read it and 1 procedure to write it.

Now I see you also said 'write it'. So you probably mean EEPROM and not Flash memory.
Here are functions to read/write bytes and words, you can easy use them to make it work with string:
Code: Pascal  [Select][+][-]
  1. procedure EEPROM_write(Address: Int16; Data: byte);
  2. begin
  3.   while (EECR and (1 shl EEPE)) <> 0 do
  4.   begin
  5.     ;
  6.   end;
  7.   EEAR := Address;
  8.   EEDR := Data;
  9.   EECR := EECR or (1 shl EEMPE);
  10.   EECR := EECR or (1 shl EEPE);
  11. end;
  12.  
  13. procedure EEPROM_writew(Address: Int16; Data: Word);
  14. begin
  15.   EEPROM_write(Address, Data and 255); // zapišem u little-endian
  16.   EEPROM_write(Address + 1, Data shr 8);
  17. end;
  18.  
  19. function EEPROM_read(Address: Int16): Byte;
  20. begin
  21.   while (EECR and (1 shl EEPE )) <> 0 do
  22.   begin
  23.     ;
  24.   end;
  25.   EEAR   := Address;
  26.   EECR   := EECR or (1 shl EERE);
  27.   Result := EEDR;
  28. end;
  29.  
  30. function EEPROM_readw(Address: Int16): Word;
  31. begin
  32.   Result := EEPROM_read(Address) + EEPROM_read(Address + 1) * 256;
  33. end;

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Store string in flash memory
« Reply #3 on: November 27, 2023, 05:20:01 pm »
Thank you, now going to try this working on it for a few days...

Code: Pascal  [Select][+][-]
  1. procedure EEPROM_writew(Address: Int16; Data: Word);

It compiles...

Do you also got a example Value  for the address variable ?
« Last Edit: November 27, 2023, 05:31:28 pm by pascalbythree »

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: Store string in flash memory
« Reply #4 on: November 27, 2023, 08:01:09 pm »
Thank you, now going to try this working on it for a few days...

Code: Pascal  [Select][+][-]
  1. procedure EEPROM_writew(Address: Int16; Data: Word);

It compiles...

Do you also got a example Value  for the address variable ?

ATtiny841 has 512 bytes of EEPROM, so Address can be from 0 to 511. If you use EEPROM_writew that from 0 to 510 because that procedure writes two bytes in a row.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Store string in flash memory
« Reply #5 on: November 28, 2023, 09:12:41 am »
ATtiny841 has 512 bytes of EEPROM, so Address can be from 0 to 511. If you use EEPROM_writew that from 0 to 510 because that procedure writes two bytes in a row.

Usual health warning: EEPROM only supports a limited number of writes before the chip is damaged.

The projected lifetime will be in the device data sheet, which is a resource that OP needs to get used to using.

Apropos Flash, /some/ chips allow it to be updated from running code. The details- alignment/blocking, selective erasure, fuse settings etc.- will be in the datasheet ,which might need to be read in conjunction with a detailed description of the bootloader if used.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018