cancel
Showing results for 
Search instead for 
Did you mean: 

Hardfault in RDP Level Change

PoojaAgri
Associate II

Hello, I am writing code for changing the RDP Level, but I am facing a hard fault trigger in the code:

this is the line after unlocking the FLASH_CR_LOCK, and FLASH_CR_OPTLOCK. After unlocking able to change the RDP level to 1 or 2 but as soon the debugger goes to line 2 (below), the hard fault is triggered.

 

 

while((FLASH->SR & FLASH_SR_BSY1) != 0x00);
FLASH->CR = FLASH->CR | FLASH_CR_OPTSTRT; // this line triggers the hard fault

 

Can anybody suggest any solution?

5 REPLIES 5
Imen.D
ST Employee

Hello @PoojaAgri and welcome to the Community ,

Which device used?

Check the option bytes configuration. Check FLASH->SR flag. 

How are you reading the register? 

Did you have secure or non-secure program ?
Please provide more details on your case in order to help you.
When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Jocelyn RICARD
ST Employee

Hello @PoojaAgri,

RDP is memory protection.

As soon as you setup RDP1, connected to debugger you will get a hardfault. This is part of the protection.

This is also what happens when you connect to a target already in RDP1. It immediately go to hardfault so that no instruction can be executed while debugger is connected.

Best regards

Jocelyn

Hi @Imen.D ,

Thank you for replying.
STM32G070CB is used.
I checked the FLASH->SR flag, they are fine.

I am reading the register using the READ_BIT() function directly and using the register bit directly from flash.

Yes, I have secure program.

 

AS for more information:

I read the rdp level directly from FLASH->OPTR register. Then I unlock the FLASH_CR_LOCK and FLASH_CR_OPTLOCK following the steps in the manual of STM32G070CB respectively.

Then according to the input whether I want to change the rdp level to 0, 1, or 2. I wrote if condition and write the value directly to the FLASH->OPTR register where we have rdp bit.

Then I check whether the FLASH_SR_BSY1 != 0 and then I set the FLASH_CR_OPTSRT.

And as soon as I the debugger process this line, it goes directly to hard fault handler.

 

Hope this information will be helpful.

 

PoojaAgri
Associate II

Hello @Jocelyn RICARD 

Thank you for your reply. I understand now why it is going to the hard fault.

Then how can I check using debugger that my code is working fine, because even if it is changing the rdp level, it is throwing error, which means it is coming back to that loop value.

Before changing rdp level I am checking if the enter value is same with the current rdp level value then, it will throw error.

But when I am giving different value, it is changing the rdp level, but it is throwing the same error which means it is coming back to that loop. why? It should not come back to that loop because the rdp level is changed successfully.

 

Jocelyn RICARD
ST Employee

Hello @PoojaAgri ,

you have actually no way to debug the change to RDP level 1.

Just launch your application without debugger.

Once RDP Level 1 is set (following your code), you will need to perform a power-on reset.

After restarting, your application should be able to check you are already in RDP Level 1.

Also, you should be able to connect with STM32CubeProgrammer and see the option byte is correctly set.

Best regards

Jocelyn