<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Writing Flash Memory in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142452#M6221</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;It is not C but I got this from Freescale. Works for me.&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* FlashErase1 - erases one page of FLASH (512 bytes)&lt;BR /&gt;;*&lt;BR /&gt;;* On entry... H:X - points at a location in the page to be erased&lt;BR /&gt;;*&lt;BR /&gt;;* Calling convention:&lt;BR /&gt;;* jsr FlashErase1&lt;BR /&gt;;*&lt;BR /&gt;;* Uses: DoOnStack which uses SpSub&lt;BR /&gt;;* Returns: H:X unchanged and A = FSTAT shifted left by 2 bits&lt;BR /&gt;;* Z=1 if OK, Z=0 if protect violation or access error&lt;BR /&gt;;* uses 32 bytes of stack space + 2 bytes for BSR/JSR used to call it&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;FlashErase1:&lt;BR /&gt; psha ;adjust sp for DoOnStack entry&lt;BR /&gt; lda #(FPVIOL+FACCERR) ;mask&lt;BR /&gt; sta FSTAT ;abort any command and clear errors&lt;BR /&gt; lda #PageErase ;mask pattern for page erase command&lt;BR /&gt; bsr DoOnStack ;finish command from stack-based sub&lt;BR /&gt; ais #1 ;deallocate data location from stack&lt;BR /&gt; rts ;Z = 0 means there was an error&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* FlashProg1 - programs one byte of FLASH&lt;BR /&gt;;* This routine waits for the command to complete before returning.&lt;BR /&gt;;* assumes location was blank. This routine can be run from FLASH&lt;BR /&gt;;*&lt;BR /&gt;;* On entry... H:X - points at the FLASH byte to be programmed&lt;BR /&gt;;* A holds the data for the location to be programmed&lt;BR /&gt;;*&lt;BR /&gt;;* Calling convention:&lt;BR /&gt;;* jsr FlashProg1&lt;BR /&gt;;*&lt;BR /&gt;;* Uses: DoOnStack which uses SpSub&lt;BR /&gt;;* Returns: H:X unchanged and A = FSTAT shifted left by 2 bits&lt;BR /&gt;;* Z=1 if OK, Z=0 if protect violation or access error&lt;BR /&gt;;* uses 32 bytes of stack space + 2 bytes for BSR/JSR used to call it&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;FlashProg1:&lt;BR /&gt; psha ;temporarily save entry data&lt;BR /&gt; lda #(FPVIOL+FACCERR) ;mask&lt;BR /&gt; sta FSTAT ;abort any command and clear errors&lt;BR /&gt; lda #ByteProgram ;mask pattern for byte prog command&lt;BR /&gt; bsr DoOnStack ;execute prog code from stack RAM&lt;BR /&gt; ais #1 ;deallocate data location from stack&lt;BR /&gt; rts ;Z = 0 means there was an error&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* DoOnStack - copy SpSub onto stack and call it (see also SpSub)&lt;BR /&gt;;* Deallocates the stack space used by SpSub after returning from it.&lt;BR /&gt;;* Allows flash prog/erase command to execute out of RAM (on stack)&lt;BR /&gt;;* while flash is out of the memory map.&lt;BR /&gt;;* This routine can be used for flash byte-program or erase commands&lt;BR /&gt;;*&lt;BR /&gt;;* Calling Convention:&lt;BR /&gt;;* psha ;save data to program (or dummy&lt;BR /&gt;;* ; data for an erase command)&lt;BR /&gt;;* lda #(mFPVIOL+mFACCERR) ;mask&lt;BR /&gt;;* sta FSTAT ;abort any command and clear errors&lt;BR /&gt;;* lda #mByteProg ;mask pattern for byte prog command&lt;BR /&gt;;* jsr DoOnStack ;execute prog code from stack RAM&lt;BR /&gt;;* ais #1 ;deallocate data location from stack&lt;BR /&gt;;* ; without disturbing A or CCR&lt;BR /&gt;;*&lt;BR /&gt;;* or substitute #mPageErase for page erase&lt;BR /&gt;;*&lt;BR /&gt;;* Uses 29 bytes on stack + 2 bytes for BSR/JSR used to call it&lt;BR /&gt;;* returns H:X unchanged and A=0 and Z=1 if no flash errors&lt;BR /&gt;;********************************************************************&lt;BR /&gt;DoOnStack:&lt;BR /&gt; pshx&lt;BR /&gt; pshh ;save pointer to flash&lt;BR /&gt; psha ;save command on stack&lt;BR /&gt; ldhx #SpSubEnd ;point at last byte to move to stack&lt;BR /&gt;&lt;BR /&gt;SpMoveLoop:&lt;BR /&gt; lda 0,x ;read from flash&lt;BR /&gt; psha ;move onto stack&lt;BR /&gt; aix #-1 ;next byte to move&lt;BR /&gt; cphx #SpSub-1 ;past end?&lt;BR /&gt; bne SpMoveLoop ;loop till whole sub on stack&lt;BR /&gt; tsx ;point to sub on stack&lt;BR /&gt; tpa ;move CCR to A for testing&lt;BR /&gt; and #$08 ;check the I mask&lt;BR /&gt; bne I_set ;skip if I already set&lt;BR /&gt; sei ;block interrupts while FLASH busy&lt;BR /&gt; lda SpSubSize+6,sp ;preload data for command&lt;BR /&gt; jsr 0,x ;execute the sub on the stack&lt;BR /&gt; cli ;ok to clear I mask now&lt;BR /&gt; bra I_cont ;continue to stack de-allocation&lt;BR /&gt;&lt;BR /&gt;I_set:&lt;BR /&gt; lda SpSubSize+6,sp ;preload data for command&lt;BR /&gt; jsr 0,x ;execute the sub on the stack&lt;BR /&gt;&lt;BR /&gt;I_cont:&lt;BR /&gt; ais #SpSubSize+3 ;deallocate sub body + H:X + command&lt;BR /&gt; ;H:X flash pointer OK from SpSub&lt;BR /&gt; lsla ;A=00 &amp;amp; Z=1 unless PVIOL or ACCERR&lt;BR /&gt; rts ;to flash where DoOnStack was called&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* SpSub - This variation of SpSub performs all of the steps for&lt;BR /&gt;;* programming or erasing flash from RAM. SpSub is copied onto the&lt;BR /&gt;;* stack, SP is copied to H:X, and then the copy of SpSub in RAM is&lt;BR /&gt;;* called using a JSR 0,X instruction.&lt;BR /&gt;;*&lt;BR /&gt;;* At the time SpSub is called, the data to be programmed (dummy data&lt;BR /&gt;;* for an erase command), is in A and the flash address is on the&lt;BR /&gt;;* stack above SpSub. After return, PVIOL and ACCERR flags are in bits&lt;BR /&gt;;* 6 and 5 of A. If A is shifted left by one bit after return, it&lt;BR /&gt;;* should be zero unless there was a flash error.&lt;BR /&gt;;*&lt;BR /&gt;;* Uses 24 bytes on stack + 2 bytes if a BSR/JSR calls it&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;SpSub:&lt;BR /&gt; ldhx SpSubSize+4,sp ;get flash address from stack&lt;BR /&gt; sta 0,x ;write to flash; latch addr and data&lt;BR /&gt; lda SpSubSize+3,sp ;get flash command&lt;BR /&gt; sta FCMD ;write the flash command&lt;BR /&gt; lda #FCBEF ;mask to initiate command&lt;BR /&gt; sta FSTAT ;[pwpp] register command&lt;BR /&gt; nop ;[p] want min 4~ from w cycle to r&lt;BR /&gt;&lt;BR /&gt;ChkDone:&lt;BR /&gt; lda FSTAT ;[prpp] so FCCF is valid&lt;BR /&gt; lsla ;FCCF now in MSB&lt;BR /&gt; bpl ChkDone ;loop if FCCF = 0&lt;BR /&gt;&lt;BR /&gt;SpSubEnd:&lt;BR /&gt; rts ;back into DoOnStack in flash&lt;BR /&gt;&lt;BR /&gt;SpSubSize: equ (*-SpSub)&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Jul 2006 08:36:26 GMT</pubDate>
    <dc:creator>Ilocano</dc:creator>
    <dc:date>2006-07-10T08:36:26Z</dc:date>
    <item>
      <title>Writing Flash Memory</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142450#M6219</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Does anyone have advice on how I can write to the internal flash memory to save data on a MC9S08GB60.&amp;nbsp; I would like to do this from C.&lt;BR /&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 08 Jul 2006 00:24:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142450#M6219</guid>
      <dc:creator>billp</dc:creator>
      <dc:date>2006-07-08T00:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: Writing Flash Memory</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142451#M6220</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;The Datasheet has a pretty good description of the process to write to the Flash Memory.&lt;BR /&gt;&lt;BR /&gt;If you have Processor Expert, you could check the Internal Flash Bean.&lt;BR /&gt;&lt;BR /&gt;YOu can check the Bootloader application notes, that certainly will have code to write the Flash Memory.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Alex&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 08 Jul 2006 01:14:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142451#M6220</guid>
      <dc:creator>alex_spotw</dc:creator>
      <dc:date>2006-07-08T01:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: Writing Flash Memory</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142452#M6221</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;It is not C but I got this from Freescale. Works for me.&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* FlashErase1 - erases one page of FLASH (512 bytes)&lt;BR /&gt;;*&lt;BR /&gt;;* On entry... H:X - points at a location in the page to be erased&lt;BR /&gt;;*&lt;BR /&gt;;* Calling convention:&lt;BR /&gt;;* jsr FlashErase1&lt;BR /&gt;;*&lt;BR /&gt;;* Uses: DoOnStack which uses SpSub&lt;BR /&gt;;* Returns: H:X unchanged and A = FSTAT shifted left by 2 bits&lt;BR /&gt;;* Z=1 if OK, Z=0 if protect violation or access error&lt;BR /&gt;;* uses 32 bytes of stack space + 2 bytes for BSR/JSR used to call it&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;FlashErase1:&lt;BR /&gt; psha ;adjust sp for DoOnStack entry&lt;BR /&gt; lda #(FPVIOL+FACCERR) ;mask&lt;BR /&gt; sta FSTAT ;abort any command and clear errors&lt;BR /&gt; lda #PageErase ;mask pattern for page erase command&lt;BR /&gt; bsr DoOnStack ;finish command from stack-based sub&lt;BR /&gt; ais #1 ;deallocate data location from stack&lt;BR /&gt; rts ;Z = 0 means there was an error&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* FlashProg1 - programs one byte of FLASH&lt;BR /&gt;;* This routine waits for the command to complete before returning.&lt;BR /&gt;;* assumes location was blank. This routine can be run from FLASH&lt;BR /&gt;;*&lt;BR /&gt;;* On entry... H:X - points at the FLASH byte to be programmed&lt;BR /&gt;;* A holds the data for the location to be programmed&lt;BR /&gt;;*&lt;BR /&gt;;* Calling convention:&lt;BR /&gt;;* jsr FlashProg1&lt;BR /&gt;;*&lt;BR /&gt;;* Uses: DoOnStack which uses SpSub&lt;BR /&gt;;* Returns: H:X unchanged and A = FSTAT shifted left by 2 bits&lt;BR /&gt;;* Z=1 if OK, Z=0 if protect violation or access error&lt;BR /&gt;;* uses 32 bytes of stack space + 2 bytes for BSR/JSR used to call it&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;FlashProg1:&lt;BR /&gt; psha ;temporarily save entry data&lt;BR /&gt; lda #(FPVIOL+FACCERR) ;mask&lt;BR /&gt; sta FSTAT ;abort any command and clear errors&lt;BR /&gt; lda #ByteProgram ;mask pattern for byte prog command&lt;BR /&gt; bsr DoOnStack ;execute prog code from stack RAM&lt;BR /&gt; ais #1 ;deallocate data location from stack&lt;BR /&gt; rts ;Z = 0 means there was an error&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* DoOnStack - copy SpSub onto stack and call it (see also SpSub)&lt;BR /&gt;;* Deallocates the stack space used by SpSub after returning from it.&lt;BR /&gt;;* Allows flash prog/erase command to execute out of RAM (on stack)&lt;BR /&gt;;* while flash is out of the memory map.&lt;BR /&gt;;* This routine can be used for flash byte-program or erase commands&lt;BR /&gt;;*&lt;BR /&gt;;* Calling Convention:&lt;BR /&gt;;* psha ;save data to program (or dummy&lt;BR /&gt;;* ; data for an erase command)&lt;BR /&gt;;* lda #(mFPVIOL+mFACCERR) ;mask&lt;BR /&gt;;* sta FSTAT ;abort any command and clear errors&lt;BR /&gt;;* lda #mByteProg ;mask pattern for byte prog command&lt;BR /&gt;;* jsr DoOnStack ;execute prog code from stack RAM&lt;BR /&gt;;* ais #1 ;deallocate data location from stack&lt;BR /&gt;;* ; without disturbing A or CCR&lt;BR /&gt;;*&lt;BR /&gt;;* or substitute #mPageErase for page erase&lt;BR /&gt;;*&lt;BR /&gt;;* Uses 29 bytes on stack + 2 bytes for BSR/JSR used to call it&lt;BR /&gt;;* returns H:X unchanged and A=0 and Z=1 if no flash errors&lt;BR /&gt;;********************************************************************&lt;BR /&gt;DoOnStack:&lt;BR /&gt; pshx&lt;BR /&gt; pshh ;save pointer to flash&lt;BR /&gt; psha ;save command on stack&lt;BR /&gt; ldhx #SpSubEnd ;point at last byte to move to stack&lt;BR /&gt;&lt;BR /&gt;SpMoveLoop:&lt;BR /&gt; lda 0,x ;read from flash&lt;BR /&gt; psha ;move onto stack&lt;BR /&gt; aix #-1 ;next byte to move&lt;BR /&gt; cphx #SpSub-1 ;past end?&lt;BR /&gt; bne SpMoveLoop ;loop till whole sub on stack&lt;BR /&gt; tsx ;point to sub on stack&lt;BR /&gt; tpa ;move CCR to A for testing&lt;BR /&gt; and #$08 ;check the I mask&lt;BR /&gt; bne I_set ;skip if I already set&lt;BR /&gt; sei ;block interrupts while FLASH busy&lt;BR /&gt; lda SpSubSize+6,sp ;preload data for command&lt;BR /&gt; jsr 0,x ;execute the sub on the stack&lt;BR /&gt; cli ;ok to clear I mask now&lt;BR /&gt; bra I_cont ;continue to stack de-allocation&lt;BR /&gt;&lt;BR /&gt;I_set:&lt;BR /&gt; lda SpSubSize+6,sp ;preload data for command&lt;BR /&gt; jsr 0,x ;execute the sub on the stack&lt;BR /&gt;&lt;BR /&gt;I_cont:&lt;BR /&gt; ais #SpSubSize+3 ;deallocate sub body + H:X + command&lt;BR /&gt; ;H:X flash pointer OK from SpSub&lt;BR /&gt; lsla ;A=00 &amp;amp; Z=1 unless PVIOL or ACCERR&lt;BR /&gt; rts ;to flash where DoOnStack was called&lt;BR /&gt;&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;;* SpSub - This variation of SpSub performs all of the steps for&lt;BR /&gt;;* programming or erasing flash from RAM. SpSub is copied onto the&lt;BR /&gt;;* stack, SP is copied to H:X, and then the copy of SpSub in RAM is&lt;BR /&gt;;* called using a JSR 0,X instruction.&lt;BR /&gt;;*&lt;BR /&gt;;* At the time SpSub is called, the data to be programmed (dummy data&lt;BR /&gt;;* for an erase command), is in A and the flash address is on the&lt;BR /&gt;;* stack above SpSub. After return, PVIOL and ACCERR flags are in bits&lt;BR /&gt;;* 6 and 5 of A. If A is shifted left by one bit after return, it&lt;BR /&gt;;* should be zero unless there was a flash error.&lt;BR /&gt;;*&lt;BR /&gt;;* Uses 24 bytes on stack + 2 bytes if a BSR/JSR calls it&lt;BR /&gt;;*********************************************************************&lt;BR /&gt;SpSub:&lt;BR /&gt; ldhx SpSubSize+4,sp ;get flash address from stack&lt;BR /&gt; sta 0,x ;write to flash; latch addr and data&lt;BR /&gt; lda SpSubSize+3,sp ;get flash command&lt;BR /&gt; sta FCMD ;write the flash command&lt;BR /&gt; lda #FCBEF ;mask to initiate command&lt;BR /&gt; sta FSTAT ;[pwpp] register command&lt;BR /&gt; nop ;[p] want min 4~ from w cycle to r&lt;BR /&gt;&lt;BR /&gt;ChkDone:&lt;BR /&gt; lda FSTAT ;[prpp] so FCCF is valid&lt;BR /&gt; lsla ;FCCF now in MSB&lt;BR /&gt; bpl ChkDone ;loop if FCCF = 0&lt;BR /&gt;&lt;BR /&gt;SpSubEnd:&lt;BR /&gt; rts ;back into DoOnStack in flash&lt;BR /&gt;&lt;BR /&gt;SpSubSize: equ (*-SpSub)&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Jul 2006 08:36:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Writing-Flash-Memory/m-p/142452#M6221</guid>
      <dc:creator>Ilocano</dc:creator>
      <dc:date>2006-07-10T08:36:26Z</dc:date>
    </item>
  </channel>
</rss>

