AnsweredAssumed Answered

uVision, flash writing and protection...

Question asked by ThorMJ on Jun 24, 2013
Latest reply on Jul 8, 2013 by ThorMJ

Is there example code for dealing with flash?

In the end, I want to protect the chip; using the current address I should set FEEPRO = BIT30; but I don't know what "this requires a key" means..

I'm stepping through the code using Keil uVision 4, and I don't understand what I'm seeing [In Blue]; my code:


const MBREG_ROM mbreg_rom

  __attribute__ ((at(0x87608)));

const unsigned romcrc

                                                  __attribute__ ((at(0x87600)));


//Ram Copy so you can modify the junk.  Conveniently, it's one page, so you don't need to have a buffer to hold a partial.

MBREG_ROM ramcopy;


//Stuff that's in ram

MBREG_RAM mbreg_ram;


//Flag for copy...

char flashwrite=0;


void flash_wait(void)


          char status = 0;


          //Wait for previous operation to complete...



              status = FEESTA&0x03;

          } while(!status);


          status=status;//Status is always 0x01 (I suppose it should be because execution was blocked during flash activity)...




//Kind of const... I'm writing to it using the flash mechanism

void flash_write(const unsigned int *addr, unsigned int data)


                    FEEDAT = data;                                                                                //Data

                    FEEADR = (unsigned int) addr;          //Address

                    FEECON = 0x02;                                                                                //Write single word [50us] -- up to 25ms for the whole page



void mbreg_write(void)


          int i;

          unsigned int crc16;


          if(memcmp(&ramcopy, &mbreg_rom, sizeof(mbreg_rom)) == 0)                    //Nothing to do... the reg_write wrote to ram, not ramcopy.


                    flashwrite = 0;




          //Lockout changes as we do this

          flashwrite = 3;


          //Start flash page erase...

          FEEMOD = BIT8                     //Always 1

                              + BIT3;           //Enable write/erase



          FEEADR = (unsigned long)&mbreg_rom;          //Address of page to erase

          FEECON = 0x05;                    //Erase Page


//Here, the mbreg_rom (0x87608, so 0x87600-877FF) should be erased to 0xFF's, right? -- I have 4 0xFF's, at 0x87608 but the rest of the page is 0x00....


          //Write page back.



                    //Flash erases to 0xFFFF, so don't write blanks - speedup

                    if((unsigned)*(((char *)&ramcopy)+i)== 0xFFFF)



                    //Wait for previous operation to complete...



                    flash_write(((unsigned int *)&mbreg_rom) + i, (unsigned)*(((char *)&ramcopy) +i));



          //Compute the checksum

          crc16 = CalcCRC(&ramcopy, sizeof(ramcopy));


          flash_write(&romcrc, crc16);




          FEEMOD &= ~BIT3;          //Turn off flash write key

          flashwrite = 0;