XDP512 Runtime flash writing

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

XDP512 Runtime flash writing

2,133 Views
sfb
Contributor I
Hi,
I am trying to write in flash memory on runtime. There are some faults.
Code:
 
#define ALIGNED_WORD_MASK 0x0001
#define PROG 0x20
 
#pragma push
#pragma DATA_SEG FLASH_DIAG
unsigned int FLASH_DiagSave[16];
#pragma pop
unsigned int ProgFlash(unsigned int* progAdr,unsigned int* bufferPtr,                       unsigned int size){ if(((unsigned int)progAdr & ALIGNED_WORD_MASK) != 0)    /* Check for aligned word */ {  return(FAIL); }   while (!FSTAT_CBEIF);     if (FSTAT_ACCERR || FSTAT_PVIOL)      FSTAT = 0x30;         *progAdr = 15;         FCMD = PROG;         FSTAT = FSTAT_CBEIF_MASK;               while (!FSTAT_CBEIF);      while (!FSTAT_CCIF);     return(PASS);}//------------------------------------------------------------------------------void ConfigFCLKDIV(void)  {    if (!FCLKDIV_FDIVLD)      {        FCLKDIV_PRDIV8 = 1;        FCLKDIV_FDIV = 10;      }    return;  }//------------------------------------------------------------------------------void main(void) {  unsigned int result = 0;  volatile byte flashValue;  byte debug=0;  unsigned char i;  /* put your own code here */  SetupXGATE();  EnableInterrupts;    ConfigFCLKDIV(); result = ProgFlash(FLASH_DiagSave, DiagBuffer, 16);  flashValue = (byte)FLASH_DiagSave[0];  for(;;) {    } /* wait forever */  /* please make sure that you never leave this function */}

 If I set a break point at the line of           "flashValue = (byte)FLASH_DiagSave[0];"    program don't stop and run forever. If I halt it stops unknown area. But if I set a breakpoint at the line of                              "while (!FSTAT_CBEIF);"            in  "ProgFlash" function there is no problem writing 15 to  FLASH_DiagSave[0]. 
I am using 16MHz osc. And FLASH_DIAG is in ROM_4000.
What is problem?


Labels (1)
0 Kudos
Reply
3 Replies

520 Views
kef
Specialist I
again and again and again, flash memory is not readable while it's being programmed. It's not readable and MCU is unable to read and execute program in flash from the moment you clear CBEIF bit until CCIF is set. You should move relevant code to RAM, EEPROM, external memory etc.
Flash memory is also not readable during backdoor unsecure procedure and during all other flash commands, including erase verify (blank check) and compress (checksum) commands.
 
 
BTW this is wrong:
 
        FCLKDIV_PRDIV8 = 1;
        FCLKDIV_FDIV = 10;
above won't work in normal mode since FCLKDIV is write once register in normal mode.
 
0 Kudos
Reply

520 Views
CompilerGuru
NXP Employee
NXP Employee
Doesn't the XDP512 have multiple flash blocks (hmm. quite sure this is not the right term :smileyhappy: ) that can be programmed independently? But the code looks like it would only use the last one, which contains all the unbanked flash so this setup does not work.
But by moving the write code into another block, or probably better, the stored data, the copy to ram step would not be necessary.
Also to consider are interrupts which must not access the block being programmed either and possibly runtime routines used for the C code (I doubt that the given version uses any though).

Daniel
0 Kudos
Reply

520 Views
J2MEJediMaster
Specialist I
You can have code in another bank of Flash memory erase and program a different Flash memory bank. You just need to be careful that you don't step outside of the address range of the bank you're programming. This technique only works for MCUs with two of more banks of Flash, so it won't work on all MCUs. Executing the Flash programming code from RAM will work across all processors, which is why folks keep emphasizing the technique. However, if allyou're using is this specific MCU then the program-from-another-Flash-bank technique will work. More information on the subject can be found on this thread.
0 Kudos
Reply