cancel
Showing results for 
Search instead for 
Did you mean: 

Read protection self reset

confucij
Associate II

I'm trying to set read protection on STM32F412 to level 1 via JLink during initial flashing. I'm issuing next commands:

J-Link>w4 0x40023C08 0x08192A3B
Writing 08192A3B -> 40023C08
J-Link>w4 0x40023C08 0x4C5D6E7F
Writing 4C5D6E7F -> 40023C08
J-Link>mem32 0x40023C14 1
40023C14 = 0FFFAAEC 
J-Link>w4 0x40023C14 0x0fff55ED
Writing 0FFF55ED -> 40023C14
J-Link>mem32 0x40023C14 1
40023C14 = 0FFF55ED 
J-Link>r
Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
J-Link>mem32 0x40023C14 1
40023C14 = 0FFFAAED 

But AFTER rest and BEFORE any user code can run it self-resets from 0x55 - LEVEL1 to AA - LEVEL0.

A few hours before this happened I was able to lock it from bootloader with next code:

  FLASH_OBProgramInitTypeDef flash;
  HAL_FLASH_OB_Unlock();
  flash.RDPLevel = OB_RDP_LEVEL_1;
  flash.WRPState = OB_WRPSTATE_ENABLE;
  flash.WRPSector = OB_WRP_SECTOR_All;
  HAL_FLASHEx_OBProgram(&flash);
  HAL_FLASH_OB_Lock();

After issuing this code I was not able connect to MCU via JLink. Next I run code to switch from Level 1 to Level 0:

    FLASH_OBProgramInitTypeDef flash;
    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();
    flash.RDPLevel = OB_RDP_LEVEL_0;
    flash.WRPState = OB_WRPSTATE_DISABLE;
    HAL_FLASHEx_OBProgram(&flash);

After this I was able to connect to MCU via JLink BUT no flash erase was performed.

And now I can't set read protection nor with JLink nor with user code.

Am I doing something wrong or MCU just broke?

7 REPLIES 7
confucij
Associate II

I've update my own script and now it looks like it works now. I should read docs better.

loadbin stm32_initial_flash_full.bin 0x08000000
rh
sleep 200
w4 0x40023C08 0x08192A3B
sleep 100
w4 0x40023C08 0x4C5D6E7F
sleep 200
mem32 0x40023C14 1
sleep 200
w4 0x40023C14 0x0fff55EC
sleep 200
mem32 0x40023C14 1
sleep 200
w4 0x40023C14 0x0fff55EE
sleep 200
rnh

I've changed user code to just register access but after Flash lock device hangs until power down reset

  FLASH->OPTKEYR = FLASH_OPT_KEY1;
  FLASH->OPTKEYR = FLASH_OPT_KEY2;
  FLASH->OPTCR = 0x0FFF55EC;
  FLASH->OPTCR = 0x0FFF55EE;

confucij
Associate II

MCU hangs after setting read protection both via JLink and from user code. Only complete power cycle helps. Is it anything I'm missing or this is an expected behavior?

Bob S
Principal

That is (probably) the documented behavior when programming RDP levels via JTAG. Once you program the device to level 1 or level 2 RDP, any attempted access via JTAG to memory halts the CPU. As for why the CPU hangs after your user code sets the RDP level, do you have the JTAG debugger connected? If so, that will likewise cause the halt.

I've disconnected JTAG from MCU during update via bootloader and this does not help - MCU still hungs until power off.

Try to use watchdog, to reset automatically after some time

-- pa

I've tried to reset it via rest pin multiple times. Does watchdog reset differ from pin reset?

SRouz.1
Associate

Hi.

I am working with a mcu stm32f030k6

.

My problem is that I can not protect my memory reading.

I used a few code samples but got no results.

Is there a solution?

Using with writing code of the the main mcu or 

This can be done through the programmer J-link

Thank you very much for your help.

An example of my code that That did not work and does not run mcu

main{

 ReadProtectionConfig (1) 

}

uint8_t ReadProtectionConfig(uint8_t ProtectionLevel)

{

 uint8_t status = 0;

 FLASH_OBProgramInitTypeDef OB_Init;

 HAL_FLASH_Unlock();

 HAL_FLASH_OB_Unlock();

 OB_Init.OptionType = OPTIONBYTE_RDP;

 if (ProtectionLevel == 0) {

 OB_Init.RDPLevel = OB_RDP_LEVEL_0;

 } else if (ProtectionLevel == 1) {

 OB_Init.RDPLevel = OB_RDP_LEVEL_1;

 } else if (ProtectionLevel == 2) {

 OB_Init.RDPLevel = OB_RDP_LEVEL_2;

 } else {

 status = 1;

 return status;

 }

HAL_FLASHEx_OBProgram(&OB_Init);

 HAL_FLASH_OB_Launch();

 HAL_FLASH_OB_Lock();

 HAL_FLASH_Lock();

 return status;

}