I have a MK20DX256VLK7 that was previously partitioned, yet my data wasn't persisting. So I wanted to force a re partition. I used the attached code, yet I get an Access Error (ACCERR). I have reviewed the documentation and believe I am configuring the command properly, but something is wrong. Any help would be appreciated, thanks
Leif.
#include "derivative.h" /* include peripheral declarations */
/* function prototypes */
int partition_flash(int, int);
#define EEPROM_256_256 0x35 // subsystem A = 256 bytes, subsystem B = 256 bytes
#define DFLASH_SIZE_16 0x02
/* Define globals for address of counters */
#define LONGWORD_COUNTER_ADDR 0x14000000
#define WORD_COUNTER_ADDR 0x14000004
#define BYTE_COUNTER_ADDR 0x14000006
/********************************************************************/
void main(void) {
SCB_SHCSR |= SCB_SHCSR_BUSFAULTENA_MASK | SCB_SHCSR_MEMFAULTENA_MASK
| SCB_SHCSR_USGFAULTENA_MASK;
/* Partition the memory to enable FlexMem mode */
if (partition_flash(EEPROM_256_256, DFLASH_SIZE_16)) {
/* Device has been partitioned for the first time, so this
* means the counters have not been initialized yet. We'll
* zero them out now.
*/
*((uint32_t *) (LONGWORD_COUNTER_ADDR)) = 0x0;
/* Wait for the command to complete */
while (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY_MASK))
;
*((uint16_t *) (WORD_COUNTER_ADDR)) = 0x0;
/* Wait for the command to complete */
while (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY_MASK))
;
*((uint8_t *) (BYTE_COUNTER_ADDR)) = 0x0;
/* Wait for the command to complete */
while (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY_MASK))
;
}
}
/********************************************************************/
/* Partition flash routine. This function can be used to setup
* the flash for enhanced EEPROM operation. In order to guarantee
* the eEE endurance the partition command should only be used one
* time (re-partitioning in a different configuration will erase
* wear-leveling data, so endurance can no longer be guaranteed).
* This function will test to make sure the flash has not been
* partitioned already.
*
* Parameters:
* eeprom_size size of the two EEPROM data sets (A and B) defines in flexmem_demo.h
* dlfash_size amount of dflash memory available after partitioning defines in flexmem_demo.h
*
* Returns:
* 1 partitioning completed successfully
* 0 partitioning not completed (device is already partitioned)
*/
int partition_flash(int eeprom_size, int dflash_size) {
/* Test to make sure the device is not already partitioned. If it
* is already partitioned, then return with no action performed.
*/
if ((SIM_FCFG1 & SIM_FCFG1_DEPART(0xF))!= 0x00000F00){
// return 0; //Force a re partition
}
/* Write the FCCOB registers */FTFL_FCCOB0 = FTFL_FCCOB0_CCOBn(0x80); // Selects the PGMPART command
FTFL_FCCOB1 = 0x00;
FTFL_FCCOB2 = 0x00;
FTFL_FCCOB3 = 0x00;
/* FCCOB4 is written with the code for the subsystem sizes (eeprom_size define) */
FTFL_FCCOB4 = eeprom_size;
/* FFCOB5 is written with the code for the Dflash size (dflash_size define) */
FTFL_FCCOB5 = dflash_size;
/* All required FCCOBx registers are written, so launch the command */
FTFL_FSTAT = FTFL_FSTAT_CCIF_MASK;
/* Wait for the command to complete */
while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF_MASK));
return 1;
}