Flash Program Routine - Appears To Do Nothing

Started by andr...@yahoo.co.uk February 17, 2012
Hi - I need some help please ....

I'm trying to write some code to write to the Flash area on an MC9S12DP512.

But although it runs, and doesn't appear to throw any Acc or PViol errors, it also doesn't appear to actually write to the Flash - it remains @ FF FF ..

What am I doing wrong ?

I've looked the the FSL app notes, (AN2400 for example) and done some digging around on here - and as far as I can see - I'm doing it correctly - but obviously I'm not !

Hope someone can help me ...

Rachel

Here is the code ...
ORG $5000

; Test to see if FLASH writing works
; Copy from $5000 into RAM (ShadowStart)
; Then Erase Sector 1 Page $20 (Bank 3)
; Then write the data from RAM into Page $20

TestFlashWrite:

LDX #$5000
LDY #ShadowStart
CopyLoop:
MOVW X,Y
CPX #$53FE
BEQ CopyDone
INX
INX
INY
INY
BRA CopyLoop

CopyDone:

; Erase the Flash Sector

; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
MOVB #$20,PPAGE
MOVB #$03,FCNFG

; Wait for Command Registers to be free

BRCLR FSTAT,#$80,*

; Clear any outstanding PVIOL / ACCERR Errors
MOVB #$30,FSTAT

; Write any value, to the address of the sector we want to erase.

MOVW #$0000,$8000

; Issue the 'Sector Erase' Command
MOVB #$40,FCMD

; Start the sequence
MOVB #$80,FSTAT

NOP
NOP
NOP
NOP
NOP

; Check that command has completed
BRCLR FSTAT,#$40,*

; Check for Errors
BRSET FSTAT,#$20,FlashPviol
BRSET FSTAT,#$10,FlashAccErr

; ***** OK, we can start the programming now *****
LDX #$8000
LDY #ShadowStart

ProgLoop:
; Check that command has completed
BRCLR FSTAT,#$80,*

LDD Y
STD X

; Issue the 'Program' Command
MOVB #$20,FCMD

; Start the sequence
BSET FSTAT,#$80

NOP
NOP
NOP
NOP
NOP

; Check that command has completed
BRCLR FSTAT,#$40,*

; Check for Errors
BRSET FSTAT,#$20,FlashPviol
BRSET FSTAT,#$10,FlashAccErr

; Move onto next word
CPX #$83FE
BEQ ProgDone
INX
INX
INY
INY
BRA ProgLoop

ProgDone:
MOVB #$08,PORTB

RTS

; Loop around for ever, so we can catch in debug

FlashPviol:
MOVB #$10,PORTB
BRA FlashPviol

FlashAccErr:
MOVB #$1F,PORTB
BRA FlashAccErr

What assembler and debugger are you using?

> MOVW X,Y

^^ this should copy the word from variable called X to variable called Y.
Certainly you meant here indexed addressing, so you should edit it to this
form

MOVW 0,X, 0,Y

> LDD Y
> STD X

^^ Same here

Provided assembler treat this like it was expected by you, Codewarrior
debugger by default is not refreshing flash and EEPROM memories from device
memory. So you see no change after erasing or programming. You need to edit
memory mapping from debuggger menus and tell you want these memories to be
refreshed on stop or something like that.

Edward

----- Original Message -----
From:
To: <6...>
Sent: Friday, February 17, 2012 3:07 AM
Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
> Hi - I need some help please ....
>
> I'm trying to write some code to write to the Flash area on an
> MC9S12DP512.
>
> But although it runs, and doesn't appear to throw any Acc or PViol errors,
> it also doesn't appear to actually write to the Flash - it remains @ FF FF
> ..
>
> What am I doing wrong ?
>
> I've looked the the FSL app notes, (AN2400 for example) and done some
> digging around on here - and as far as I can see - I'm doing it
> correctly - but obviously I'm not !
>
> Hope someone can help me ...
>
> Rachel
>
> Here is the code ...
> ORG $5000
>
> ; Test to see if FLASH writing works
> ; Copy from $5000 into RAM (ShadowStart)
> ; Then Erase Sector 1 Page $20 (Bank 3)
> ; Then write the data from RAM into Page $20
>
> TestFlashWrite:
>
> LDX #$5000
> LDY #ShadowStart
> CopyLoop:
> MOVW X,Y
> CPX #$53FE
> BEQ CopyDone
> INX
> INX
> INY
> INY
> BRA CopyLoop
>
> CopyDone:
> ; Erase the Flash Sector
>
> ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
> MOVB #$20,PPAGE
> MOVB #$03,FCNFG
>
> ; Wait for Command Registers to be free
>
> BRCLR FSTAT,#$80,*
>
> ; Clear any outstanding PVIOL / ACCERR Errors
> MOVB #$30,FSTAT
>
> ; Write any value, to the address of the sector we want to erase.
>
> MOVW #$0000,$8000
>
> ; Issue the 'Sector Erase' Command
> MOVB #$40,FCMD
>
> ; Start the sequence
> MOVB #$80,FSTAT
>
> NOP
> NOP
> NOP
> NOP
> NOP
>
> ; Check that command has completed
> BRCLR FSTAT,#$40,*
>
> ; Check for Errors
> BRSET FSTAT,#$20,FlashPviol
> BRSET FSTAT,#$10,FlashAccErr
>
> ; ***** OK, we can start the programming now *****
> LDX #$8000
> LDY #ShadowStart
>
> ProgLoop:
> ; Check that command has completed
> BRCLR FSTAT,#$80,*
>
> LDD Y
> STD X
>
> ; Issue the 'Program' Command
> MOVB #$20,FCMD
>
> ; Start the sequence
> BSET FSTAT,#$80
>
> NOP
> NOP
> NOP
> NOP
> NOP
>
> ; Check that command has completed
> BRCLR FSTAT,#$40,*
>
> ; Check for Errors
> BRSET FSTAT,#$20,FlashPviol
> BRSET FSTAT,#$10,FlashAccErr
>
> ; Move onto next word
> CPX #$83FE
> BEQ ProgDone
> INX
> INX
> INY
> INY
> BRA ProgLoop
>
> ProgDone:
> MOVB #$08,PORTB
>
> RTS
>
> ; Loop around for ever, so we can catch in debug
>
> FlashPviol:
> MOVB #$10,PORTB
> BRA FlashPviol
>
> FlashAccErr:
> MOVB #$1F,PORTB
> BRA FlashAccErr
>
>
> What assembler and debugger are you using?

I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)

> Certainly you meant here indexed addressing, so you should edit it to this
> form

OK - I did this, but it has made no difference - I think that CW accepts it the way I had it - but I agree it's not the 'correct' way to write it !

> debugger by default is not refreshing flash and EEPROM memories from device
> memory.

I thought that too. So I wrote some more code to read the contents after the write - and it is all FF FF ...

Also - I think the debugger is showing memory correctly - If I change PPAGE to $3F and then go back to read address $8000 - i can see the top page, as I should be able to.

I also tried using ICD12Z to read the memory - it also indicates it is FF FF ....

I am lost !

Rachel
--- In 6..., "Edward Karpicz" wrote:
>
> What assembler and debugger are you using?
>
> > MOVW X,Y
>
> ^^ this should copy the word from variable called X to variable called Y.
> Certainly you meant here indexed addressing, so you should edit it to this
> form
>
> MOVW 0,X, 0,Y
>
> > LDD Y
> > STD X
>
> ^^ Same here
>
> Provided assembler treat this like it was expected by you, Codewarrior
> debugger by default is not refreshing flash and EEPROM memories from device
> memory. So you see no change after erasing or programming. You need to edit
> memory mapping from debuggger menus and tell you want these memories to be
> refreshed on stop or something like that.
>
> Edward
>
> ----- Original Message -----
> From:
> To: <6...>
> Sent: Friday, February 17, 2012 3:07 AM
> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
> > Hi - I need some help please ....
> >
> > I'm trying to write some code to write to the Flash area on an
> > MC9S12DP512.
> >
> > But although it runs, and doesn't appear to throw any Acc or PViol errors,
> > it also doesn't appear to actually write to the Flash - it remains @ FF FF
> > ..
> >
> > What am I doing wrong ?
> >
> > I've looked the the FSL app notes, (AN2400 for example) and done some
> > digging around on here - and as far as I can see - I'm doing it
> > correctly - but obviously I'm not !
> >
> > Hope someone can help me ...
> >
> > Rachel
> >
> > Here is the code ...
> >
> >
> > ORG $5000
> >
> > ; Test to see if FLASH writing works
> > ; Copy from $5000 into RAM (ShadowStart)
> > ; Then Erase Sector 1 Page $20 (Bank 3)
> > ; Then write the data from RAM into Page $20
> >
> >
> >
> > TestFlashWrite:
> >
> > LDX #$5000
> > LDY #ShadowStart
> > CopyLoop:
> > MOVW X,Y
> > CPX #$53FE
> > BEQ CopyDone
> > INX
> > INX
> > INY
> > INY
> > BRA CopyLoop
> >
> > CopyDone:
> >
> >
> > ; Erase the Flash Sector
> >
> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
> >
> >
> > MOVB #$20,PPAGE
> > MOVB #$03,FCNFG
> >
> >
> >
> > ; Wait for Command Registers to be free
> >
> > BRCLR FSTAT,#$80,*
> >
> > ; Clear any outstanding PVIOL / ACCERR Errors
> > MOVB #$30,FSTAT
> >
> > ; Write any value, to the address of the sector we want to erase.
> >
> > MOVW #$0000,$8000
> >
> > ; Issue the 'Sector Erase' Command
> > MOVB #$40,FCMD
> >
> > ; Start the sequence
> > MOVB #$80,FSTAT
> >
> > NOP
> > NOP
> > NOP
> > NOP
> > NOP
> >
> > ; Check that command has completed
> > BRCLR FSTAT,#$40,*
> >
> > ; Check for Errors
> > BRSET FSTAT,#$20,FlashPviol
> > BRSET FSTAT,#$10,FlashAccErr
> >
> > ; ***** OK, we can start the programming now *****
> >
> >
> > LDX #$8000
> > LDY #ShadowStart
> >
> > ProgLoop:
> > ; Check that command has completed
> > BRCLR FSTAT,#$80,*
> >
> > LDD Y
> > STD X
> >
> > ; Issue the 'Program' Command
> > MOVB #$20,FCMD
> >
> > ; Start the sequence
> > BSET FSTAT,#$80
> >
> > NOP
> > NOP
> > NOP
> > NOP
> > NOP
> >
> > ; Check that command has completed
> > BRCLR FSTAT,#$40,*
> >
> > ; Check for Errors
> > BRSET FSTAT,#$20,FlashPviol
> > BRSET FSTAT,#$10,FlashAccErr
> >
> > ; Move onto next word
> > CPX #$83FE
> > BEQ ProgDone
> > INX
> > INX
> > INY
> > INY
> > BRA ProgLoop
> >
> > ProgDone:
> > MOVB #$08,PORTB
> >
> > RTS
> >
> > ; Loop around for ever, so we can catch in debug
> >
> > FlashPviol:
> > MOVB #$10,PORTB
> > BRA FlashPviol
> >
> > FlashAccErr:
> > MOVB #$1F,PORTB
> > BRA FlashAccErr
> >
> >
> >
> >
> >
> >
How and where do you set up the FCLKDIV?
It is not very clear what is purpose of copying code to ShadowStart. It
would make sense to execute from RAM, because code will hang if you
program/erase flash in the same bank like code. But you don't jump to RAM,
isn't it?

Changing PPAGE doesn't show how debugger is caching memory. Just find memory
map in Hiwave and set up required checkboxes, else you won't see how flash
changes on erase/program

Edward

----- Original Message -----
From: "andrew"
To: <6...>
Sent: Friday, February 17, 2012 8:40 PM
Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
>
>> What assembler and debugger are you using?
>
> I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
>
>> Certainly you meant here indexed addressing, so you should edit it to
>> this
>> form
>
> OK - I did this, but it has made no difference - I think that CW accepts
> it the way I had it - but I agree it's not the 'correct' way to write it !
>
>> debugger by default is not refreshing flash and EEPROM memories from
>> device
>> memory.
>
> I thought that too. So I wrote some more code to read the contents after
> the write - and it is all FF FF ...
>
> Also - I think the debugger is showing memory correctly - If I change
> PPAGE to $3F and then go back to read address $8000 - i can see the top
> page, as I should be able to.
>
> I also tried using ICD12Z to read the memory - it also indicates it is FF
> FF ....
>
> I am lost !
>
> Rachel
> --- In 6..., "Edward Karpicz" wrote:
>>
>> What assembler and debugger are you using?
>>
>> > MOVW X,Y
>>
>> ^^ this should copy the word from variable called X to variable called Y.
>> Certainly you meant here indexed addressing, so you should edit it to
>> this
>> form
>>
>> MOVW 0,X, 0,Y
>>
>> > LDD Y
>> > STD X
>>
>> ^^ Same here
>>
>> Provided assembler treat this like it was expected by you, Codewarrior
>> debugger by default is not refreshing flash and EEPROM memories from
>> device
>> memory. So you see no change after erasing or programming. You need to
>> edit
>> memory mapping from debuggger menus and tell you want these memories to
>> be
>> refreshed on stop or something like that.
>>
>> Edward
>>
>> ----- Original Message -----
>> From:
>> To: <6...>
>> Sent: Friday, February 17, 2012 3:07 AM
>> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
>> > Hi - I need some help please ....
>> >
>> > I'm trying to write some code to write to the Flash area on an
>> > MC9S12DP512.
>> >
>> > But although it runs, and doesn't appear to throw any Acc or PViol
>> > errors,
>> > it also doesn't appear to actually write to the Flash - it remains @ FF
>> > FF
>> > ..
>> >
>> > What am I doing wrong ?
>> >
>> > I've looked the the FSL app notes, (AN2400 for example) and done some
>> > digging around on here - and as far as I can see - I'm doing it
>> > correctly - but obviously I'm not !
>> >
>> > Hope someone can help me ...
>> >
>> > Rachel
>> >
>> > Here is the code ...
>> >
>> >
>> > ORG $5000
>> >
>> > ; Test to see if FLASH writing works
>> > ; Copy from $5000 into RAM (ShadowStart)
>> > ; Then Erase Sector 1 Page $20 (Bank 3)
>> > ; Then write the data from RAM into Page $20
>> >
>> >
>> >
>> > TestFlashWrite:
>> >
>> > LDX #$5000
>> > LDY #ShadowStart
>> > CopyLoop:
>> > MOVW X,Y
>> > CPX #$53FE
>> > BEQ CopyDone
>> > INX
>> > INX
>> > INY
>> > INY
>> > BRA CopyLoop
>> >
>> > CopyDone:
>> >
>> >
>> > ; Erase the Flash Sector
>> >
>> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
>> >
>> >
>> > MOVB #$20,PPAGE
>> > MOVB #$03,FCNFG
>> >
>> >
>> >
>> > ; Wait for Command Registers to be free
>> >
>> > BRCLR FSTAT,#$80,*
>> >
>> > ; Clear any outstanding PVIOL / ACCERR Errors
>> > MOVB #$30,FSTAT
>> >
>> > ; Write any value, to the address of the sector we want to erase.
>> >
>> > MOVW #$0000,$8000
>> >
>> > ; Issue the 'Sector Erase' Command
>> > MOVB #$40,FCMD
>> >
>> > ; Start the sequence
>> > MOVB #$80,FSTAT
>> >
>> > NOP
>> > NOP
>> > NOP
>> > NOP
>> > NOP
>> >
>> > ; Check that command has completed
>> > BRCLR FSTAT,#$40,*
>> >
>> > ; Check for Errors
>> > BRSET FSTAT,#$20,FlashPviol
>> > BRSET FSTAT,#$10,FlashAccErr
>> >
>> > ; ***** OK, we can start the programming now *****
>> >
>> >
>> > LDX #$8000
>> > LDY #ShadowStart
>> >
>> > ProgLoop:
>> > ; Check that command has completed
>> > BRCLR FSTAT,#$80,*
>> >
>> > LDD Y
>> > STD X
>> >
>> > ; Issue the 'Program' Command
>> > MOVB #$20,FCMD
>> >
>> > ; Start the sequence
>> > BSET FSTAT,#$80
>> >
>> > NOP
>> > NOP
>> > NOP
>> > NOP
>> > NOP
>> >
>> > ; Check that command has completed
>> > BRCLR FSTAT,#$40,*
>> >
>> > ; Check for Errors
>> > BRSET FSTAT,#$20,FlashPviol
>> > BRSET FSTAT,#$10,FlashAccErr
>> >
>> > ; Move onto next word
>> > CPX #$83FE
>> > BEQ ProgDone
>> > INX
>> > INX
>> > INY
>> > INY
>> > BRA ProgLoop
>> >
>> > ProgDone:
>> > MOVB #$08,PORTB
>> >
>> > RTS
>> >
>> > ; Loop around for ever, so we can catch in debug
>> >
>> > FlashPviol:
>> > MOVB #$10,PORTB
>> > BRA FlashPviol
>> >
>> > FlashAccErr:
>> > MOVB #$1F,PORTB
>> > BRA FlashAccErr
>> >
>> >
>> >
>> >
>> >
>> >
> How and where do you set up the FCLKDIV?

MOVB #$4A,FCLKDIV ; Set speed of Flash , for a 16MHz Xtal
MOVB #$00,FCNFG ; Disable Flash generated INTs

Sorry - that was in the init routine, and I hadn't copied that across.

The purpose of copying the code to RAM is not useful - when I finally get this to work, it will store some 'useful' data from RAM, into a page in FLASH - but for now, to simplfy whilst testing - I just copied some 'known' bytes into RAM, and tried to write them to FLASH.

The data does get copied into the RAM - I can step through and see that filling up the RAM - so this part is not the problem I think.

> Changing PPAGE doesn't show how debugger is caching memory. Just find memory
> map in Hiwave and set up required checkboxes, else you won't see how flash
> changes on erase/program

I can't find any option for this in HIWAVE. - And I have used two other methods to examine the value in the FLASH (internal code and another debugger) and they both say the data is FF FF FF ....

So I'm not sure that the problem is me not seeing what is written - I think the data is not ever written !

I could be wrong of course .....

Thanks

Rachel

--- In 6..., "Edward Karpicz" wrote:
>
> How and where do you set up the FCLKDIV?
> It is not very clear what is purpose of copying code to ShadowStart. It
> would make sense to execute from RAM, because code will hang if you
> program/erase flash in the same bank like code. But you don't jump to RAM,
> isn't it?
>
> Changing PPAGE doesn't show how debugger is caching memory. Just find memory
> map in Hiwave and set up required checkboxes, else you won't see how flash
> changes on erase/program
>
> Edward
>
> ----- Original Message -----
> From: "andrew"
> To: <6...>
> Sent: Friday, February 17, 2012 8:40 PM
> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >
> >> What assembler and debugger are you using?
> >
> > I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
> >
> >> Certainly you meant here indexed addressing, so you should edit it to
> >> this
> >> form
> >
> > OK - I did this, but it has made no difference - I think that CW accepts
> > it the way I had it - but I agree it's not the 'correct' way to write it !
> >
> >> debugger by default is not refreshing flash and EEPROM memories from
> >> device
> >> memory.
> >
> > I thought that too. So I wrote some more code to read the contents after
> > the write - and it is all FF FF ...
> >
> > Also - I think the debugger is showing memory correctly - If I change
> > PPAGE to $3F and then go back to read address $8000 - i can see the top
> > page, as I should be able to.
> >
> > I also tried using ICD12Z to read the memory - it also indicates it is FF
> > FF ....
> >
> > I am lost !
> >
> > Rachel
> >
> >
> > --- In 6..., "Edward Karpicz" wrote:
> >>
> >> What assembler and debugger are you using?
> >>
> >> > MOVW X,Y
> >>
> >> ^^ this should copy the word from variable called X to variable called Y.
> >> Certainly you meant here indexed addressing, so you should edit it to
> >> this
> >> form
> >>
> >> MOVW 0,X, 0,Y
> >>
> >> > LDD Y
> >> > STD X
> >>
> >> ^^ Same here
> >>
> >> Provided assembler treat this like it was expected by you, Codewarrior
> >> debugger by default is not refreshing flash and EEPROM memories from
> >> device
> >> memory. So you see no change after erasing or programming. You need to
> >> edit
> >> memory mapping from debuggger menus and tell you want these memories to
> >> be
> >> refreshed on stop or something like that.
> >>
> >> Edward
> >>
> >> ----- Original Message -----
> >> From:
> >> To: <6...>
> >> Sent: Friday, February 17, 2012 3:07 AM
> >> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
> >>
> >>
> >> > Hi - I need some help please ....
> >> >
> >> > I'm trying to write some code to write to the Flash area on an
> >> > MC9S12DP512.
> >> >
> >> > But although it runs, and doesn't appear to throw any Acc or PViol
> >> > errors,
> >> > it also doesn't appear to actually write to the Flash - it remains @ FF
> >> > FF
> >> > ..
> >> >
> >> > What am I doing wrong ?
> >> >
> >> > I've looked the the FSL app notes, (AN2400 for example) and done some
> >> > digging around on here - and as far as I can see - I'm doing it
> >> > correctly - but obviously I'm not !
> >> >
> >> > Hope someone can help me ...
> >> >
> >> > Rachel
> >> >
> >> > Here is the code ...
> >> >
> >> >
> >> > ORG $5000
> >> >
> >> > ; Test to see if FLASH writing works
> >> > ; Copy from $5000 into RAM (ShadowStart)
> >> > ; Then Erase Sector 1 Page $20 (Bank 3)
> >> > ; Then write the data from RAM into Page $20
> >> >
> >> >
> >> >
> >> > TestFlashWrite:
> >> >
> >> > LDX #$5000
> >> > LDY #ShadowStart
> >> > CopyLoop:
> >> > MOVW X,Y
> >> > CPX #$53FE
> >> > BEQ CopyDone
> >> > INX
> >> > INX
> >> > INY
> >> > INY
> >> > BRA CopyLoop
> >> >
> >> > CopyDone:
> >> >
> >> >
> >> > ; Erase the Flash Sector
> >> >
> >> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
> >> >
> >> >
> >> > MOVB #$20,PPAGE
> >> > MOVB #$03,FCNFG
> >> >
> >> >
> >> >
> >> > ; Wait for Command Registers to be free
> >> >
> >> > BRCLR FSTAT,#$80,*
> >> >
> >> > ; Clear any outstanding PVIOL / ACCERR Errors
> >> > MOVB #$30,FSTAT
> >> >
> >> > ; Write any value, to the address of the sector we want to erase.
> >> >
> >> > MOVW #$0000,$8000
> >> >
> >> > ; Issue the 'Sector Erase' Command
> >> > MOVB #$40,FCMD
> >> >
> >> > ; Start the sequence
> >> > MOVB #$80,FSTAT
> >> >
> >> > NOP
> >> > NOP
> >> > NOP
> >> > NOP
> >> > NOP
> >> >
> >> > ; Check that command has completed
> >> > BRCLR FSTAT,#$40,*
> >> >
> >> > ; Check for Errors
> >> > BRSET FSTAT,#$20,FlashPviol
> >> > BRSET FSTAT,#$10,FlashAccErr
> >> >
> >> > ; ***** OK, we can start the programming now *****
> >> >
> >> >
> >> > LDX #$8000
> >> > LDY #ShadowStart
> >> >
> >> > ProgLoop:
> >> > ; Check that command has completed
> >> > BRCLR FSTAT,#$80,*
> >> >
> >> > LDD Y
> >> > STD X
> >> >
> >> > ; Issue the 'Program' Command
> >> > MOVB #$20,FCMD
> >> >
> >> > ; Start the sequence
> >> > BSET FSTAT,#$80
> >> >
> >> > NOP
> >> > NOP
> >> > NOP
> >> > NOP
> >> > NOP
> >> >
> >> > ; Check that command has completed
> >> > BRCLR FSTAT,#$40,*
> >> >
> >> > ; Check for Errors
> >> > BRSET FSTAT,#$20,FlashPviol
> >> > BRSET FSTAT,#$10,FlashAccErr
> >> >
> >> > ; Move onto next word
> >> > CPX #$83FE
> >> > BEQ ProgDone
> >> > INX
> >> > INX
> >> > INY
> >> > INY
> >> > BRA ProgLoop
> >> >
> >> > ProgDone:
> >> > MOVB #$08,PORTB
> >> >
> >> > RTS
> >> >
> >> > ; Loop around for ever, so we can catch in debug
> >> >
> >> > FlashPviol:
> >> > MOVB #$10,PORTB
> >> > BRA FlashPviol
> >> >
> >> > FlashAccErr:
> >> > MOVB #$1F,PORTB
> >> > BRA FlashAccErr
> >> >
> >> >
> >> >
> >> >
> >> >
> >> >
Except for X vs 0,X code looks almost right. One wrong thing left is that
you are clearing CBEIF flag using BSET instruction, which is clearing all
writeable FSTAT flags. But this shouldn't prevent launch of flash command.

Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
ACCERR bits set and this prevents flash operations in other banks? I would
try iterating over all BKSELx combinations and clearing error bits for all
banks.
Memory mapping menu should be available under PE multilink menu after
connection to device succeeds. For some reason this menu item is not visible
until you connect to device. (Ant this is why I can't tell you how it is
called exactly, I don't have access to it at the moment.) If you are using
other BDM adapters, then I dont't know how to disable flash caching.

Edward

----- Original Message -----
From: "andrew"
To: <6...>
Sent: Friday, February 17, 2012 9:18 PM
Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
>
>> How and where do you set up the FCLKDIV?
>
> MOVB #$4A,FCLKDIV ; Set speed of Flash , for a 16MHz Xtal
> MOVB #$00,FCNFG ; Disable Flash generated INTs
>
> Sorry - that was in the init routine, and I hadn't copied that across.
>
> The purpose of copying the code to RAM is not useful - when I finally get
> this to work, it will store some 'useful' data from RAM, into a page in
> FLASH - but for now, to simplfy whilst testing - I just copied some
> 'known' bytes into RAM, and tried to write them to FLASH.
>
> The data does get copied into the RAM - I can step through and see that
> filling up the RAM - so this part is not the problem I think.
>
>> Changing PPAGE doesn't show how debugger is caching memory. Just find
>> memory
>> map in Hiwave and set up required checkboxes, else you won't see how
>> flash
>> changes on erase/program
>
> I can't find any option for this in HIWAVE. - And I have used two other
> methods to examine the value in the FLASH (internal code and another
> debugger) and they both say the data is FF FF FF ....
>
> So I'm not sure that the problem is me not seeing what is written - I
> think the data is not ever written !
>
> I could be wrong of course .....
>
> Thanks
>
> Rachel
>
> --- In 6..., "Edward Karpicz" wrote:
>>
>> How and where do you set up the FCLKDIV?
>> It is not very clear what is purpose of copying code to ShadowStart. It
>> would make sense to execute from RAM, because code will hang if you
>> program/erase flash in the same bank like code. But you don't jump to
>> RAM,
>> isn't it?
>>
>> Changing PPAGE doesn't show how debugger is caching memory. Just find
>> memory
>> map in Hiwave and set up required checkboxes, else you won't see how
>> flash
>> changes on erase/program
>>
>> Edward
>>
>> ----- Original Message -----
>> From: "andrew"
>> To: <6...>
>> Sent: Friday, February 17, 2012 8:40 PM
>> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
>> >
>> >> What assembler and debugger are you using?
>> >
>> > I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
>> >
>> >> Certainly you meant here indexed addressing, so you should edit it to
>> >> this
>> >> form
>> >
>> > OK - I did this, but it has made no difference - I think that CW
>> > accepts
>> > it the way I had it - but I agree it's not the 'correct' way to write
>> > it !
>> >
>> >> debugger by default is not refreshing flash and EEPROM memories from
>> >> device
>> >> memory.
>> >
>> > I thought that too. So I wrote some more code to read the contents
>> > after
>> > the write - and it is all FF FF ...
>> >
>> > Also - I think the debugger is showing memory correctly - If I change
>> > PPAGE to $3F and then go back to read address $8000 - i can see the top
>> > page, as I should be able to.
>> >
>> > I also tried using ICD12Z to read the memory - it also indicates it is
>> > FF
>> > FF ....
>> >
>> > I am lost !
>> >
>> > Rachel
>> >
>> >
>> > --- In 6..., "Edward Karpicz" wrote:
>> >>
>> >> What assembler and debugger are you using?
>> >>
>> >> > MOVW X,Y
>> >>
>> >> ^^ this should copy the word from variable called X to variable called
>> >> Y.
>> >> Certainly you meant here indexed addressing, so you should edit it to
>> >> this
>> >> form
>> >>
>> >> MOVW 0,X, 0,Y
>> >>
>> >> > LDD Y
>> >> > STD X
>> >>
>> >> ^^ Same here
>> >>
>> >> Provided assembler treat this like it was expected by you, Codewarrior
>> >> debugger by default is not refreshing flash and EEPROM memories from
>> >> device
>> >> memory. So you see no change after erasing or programming. You need to
>> >> edit
>> >> memory mapping from debuggger menus and tell you want these memories
>> >> to
>> >> be
>> >> refreshed on stop or something like that.
>> >>
>> >> Edward
>> >>
>> >> ----- Original Message -----
>> >> From:
>> >> To: <6...>
>> >> Sent: Friday, February 17, 2012 3:07 AM
>> >> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
>> >>
>> >>
>> >> > Hi - I need some help please ....
>> >> >
>> >> > I'm trying to write some code to write to the Flash area on an
>> >> > MC9S12DP512.
>> >> >
>> >> > But although it runs, and doesn't appear to throw any Acc or PViol
>> >> > errors,
>> >> > it also doesn't appear to actually write to the Flash - it remains @
>> >> > FF
>> >> > FF
>> >> > ..
>> >> >
>> >> > What am I doing wrong ?
>> >> >
>> >> > I've looked the the FSL app notes, (AN2400 for example) and done
>> >> > some
>> >> > digging around on here - and as far as I can see - I'm doing it
>> >> > correctly - but obviously I'm not !
>> >> >
>> >> > Hope someone can help me ...
>> >> >
>> >> > Rachel
>> >> >
>> >> > Here is the code ...
>> >> >
>> >> >
>> >> > ORG $5000
>> >> >
>> >> > ; Test to see if FLASH writing works
>> >> > ; Copy from $5000 into RAM (ShadowStart)
>> >> > ; Then Erase Sector 1 Page $20 (Bank 3)
>> >> > ; Then write the data from RAM into Page $20
>> >> >
>> >> >
>> >> >
>> >> > TestFlashWrite:
>> >> >
>> >> > LDX #$5000
>> >> > LDY #ShadowStart
>> >> > CopyLoop:
>> >> > MOVW X,Y
>> >> > CPX #$53FE
>> >> > BEQ CopyDone
>> >> > INX
>> >> > INX
>> >> > INY
>> >> > INY
>> >> > BRA CopyLoop
>> >> >
>> >> > CopyDone:
>> >> >
>> >> >
>> >> > ; Erase the Flash Sector
>> >> >
>> >> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
>> >> >
>> >> >
>> >> > MOVB #$20,PPAGE
>> >> > MOVB #$03,FCNFG
>> >> >
>> >> >
>> >> >
>> >> > ; Wait for Command Registers to be free
>> >> >
>> >> > BRCLR FSTAT,#$80,*
>> >> >
>> >> > ; Clear any outstanding PVIOL / ACCERR Errors
>> >> > MOVB #$30,FSTAT
>> >> >
>> >> > ; Write any value, to the address of the sector we want to erase.
>> >> >
>> >> > MOVW #$0000,$8000
>> >> >
>> >> > ; Issue the 'Sector Erase' Command
>> >> > MOVB #$40,FCMD
>> >> >
>> >> > ; Start the sequence
>> >> > MOVB #$80,FSTAT
>> >> >
>> >> > NOP
>> >> > NOP
>> >> > NOP
>> >> > NOP
>> >> > NOP
>> >> >
>> >> > ; Check that command has completed
>> >> > BRCLR FSTAT,#$40,*
>> >> >
>> >> > ; Check for Errors
>> >> > BRSET FSTAT,#$20,FlashPviol
>> >> > BRSET FSTAT,#$10,FlashAccErr
>> >> >
>> >> > ; ***** OK, we can start the programming now *****
>> >> >
>> >> >
>> >> > LDX #$8000
>> >> > LDY #ShadowStart
>> >> >
>> >> > ProgLoop:
>> >> > ; Check that command has completed
>> >> > BRCLR FSTAT,#$80,*
>> >> >
>> >> > LDD Y
>> >> > STD X
>> >> >
>> >> > ; Issue the 'Program' Command
>> >> > MOVB #$20,FCMD
>> >> >
>> >> > ; Start the sequence
>> >> > BSET FSTAT,#$80
>> >> >
>> >> > NOP
>> >> > NOP
>> >> > NOP
>> >> > NOP
>> >> > NOP
>> >> >
>> >> > ; Check that command has completed
>> >> > BRCLR FSTAT,#$40,*
>> >> >
>> >> > ; Check for Errors
>> >> > BRSET FSTAT,#$20,FlashPviol
>> >> > BRSET FSTAT,#$10,FlashAccErr
>> >> >
>> >> > ; Move onto next word
>> >> > CPX #$83FE
>> >> > BEQ ProgDone
>> >> > INX
>> >> > INX
>> >> > INY
>> >> > INY
>> >> > BRA ProgLoop
>> >> >
>> >> > ProgDone:
>> >> > MOVB #$08,PORTB
>> >> >
>> >> > RTS
>> >> >
>> >> > ; Loop around for ever, so we can catch in debug
>> >> >
>> >> > FlashPviol:
>> >> > MOVB #$10,PORTB
>> >> > BRA FlashPviol
>> >> >
>> >> > FlashAccErr:
>> >> > MOVB #$1F,PORTB
>> >> > BRA FlashAccErr
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >
> Except for X vs 0,X code looks almost right. One wrong thing left is that
> you are clearing CBEIF flag using BSET instruction, which is clearing all
> writeable FSTAT flags.

OK - I've changed all the 'X' (or Y) to be 0,X - but it has made no difference.

My understanding is that using BSET is the correct way to set one bit - is this not correct ? The CPU Ref Man states the following ...

"Sets bits in memory location M. To set a bit, set the corresponding bit in the mask byte. All other bits in M are unchanged."

> Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
> ACCERR bits set and this prevents flash operations in other banks? I would
> try iterating over all BKSELx combinations and clearing error bits for all
> banks.

That is a godd suggestion - I must admit I haven't done that, I have only cleared them in the bank I am working in. I assumed that the other banks could be OK - but you should never assume should you !!

I shall try that and report back.

> Memory mapping menu should be available under PE multilink menu after
> connection to device succeeds.

There is a 'Memory Mapping' dialogue box - but I can't see how you tell it about updating of FLASH / cache etc

Thanks for your help.

Rachel

--- In 6..., "Edward Karpicz" wrote:
>
> Except for X vs 0,X code looks almost right. One wrong thing left is that
> you are clearing CBEIF flag using BSET instruction, which is clearing all
> writeable FSTAT flags. But this shouldn't prevent launch of flash command.
>
> Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
> ACCERR bits set and this prevents flash operations in other banks? I would
> try iterating over all BKSELx combinations and clearing error bits for all
> banks.
> Memory mapping menu should be available under PE multilink menu after
> connection to device succeeds. For some reason this menu item is not visible
> until you connect to device. (Ant this is why I can't tell you how it is
> called exactly, I don't have access to it at the moment.) If you are using
> other BDM adapters, then I dont't know how to disable flash caching.
>
> Edward
>
> ----- Original Message -----
> From: "andrew"
> To: <6...>
> Sent: Friday, February 17, 2012 9:18 PM
> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >
> >> How and where do you set up the FCLKDIV?
> >
> > MOVB #$4A,FCLKDIV ; Set speed of Flash , for a 16MHz Xtal
> > MOVB #$00,FCNFG ; Disable Flash generated INTs
> >
> > Sorry - that was in the init routine, and I hadn't copied that across.
> >
> > The purpose of copying the code to RAM is not useful - when I finally get
> > this to work, it will store some 'useful' data from RAM, into a page in
> > FLASH - but for now, to simplfy whilst testing - I just copied some
> > 'known' bytes into RAM, and tried to write them to FLASH.
> >
> > The data does get copied into the RAM - I can step through and see that
> > filling up the RAM - so this part is not the problem I think.
> >
> >> Changing PPAGE doesn't show how debugger is caching memory. Just find
> >> memory
> >> map in Hiwave and set up required checkboxes, else you won't see how
> >> flash
> >> changes on erase/program
> >
> > I can't find any option for this in HIWAVE. - And I have used two other
> > methods to examine the value in the FLASH (internal code and another
> > debugger) and they both say the data is FF FF FF ....
> >
> > So I'm not sure that the problem is me not seeing what is written - I
> > think the data is not ever written !
> >
> > I could be wrong of course .....
> >
> > Thanks
> >
> > Rachel
> >
> > --- In 6..., "Edward Karpicz" wrote:
> >>
> >> How and where do you set up the FCLKDIV?
> >> It is not very clear what is purpose of copying code to ShadowStart. It
> >> would make sense to execute from RAM, because code will hang if you
> >> program/erase flash in the same bank like code. But you don't jump to
> >> RAM,
> >> isn't it?
> >>
> >> Changing PPAGE doesn't show how debugger is caching memory. Just find
> >> memory
> >> map in Hiwave and set up required checkboxes, else you won't see how
> >> flash
> >> changes on erase/program
> >>
> >> Edward
> >>
> >> ----- Original Message -----
> >> From: "andrew"
> >> To: <6...>
> >> Sent: Friday, February 17, 2012 8:40 PM
> >> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >>
> >>
> >> >
> >> >> What assembler and debugger are you using?
> >> >
> >> > I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
> >> >
> >> >> Certainly you meant here indexed addressing, so you should edit it to
> >> >> this
> >> >> form
> >> >
> >> > OK - I did this, but it has made no difference - I think that CW
> >> > accepts
> >> > it the way I had it - but I agree it's not the 'correct' way to write
> >> > it !
> >> >
> >> >> debugger by default is not refreshing flash and EEPROM memories from
> >> >> device
> >> >> memory.
> >> >
> >> > I thought that too. So I wrote some more code to read the contents
> >> > after
> >> > the write - and it is all FF FF ...
> >> >
> >> > Also - I think the debugger is showing memory correctly - If I change
> >> > PPAGE to $3F and then go back to read address $8000 - i can see the top
> >> > page, as I should be able to.
> >> >
> >> > I also tried using ICD12Z to read the memory - it also indicates it is
> >> > FF
> >> > FF ....
> >> >
> >> > I am lost !
> >> >
> >> > Rachel
> >> >
> >> >
> >> > --- In 6..., "Edward Karpicz" wrote:
> >> >>
> >> >> What assembler and debugger are you using?
> >> >>
> >> >> > MOVW X,Y
> >> >>
> >> >> ^^ this should copy the word from variable called X to variable called
> >> >> Y.
> >> >> Certainly you meant here indexed addressing, so you should edit it to
> >> >> this
> >> >> form
> >> >>
> >> >> MOVW 0,X, 0,Y
> >> >>
> >> >> > LDD Y
> >> >> > STD X
> >> >>
> >> >> ^^ Same here
> >> >>
> >> >> Provided assembler treat this like it was expected by you, Codewarrior
> >> >> debugger by default is not refreshing flash and EEPROM memories from
> >> >> device
> >> >> memory. So you see no change after erasing or programming. You need to
> >> >> edit
> >> >> memory mapping from debuggger menus and tell you want these memories
> >> >> to
> >> >> be
> >> >> refreshed on stop or something like that.
> >> >>
> >> >> Edward
> >> >>
> >> >> ----- Original Message -----
> >> >> From:
> >> >> To: <6...>
> >> >> Sent: Friday, February 17, 2012 3:07 AM
> >> >> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
> >> >>
> >> >>
> >> >> > Hi - I need some help please ....
> >> >> >
> >> >> > I'm trying to write some code to write to the Flash area on an
> >> >> > MC9S12DP512.
> >> >> >
> >> >> > But although it runs, and doesn't appear to throw any Acc or PViol
> >> >> > errors,
> >> >> > it also doesn't appear to actually write to the Flash - it remains @
> >> >> > FF
> >> >> > FF
> >> >> > ..
> >> >> >
> >> >> > What am I doing wrong ?
> >> >> >
> >> >> > I've looked the the FSL app notes, (AN2400 for example) and done
> >> >> > some
> >> >> > digging around on here - and as far as I can see - I'm doing it
> >> >> > correctly - but obviously I'm not !
> >> >> >
> >> >> > Hope someone can help me ...
> >> >> >
> >> >> > Rachel
> >> >> >
> >> >> > Here is the code ...
> >> >> >
> >> >> >
> >> >> > ORG $5000
> >> >> >
> >> >> > ; Test to see if FLASH writing works
> >> >> > ; Copy from $5000 into RAM (ShadowStart)
> >> >> > ; Then Erase Sector 1 Page $20 (Bank 3)
> >> >> > ; Then write the data from RAM into Page $20
> >> >> >
> >> >> >
> >> >> >
> >> >> > TestFlashWrite:
> >> >> >
> >> >> > LDX #$5000
> >> >> > LDY #ShadowStart
> >> >> > CopyLoop:
> >> >> > MOVW X,Y
> >> >> > CPX #$53FE
> >> >> > BEQ CopyDone
> >> >> > INX
> >> >> > INX
> >> >> > INY
> >> >> > INY
> >> >> > BRA CopyLoop
> >> >> >
> >> >> > CopyDone:
> >> >> >
> >> >> >
> >> >> > ; Erase the Flash Sector
> >> >> >
> >> >> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
> >> >> >
> >> >> >
> >> >> > MOVB #$20,PPAGE
> >> >> > MOVB #$03,FCNFG
> >> >> >
> >> >> >
> >> >> >
> >> >> > ; Wait for Command Registers to be free
> >> >> >
> >> >> > BRCLR FSTAT,#$80,*
> >> >> >
> >> >> > ; Clear any outstanding PVIOL / ACCERR Errors
> >> >> > MOVB #$30,FSTAT
> >> >> >
> >> >> > ; Write any value, to the address of the sector we want to erase.
> >> >> >
> >> >> > MOVW #$0000,$8000
> >> >> >
> >> >> > ; Issue the 'Sector Erase' Command
> >> >> > MOVB #$40,FCMD
> >> >> >
> >> >> > ; Start the sequence
> >> >> > MOVB #$80,FSTAT
> >> >> >
> >> >> > NOP
> >> >> > NOP
> >> >> > NOP
> >> >> > NOP
> >> >> > NOP
> >> >> >
> >> >> > ; Check that command has completed
> >> >> > BRCLR FSTAT,#$40,*
> >> >> >
> >> >> > ; Check for Errors
> >> >> > BRSET FSTAT,#$20,FlashPviol
> >> >> > BRSET FSTAT,#$10,FlashAccErr
> >> >> >
> >> >> > ; ***** OK, we can start the programming now *****
> >> >> >
> >> >> >
> >> >> > LDX #$8000
> >> >> > LDY #ShadowStart
> >> >> >
> >> >> > ProgLoop:
> >> >> > ; Check that command has completed
> >> >> > BRCLR FSTAT,#$80,*
> >> >> >
> >> >> > LDD Y
> >> >> > STD X
> >> >> >
> >> >> > ; Issue the 'Program' Command
> >> >> > MOVB #$20,FCMD
> >> >> >
> >> >> > ; Start the sequence
> >> >> > BSET FSTAT,#$80
> >> >> >
> >> >> > NOP
> >> >> > NOP
> >> >> > NOP
> >> >> > NOP
> >> >> > NOP
> >> >> >
> >> >> > ; Check that command has completed
> >> >> > BRCLR FSTAT,#$40,*
> >> >> >
> >> >> > ; Check for Errors
> >> >> > BRSET FSTAT,#$20,FlashPviol
> >> >> > BRSET FSTAT,#$10,FlashAccErr
> >> >> >
> >> >> > ; Move onto next word
> >> >> > CPX #$83FE
> >> >> > BEQ ProgDone
> >> >> > INX
> >> >> > INX
> >> >> > INY
> >> >> > INY
> >> >> > BRA ProgLoop
> >> >> >
> >> >> > ProgDone:
> >> >> > MOVB #$08,PORTB
> >> >> >
> >> >> > RTS
> >> >> >
> >> >> > ; Loop around for ever, so we can catch in debug
> >> >> >
> >> >> > FlashPviol:
> >> >> > MOVB #$10,PORTB
> >> >> > BRA FlashPviol
> >> >> >
> >> >> > FlashAccErr:
> >> >> > MOVB #$1F,PORTB
> >> >> > BRA FlashAccErr
> >> >> >
> >> >> >
> >> >> >
> >> >> >
> >> >> >
> >> >> >
At long last - progress has been made !!!!

As Edward suggested - I have gone through all 4 banks and cleared the PVIOL & ACCERR flags like this ...

; Check that ACCERR & PVIOL are clear in all banks

MOVB #$00,FCNFG
MOVB #$30,FSTAT
MOVB #$01,FCNFG
MOVB #$30,FSTAT
MOVB #$02,FCNFG
MOVB #$30,FSTAT
MOVB #$03,FCNFG
MOVB #$30,FSTAT

The code now runs and the flash does get updated !!!

If you read the FTS_512K4 guide - it does tell you that if any ACCERR or PVIOL flag is set (including those in a bank you are not working in) then all command state machines are locked out. I had made two assumptions....

a) That because I hadn't done anything in the other three banks - the flags would not be set.

b) That when I tried to run the command - I would get an ACCERR or PVIOL flag being set, if something was wrong.

WELL I WAS WRONG !

You don't get a flag set - the command just doesn't run, despite you asking it to.

Thanks to Edward for pointing out my error.

Now I just need to figure out how to move the code into RAM so I can program the all 4 banks.

Thanks

Rachel

--- In 6..., "andrew" wrote:
>
> > Except for X vs 0,X code looks almost right. One wrong thing left is that
> > you are clearing CBEIF flag using BSET instruction, which is clearing all
> > writeable FSTAT flags.
>
> OK - I've changed all the 'X' (or Y) to be 0,X - but it has made no difference.
>
> My understanding is that using BSET is the correct way to set one bit - is this not correct ? The CPU Ref Man states the following ...
>
> "Sets bits in memory location M. To set a bit, set the corresponding bit in the mask byte. All other bits in M are unchanged."
>
> > Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
> > ACCERR bits set and this prevents flash operations in other banks? I would
> > try iterating over all BKSELx combinations and clearing error bits for all
> > banks.
>
> That is a godd suggestion - I must admit I haven't done that, I have only cleared them in the bank I am working in. I assumed that the other banks could be OK - but you should never assume should you !!
>
> I shall try that and report back.
>
> > Memory mapping menu should be available under PE multilink menu after
> > connection to device succeeds.
>
> There is a 'Memory Mapping' dialogue box - but I can't see how you tell it about updating of FLASH / cache etc
>
> Thanks for your help.
>
> Rachel
>
> --- In 6..., "Edward Karpicz" wrote:
> >
> > Except for X vs 0,X code looks almost right. One wrong thing left is that
> > you are clearing CBEIF flag using BSET instruction, which is clearing all
> > writeable FSTAT flags. But this shouldn't prevent launch of flash command.
> >
> > Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
> > ACCERR bits set and this prevents flash operations in other banks? I would
> > try iterating over all BKSELx combinations and clearing error bits for all
> > banks.
> >
> >
> > Memory mapping menu should be available under PE multilink menu after
> > connection to device succeeds. For some reason this menu item is not visible
> > until you connect to device. (Ant this is why I can't tell you how it is
> > called exactly, I don't have access to it at the moment.) If you are using
> > other BDM adapters, then I dont't know how to disable flash caching.
> >
> > Edward
> >
> > ----- Original Message -----
> > From: "andrew"
> > To: <6...>
> > Sent: Friday, February 17, 2012 9:18 PM
> > Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >
> >
> > >
> > >> How and where do you set up the FCLKDIV?
> > >
> > > MOVB #$4A,FCLKDIV ; Set speed of Flash , for a 16MHz Xtal
> > > MOVB #$00,FCNFG ; Disable Flash generated INTs
> > >
> > > Sorry - that was in the init routine, and I hadn't copied that across.
> > >
> > > The purpose of copying the code to RAM is not useful - when I finally get
> > > this to work, it will store some 'useful' data from RAM, into a page in
> > > FLASH - but for now, to simplfy whilst testing - I just copied some
> > > 'known' bytes into RAM, and tried to write them to FLASH.
> > >
> > > The data does get copied into the RAM - I can step through and see that
> > > filling up the RAM - so this part is not the problem I think.
> > >
> > >> Changing PPAGE doesn't show how debugger is caching memory. Just find
> > >> memory
> > >> map in Hiwave and set up required checkboxes, else you won't see how
> > >> flash
> > >> changes on erase/program
> > >
> > > I can't find any option for this in HIWAVE. - And I have used two other
> > > methods to examine the value in the FLASH (internal code and another
> > > debugger) and they both say the data is FF FF FF ....
> > >
> > > So I'm not sure that the problem is me not seeing what is written - I
> > > think the data is not ever written !
> > >
> > > I could be wrong of course .....
> > >
> > > Thanks
> > >
> > > Rachel
> > >
> > > --- In 6..., "Edward Karpicz" wrote:
> > >>
> > >> How and where do you set up the FCLKDIV?
> > >> It is not very clear what is purpose of copying code to ShadowStart. It
> > >> would make sense to execute from RAM, because code will hang if you
> > >> program/erase flash in the same bank like code. But you don't jump to
> > >> RAM,
> > >> isn't it?
> > >>
> > >> Changing PPAGE doesn't show how debugger is caching memory. Just find
> > >> memory
> > >> map in Hiwave and set up required checkboxes, else you won't see how
> > >> flash
> > >> changes on erase/program
> > >>
> > >> Edward
> > >>
> > >> ----- Original Message -----
> > >> From: "andrew"
> > >> To: <6...>
> > >> Sent: Friday, February 17, 2012 8:40 PM
> > >> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> > >>
> > >>
> > >> >
> > >> >> What assembler and debugger are you using?
> > >> >
> > >> > I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
> > >> >
> > >> >> Certainly you meant here indexed addressing, so you should edit it to
> > >> >> this
> > >> >> form
> > >> >
> > >> > OK - I did this, but it has made no difference - I think that CW
> > >> > accepts
> > >> > it the way I had it - but I agree it's not the 'correct' way to write
> > >> > it !
> > >> >
> > >> >> debugger by default is not refreshing flash and EEPROM memories from
> > >> >> device
> > >> >> memory.
> > >> >
> > >> > I thought that too. So I wrote some more code to read the contents
> > >> > after
> > >> > the write - and it is all FF FF ...
> > >> >
> > >> > Also - I think the debugger is showing memory correctly - If I change
> > >> > PPAGE to $3F and then go back to read address $8000 - i can see the top
> > >> > page, as I should be able to.
> > >> >
> > >> > I also tried using ICD12Z to read the memory - it also indicates it is
> > >> > FF
> > >> > FF ....
> > >> >
> > >> > I am lost !
> > >> >
> > >> > Rachel
> > >> >
> > >> >
> > >> > --- In 6..., "Edward Karpicz" wrote:
> > >> >>
> > >> >> What assembler and debugger are you using?
> > >> >>
> > >> >> > MOVW X,Y
> > >> >>
> > >> >> ^^ this should copy the word from variable called X to variable called
> > >> >> Y.
> > >> >> Certainly you meant here indexed addressing, so you should edit it to
> > >> >> this
> > >> >> form
> > >> >>
> > >> >> MOVW 0,X, 0,Y
> > >> >>
> > >> >> > LDD Y
> > >> >> > STD X
> > >> >>
> > >> >> ^^ Same here
> > >> >>
> > >> >> Provided assembler treat this like it was expected by you, Codewarrior
> > >> >> debugger by default is not refreshing flash and EEPROM memories from
> > >> >> device
> > >> >> memory. So you see no change after erasing or programming. You need to
> > >> >> edit
> > >> >> memory mapping from debuggger menus and tell you want these memories
> > >> >> to
> > >> >> be
> > >> >> refreshed on stop or something like that.
> > >> >>
> > >> >> Edward
> > >> >>
> > >> >> ----- Original Message -----
> > >> >> From:
> > >> >> To: <6...>
> > >> >> Sent: Friday, February 17, 2012 3:07 AM
> > >> >> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
> > >> >>
> > >> >>
> > >> >> > Hi - I need some help please ....
> > >> >> >
> > >> >> > I'm trying to write some code to write to the Flash area on an
> > >> >> > MC9S12DP512.
> > >> >> >
> > >> >> > But although it runs, and doesn't appear to throw any Acc or PViol
> > >> >> > errors,
> > >> >> > it also doesn't appear to actually write to the Flash - it remains @
> > >> >> > FF
> > >> >> > FF
> > >> >> > ..
> > >> >> >
> > >> >> > What am I doing wrong ?
> > >> >> >
> > >> >> > I've looked the the FSL app notes, (AN2400 for example) and done
> > >> >> > some
> > >> >> > digging around on here - and as far as I can see - I'm doing it
> > >> >> > correctly - but obviously I'm not !
> > >> >> >
> > >> >> > Hope someone can help me ...
> > >> >> >
> > >> >> > Rachel
> > >> >> >
> > >> >> > Here is the code ...
> > >> >> >
> > >> >> >
> > >> >> > ORG $5000
> > >> >> >
> > >> >> > ; Test to see if FLASH writing works
> > >> >> > ; Copy from $5000 into RAM (ShadowStart)
> > >> >> > ; Then Erase Sector 1 Page $20 (Bank 3)
> > >> >> > ; Then write the data from RAM into Page $20
> > >> >> >
> > >> >> >
> > >> >> >
> > >> >> > TestFlashWrite:
> > >> >> >
> > >> >> > LDX #$5000
> > >> >> > LDY #ShadowStart
> > >> >> > CopyLoop:
> > >> >> > MOVW X,Y
> > >> >> > CPX #$53FE
> > >> >> > BEQ CopyDone
> > >> >> > INX
> > >> >> > INX
> > >> >> > INY
> > >> >> > INY
> > >> >> > BRA CopyLoop
> > >> >> >
> > >> >> > CopyDone:
> > >> >> >
> > >> >> >
> > >> >> > ; Erase the Flash Sector
> > >> >> >
> > >> >> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
> > >> >> >
> > >> >> >
> > >> >> > MOVB #$20,PPAGE
> > >> >> > MOVB #$03,FCNFG
> > >> >> >
> > >> >> >
> > >> >> >
> > >> >> > ; Wait for Command Registers to be free
> > >> >> >
> > >> >> > BRCLR FSTAT,#$80,*
> > >> >> >
> > >> >> > ; Clear any outstanding PVIOL / ACCERR Errors
> > >> >> > MOVB #$30,FSTAT
> > >> >> >
> > >> >> > ; Write any value, to the address of the sector we want to erase.
> > >> >> >
> > >> >> > MOVW #$0000,$8000
> > >> >> >
> > >> >> > ; Issue the 'Sector Erase' Command
> > >> >> > MOVB #$40,FCMD
> > >> >> >
> > >> >> > ; Start the sequence
> > >> >> > MOVB #$80,FSTAT
> > >> >> >
> > >> >> > NOP
> > >> >> > NOP
> > >> >> > NOP
> > >> >> > NOP
> > >> >> > NOP
> > >> >> >
> > >> >> > ; Check that command has completed
> > >> >> > BRCLR FSTAT,#$40,*
> > >> >> >
> > >> >> > ; Check for Errors
> > >> >> > BRSET FSTAT,#$20,FlashPviol
> > >> >> > BRSET FSTAT,#$10,FlashAccErr
> > >> >> >
> > >> >> > ; ***** OK, we can start the programming now *****
> > >> >> >
> > >> >> >
> > >> >> > LDX #$8000
> > >> >> > LDY #ShadowStart
> > >> >> >
> > >> >> > ProgLoop:
> > >> >> > ; Check that command has completed
> > >> >> > BRCLR FSTAT,#$80,*
> > >> >> >
> > >> >> > LDD Y
> > >> >> > STD X
> > >> >> >
> > >> >> > ; Issue the 'Program' Command
> > >> >> > MOVB #$20,FCMD
> > >> >> >
> > >> >> > ; Start the sequence
> > >> >> > BSET FSTAT,#$80
> > >> >> >
> > >> >> > NOP
> > >> >> > NOP
> > >> >> > NOP
> > >> >> > NOP
> > >> >> > NOP
> > >> >> >
> > >> >> > ; Check that command has completed
> > >> >> > BRCLR FSTAT,#$40,*
> > >> >> >
> > >> >> > ; Check for Errors
> > >> >> > BRSET FSTAT,#$20,FlashPviol
> > >> >> > BRSET FSTAT,#$10,FlashAccErr
> > >> >> >
> > >> >> > ; Move onto next word
> > >> >> > CPX #$83FE
> > >> >> > BEQ ProgDone
> > >> >> > INX
> > >> >> > INX
> > >> >> > INY
> > >> >> > INY
> > >> >> > BRA ProgLoop
> > >> >> >
> > >> >> > ProgDone:
> > >> >> > MOVB #$08,PORTB
> > >> >> >
> > >> >> > RTS
> > >> >> >
> > >> >> > ; Loop around for ever, so we can catch in debug
> > >> >> >
> > >> >> > FlashPviol:
> > >> >> > MOVB #$10,PORTB
> > >> >> > BRA FlashPviol
> > >> >> >
> > >> >> > FlashAccErr:
> > >> >> > MOVB #$1F,PORTB
> > >> >> > BRA FlashAccErr
> > >> >> >
> > >> >> >
> > >> >> >
> > >> >> >
> > >> >> >
> > >> >> >
> My understanding is that using BSET is the correct way to set one bit - is
> this not correct ? The CPU Ref Man states the following ...
>
> "Sets bits in memory location M. To set a bit, set the corresponding bit
> in the mask byte. All other bits in M are unchanged."

(I wonder how many times I wrote about this here and on Freescale forums.)
BSET is correct way to set one or more bits in regular byte of memory.
Registers like FSTAT, which have more than one bit, which are clearable
writing '1' to them, are not compatible:
- with BSET instruction
- with sequences like LDAA FSTAT- ORAA 0x80 - STAA FSTAT
- with C statements like FSTAT |= 0x80
- with C bitfields, like those defined in CodeWarrior header files. For
example FSTAT_CBEIF = 1
All these 4 are wrong and have sideeffects.

This is so because individual bits are not addressable. Only whole bytes are
addressable. BSET instruction operates the same way like sequnce LDAA
FSTAT - ORAA #bitpattern - STAA FSTAT. Bits, which you didn't tell BSET
to set are PRESERVED. So, for example BSET with FSTAT will write ones to all
FSTAT bits that are set and will clear them all.

To clear just CBEIF, you need to either
MOVB #0x80, FSTAT
or
BCLR FSTAT, #0x7F; 0x7F = ~0x80

In C
FSTAT = 0x80;
or
FSTAT &= 0x80; // note no ~ sign on the right

So be aware with timer flags, CAN module flags, port interrupt flags etc
etc.

Edward

----- Original Message -----
From: "andrew"
To: <6...>
Sent: Saturday, February 18, 2012 2:48 PM
Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
>> Except for X vs 0,X code looks almost right. One wrong thing left is that
>> you are clearing CBEIF flag using BSET instruction, which is clearing all
>> writeable FSTAT flags.
>
> OK - I've changed all the 'X' (or Y) to be 0,X - but it has made no
> difference.
>
> My understanding is that using BSET is the correct way to set one bit - is
> this not correct ? The CPU Ref Man states the following ...
>
> "Sets bits in memory location M. To set a bit, set the corresponding bit
> in the mask byte. All other bits in M are unchanged."
>
>> Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
>> ACCERR bits set and this prevents flash operations in other banks? I
>> would
>> try iterating over all BKSELx combinations and clearing error bits for
>> all
>> banks.
>
> That is a godd suggestion - I must admit I haven't done that, I have only
> cleared them in the bank I am working in. I assumed that the other banks
> could be OK - but you should never assume should you !!
>
> I shall try that and report back.
>
>> Memory mapping menu should be available under PE multilink menu after
>> connection to device succeeds.
>
> There is a 'Memory Mapping' dialogue box - but I can't see how you tell it
> about updating of FLASH / cache etc
>
> Thanks for your help.
>
> Rachel
>
> --- In 6..., "Edward Karpicz" wrote:
>>
>> Except for X vs 0,X code looks almost right. One wrong thing left is that
>> you are clearing CBEIF flag using BSET instruction, which is clearing all
>> writeable FSTAT flags. But this shouldn't prevent launch of flash
>> command.
>>
>> Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
>> ACCERR bits set and this prevents flash operations in other banks? I
>> would
>> try iterating over all BKSELx combinations and clearing error bits for
>> all
>> banks.
>> Memory mapping menu should be available under PE multilink menu after
>> connection to device succeeds. For some reason this menu item is not
>> visible
>> until you connect to device. (Ant this is why I can't tell you how it is
>> called exactly, I don't have access to it at the moment.) If you are
>> using
>> other BDM adapters, then I dont't know how to disable flash caching.
>>
>> Edward
>>
>> ----- Original Message -----
>> From: "andrew"
>> To: <6...>
>> Sent: Friday, February 17, 2012 9:18 PM
>> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
>> >
>> >> How and where do you set up the FCLKDIV?
>> >
>> > MOVB #$4A,FCLKDIV ; Set speed of Flash , for a 16MHz Xtal
>> > MOVB #$00,FCNFG ; Disable Flash generated INTs
>> >
>> > Sorry - that was in the init routine, and I hadn't copied that across.
>> >
>> > The purpose of copying the code to RAM is not useful - when I finally
>> > get
>> > this to work, it will store some 'useful' data from RAM, into a page in
>> > FLASH - but for now, to simplfy whilst testing - I just copied some
>> > 'known' bytes into RAM, and tried to write them to FLASH.
>> >
>> > The data does get copied into the RAM - I can step through and see that
>> > filling up the RAM - so this part is not the problem I think.
>> >
>> >> Changing PPAGE doesn't show how debugger is caching memory. Just find
>> >> memory
>> >> map in Hiwave and set up required checkboxes, else you won't see how
>> >> flash
>> >> changes on erase/program
>> >
>> > I can't find any option for this in HIWAVE. - And I have used two other
>> > methods to examine the value in the FLASH (internal code and another
>> > debugger) and they both say the data is FF FF FF ....
>> >
>> > So I'm not sure that the problem is me not seeing what is written - I
>> > think the data is not ever written !
>> >
>> > I could be wrong of course .....
>> >
>> > Thanks
>> >
>> > Rachel
>> >
>> > --- In 6..., "Edward Karpicz" wrote:
>> >>
>> >> How and where do you set up the FCLKDIV?
>> >> It is not very clear what is purpose of copying code to ShadowStart.
>> >> It
>> >> would make sense to execute from RAM, because code will hang if you
>> >> program/erase flash in the same bank like code. But you don't jump to
>> >> RAM,
>> >> isn't it?
>> >>
>> >> Changing PPAGE doesn't show how debugger is caching memory. Just find
>> >> memory
>> >> map in Hiwave and set up required checkboxes, else you won't see how
>> >> flash
>> >> changes on erase/program
>> >>
>> >> Edward
>> >>
>> >> ----- Original Message -----
>> >> From: "andrew"
>> >> To: <6...>
>> >> Sent: Friday, February 17, 2012 8:40 PM
>> >> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
>> >>
>> >>
>> >> >
>> >> >> What assembler and debugger are you using?
>> >> >
>> >> > I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
>> >> >
>> >> >> Certainly you meant here indexed addressing, so you should edit it
>> >> >> to
>> >> >> this
>> >> >> form
>> >> >
>> >> > OK - I did this, but it has made no difference - I think that CW
>> >> > accepts
>> >> > it the way I had it - but I agree it's not the 'correct' way to
>> >> > write
>> >> > it !
>> >> >
>> >> >> debugger by default is not refreshing flash and EEPROM memories
>> >> >> from
>> >> >> device
>> >> >> memory.
>> >> >
>> >> > I thought that too. So I wrote some more code to read the contents
>> >> > after
>> >> > the write - and it is all FF FF ...
>> >> >
>> >> > Also - I think the debugger is showing memory correctly - If I
>> >> > change
>> >> > PPAGE to $3F and then go back to read address $8000 - i can see the
>> >> > top
>> >> > page, as I should be able to.
>> >> >
>> >> > I also tried using ICD12Z to read the memory - it also indicates it
>> >> > is
>> >> > FF
>> >> > FF ....
>> >> >
>> >> > I am lost !
>> >> >
>> >> > Rachel
>> >> >
>> >> >
>> >> > --- In 6..., "Edward Karpicz" wrote:
>> >> >>
>> >> >> What assembler and debugger are you using?
>> >> >>
>> >> >> > MOVW X,Y
>> >> >>
>> >> >> ^^ this should copy the word from variable called X to variable
>> >> >> called
>> >> >> Y.
>> >> >> Certainly you meant here indexed addressing, so you should edit it
>> >> >> to
>> >> >> this
>> >> >> form
>> >> >>
>> >> >> MOVW 0,X, 0,Y
>> >> >>
>> >> >> > LDD Y
>> >> >> > STD X
>> >> >>
>> >> >> ^^ Same here
>> >> >>
>> >> >> Provided assembler treat this like it was expected by you,
>> >> >> Codewarrior
>> >> >> debugger by default is not refreshing flash and EEPROM memories
>> >> >> from
>> >> >> device
>> >> >> memory. So you see no change after erasing or programming. You need
>> >> >> to
>> >> >> edit
>> >> >> memory mapping from debuggger menus and tell you want these
>> >> >> memories
>> >> >> to
>> >> >> be
>> >> >> refreshed on stop or something like that.
>> >> >>
>> >> >> Edward
>> >> >>
>> >> >> ----- Original Message -----
>> >> >> From:
>> >> >> To: <6...>
>> >> >> Sent: Friday, February 17, 2012 3:07 AM
>> >> >> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
>> >> >>
>> >> >>
>> >> >> > Hi - I need some help please ....
>> >> >> >
>> >> >> > I'm trying to write some code to write to the Flash area on an
>> >> >> > MC9S12DP512.
>> >> >> >
>> >> >> > But although it runs, and doesn't appear to throw any Acc or
>> >> >> > PViol
>> >> >> > errors,
>> >> >> > it also doesn't appear to actually write to the Flash - it
>> >> >> > remains @
>> >> >> > FF
>> >> >> > FF
>> >> >> > ..
>> >> >> >
>> >> >> > What am I doing wrong ?
>> >> >> >
>> >> >> > I've looked the the FSL app notes, (AN2400 for example) and done
>> >> >> > some
>> >> >> > digging around on here - and as far as I can see - I'm doing it
>> >> >> > correctly - but obviously I'm not !
>> >> >> >
>> >> >> > Hope someone can help me ...
>> >> >> >
>> >> >> > Rachel
>> >> >> >
>> >> >> > Here is the code ...
>> >> >> >
>> >> >> >
>> >> >> > ORG $5000
>> >> >> >
>> >> >> > ; Test to see if FLASH writing works
>> >> >> > ; Copy from $5000 into RAM (ShadowStart)
>> >> >> > ; Then Erase Sector 1 Page $20 (Bank 3)
>> >> >> > ; Then write the data from RAM into Page $20
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> > TestFlashWrite:
>> >> >> >
>> >> >> > LDX #$5000
>> >> >> > LDY #ShadowStart
>> >> >> > CopyLoop:
>> >> >> > MOVW X,Y
>> >> >> > CPX #$53FE
>> >> >> > BEQ CopyDone
>> >> >> > INX
>> >> >> > INX
>> >> >> > INY
>> >> >> > INY
>> >> >> > BRA CopyLoop
>> >> >> >
>> >> >> > CopyDone:
>> >> >> >
>> >> >> >
>> >> >> > ; Erase the Flash Sector
>> >> >> >
>> >> >> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
>> >> >> >
>> >> >> >
>> >> >> > MOVB #$20,PPAGE
>> >> >> > MOVB #$03,FCNFG
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> > ; Wait for Command Registers to be free
>> >> >> >
>> >> >> > BRCLR FSTAT,#$80,*
>> >> >> >
>> >> >> > ; Clear any outstanding PVIOL / ACCERR Errors
>> >> >> > MOVB #$30,FSTAT
>> >> >> >
>> >> >> > ; Write any value, to the address of the sector we want to erase.
>> >> >> >
>> >> >> > MOVW #$0000,$8000
>> >> >> >
>> >> >> > ; Issue the 'Sector Erase' Command
>> >> >> > MOVB #$40,FCMD
>> >> >> >
>> >> >> > ; Start the sequence
>> >> >> > MOVB #$80,FSTAT
>> >> >> >
>> >> >> > NOP
>> >> >> > NOP
>> >> >> > NOP
>> >> >> > NOP
>> >> >> > NOP
>> >> >> >
>> >> >> > ; Check that command has completed
>> >> >> > BRCLR FSTAT,#$40,*
>> >> >> >
>> >> >> > ; Check for Errors
>> >> >> > BRSET FSTAT,#$20,FlashPviol
>> >> >> > BRSET FSTAT,#$10,FlashAccErr
>> >> >> >
>> >> >> > ; ***** OK, we can start the programming now *****
>> >> >> >
>> >> >> >
>> >> >> > LDX #$8000
>> >> >> > LDY #ShadowStart
>> >> >> >
>> >> >> > ProgLoop:
>> >> >> > ; Check that command has completed
>> >> >> > BRCLR FSTAT,#$80,*
>> >> >> >
>> >> >> > LDD Y
>> >> >> > STD X
>> >> >> >
>> >> >> > ; Issue the 'Program' Command
>> >> >> > MOVB #$20,FCMD
>> >> >> >
>> >> >> > ; Start the sequence
>> >> >> > BSET FSTAT,#$80
>> >> >> >
>> >> >> > NOP
>> >> >> > NOP
>> >> >> > NOP
>> >> >> > NOP
>> >> >> > NOP
>> >> >> >
>> >> >> > ; Check that command has completed
>> >> >> > BRCLR FSTAT,#$40,*
>> >> >> >
>> >> >> > ; Check for Errors
>> >> >> > BRSET FSTAT,#$20,FlashPviol
>> >> >> > BRSET FSTAT,#$10,FlashAccErr
>> >> >> >
>> >> >> > ; Move onto next word
>> >> >> > CPX #$83FE
>> >> >> > BEQ ProgDone
>> >> >> > INX
>> >> >> > INX
>> >> >> > INY
>> >> >> > INY
>> >> >> > BRA ProgLoop
>> >> >> >
>> >> >> > ProgDone:
>> >> >> > MOVB #$08,PORTB
>> >> >> >
>> >> >> > RTS
>> >> >> >
>> >> >> > ; Loop around for ever, so we can catch in debug
>> >> >> >
>> >> >> > FlashPviol:
>> >> >> > MOVB #$10,PORTB
>> >> >> > BRA FlashPviol
>> >> >> >
>> >> >> > FlashAccErr:
>> >> >> > MOVB #$1F,PORTB
>> >> >> > BRA FlashAccErr
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> >
Thank you for that - I have learned something there - I will need to look at the rest of my code, perhaps I am making mistakes like that elsewhere ... ??

I have no started to try and make the routine do something useful, and I have broken it again !

So I am going to back track and see what I have done wrong. Doh !

Rachel

--- In 6..., "Edward Karpicz" wrote:
>
> > My understanding is that using BSET is the correct way to set one bit - is
> > this not correct ? The CPU Ref Man states the following ...
> >
> > "Sets bits in memory location M. To set a bit, set the corresponding bit
> > in the mask byte. All other bits in M are unchanged."
>
> (I wonder how many times I wrote about this here and on Freescale forums.)
> BSET is correct way to set one or more bits in regular byte of memory.
> Registers like FSTAT, which have more than one bit, which are clearable
> writing '1' to them, are not compatible:
> - with BSET instruction
> - with sequences like LDAA FSTAT- ORAA 0x80 - STAA FSTAT
> - with C statements like FSTAT |= 0x80
> - with C bitfields, like those defined in CodeWarrior header files. For
> example FSTAT_CBEIF = 1
> All these 4 are wrong and have sideeffects.
>
> This is so because individual bits are not addressable. Only whole bytes are
> addressable. BSET instruction operates the same way like sequnce LDAA
> FSTAT - ORAA #bitpattern - STAA FSTAT. Bits, which you didn't tell BSET
> to set are PRESERVED. So, for example BSET with FSTAT will write ones to all
> FSTAT bits that are set and will clear them all.
>
> To clear just CBEIF, you need to either
> MOVB #0x80, FSTAT
> or
> BCLR FSTAT, #0x7F; 0x7F = ~0x80
>
> In C
> FSTAT = 0x80;
> or
> FSTAT &= 0x80; // note no ~ sign on the right
>
> So be aware with timer flags, CAN module flags, port interrupt flags etc
> etc.
>
> Edward
>
> ----- Original Message -----
> From: "andrew"
> To: <6...>
> Sent: Saturday, February 18, 2012 2:48 PM
> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >> Except for X vs 0,X code looks almost right. One wrong thing left is that
> >> you are clearing CBEIF flag using BSET instruction, which is clearing all
> >> writeable FSTAT flags.
> >
> > OK - I've changed all the 'X' (or Y) to be 0,X - but it has made no
> > difference.
> >
> > My understanding is that using BSET is the correct way to set one bit - is
> > this not correct ? The CPU Ref Man states the following ...
> >
> > "Sets bits in memory location M. To set a bit, set the corresponding bit
> > in the mask byte. All other bits in M are unchanged."
> >
> >> Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
> >> ACCERR bits set and this prevents flash operations in other banks? I
> >> would
> >> try iterating over all BKSELx combinations and clearing error bits for
> >> all
> >> banks.
> >
> > That is a godd suggestion - I must admit I haven't done that, I have only
> > cleared them in the bank I am working in. I assumed that the other banks
> > could be OK - but you should never assume should you !!
> >
> > I shall try that and report back.
> >
> >> Memory mapping menu should be available under PE multilink menu after
> >> connection to device succeeds.
> >
> > There is a 'Memory Mapping' dialogue box - but I can't see how you tell it
> > about updating of FLASH / cache etc
> >
> > Thanks for your help.
> >
> > Rachel
> >
> >
> >
> > --- In 6..., "Edward Karpicz" wrote:
> >>
> >> Except for X vs 0,X code looks almost right. One wrong thing left is that
> >> you are clearing CBEIF flag using BSET instruction, which is clearing all
> >> writeable FSTAT flags. But this shouldn't prevent launch of flash
> >> command.
> >>
> >> Since 12DP512 has 4 flash banks, maybe one of these banks has PVIOL or
> >> ACCERR bits set and this prevents flash operations in other banks? I
> >> would
> >> try iterating over all BKSELx combinations and clearing error bits for
> >> all
> >> banks.
> >>
> >>
> >> Memory mapping menu should be available under PE multilink menu after
> >> connection to device succeeds. For some reason this menu item is not
> >> visible
> >> until you connect to device. (Ant this is why I can't tell you how it is
> >> called exactly, I don't have access to it at the moment.) If you are
> >> using
> >> other BDM adapters, then I dont't know how to disable flash caching.
> >>
> >> Edward
> >>
> >> ----- Original Message -----
> >> From: "andrew"
> >> To: <6...>
> >> Sent: Friday, February 17, 2012 9:18 PM
> >> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >>
> >>
> >> >
> >> >> How and where do you set up the FCLKDIV?
> >> >
> >> > MOVB #$4A,FCLKDIV ; Set speed of Flash , for a 16MHz Xtal
> >> > MOVB #$00,FCNFG ; Disable Flash generated INTs
> >> >
> >> > Sorry - that was in the init routine, and I hadn't copied that across.
> >> >
> >> > The purpose of copying the code to RAM is not useful - when I finally
> >> > get
> >> > this to work, it will store some 'useful' data from RAM, into a page in
> >> > FLASH - but for now, to simplfy whilst testing - I just copied some
> >> > 'known' bytes into RAM, and tried to write them to FLASH.
> >> >
> >> > The data does get copied into the RAM - I can step through and see that
> >> > filling up the RAM - so this part is not the problem I think.
> >> >
> >> >> Changing PPAGE doesn't show how debugger is caching memory. Just find
> >> >> memory
> >> >> map in Hiwave and set up required checkboxes, else you won't see how
> >> >> flash
> >> >> changes on erase/program
> >> >
> >> > I can't find any option for this in HIWAVE. - And I have used two other
> >> > methods to examine the value in the FLASH (internal code and another
> >> > debugger) and they both say the data is FF FF FF ....
> >> >
> >> > So I'm not sure that the problem is me not seeing what is written - I
> >> > think the data is not ever written !
> >> >
> >> > I could be wrong of course .....
> >> >
> >> > Thanks
> >> >
> >> > Rachel
> >> >
> >> > --- In 6..., "Edward Karpicz" wrote:
> >> >>
> >> >> How and where do you set up the FCLKDIV?
> >> >> It is not very clear what is purpose of copying code to ShadowStart.
> >> >> It
> >> >> would make sense to execute from RAM, because code will hang if you
> >> >> program/erase flash in the same bank like code. But you don't jump to
> >> >> RAM,
> >> >> isn't it?
> >> >>
> >> >> Changing PPAGE doesn't show how debugger is caching memory. Just find
> >> >> memory
> >> >> map in Hiwave and set up required checkboxes, else you won't see how
> >> >> flash
> >> >> changes on erase/program
> >> >>
> >> >> Edward
> >> >>
> >> >> ----- Original Message -----
> >> >> From: "andrew"
> >> >> To: <6...>
> >> >> Sent: Friday, February 17, 2012 8:40 PM
> >> >> Subject: [68HC12] Re: Flash Program Routine - Appears To Do Nothing
> >> >>
> >> >>
> >> >> >
> >> >> >> What assembler and debugger are you using?
> >> >> >
> >> >> > I'm using CW 5.9 - and the debugger is the inbuilt (HIWAVE ?)
> >> >> >
> >> >> >> Certainly you meant here indexed addressing, so you should edit it
> >> >> >> to
> >> >> >> this
> >> >> >> form
> >> >> >
> >> >> > OK - I did this, but it has made no difference - I think that CW
> >> >> > accepts
> >> >> > it the way I had it - but I agree it's not the 'correct' way to
> >> >> > write
> >> >> > it !
> >> >> >
> >> >> >> debugger by default is not refreshing flash and EEPROM memories
> >> >> >> from
> >> >> >> device
> >> >> >> memory.
> >> >> >
> >> >> > I thought that too. So I wrote some more code to read the contents
> >> >> > after
> >> >> > the write - and it is all FF FF ...
> >> >> >
> >> >> > Also - I think the debugger is showing memory correctly - If I
> >> >> > change
> >> >> > PPAGE to $3F and then go back to read address $8000 - i can see the
> >> >> > top
> >> >> > page, as I should be able to.
> >> >> >
> >> >> > I also tried using ICD12Z to read the memory - it also indicates it
> >> >> > is
> >> >> > FF
> >> >> > FF ....
> >> >> >
> >> >> > I am lost !
> >> >> >
> >> >> > Rachel
> >> >> >
> >> >> >
> >> >> > --- In 6..., "Edward Karpicz" wrote:
> >> >> >>
> >> >> >> What assembler and debugger are you using?
> >> >> >>
> >> >> >> > MOVW X,Y
> >> >> >>
> >> >> >> ^^ this should copy the word from variable called X to variable
> >> >> >> called
> >> >> >> Y.
> >> >> >> Certainly you meant here indexed addressing, so you should edit it
> >> >> >> to
> >> >> >> this
> >> >> >> form
> >> >> >>
> >> >> >> MOVW 0,X, 0,Y
> >> >> >>
> >> >> >> > LDD Y
> >> >> >> > STD X
> >> >> >>
> >> >> >> ^^ Same here
> >> >> >>
> >> >> >> Provided assembler treat this like it was expected by you,
> >> >> >> Codewarrior
> >> >> >> debugger by default is not refreshing flash and EEPROM memories
> >> >> >> from
> >> >> >> device
> >> >> >> memory. So you see no change after erasing or programming. You need
> >> >> >> to
> >> >> >> edit
> >> >> >> memory mapping from debuggger menus and tell you want these
> >> >> >> memories
> >> >> >> to
> >> >> >> be
> >> >> >> refreshed on stop or something like that.
> >> >> >>
> >> >> >> Edward
> >> >> >>
> >> >> >> ----- Original Message -----
> >> >> >> From:
> >> >> >> To: <6...>
> >> >> >> Sent: Friday, February 17, 2012 3:07 AM
> >> >> >> Subject: [68HC12] Flash Program Routine - Appears To Do Nothing
> >> >> >>
> >> >> >>
> >> >> >> > Hi - I need some help please ....
> >> >> >> >
> >> >> >> > I'm trying to write some code to write to the Flash area on an
> >> >> >> > MC9S12DP512.
> >> >> >> >
> >> >> >> > But although it runs, and doesn't appear to throw any Acc or
> >> >> >> > PViol
> >> >> >> > errors,
> >> >> >> > it also doesn't appear to actually write to the Flash - it
> >> >> >> > remains @
> >> >> >> > FF
> >> >> >> > FF
> >> >> >> > ..
> >> >> >> >
> >> >> >> > What am I doing wrong ?
> >> >> >> >
> >> >> >> > I've looked the the FSL app notes, (AN2400 for example) and done
> >> >> >> > some
> >> >> >> > digging around on here - and as far as I can see - I'm doing it
> >> >> >> > correctly - but obviously I'm not !
> >> >> >> >
> >> >> >> > Hope someone can help me ...
> >> >> >> >
> >> >> >> > Rachel
> >> >> >> >
> >> >> >> > Here is the code ...
> >> >> >> >
> >> >> >> >
> >> >> >> > ORG $5000
> >> >> >> >
> >> >> >> > ; Test to see if FLASH writing works
> >> >> >> > ; Copy from $5000 into RAM (ShadowStart)
> >> >> >> > ; Then Erase Sector 1 Page $20 (Bank 3)
> >> >> >> > ; Then write the data from RAM into Page $20
> >> >> >> >
> >> >> >> >
> >> >> >> >
> >> >> >> > TestFlashWrite:
> >> >> >> >
> >> >> >> > LDX #$5000
> >> >> >> > LDY #ShadowStart
> >> >> >> > CopyLoop:
> >> >> >> > MOVW X,Y
> >> >> >> > CPX #$53FE
> >> >> >> > BEQ CopyDone
> >> >> >> > INX
> >> >> >> > INX
> >> >> >> > INY
> >> >> >> > INY
> >> >> >> > BRA CopyLoop
> >> >> >> >
> >> >> >> > CopyDone:
> >> >> >> >
> >> >> >> >
> >> >> >> > ; Erase the Flash Sector
> >> >> >> >
> >> >> >> > ; Set 'PPAGE' and 'BKSEL1-0' (in FCNFG)
> >> >> >> >
> >> >> >> >
> >> >> >> > MOVB #$20,PPAGE
> >> >> >> > MOVB #$03,FCNFG
> >> >> >> >
> >> >> >> >
> >> >> >> >
> >> >> >> > ; Wait for Command Registers to be free
> >> >> >> >
> >> >> >> > BRCLR FSTAT,#$80,*
> >> >> >> >
> >> >> >> > ; Clear any outstanding PVIOL / ACCERR Errors
> >> >> >> > MOVB #$30,FSTAT
> >> >> >> >
> >> >> >> > ; Write any value, to the address of the sector we want to erase.
> >> >> >> >
> >> >> >> > MOVW #$0000,$8000
> >> >> >> >
> >> >> >> > ; Issue the 'Sector Erase' Command
> >> >> >> > MOVB #$40,FCMD
> >> >> >> >
> >> >> >> > ; Start the sequence
> >> >> >> > MOVB #$80,FSTAT
> >> >> >> >
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> >
> >> >> >> > ; Check that command has completed
> >> >> >> > BRCLR FSTAT,#$40,*
> >> >> >> >
> >> >> >> > ; Check for Errors
> >> >> >> > BRSET FSTAT,#$20,FlashPviol
> >> >> >> > BRSET FSTAT,#$10,FlashAccErr
> >> >> >> >
> >> >> >> > ; ***** OK, we can start the programming now *****
> >> >> >> >
> >> >> >> >
> >> >> >> > LDX #$8000
> >> >> >> > LDY #ShadowStart
> >> >> >> >
> >> >> >> > ProgLoop:
> >> >> >> > ; Check that command has completed
> >> >> >> > BRCLR FSTAT,#$80,*
> >> >> >> >
> >> >> >> > LDD Y
> >> >> >> > STD X
> >> >> >> >
> >> >> >> > ; Issue the 'Program' Command
> >> >> >> > MOVB #$20,FCMD
> >> >> >> >
> >> >> >> > ; Start the sequence
> >> >> >> > BSET FSTAT,#$80
> >> >> >> >
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> > NOP
> >> >> >> >
> >> >> >> > ; Check that command has completed
> >> >> >> > BRCLR FSTAT,#$40,*
> >> >> >> >
> >> >> >> > ; Check for Errors
> >> >> >> > BRSET FSTAT,#$20,FlashPviol
> >> >> >> > BRSET FSTAT,#$10,FlashAccErr
> >> >> >> >
> >> >> >> > ; Move onto next word
> >> >> >> > CPX #$83FE
> >> >> >> > BEQ ProgDone
> >> >> >> > INX
> >> >> >> > INX
> >> >> >> > INY
> >> >> >> > INY
> >> >> >> > BRA ProgLoop
> >> >> >> >
> >> >> >> > ProgDone:
> >> >> >> > MOVB #$08,PORTB
> >> >> >> >
> >> >> >> > RTS
> >> >> >> >
> >> >> >> > ; Loop around for ever, so we can catch in debug
> >> >> >> >
> >> >> >> > FlashPviol:
> >> >> >> > MOVB #$10,PORTB
> >> >> >> > BRA FlashPviol
> >> >> >> >
> >> >> >> > FlashAccErr:
> >> >> >> > MOVB #$1F,PORTB
> >> >> >> > BRA FlashAccErr
> >> >> >> >
> >> >> >> >
> >> >> >> >
> >> >> >> >
> >> >> >> >
> >> >> >> >