XDP512 Runtime flash writing

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

XDP512 Runtime flash writing

2,366 次查看
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?


标签 (1)
0 项奖励
回复
3 回复数

753 次查看
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 项奖励
回复

753 次查看
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 项奖励
回复

753 次查看
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 项奖励
回复