Hi, Jason,
I think the following customer code is correct.
For the MC56F82748, the 32KW data flash from X:0x00_4000–0x00_BFFF are shared the same physical Flash cell with Program flash P:0x00_0000–0x00_7FFF, the on-chip data RAM from X:0x00_0000–0x00_0FFF are shared physical cell with program RAM from P:0x00_F000–0x00_FFFF. In other words, there are only 32KW flash and 4KW RAM, which have the mirror space in both program and data field.
Pls refer to Chapter 4 Memory Map in MC56F827xxRM.pdf.
If you erase all the 32K data flash, all the program flash will be erased. Furthermore, the program flash space from P:0x400~0x40F are special bytes, they are copied to flash register for example protection register and FTFA_FSEC register after Reset. Especially, the SEC bits should be 10 in binary for the FTFA_FSEC register in order to be in unsecure mode so that JTAG port is not blocked. so you have to set the last LSB in P:0x40c as 10 in binary.
In conclusion, pls check the memory map file of your application to get the program allocation, for example, what space are used for code and constant data, what space is unused, then you can page erase the unused data flash page so that you can save your data to data flash.
Pls refer to the section in MC56F827xxRM.
20.3.1 Flash Configuration Field Description
Hope it can help you.
BR
XiangJun Rong
#define FLASH_ADDRESS_START 0x00004000
#define FLASH_ERASESECTOR_CMD 0x09
targetAddress = FLASH_ADDRESS_START;
for (i = 0; i < 64; i++)
{
FTFA_FCNFG = 0x00;
FTFA_FCCOB0 = FLASH_ERASESECTOR_CMD;
FTFA_FCCOB1 = ((targetAddress) >> 16) & 0xFF;
FTFA_FCCOB2 = ((targetAddress) >> 8) & 0xFF;
FTFA_FCCOB3 = (targetAddress) & 0xFF;
FTFA_FCCOB4 = 0;
FTFA_FCCOB5 = 0;
FTFA_FCCOB6 = 0;
FTFA_FCCOB7 = 0;
FTFA_FSTAT |= 0x70; // Clear the FPVIOL, ACCERR and RDCOLERR flag
asm(nop); // Wait for one clock ...
FTFA_FSTAT |= 0x80; // Clear the CCIF flag -> starts Flash execution
while ((FTFA_FSTAT & 0x80)==0) {}; // Wait for Flash operation to complete
targetAddress += 0x200;
tempVal = FTFA_FSTAT;
QSCI0_DATA = tempVal;
}