cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 Read Out Protection setup issues

mwp
Senior

Hi all,

I'm having issues getting ROP Level 1 setup on an STM32L412.

Below is the code which is run pretty soon after every reset.

After ROP has been set, and the system reset, ROP always reads back as 0xAA which leads to a boot loop of this function being run.

Strangely though, to erase/re-program the flash using a JLink, i first need to remove the ROP protection.

Any idea's what I'm doing wrong?

 

 

void Flash_Protect()
{
	//check RDP level
	uint32_t rdp_level = READ_REG(FLASH->OPTR) & FLASH_OPTR_RDP;
	if (rdp_level == 0xAA)
	{
		//currently LEVEL0=0xAA (LEVEL1=0xBB)
		//no read out protection is active
		//disable flash read out to external devices
		DEBUGS("FLASH IS NOT PROTECTED\n");
		//flash unlock
		if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
		{
			//authorise the FLASH Registers access
			WRITE_REG(FLASH->KEYR, 0x45670123U); //flash key 1
			WRITE_REG(FLASH->KEYR, 0xCDEF89ABU); //flash key 2
			//we'll assume it is unlocked
		}
		//flash OB unlock
		if (READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)
		{
			WRITE_REG(FLASH->OPTKEYR, 0x08192A3BU); //option byte key 1
			WRITE_REG(FLASH->OPTKEYR, 0x4C5D6E7FU); //option byte key 2
		}
		//wait for flash to not be busy
		while (READ_BIT(FLASH->SR, FLASH_SR_BSY) != 0) {}

		//configure the RDP level in the option bytes register
		MODIFY_REG(FLASH->OPTR, FLASH_OPTR_RDP, 0xBB); //set LEVEL1 (anything but 0xAA and 0xCC)
		SET_BIT(FLASH->PCROP1ER, FLASH_PCROP1ER_PCROP_RDP); //set PCROP_RDP (req for LEVEL1)

		//enable the OPTSTRT bit
		SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
		//wait for flash to not be busy
		while (READ_BIT(FLASH->SR, FLASH_SR_BSY) != 0) {}
		//disable the OPTSTRT bit
		CLEAR_BIT(FLASH->CR, FLASH_CR_OPTSTRT);

		//lock flash
		SET_BIT(FLASH->CR, FLASH_CR_LOCK);

		//Launch the option byte loading
		SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
		while (READ_BIT(FLASH->SR, FLASH_SR_BSY) != 0) {}

		//reset
		DEBUGS("FLASH PROTECT RESET\n");
		NVIC_SystemReset();
	}
	else
	{
		DEBUGS("FLASH IS PROTECTED\n");
	}
}

 

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Amel NASRI
ST Employee

Hi @mwp ,

In the materials provided with this video, you find explanation and tips to change RDP level without POR. Video is about STM32F4, but in the materials, you will find explanation for L4.

To make it short: To be able to achieve a RDP level increase ( level 1 or 2 ) without POR, you need to do a transition to StandBy state and then wakeup with RTC.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

5 REPLIES 5

waclawekjan_0-1715017624454.png

JW

Amel NASRI
ST Employee

Hi @mwp ,

In the materials provided with this video, you find explanation and tips to change RDP level without POR. Video is about STM32F4, but in the materials, you will find explanation for L4.

To make it short: To be able to achieve a RDP level increase ( level 1 or 2 ) without POR, you need to do a transition to StandBy state and then wakeup with RTC.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thank you, that's useful information.

 

Hi @Amel NASRI ,

while the workaround using Standby/wakeup is in the 'F4 RM,

waclawekjan_1-1715259557107.png

it is not mentioned in the 'L4 RM (see my previous post).

So, are you sure it *is* pertinent to 'L4?

And if yes, shouldn't it be present in the 'L4 RM?

Also, while in other instances the OBL_RELOAD operation appears to be equivalent of power-on reset, in this particular case it doesn't appear to be the case. Could this be please documented, too? Perhaps a table of effects of various types of resets on various features for each STM family could be illustrative, perhaps as an appnote or @KB-IDEA .

JW

 

 

Hi @waclawek.jan ,

Thanks for your feedback.

What you ask for is explained in the document dedicated for STM32F4 (in the previously shared materials):

AmelNASRI_0-1715264748862.png

Isn't the case?

I'll nominate this discussion to make the subject of a new article.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.