Forum : ST7/STM8
Original Post
Post Information | Post |
---|---|
July 16, 2013 - 4:06pm
|
Im attempting to write erase/write to main program memory in ASM. The problem i am having is i can seem to change the CR2 and NCR2 values, i have tries several bits i.e. block erase, block write, word write but all do not work. the CR2/NCR2 bits are not changing and the flash is not changing. I have posted to code i am using to perform this please note FLASH EQU 0505Ah FLASH_CR1 EQU (FLASH+0) FLASH_CR2 EQU (FLASH+1) FLASH_NCR2 EQU (FLASH+2) FLASH_FPR EQU (FLASH+3) FLASH_NFPR EQU (FLASH+4) FLASH_IAPSR EQU (FLASH+5) FLASH_PUKR EQU (FLASH+8) FLASH_DUKR EQU (FLASH+10) QFirmwareWrite_data SEGMENT DATA RSEG QFirmwareWrite_data blockBuffer: DS 80h PUBLIC ?upgradeApplication QFirmwareWrite_code SEGMENT CODE INRAM;TODO: allocate segment to fixed area RSEG QFirmwareWrite_code ?upgradeApplication: LDW X,#0000h LD A,#55h LD (blockBuffer,X),A INCW X LD (blockBuffer,X),A CALL UnlockFlash CALL EraseBlock CALL WaitForLastOp CALL LockFlash RET EraseBlock: LD A, FLASH_IAPSR LD A, FLASH_CR2 OR A, #040h; Erase bit LD FLASH_CR2, A LD A, FLASH_NCR2 AND A, #0BFh ; NOT 02h LD FLASH_NCR2, A LD A,FLASH_NCR2 LD A,#055h LDW Y, #09000h LD (Y),A INCW Y LD (Y),A INCW Y LD (Y),A INCW Y LD (Y),A RET UnlockFlash: MOV FLASH_PUKR,#056h ; unlock key 1 MOV FLASH_PUKR,#0AEh ; unlock key 2 RET LockFlash: LD A, FLASH_IAPSR AND A, #0FDh ;lock LD FLASH_IAPSR, A RET WaitForLastOp: LDW Y, #00F00h _timerLoop: LD A, FLASH_IAPSR AND A, #005h ;only include op complete or write protect bits JRNE _returnWaitForLastOp ;return if result if either bit is set DECW Y JRNE _timerLoop _returnWaitForLastOp: RET END |
I found this in the STM8 Flash application notes
I have changed my program to load the two registers consecutively
but found that results are inconsistent i.e. sometimes it does a block erase and sometimes it falls back to byte erase
I have sorted the issue out now, it seems that in need to do simple move operations into the CR2, NCR2 registers for it to work.