This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions
Posted: Thu Jan 12, 2006 4:29am
Hi,
I've a problem on MSC9S12DG256 chip.
I try to write a word in flash memory. I write the code in flash.
I copy it into RAM and I execute it in RAM.
My code is that :
Code:
static bool ProgData(void) { unsigned short * sectAddr = (unsigned short *)0x8000; B = 0x34; PPAGE = B; B = B >> 2; B = ~B; B &= 0x3; FCNFG = B; *sectAddr = 0xABAB; FCMD = PROG; FSTAT |= FSTAT_CBEIF_MASK; if (FSTAT_ACCERR) {return 0;} if (FSTAT_PVIOL) {return 0;} asm nop; asm nop; asm nop; asm nop; asm nop; while(FSTAT_CBEIF == 0); while(FSTAT_CCIF == 0); }
The code is executed without error or crash. But the data is not written in Flash
Have you got an idea about my problem.
Thanks in advance for your help.
Posted: Thu Jan 12, 2006 5:17am
You apparently forgot to set FCLKDIV.
You can have a look in the "P&E_ICD_Erase_unsecure_hcs12.cmd" of your project.
Here is the code I tested successfully. Note the "NON_BANKED" pragma.
Code:
static volatile unsigned char B; #pragma CODE_SEG NON_BANKED static Bool ProgData(void) { unsigned short * sectAddr = (unsigned short *)0x8000; FPROT = 0xFF; // XTAL : // 16 MHz $49 // 8 MHz $27 // 4 MHz $13 // 2 MHz $9 // 1 MHz $4 FCLKDIV = 0x49; B = 0x34; PPAGE = B; B = B >> 2; B = ~B; B &= 0x3; FCNFG = B; *sectAddr = 0xABAB; FCMD = 0x20; //PROG; FSTAT |= FSTAT_CBEIF_MASK; if (FSTAT_ACCERR) {return 0;} if (FSTAT_PVIOL) {return 0;} asm nop; asm nop; asm nop; asm nop; asm nop; while(FSTAT_CBEIF == 0); while(FSTAT_CCIF == 0); } void main(void) { /* put your own code here */ EnableInterrupts; ProgData(); for(;;) {} /* wait forever */ /* please make sure that you never leave this function */ }
Thanks a lot. Now it's working. The problem was FCLKDIV register. Now , I want to erase a page. For that, my code is:
Code:
static void EraseFlash(void) { unsigned short * addr = (unsigned short *)0x8000; FCLKDIV = 0x49; B = 0x34; PPAGE = B; B = B >> 2; B = ~B; B &= 0x3; FCNFG = B; cmd = ERASE + MASS; asm nop; if (EraseCmd(cmd, addr) == 0) { bError = 1; } cmd = ERVER + MASS; if (EraseCmd(cmd, addr) == 0) { bError = 1; } if (FSTAT_BLANK) { bError = 1; } FCNFG = FSTAT_BLANK_MASK; } static bool EraseCmd(unsigned short cmd, unsigned short * addr) { asm nop; asm nop; *addr = 0xFFFF; *(addr+1) = 0xFFFF; FCMD = cmd; FSTAT = FSTAT_CBEIF_MASK; if (FSTAT_ACCERR) {return 0;} if (FSTAT_PVIOL) {return 0;} EraseCmdOK(); return 1; } static void EraseCmdOK(void) { while(FSTAT_CCIF == 0); }
This code doesn't work.
Any idea ???
Thanks in advance.
Posted: Thu Jan 12, 2006 7:30am
Yes,
I did not try your code this time.
But apparently you forgot to unprotect the flash.
Check my previously sent code:
FPROT = 0xFF; /* to unprotect all blocks */
Also, why do you have:
*addr = 0xFFFF;
*(addr+1) = 0xFFFF; ??
the second line is useless, to my opinion.
Regards,
Posted: Thu Jan 12, 2006 7:57am
I add command "FPROT = 0xFF;" before doing other thing.
And I suppressed command "*(addr+1) = 0xFFFF;"
But it's not working also.
Message Edited by khumphri on 01-26-2006 11:57 AM