EmbeddedRelated.com
Forums

MC9S12KG256 FLASH bank 1 erased, but not programming

Started by serg...@mail.ru March 26, 2010
Hi,

I am working the MC9S12KG256 and writing flash driver to program CPU. Code executed from RAM.

FLASH memory is fully unsecured, FPROT for bank 0 is $84, for bank 1 is $FF.

Call to EraseFL (B=$00,A=$00) succesfully erases all FLASH sectors except protected.

Call to WriteFL succesfully program only bank 0. For bank 1 it doesn't return any error on programming stage, but there is no data programmed, so comparsion always failed.

Is it my fault or i miss something from datasheet?

Driver code below:

InitFLASH: brset FCLKDIV,#%10000000,IF_Skip
movb #%101001,FCLKDIV
IF_Skip: movb #%110000,FSTAT
rts

ExecFLASHCmd: ; C set on error
movb #%10000000,FSTAT
EFC_Wait: jsr AckCOP
brclr FSTAT,#%10000000,EFC_Wait
ChkFLASH: pshb
ldab FSTAT
andb #$30
addb #$FF
pulb
rts

WaitFLASHCmd: ; C set on error
jsr AckCOP
brclr FSTAT,#%1000000,WaitFLASHCmd
bra ChkFLASH

EraseFL: ; > B - StartPage; A - Count
bsr SelectFLASH
bsr InitFLASH
pshb
EraseFL_Loop: ldab 0,sp
lsrb
lsrb
lsrb
lsrb
orab #$30
stab PPAGE
bsr SelectFLASH
ldab 0,sp
psha
tfr b,a
anda #$0F
lsla
lsla
oraa #$80
clrb
tfr d,y
pula
std 0,y
movb #$40,FCMD
bsr ExecFLASHCmd
bcs EFL_Error
inc 0,sp
dbne a,EraseFL_Loop
EFL_Error: bsr WaitFLASHCmd
pulb
rts

SelectFLASH: brset PPAGE,#$08,SFL_M0
movb #$01,FCNFG
rts
SFL_M0: clr FCNFG
rts

; program $40 bytes from PgmPtr to PgmAddr[8000..BFFF] (PPAGE already set)
WriteFL: ; < B - 0=OK
bsr SelectFLASH
bsr InitFLASH
ldx PgmPtr
ldy PgmAddr
clra
WriteFL_PLoop: movw 2,x+,2,y+
movb #$20,FCMD
bsr ExecFLASHCmd
bcs WriteFL_Wait
inca
cmpa #$20
blo WriteFL_PLoop
WriteFL_Wait: bsr WaitFLASHCmd
bcs WriteFL_Err

ldx PgmPtr
ldy PgmAddr
clra
WriteFL_CLoop: ldab a,x
cmpb a,y
bne WriteFL_CErr
inca
cmpa #$40
blo WriteFL_CLoop
clrb
rts
WriteFL_Err: ldab FSTAT
orab #$01
rts
WriteFL_CErr: ldab #$CE
rts
I am trying to write a routine that will
program flash bytes at the security location in fixed memory at location
$ff00. The flash is unsecured (FSEC = 0xfe) All the code is executed
from RAM. The first thing I want to do is erase the sector with command
$40. The FCLKDIV is set to program at just under 200khz, PVIOL and
ACCERR is checked for all banks, the bank is reset to bank 0 (FCNFG = 0)
the sector address is sent by writing something to $fe00, then the
command to erase the sector is sent (0x40 to FCMD). But as soon as that
is done, the ACCERR is set in the FSTAT register. I can't understand
why, this is a valid command and all the other conditions seem to be OK.
Does anyone have any insight on this?, thanks in advance. Oh, and PPAGE
is $3f, but the doc says it doesn't matter when programming fixed
memory.

- Kevin Longworth
Where is the subroutine 'AckCOP' located? Does this call use
absolute rather than relative addressing? Is the subroutine located
in a common page?

Bill
At 12:52 PM 3/26/2010, you wrote:
>Hi,
>
>I am working the MC9S12KG256 and writing flash driver to program
>CPU. Code executed from RAM.

[snip]
>ExecFLASHCmd: ; C set on error
> movb #%10000000,FSTAT
>EFC_Wait: jsr AckCOP
> brclr FSTAT,#%10000000,EFC_Wait
>ChkFLASH: pshb
> ldab FSTAT
> andb #$30
> addb #$FF
> pulb
> rts
>
>WaitFLASHCmd: ; C set on error
> jsr AckCOP
> brclr FSTAT,#%1000000,WaitFLASHCmd
> bra ChkFLASH

[snip]

Hello, William.

26/03/2010, 22:23:26 you wrote:

WRE> Where is the subroutine 'AckCOP' located? Does this call use
WRE> absolute rather than relative addressing? Is the subroutine located
WRE> in a common page?

AckCOP is also in RAM (at $30CE). FLASH routines located from $3625.

AckCOP: movb #$55,ARMCOP
movb #$AA,ARMCOP
rts

>>I am working the MC9S12KG256 and writing flash driver to program
>>CPU. Code executed from RAM.

WRE> [snip]

>>ExecFLASHCmd: ; C set on error
>> movb #%10000000,FSTAT
>>EFC_Wait: jsr AckCOP
>> brclr FSTAT,#%10000000,EFC_Wait
>>ChkFLASH: pshb
>> ldab FSTAT
>> andb #$30
>> addb #$FF
>> pulb
>> rts
>>
>>WaitFLASHCmd: ; C set on error
>> jsr AckCOP
>> brclr FSTAT,#%1000000,WaitFLASHCmd
>> bra ChkFLASH

WRE> [snip]

-- Sergey