AnsweredAssumed Answered

K64 Programming Flash

Question asked by joe hinkle on Sep 11, 2016
Latest reply on Sep 14, 2016 by Mark Butcher

How do you place code in a post?  I tried the "code" block selection in the toolbar "<>" but it just garbles things up.

 

Let me explain my question.  If someone can tell me how to post code that shows proper format -- I can then include it for clarity.

 

I have written my own flash programming routines in an attempt to understand the process.

 

I am using a K64 with NO FlexNVM

 

If I understand the documentation correctly, you can not execute code from the same flash block you are attempting to program.  I read somewhere that code executing in RAM allows you to program and flash block without a collision error.

 

I believe the the code that needs to run from ram is code that actually read and writes to FTFE_FSTAT.

 

I wrote a small function that perform 2 functions based on an argument passed to it.

 

1.  Read FTFE_FSTAT and check that CCIF is not zero which would indicate active flash operation in progress.  Once set, clear any of the error flags from the last flash operation.  Leave function with only CCIF set.

 

2.  Enter with FTFE_FSTAT having only CCIF set.  Perform FTFE_FSTAT = FTFE_FSTAT_CCIF_MASK; which should start the flash operation.  Wait until FTFE_FSTAT - CCIF mis set indicating flash operation is complete.  Return FTFE_FSTAT to return any errors that may have occurred.

 

I acquire 4 byte align space in RAM and copy the function above into that ram space.  I assign a ram pointer to that RAM space cast to be my function.  Now I can just call the function I described above from its ram image.

 

I have reserved 16 bytes a flash at address 0x410 to hold my MAC address.  When I start, this address range contains all FF's.

 

I perform the following (I wish I knew how to post code)

 

FTFE_FCCOB0 = 7; // PGM8
FTFE_FCCOB1 = (byte)(Address >> 16);
FTFE_FCCOB2 = (byte)(Address >> 8);
FTFE_FCCOB3 = (byte)(Address);
FTFE_FCCOB4 = D[0];
FTFE_FCCOB5 = D[1];
FTFE_FCCOB6 = D[2];
FTFE_FCCOB7 = D[3];
FTFE_FCCOB8 = D[4];
FTFE_FCCOB9 = D[5];
FTFE_FCCOBA = D[6];
FTFE_FCCOBB = D[7];

 

Where Address is 0x410 and array D contains my 6 byte MAC and 2 bytes of FF.

 

I call my ram function and it return zero indicating a successful write .. BUT ... flash has NOT changed.

 

I set the Address to 0x414 which violates the 64 bit alignment requirement for phase programming as a test and get a return value indicating ACCERR error which is valid and also indicates my ram function is functioning properly.

 

This is the Function that is moved from flash into ram .. sorry for the formatting .

 

byte FlashBased_Perform_Flash_Operation(byte Operation)
{
       hwInterruptsDisable();
        switch(Operation)
         {
               case Flash_Operation_WaitTilReady:
                while(1)
                {
                     if((FTFE_FSTAT & FTFE_FSTAT_CCIF_MASK))
                              break;
                 }

                 FTFE_FSTAT = (FTFE_FSTAT_RDCOLERR_MASK | FTFE_FSTAT_ACCERR_MASK |                               FTFE_FSTAT_FPVIOL_MASK); // clear any errors;

                 hwInterruptsEnable();

                 return (FTFE_FSTAT & ~FTFE_FSTAT_CCIF_MASK);
                 break;

     case Flash_Operation_PerformWrite:
             FTFE_FSTAT = FTFE_FSTAT_CCIF_MASK;  // start flash operation
                while(1)
                 {
                       if((FTFE_FSTAT & FTFE_FSTAT_CCIF_MASK))
                            break;
                   }

                   hwInterruptsEnable();

                   return (FTFE_FSTAT & ~FTFE_FSTAT_CCIF_MASK);
                  break;

          }  // end of switch
}

 

The process should be simple but I'm not write to flash.

 

Any comment or suggestions?

 

Thanks.

 

Joe

Outcomes