Rikard Almqvist

S12XDT flash erase/write issue

Discussion created by Rikard Almqvist on Mar 7, 2012
Latest reply on Mar 8, 2012 by Rikard Almqvist

Hi, I have been working on a flash loader for S12XDT. The code I've been using as a base was originally written for, and runs fine on a S12C. While porting the code I've ran into a problem, let me try to explain.


I erase a flash sector using the following function:


sbyte FlashEraseSector(word *__pptr pAddress){ static word* address; address = (word *)pAddress; FSTAT = FSTAT_ACCERR | FSTAT_PVIOL; // clear errors (*address) = 0xFFFF;     // Dummy store to page to be erased FCMD = SECTOR_ERASE; (void)DoOnStack(pAddress); if (FSTAT_ACCERR) {  return ACCESS_ERROR; } else if (FSTAT_PVIOL) {  return PROTECTION_ERROR; } else {  return PASS; }}


 This "seems" to work just well and a passed result is returned. But when I want to confirm that the memory is in fact erased I found out that it's not. This is the function used to write a word to flash, and it returns flash not erased.



sbyte FlashWriteWord(word *__pptr pAddress, word data){ FSTAT = FSTAT_PVIOL | FSTAT_ACCERR;  if(*pAddress != FLASH_ERASED_WORD)  {  return FLASH_NOT_ERASED;// It returns here when I run... } else if(*pAddress == data) {   return PASS; } else {  *pAddress = data;        // Store desired data to address being programmed  FCMD = WORD_PROGRAM;     // Store programming command in FCMD  (void)DoOnStack(pAddress);    // just passed for PPAGE  if (FSTAT_ACCERR)  {   return ACCESS_ERROR;  }  else if (FSTAT_PVIOL)  {   return PROTECTION_ERROR;  }  else  {   if (*pAddress == data)   {    return PASS;   }   else   {    return FAIL;   }  } }}


I also checked while debugging and find that the memory is not been erased. The memory I try to erase and write to is within paged flash (page F8 to FE).


Any clue as to why the memory is no erased? I don't have alot of experience with S12C/S12XDT so I can't quite figure out how to tackle this problem.