<?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 erasing MC9S08QE128 flash in C in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203271#M16785</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an application that's being ported over from the MC9S08GB60 to the MS9S08QE128.&amp;nbsp; I'm using the Cosmic compiler version 4.6.3, and I'm trying to figure out how to modify the bootloader to work with the paged memory structure.&amp;nbsp; The function I've pasted below is passed a page 0-7, and an address 0x8000-0xBFFF.&amp;nbsp; It is supposed to erase the page, and then verify it has been erased successfully.&amp;nbsp; After the verification, I have some debug information printed out the serial port which is saying I've failed to erase.&amp;nbsp; I see this each time I call the function, regardless of which page I call it with (I haven't tried page 0, which is where my bootloader is stored).&amp;nbsp; I've also tried ignoring the messages and programming anyway, which obviously failed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some things I've considered, or seen pointed out on other threads:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The address range is incorrect:&amp;nbsp; I'm printing the addresses out the serial port with the RAMDebugString function, and the values I'm seeing are valid.&lt;/LI&gt;&lt;LI&gt;Flash clock speed is off: I've set the flash clock according to table 4-12 in MC9S08QE128RM Rev. 2, and verified that FDIVLD is set before calling this function&lt;/LI&gt;&lt;LI&gt;Not running from RAM: I'm using a Cosmic-provided function to copy the code from flash to RAM&lt;/LI&gt;&lt;LI&gt;I noticed on &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fcommunity.freescale.com%2Fmessage%2F56096%2356096" rel="nofollow noopener noreferrer" target="_blank"&gt;https://community.freescale.com/message/56096#56096&lt;/A&gt;that there was a delay needed between writing FSTAT = FCBEF and checking FSTAT to see if FCCF is set.&amp;nbsp; I've inserted the four "nop" cycles, and the code still does not work.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Obviously I'm doing something wrong, but I can't see it.&amp;nbsp; I've tried to follow figures 4-19 (erase) and 4-16 (erase verify) from the reference manual as closely as possible.&amp;nbsp; Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;static unsigned char ucRAMEraseFlashPage(unsigned char ucPage, unsigned char * pucAddr)
{

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp; unsigned int uiaDebug[2];

&amp;nbsp;&amp;nbsp;&amp;nbsp; uiaDebug[0] = (unsigned int) ucPage;
&amp;nbsp;&amp;nbsp;&amp;nbsp; uiaDebug[1] = pucAddr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMDebugString("% %\r", uiaDebug);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp; while((FSTAT &amp;amp; FCBEF) != FCBEF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Wait until flash command buffer is empty */
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Check for a flash access error */
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(FSTAT &amp;amp; FACCERR || FSTAT &amp;amp; FPVIOL) {

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Clear flash access error */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT |= FACCERR | FPVIOL;

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("Acc err or prot viol cleared prior to erase\r", 2);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp; } // if(FSTAT &amp;amp; FACCERR || FSTAT &amp;amp; FPVIOL)

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Write any data to a 512 byte page
&amp;nbsp;&amp;nbsp;&amp;nbsp; PPAGE = ucPage;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *pucAddr = DEFAULT_ERASE_DATA;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Issue the erase command
&amp;nbsp;&amp;nbsp;&amp;nbsp; FCMD = PAGE_ERASE;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Start command
&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT = FCBEF;

&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Undocumented wait needed.&amp;nbsp; See
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fcommunity.freescale.com%2Fmessage%2F56096%2356096" rel="nofollow noopener noreferrer" target="_blank"&gt;https://community.freescale.com/message/56096#56096&lt;/A&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for details. */
&amp;nbsp;&amp;nbsp;&amp;nbsp; _asm("nop\nnop\nnop\nnop\n");
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Wait until all flash commands are completed
&amp;nbsp;&amp;nbsp;&amp;nbsp; while((FSTAT &amp;amp; FCCF) != FCCF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Begin to verify that we've erased properly.
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(FSTAT &amp;amp; FCBEF != FCBEF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Make sure we didn't break any rules.
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(FSTAT &amp;amp; FPVIOL || FSTAT &amp;amp; FACCERR) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT = FPVIOL | FACCERR;
#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("2 - viol or acc err after erase\r", 2);
#endif
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Again set up where we're writing
&amp;nbsp;&amp;nbsp;&amp;nbsp; PPAGE = ucPage;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *pucAddr = DEFAULT_ERASE_DATA;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Issue the command
&amp;nbsp;&amp;nbsp;&amp;nbsp; FCMD = ERASE_VERIFY;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // And away we go.
&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT = FCBEF;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Undocumented wait needed.
&amp;nbsp;&amp;nbsp;&amp;nbsp; _asm("nop\nnop\nnop\nnop\n");
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(FSTAT &amp;amp; FCCF != FCCF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Wait until all flash commands are completed */
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; if(FSTAT &amp;amp; FBLANK) {

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("Successful erase\r", 2);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return FLASH_ERASE_OK;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("Failed to erase\r", 2);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return FLASH_ERASE_NOT_BLANK;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } // if(FSTAT &amp;amp; FBLANK)... else...

} /* ucRAMEraseFlashPage() */&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 May 2010 01:59:19 GMT</pubDate>
    <dc:creator>hectron</dc:creator>
    <dc:date>2010-05-26T01:59:19Z</dc:date>
    <item>
      <title>erasing MC9S08QE128 flash in C</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203271#M16785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an application that's being ported over from the MC9S08GB60 to the MS9S08QE128.&amp;nbsp; I'm using the Cosmic compiler version 4.6.3, and I'm trying to figure out how to modify the bootloader to work with the paged memory structure.&amp;nbsp; The function I've pasted below is passed a page 0-7, and an address 0x8000-0xBFFF.&amp;nbsp; It is supposed to erase the page, and then verify it has been erased successfully.&amp;nbsp; After the verification, I have some debug information printed out the serial port which is saying I've failed to erase.&amp;nbsp; I see this each time I call the function, regardless of which page I call it with (I haven't tried page 0, which is where my bootloader is stored).&amp;nbsp; I've also tried ignoring the messages and programming anyway, which obviously failed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some things I've considered, or seen pointed out on other threads:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The address range is incorrect:&amp;nbsp; I'm printing the addresses out the serial port with the RAMDebugString function, and the values I'm seeing are valid.&lt;/LI&gt;&lt;LI&gt;Flash clock speed is off: I've set the flash clock according to table 4-12 in MC9S08QE128RM Rev. 2, and verified that FDIVLD is set before calling this function&lt;/LI&gt;&lt;LI&gt;Not running from RAM: I'm using a Cosmic-provided function to copy the code from flash to RAM&lt;/LI&gt;&lt;LI&gt;I noticed on &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fcommunity.freescale.com%2Fmessage%2F56096%2356096" rel="nofollow noopener noreferrer" target="_blank"&gt;https://community.freescale.com/message/56096#56096&lt;/A&gt;that there was a delay needed between writing FSTAT = FCBEF and checking FSTAT to see if FCCF is set.&amp;nbsp; I've inserted the four "nop" cycles, and the code still does not work.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Obviously I'm doing something wrong, but I can't see it.&amp;nbsp; I've tried to follow figures 4-19 (erase) and 4-16 (erase verify) from the reference manual as closely as possible.&amp;nbsp; Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;static unsigned char ucRAMEraseFlashPage(unsigned char ucPage, unsigned char * pucAddr)
{

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp; unsigned int uiaDebug[2];

&amp;nbsp;&amp;nbsp;&amp;nbsp; uiaDebug[0] = (unsigned int) ucPage;
&amp;nbsp;&amp;nbsp;&amp;nbsp; uiaDebug[1] = pucAddr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMDebugString("% %\r", uiaDebug);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp; while((FSTAT &amp;amp; FCBEF) != FCBEF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Wait until flash command buffer is empty */
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Check for a flash access error */
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(FSTAT &amp;amp; FACCERR || FSTAT &amp;amp; FPVIOL) {

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Clear flash access error */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT |= FACCERR | FPVIOL;

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("Acc err or prot viol cleared prior to erase\r", 2);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp; } // if(FSTAT &amp;amp; FACCERR || FSTAT &amp;amp; FPVIOL)

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Write any data to a 512 byte page
&amp;nbsp;&amp;nbsp;&amp;nbsp; PPAGE = ucPage;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *pucAddr = DEFAULT_ERASE_DATA;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Issue the erase command
&amp;nbsp;&amp;nbsp;&amp;nbsp; FCMD = PAGE_ERASE;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Start command
&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT = FCBEF;

&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Undocumented wait needed.&amp;nbsp; See
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fcommunity.freescale.com%2Fmessage%2F56096%2356096" rel="nofollow noopener noreferrer" target="_blank"&gt;https://community.freescale.com/message/56096#56096&lt;/A&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for details. */
&amp;nbsp;&amp;nbsp;&amp;nbsp; _asm("nop\nnop\nnop\nnop\n");
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; // Wait until all flash commands are completed
&amp;nbsp;&amp;nbsp;&amp;nbsp; while((FSTAT &amp;amp; FCCF) != FCCF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Begin to verify that we've erased properly.
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(FSTAT &amp;amp; FCBEF != FCBEF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Make sure we didn't break any rules.
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(FSTAT &amp;amp; FPVIOL || FSTAT &amp;amp; FACCERR) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT = FPVIOL | FACCERR;
#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("2 - viol or acc err after erase\r", 2);
#endif
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Again set up where we're writing
&amp;nbsp;&amp;nbsp;&amp;nbsp; PPAGE = ucPage;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *pucAddr = DEFAULT_ERASE_DATA;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Issue the command
&amp;nbsp;&amp;nbsp;&amp;nbsp; FCMD = ERASE_VERIFY;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // And away we go.
&amp;nbsp;&amp;nbsp;&amp;nbsp; FSTAT = FCBEF;

&amp;nbsp;&amp;nbsp;&amp;nbsp; // Undocumented wait needed.
&amp;nbsp;&amp;nbsp;&amp;nbsp; _asm("nop\nnop\nnop\nnop\n");
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(FSTAT &amp;amp; FCCF != FCCF) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Wait until all flash commands are completed */
&amp;nbsp;&amp;nbsp;&amp;nbsp; }

&amp;nbsp;&amp;nbsp;&amp;nbsp; if(FSTAT &amp;amp; FBLANK) {

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("Successful erase\r", 2);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return FLASH_ERASE_OK;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {

#if DEBUG_MODE
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RAMWriteString("Failed to erase\r", 2);
#endif

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return FLASH_ERASE_NOT_BLANK;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } // if(FSTAT &amp;amp; FBLANK)... else...

} /* ucRAMEraseFlashPage() */&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 May 2010 01:59:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203271#M16785</guid>
      <dc:creator>hectron</dc:creator>
      <dc:date>2010-05-26T01:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: erasing MC9S08QE128 flash in C</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203272#M16786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Erase verify command checks if all flash bytes are erased or not. To make FBLANK set you need to erase everything. Erase verify is used by BDM flash burners. Executing this command is required step after mass erase to unsecure secured chip.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 May 2010 10:53:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203272#M16786</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-05-26T10:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: erasing MC9S08QE128 flash in C</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203273#M16787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you, kef.&amp;nbsp; I was confused by the first line of section 4.6.3.2.1, where it says that "the erase verify operation will verify that &lt;STRONG&gt;a flash block&lt;/STRONG&gt; is erased" (emphasis added). I'd assumed that this referred to the 512 byte page, not the whole flash array.&amp;nbsp; When I re-read the section more carefully, what you said made sense.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 May 2010 22:10:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/erasing-MC9S08QE128-flash-in-C/m-p/203273#M16787</guid>
      <dc:creator>hectron</dc:creator>
      <dc:date>2010-05-26T22:10:43Z</dc:date>
    </item>
  </channel>
</rss>

