Thanks Diego,
but the FLASH can't be written to from code running from FLASH, so must run from SRAM.
I am using the KL26 SDK with my KL16 MCU.
I defined a APP_FLASH_1KB in Properties | C/C++ Build | MCU settings.
address=0x0003FC00, size=0x400
In the debugger's memory window, I can see the top of FLASH at 0x0003FC00.
0x0003FC00 to 0x00040000 is filled with 0xFF.
I used the debugger's memory window to set 0x01020304 at 0x0003FC00.
0x0003FC00 01020304 FFFFFFFF FFFFFFFF FFFFFFFF ....ÿÿÿÿÿÿÿÿÿÿÿÿ
0x0003FC10 FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
memcpy() from 0x0003FC00 returns 0x01020304, so the memcpy() worked.
memcpy() to 0x0003FC00, generates a Hard Fault, IPSR = 0x3.
The buffer used by memcpy() is aligned on the 32-bit address 0x1ffffc40, so I doubt the Hard Fault was caused by an alignment fault.
I read
KL16 Sub-Family Reference Manual, Rev. 3.2, October 2013, and
Production Flash Programming Best Practices for Kinetis K- and L-series MCUs, Rev 1, 05/2014, and
Avoiding Read While Write Errors When Developing In-Software Flash Programming Applications for Kinetis and ColdFire+ MCUs, Rev. 0, 04/2013, and
Kinetis SDK v.2.0 API Reference Manual
I got it to work using the SDK APIs.
main()
init()
read()
init()
FLASH_Init(&m_flashDriver);
FLASH_GetProperty(&m_flashDriver, kFLASH_PropertyPflash0BlockBaseAddr, &iflashBlockBase);
FLASH_GetProperty(&m_flashDriver, kFLASH_PropertyPflash0TotalSize, &iflashTotalSize);
FLASH_GetProperty(&m_flashDriver, kFLASH_PropertyPflash0SectorSize, &m_iflashSectorSize);
m_destAdrss = iflashBlockBase + (iflashTotalSize - m_iflashSectorSize);
read()
memcpy((void *)&m_AppSettingsInFLASH, (const void *)m_destAdrss, sizeof(T_APP_SETTING_IN_FLASH));
write()
FLASH_GetSecurityState(&m_flashDriver, &securityStatus);
FLASH_Erase(&m_flashDriver, m_destAdrss, m_iflashSectorSize, kFLASH_ApiEraseKey);
FLASH_VerifyErase(&m_flashDriver, m_destAdrss, m_iflashSectorSize, kFTFx_MarginValueUser);
FLASH_Program(&m_flashDriver, m_destAdrss, (uint8_t*)&m_AppSettingsInFLASH, sizeof(T_APP_SETTING_IN_FLASH));
FLASH_VerifyProgram(&m_flashDriver, m_destAdrss, sizeof(T_APP_SETTING_IN_FLASH), (uint8_t*)&m_AppSettingsInFLASH, kFTFx_MarginValueUser,&failAddr, &failDat);