J-link debugger cannot connect after enabling secure user memory
Hello everyone,
I am working with the STM32H7B3 and after enabling the secure user memory, i am not able to connect with the debugger anymore:
For testing if secure user memory works, i put a "secure_function" in a section at the end of my flash, then enabled the secure user memory for that section and called the function from the unsecure area (which should result to a bus fault). The questions are:
1. What did i wrong?
2. Why can i not connect to the board which the jlink debugger? I didn't change any option byte except the security OB.
Also i don't thing that the program got stuck in the secure area, because the unsecure area should run first after reset (see code below).
Console output:

Here is the code i used to configure the secure user memory:
void __attribute__((__section__(".secureareatest"))) secure_function(){
count++;
}
bool setSecureUserMemoryProtection(FLASH_OBProgramInitTypeDef& flash_option_bytes) {
const uint32_t secure_area_start = (0x81FDFFF >> 8U) << 8U; // shift needed for 256 bytes granularity
RSS_SecureArea_t aSecureAreas[2];
if (check_secure_memory_protection(flash_option_bytes)) {
return true;
} else {
if ((flash_option_bytes.USERConfig & FLASH_OPTSR_SECURITY) == RESET) {
flash_option_bytes.Banks = FLASH_BANK_1;
flash_option_bytes.OptionType = OPTIONBYTE_USER;
flash_option_bytes.USERType = OB_USER_SECURITY;
flash_option_bytes.USERConfig = OB_SECURITY_ENABLE;
if (HAL_FLASHEx_OBProgram(&flash_option_bytes) == HAL_OK) {
/*
Security bit set.
We need to reload the OBs before configuring the secure user memory area,
otherwise the configuration is skipped because Secure Mode is not entered at reset (not booting in RSS).
*/
if(HAL_FLASH_OB_Launch() != HAL_OK){
return false;
}
} else{
return false;
}
}
/* Set the secure user memory area */
aSecureAreas[0].sizeInBytes = 8 * 1024;
aSecureAreas[0].startAddress = secure_area_start;
aSecureAreas[0].removeDuringBankErase = 1;
/* Only 1 secure area is used */
aSecureAreas[1].sizeInBytes = 0U;
aSecureAreas[1].startAddress = 0U;
aSecureAreas[1].removeDuringBankErase = 1U;
RSS_API->resetAndInitializeSecureAreas(1, aSecureAreas);
}
}
int main(){
FLASH_OBProgramInitTypeDef flash_option_bytes;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_ALL_ERRORS_BANK1);
HAL_FLASH_OB_Unlock();
flash_option_bytes.Banks = FLASH_BANK_1;
HAL_FLASHEx_OBGetConfig(&flash_option_bytes);
set_secure_user_memory_protection(flash_option_bytes);
while (true) {
secure_function();
}
}
