2024-01-18 09:57 AM - edited 2024-01-19 03:03 AM
I am using STM32L083 and need to erase it using J-Link. If it is locked at Level1, I also need to unlock it. My goal is to completely erase the entire Flash memory. I believe I have an error in the process, as I have been unsuccessful in both erasing and unlocking. Could anyone please advise me?
Thank you very much, for any help.
My attempt:
def eraseMCU(self):
self.Jlink.connect(self._mcuToConnect, speed = self._swd_speed_khz)
self.unlockFlash()
self.unlockOptionBytesArea()
self.programoptionBytes(RDP_UNLOCK) #0xAA
self.reloadOptionBytes()
self.Jlink.reset(ms = 1,halt = False)
RDP_UNLOCK = 0xAA
PRGKEY1 = 0x8C9DAEBF
PRGKEY2 = 0x13141516
PEKEY1 = 0x89ABCDEF
PEKEY2 = 0x02030405
OPTKEY1 = 0xFBEAD9C8
OPTKEY2 = 0x24252627
PECR_LOCK_BIT = (1<<0)
PECR_PRGLOCK_BIT = (1<<1)
PECR_OPTLOCK_BIT = (1<<2)
SR_EOP = (1<<1)
SR_BUSY = (1<<0)
OBL_LAUNCH = (1<<18)
FLASH_PECR = 0x40022000+0x4
FLASH_PEKEYR= 0x40022000+0x0C
FLASH_OPTKEYR = 0x40022000+0x14
FLASH_OPTR = 0x40022000+0x1C
FLASH_PRGKEYR = 0x40022000+0x10
FLASH_SR = 0x40022000+0x18
def unlockFlash(self):
self.busy_wait()
tmp = self.readU32Mem(FLASH_PECR)
if (tmp & PECR_LOCK_BIT ) != 0:
if self.writeU32ToMem(FLASH_PEKEYR,PEKEY1) == True:
if self.writeU32ToMem(FLASH_PEKEYR,PEKEY2) == True:
return True
def unlockOptionBytesArea(self):
self.busy_wait()
tmp = self.readU32Mem(FLASH_PECR)
if not tmp & PECR_LOCK_BIT:
if tmp & PECR_OPTLOCK_BIT !=0 :
if self.writeU32ToMem(FLASH_OPTKEYR,OPTKEY1) == True:
if self.writeU32ToMem(FLASH_OPTKEYR,OPTKEY2) == True:
return True
def programoptionBytes(self,val):
val = self.option_bytes_word(val)
self.writeU32ToMem(0x1FF80000,val)
self.busy_wait()
flash_sr = self.readU32Mem(FLASH_SR)
if flash_sr & SR_EOP != 0:
self.writeU32ToMem(FLASH_SR, SR_EOP)
def reloadOptionBytes(self):
tmp = self.readU32Mem(FLASH_PECR)
tmp |= OBL_LAUNCH
print(hex(tmp))
self.writeU32ToMem(FLASH_PECR,tmp)