cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U5 HAL_FLASH_Unlock() fails

micklecat
Associate II

Hello everybody!

Our company's QA reported some devices with STM32U585VIT6 CPU in a state when write to Flash became impossible.

As a developer, I tried to reproduce this and after some experiments got stuck in a strange state when HAL_FLASH_Unlock returns error. Cannot say exactly which steps led to this result. First I set RDB option byte to BB, then to AA in Cubeprogrammer. Changed WRP1A_PSTRT and WRP1A_PEND. And now:

- write to Flash and OBs from software is impossible due to permanent HAL_FLASH_Unlock() error,

- reset doesn't help,

- at the same time chip can be programmed via J-link and ST-link.

No such case described in errata sheet nor in forums. Below are option bytes read by Cubeprogrammer.

STM32_Programmer_CLI -c port=swd freq=1800 -ob displ

     -------------------------------------------------------------------

                      STM32CubeProgrammer v2.11.0

     -------------------------------------------------------------------

ST-LINK SN : 140018001500004737364D4E

ST-LINK FW : V2J39S7

Board      : --

Voltage    : 3.25V

SWD freq   : 1800 KHz

Connect mode: Normal

Reset mode : Software reset

Device ID  : 0x482

Revision ID : Rev X

Device name : STM32U575/STM32U585

Flash size : 2 MBytes

Device type : MCU

Device CPU : Cortex-M33

BL Version : 0x92

Debug in Low Power mode enabled

UPLOADING OPTION BYTES DATA ...

 Bank         : 0x00

 Address      : 0x40022040

 Size         : 36 Bytes

 Bank         : 0x01

 Address      : 0x40022068

 Size         : 8 Bytes

OPTION BYTES BANK: 0

  Read Out Protection:

    RDP         : 0xAA (Level 0, no protection)

  BOR Level:

    BOR_LEV     : 0x0 (BOR Level 0, reset level threshold is around 1.7 V)

  User Configuration:

    TZEN        : 0x0 (Global TrustZone security disabled)

    nRST_STOP   : 0x1 (No reset generated when entering Stop mode)

    nRST_STDBY  : 0x1 (No reset generated when entering Standby mode)

    nRST_SHDW   : 0x1 (No reset generated when entering the Shutdown mode)

    SRAM134_RST : 0x1 (SRAM1, SRAM3 and SRAM4 not erased when a system reset occurs)

    IWDG_SW     : 0x1 (Software independant watchdog)

    IWDG_STOP   : 0x1 (IWDG counter active in stop mode)

    IWDG_STDBY  : 0x1 (IWDG counter active in standby mode)

    WWDG_SW     : 0x1 (Software window watchdog)

    SWAP_BANK   : 0x0 (Bank 1 and bank 2 address are not swapped)

    DBANK       : 0x1 (Dual-bank Flash with contiguous addresses)

    BKPRAM_ECC  : 0x1 (Backup RAM ECC check disabled)

    SRAM3_ECC   : 0x1 (SRAM3 ECC check disabled)

    SRAM2_ECC   : 0x1 (SRAM2 ECC check disabled)

    SRAM2_RST   : 0x1 (SRAM2 is not erased when a system reset occurs)

    nSWBOOT0    : 0x1 (BOOT0 taken from PH3/BOOT0 pin)

    nBOOT0      : 0x1 (nBOOT0 = 1)

    PA15_PUPEN  : 0x1 (USB power delivery dead-battery disabled/ TDI pull-up activated)

    IO_VDD_HSLV : 0x0 (High-speed IO at low VDD voltage feature disabled (VDD can exceed 2.5 V))

    IO_VDDIO2_HSLV: 0x0 (High-speed IO at low VDDIO2 voltage feature disabled (VDDIO2 can exceed 2.5 V))

  Boot Configuration:

    NSBOOTADD0  : 0x100000 (0x8000000)

    NSBOOTADD1  : 0x17F200 (0xBF90000)

  Write Protection 1:

    WRP1A_PSTRT : 0x0 (0x8000000)

    WRP1A_PEND  : 0xF (0x801E000)

    UNLOCK_1A   : 0x1 (WRP1A start and end pages unlocked)

    WRP1B_PSTRT : 0x7F (0x80FE000)

    WRP1B_PEND  : 0x0 (0x8000000)

    UNLOCK_1B   : 0x0 (WRP1B start and end pages locked)

OPTION BYTES BANK: 1

  Write Protection 2:

    WRP2A_PSTRT : 0x7F (0x81FE000)

    WRP2A_PEND  : 0x0 (0x8100000)

    UNLOCK_2A   : 0x1 (WRP2A start and end pages unlocked)

    WRP2B_PSTRT : 0x7F (0x81FE000)

    WRP2B_PEND  : 0x0 (0x8100000)

    UNLOCK_2B   : 0x1 (WRP2B start and end pages unlocked)

1 ACCEPTED SOLUTION

Accepted Solutions

Thanks everybody for answers,

the reason was stranger than I could expect. It was applying HAL_UART_DeInit to UART1 without previous HAL_UART_Init. After this HAL_FLASH_Unlock returned error. Obviously, the reason is inside HAL. I applied a workaround by not calling HAL_UART_DeInit without init.

BR,

Michael

View solution in original post

4 REPLIES 4
Diane POMABIA
ST Employee

Hello @micklecat​ 

To answer you better I have a small question, when you say "I set RDB option byte to BB, then to AA in Cubeprogrammer" you want to talk about the RDP ? did you first set it to DC or CC ?

Regards

DianeP

CMYL
ST Employee

Dear @micklecat​,

HAL_FLASH_Unlock() is used to unlock the flash by giving 2 keys. This is needed before write/erase accesses to the Flash.

According to your Option Byte list, there is nothing that forbids flash unlock.

Can you dump the Flags in the status register FLASH_NSSR (also the content of FLASH_NSCR is useful)

Regards,

Younes

Thanks everybody for answers,

the reason was stranger than I could expect. It was applying HAL_UART_DeInit to UART1 without previous HAL_UART_Init. After this HAL_FLASH_Unlock returned error. Obviously, the reason is inside HAL. I applied a workaround by not calling HAL_UART_DeInit without init.

BR,

Michael

CMYL
ST Employee

Thank you too :)