I cannot protect the data in USER_FLASH when I disconnect the ST-Link, connect it and then program the microcontroller via OpenOCD. I test it with the option (NOLOAD) in the linker script but the data always deleted.
STM32F103C8TX_FLASH.ld:
...
/* Memories definition */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 63K
USER_FLASH (xrw) : ORIGIN = 0x0800FC00, LENGTH = 1K
}
/* Sections */
SECTIONS
{
/* User data to be stored in the flash memory goes into USER_FLASH */
.user_data_flash (NOLOAD):
{
. = ALIGN(4);
*(.user_data_flash) /* .user_data_flash sections */
*(.user_data_flash*) /* .user_data_flash sections */
. = ALIGN(4);
} >USER_FLASH
...
The function works well while not disconnect the programmer:
void testFlash(void){
uint32_t temp = 0;
//writeFlash(test);
//Flash_Read_Data(0x0800FC00, temp);
temp = readFlashTest((uint32_t *)0x0800FC00);
temp = temp + 4;
writeFlash((uint32_t)temp);
}
uint32_t readFlashTest(uint32_t *mem){
uint32_t temp = 0;
HAL_FLASH_Unlock();
temp = *mem;
HAL_FLASH_Lock();
return temp;
} void writeFlash(uint32_t toWrite){
eraseFlash(); // Necesario si o si sino no escribe
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x0800FC00, toWrite);
HAL_FLASH_Lock();
}