Log adatok tárolása szabad flash területen ATMEGA644 chipben

Hogyan programozzak AVR chipet? Programozók beállításai...
Bascom nyelvű programok...
Válasz küldése
Avatar
cser
Újonc
Újonc
Hozzászólások: 10
Csatlakozott: 2011. február 22. kedd, 7:00

Log adatok tárolása szabad flash területen ATMEGA644 chipben

Hozzászólás Szerző: cser »

Az egészséges lustaság megakadályozott abban, hogy kiegészítő hardvert rakjak össze csak azért, hogy szerény méretű log file-t tároljak. Megpróbáltam felhasználni a chip fel nem használt flash memóriaterületét.
Láttam ilyen felvetést az mcselec fórumban, de ott rövid úton lebeszélték a srácot azzal, hogy csak 10.000 szer lehet újraírni. Ez ugyan kevésnek tűnik, de az én becslésem szerint az én igényeimet kb 200 évig elégítené ki ennyi ciklus.
Előnyök:
  • nincs külön hardver
  • áramszünet esetén sem vész el
Hátrányok:
  • max 10.000 szer írható újra
  • az adatokat page-enként lehet (célszerű) letárolni - 128 bájt
  • bele kell mélyedni a bootloader témába
  • ehhez ASM ismeretek is kellenének
  • illetve ismerni kell a flash-be írás menetrendjét
Első lépésben írnom kellene a flash részbe.

A BASCOM BootLoader.bas mintaprogram lefarigcsálásával próbáltam belefirkálni az üresen hagyott memória részbe. Soros port helyett egy LCD-t tettem fel.

Azt reméltem, hogy a program beégetése, lefordítása és futtatása után, ha kiolvasom a teljes flash tartalmat, akkor ott fogom látni a sok FF között azt, amit beleírtam:1-128ig a számokat sorban a 100. oldaltól kezdve (12800-ik byte). Innen már csak rendszerezni és persze kiolvasni kellene a log adatokat.
A program lefutott, de a flash átírása nem történt meg.

Tudna valaki segíteni?

A lefarigcsált kód:

'----------------------------------------------------------------
' (c) 1995-2009, MCS
' Bootloader.bas
' This sample demonstrates how you can write your own bootloader
' in BASCOM BASIC
' VERSION 2 of the BOOTLOADER. The waiting for the NAK is stretched
' further a bug was resolved for the M64/M128 that have a big page size
'-----------------------------------------------------------------
'This sample will be extended to support other chips with bootloader
'The loader is supported from the IDE

'AVR IDE 2.0.4.0
$regfile = "M324pdef.dat" '324 esetén
'$regfile = "M644pdef.dat"
'$regfile = "M168def.dat"
'$regfile = "m328pdef.dat"
$crystal = 8000000
$hwstack = 256
$swstack = 160
$framesize = 320


Const Loaderchip = 644


Const Maxwordbit = 7 'Z7 is maximum bit '

Const Maxword =(2 ^ Maxwordbit) ' * 2 '128 *******
Const Maxwordshift = Maxwordbit + 1 '8 *******


'Dim the used variables

Dim Buf(128) As Byte
Dim J As Byte , Spmcrval As Byte ' self program command byte value

Dim Z As Long 'this is the Z pointer word
Dim Vl As Byte , Vh As Byte ' these bytes are used for the data values
Dim Wrd As Word , Page As Word 'these vars contain the page and word address

Page = 100 'starting point to store log data

Disable Interrupts 'we do not use ints

Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.5 , Db5 = Portc.4 , Db6 = Portc.3 , Db7 = Portc.2 , E = Portc.6 , Rs = Portc.7
Config Lcdmode = Port
Initlcd

Spmcrval = 3 : Gosub Do_spm ' erase the first page
'Spmcrval = 17 : Gosub Do_spm ' re-enable page

Loader:


For J = 1 To 128 'get 128 bytes
Buf(j) = j
Next



Gosub Writepage 'yes go write the page



'write one or more pages
Writepage:
For J = 1 To 128 Step 2 'we write 2 bytes into a page
Vl = Buf(j) : Vh = Buf(j + 1) 'get Low and High bytes
lds r0, {vl} 'store them into r0 and r1 registers
lds r1, {vh}
Spmcrval = 1 : Gosub Do_spm 'write value into page at word address
Wrd = Wrd + 2 ' word address increases with 2 because LS bit of Z is not used
Locate 1 , 1
Lcd Wrd ; Maxwordshift ; Buf(j)
Waitms 50
If Wrd = Maxword Then ' page is full
Locate 1 , 1
Lcd "buffer tele" ; Wrd
Wait 1
Wrd = 0 'Z pointer needs wrd to be 0
Spmcrval = 5 : Gosub Do_spm 'write page
Spmcrval = 17 : Gosub Do_spm ' re-enable page

Page = Page + 1 'next page
Spmcrval = 3 : Gosub Do_spm ' erase next page
Spmcrval = 17 : Gosub Do_spm ' re-enable page
End If
Next

'Locate 1 , 1
Lcd "writepage lefutott"
Wait 1
stop
Return


Do_spm:
Bitwait Spmcsr.0 , Reset ' check for previous SPM complete
Bitwait Eecr.1 , Reset 'wait for eeprom

Z = Page 'make equal to page
Shift Z , Left , Maxwordshift 'shift to proper place
Z = Z + Wrd 'add word
lds r30,{Z}
lds r31,{Z+1}


Spmcsr = Spmcrval 'assign register
spm 'this is an asm instruction
nop
nop
Return
Cser József

Kód: Egész kijelölése

http://napkollektor.cser.eu/
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Re: Log adatok tárolása szabad flash területen ATMEGA644 chi

Hozzászólás Szerző: kapu48 »

Lenne 2 javaslatom!
Igy jelöltem: (<<<<<<<<<<<<<<<?)

Kód: Egész kijelölése

   '----------------------------------------------------------------
' (c) 1995-2009, MCS
' Bootloader.bas
' This sample demonstrates how you can write your own bootloader
' in BASCOM BASIC
' VERSION 2 of the BOOTLOADER. The waiting for the NAK is stretched
' further a bug was resolved for the M64/M128 that have a big page size
'-----------------------------------------------------------------
'This sample will be extended to support other chips with bootloader
'The loader is supported from the IDE

'AVR IDE 2.0.4.0
'$regfile = "M324pdef.dat" '324 esetén
$regfile = "M644pdef.dat"
'$regfile = "M168def.dat"
'$regfile = "m328pdef.dat"
$crystal = 8000000
$hwstack = 256
$swstack = 160
$framesize = 320


Const Loaderchip = 644


Const Maxwordbit = 7 'Z7 is maximum bit '

Const Maxword =(2 ^ Maxwordbit) ' * 2 '128 *******
Const Maxwordshift = Maxwordbit + 1 '8 *******


'Dim the used variables

Dim Buf(128) As Byte
Dim J As Byte , Spmcrval As Byte ' self program command byte value

Dim Z As Long 'this is the Z pointer word
Dim Vl As Byte , Vh As Byte ' these bytes are used for the data values
Dim Wrd As Word , Page As Word 'these vars contain the page and word address

Page = 100 'starting point to store log data

Disable Interrupts 'we do not use ints

Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.5 , Db5 = Portc.4 , Db6 = Portc.3 , Db7 = Portc.2 , E = Portc.6 , Rs = Portc.7
Config Lcdmode = Port
Initlcd

Spmcrval = 3 : Gosub Do_spm ' erase the first page
Spmcrval = 17 : Gosub Do_spm ' re-enable page    <<<<<<<<<<?

Loader:


For J = 1 To 128 'get 128 bytes
   Buf(j) = j
Next



Gosub Writepage 'yes go write the page

End 'end program

'write one or more pages
Writepage:
   Wrd = 0 'Z pointer needs wrd to be 0    <<<<<<<<<<?

   For J = 1 To 128 Step 2 'we write 2 bytes into a page
      Vl = Buf(j)
      Vh = Buf(j + 1) 'get Low and High bytes
      lds r0, {vl} 'store them into r0 and r1 registers
      lds r1, {vh}
      Spmcrval = 1
      Gosub Do_spm 'write value into page at word address
      Wrd = Wrd + 2 ' word address increases with 2 because LS bit of Z is not used
      Locate 1 , 1
      Lcd Wrd ; Maxwordshift ; Buf(j)
      Waitms 50
      If Wrd = Maxword Then ' page is full
         Locate 1 , 1
         Lcd "buffer tele" ; Wrd
         Wait 1
         Wrd = 0 'Z pointer needs wrd to be 0
         Spmcrval = 5
         Gosub Do_spm 'write page
         Spmcrval = 17 : Gosub Do_spm ' re-enable page

         Page = Page + 1 'next page
         Spmcrval = 3 : Gosub Do_spm ' erase next page
         Spmcrval = 17 : Gosub Do_spm ' re-enable page
      End If
   Next

'  Locate 1 , 1
   Lcd "writepage lefutott"
   Wait 1
'   stop
Return


Do_spm:
   Bitwait Spmcsr.0 , Reset ' check for previous SPM complete
   Bitwait Eecr.1 , Reset 'wait for eeprom

   Z = Page 'make equal to page
   Shift Z , Left , Maxwordshift 'shift to proper place
   Z = Z + Wrd 'add word
   lds r30,{Z}
   lds r31,{Z+1}


   Spmcsr = Spmcrval 'assign register
   spm 'this is an asm instruction    Store Program Memory
   nop
   nop
Return
Avatar
cser
Újonc
Újonc
Hozzászólások: 10
Csatlakozott: 2011. február 22. kedd, 7:00

Re: Log adatok tárolása szabad flash területen ATMEGA644 chi

Hozzászólás Szerző: cser »

Köszönöm, de nem nyert.
Kipróbáltam a két módosítást egyenként is.
Spmcrval = 1 :temp bufferbe írás
Spmcrval = 3 :page törlése
Spmcrval = 5 : page írása
Spmcrval = 17 után mi történik? ezt nem találtam meg az adatlapon
Válasz küldése