9S08QG8 EEprom/Flash

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

9S08QG8 EEprom/Flash

2,242 Views
wb79
Contributor I
Hi all, ive just joined the freescale site.
 
I am using a 9S08QG8 micro on a freescale dev board. I have been trying to use the Flash to store some calibration values with some  great difficulty as i cant get it to work. I have followed the data sheet and all seems fine.
 
I have tried the QG8 Flash 6.zip example that is was suggested here, but still no success.
 
Does anyone have some working demo code or a library for doing this?
 
Thanks in advance.
 
Wes
Labels (1)
5 Replies

508 Views
fuma
Contributor I

Hi,

    where I can look the 6.zip example because I have a problem of write a 256bit data in the flash for the offset/gain regulation.

I write this code but dont work

 

InitFlash:
lda FCDIV
and mFCDIV_DIVLD ;check if DIV bits have been set
bne InitFlash1 ;skip if yes

lda #Div
 sta FCDIV     ;load divisor value

InitFlash1:
rts 

 

 

;***********************************************************
;======memorizzazione parametri in FLASH====================
;***********************************************************

MemPar
ldhx #$f080 ; blank pagina FLASH
jsr FlashErase

ldhx #ProcsDat ; indirizzo del primo dato
lda RegSerie ; contenuto del dato
jsr FlashProg1 ; scrittura del dato

aix #1
lda RegSerie+1
jsr FlashProg1
aix #1
lda RegSerie+2
jsr FlashProg1
aix #1
lda RegSerie+3
jsr FlashProg1
aix #1

lda RegFW
jsr FlashProg1
aix #1
lda RegFW
jsr FlashProg1
aix #1
rts


FlashProg1:
psha ;temporarily save entry data
lda #(mFSTAT_FPVIOL+mFSTAT_FACCERR) ;mask
  sta FSTAT ;abort any command and clear err

 lda #mByteProg ;mask pattern for byte prog comm
bsr DoOnStack ;execute prog code from stack RAM
ais #1 ;deallocate data location from stack
rts ;Z = 0 means there was an error
FlashProg1End:

FlashProg1Size equ FlashProg1End - FlashProg1

FlashErase:
psha ;temporarily save entry data
lda #(mFSTAT_FPVIOL+mFSTAT_FACCERR) ;mask
sta FSTAT ;abort any command and clear errors
lda #mSectorErase ;mask pattern for sector erase
bsr DoOnStack ;execute prog code from stack RAM
ais #1 ;deallocate data location from stack
rts ;Z = 0 means there was an error
FlashEraseEnd:

FlashEraseSize equ FlashEraseEnd - FlashErase



DoOnStack:
pshx
pshh ;save pointer to flash
psha ;save command on stack
ldhx #SpSubEnd ;point at last byte to move to stack
;----------------------------------------------------------------
;Move the routine to the stack
SpMoveLoop:
lda ,x ;read from flash
psha ;move onto stack
aix #-1 ;next byte to move
cphx #SpSub-1 ;past end?
bne SpMoveLoop ;loop till whole sub on stack
;----------------------------------------------------------------
tsx ;point to sub on stack
tpa ;move CCR to A for testing
and #$08 ;check the I mask
bne I_set ;skip if I already set
;----------------------------------------------------------------
sei ;block interrupts while FLASH busy
lda SpSubSize+6,sp ;preload data for command
jsr ,x ;execute the sub on the stack
cli ;ok to clear I mask now
bra I_cont ;continue to stack de-allocation
;================================================================
I_set:
lda SpSubSize+6,sp ;preload data for command
jsr ,x ;execute the sub on the stack
I_cont:
ais #SpSubSize+3 ;deallocate sub body + H:X + command
;H:X flash pointer OK from SpSub
lsla ;A=00 & Z=1 unless PVIOL or ACCERR
  rts ;to flash where DoOnStack was called 

SpSub:
ldhx SpSubSize+4,sp ;get flash address from stack
sta 0,x ;write to flash; latch addr and data
lda SpSubSize+3,sp ;get flash command
sta FCMD ;write the flash command
lda #mFSTAT_FCBEF ;mask to initiate command
sta FSTAT ;[pwpp] register command
nop ;[p] want min 4~ from w cycle to r
ChkDone:
lda FSTAT ;[prpp] so FCCF is valid
lsla ;FCCF now in MSB
bpl ChkDone ;loop if FCCF = 0
SpSubEnd:
rts

 

Where is a mistake?

 

Tanks in advance

fuma 

 

 

 

 

0 Kudos

508 Views
wb79
Contributor I
Thanks for the tips guys, i didnt realize it was that tricky to emulate EEprom :smileymad:
 
I managed to get it working after your insights, i used the QG8 Flash 6 demo code, and brought my code into it. So i can confirm it does work. Just have to reallocate the eeprom address from E000 to somewhere further up such as F000
 
:smileyvery-happy: chuffed as chips
 
0 Kudos

508 Views
peg
Senior Contributor IV
Hi Wes,

How about this?

0 Kudos

508 Views
RickN
Contributor I
Hi Wes,
 
   There is a Processor Expert Bean called intFlash that works great for this purpose.  Unfortunately the intFlash bean is not included with the Special Edition of CodeWarrior for MCU v6.1.   You can go to the www.freescale.com/cwregister site and request an evaluation license.  I'm pretty sure this will give you access to the intFlash bean in Processor Expert. 
 
   Programming Flash on the QG8 is a bit tricky because you have to run code out of internal RAM when doing the program or erase operations. 
 
   Let me know if using Processor Expert works for you.
 
  Thanks,
 
     Rick
0 Kudos

508 Views
bigmac
Specialist III
Hello Wes,
 
The method that is commonly used for 9S08 devices, is to load a portion of the erase or programming code to the stack, and then execute the code from the stack.
 
This issue has been raised quite often within this forum.  Perhaps use the search engine at the bottom of this page - a search for "doonstack" should achieve some relevant matches.  Most of the code examples seem to use assembler for their low level routines (the code size is more predictable), however, it is also possible to call the assembly routines from within a C program.
 
There seems to be two versions - one version uses the same code within the stack for both erase and programming operations.  The other version has different stack based routines for erase and programming.
 
Regards,
Mac
 
0 Kudos