cancel
Showing results for 
Search instead for 
Did you mean: 

How to reprogram STM32L452 chip when the flash is corrupted

Sanket
Associate II

Hello everyone,

I am new to the STM32 environment and I am trying to change the BOR level of STM32L452. I think I have corrupted the flash while trying to do this. And now when I try to reprogram the device it fails and gives the following error:

Open On-Chip Debugger 0.10.0+dev-00021-g524e8c8 (2019-04-12-08:48)

Licensed under GNU GPL v2

For bug reports, read

http://openocd.org/doc/doxygen/bugs.html

srst_only separate srst_nogate srst_open_drain connect_assert_srst

Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD

padded zone erase set to 1

adapter speed: 8000 kHz

adapter_nsrst_delay: 100

Info : clock speed 8000 kHz

Info : STLINK v2 JTAG v36 API v2 SWIM v7 VID 0x0483 PID 0x3748

Info : using stlink api v2

Info : Target voltage: 3.201051

Info : Unable to match requested speed 8000 kHz, using 4000 kHz

Info : Stlink adapter speed set to 4000 kHz

Info : STM32L452RETx.cpu: hardware has 6 breakpoints, 4 watchpoints

Info : Listening on port 3333 for gdb connections

target halted due to debug-request, current mode: Thread

xPSR: 0x01000000 pc: 0x08003340 msp: 0x20028000

Info : Unable to match requested speed 8000 kHz, using 4000 kHz

Info : Stlink adapter speed set to 4000 kHz

Info : Unable to match requested speed 8000 kHz, using 4000 kHz

adapter speed: 4000 kHz

  • Programming Started **

auto erase enabled

Info : Device id = 0x20016462

Info : STM32L4xx flash size is 512kb, base address is 0x8000000

Info : Erase the padded zone before the write

Error: Whole bank access must start at beginning of bank.

Error: corrupted fifo read pointer 0xffffffff

Error: timed out while waiting for target halted

target halted due to debug-request, current mode: Handler HardFault

xPSR: 0x61000003 pc: 0x08003406 msp: 0x20027fa8

Error: error waiting for target flash write algorithm

Error: block write failed

Error: error writing to flash at address 0x08000000 at offset 0x00000000

embedded:startup.tcl:476: Error: ** Programming Failed **

in procedure ‘program’

in procedure ‘program_error’ called at file “embedded:startup.tcl�?, line 535

at file “embedded:startup.tcl�?, line 476

And below is my code which caused the issue:

Call from Main.c

if(BOR_Set(BOR_Level_4) == BOR_Result_Ok)

{

HAL_GPIO_WritePin( LED1_PORT, LED1_G_PIN, GPIO_PIN_SET ); // Turn off

HAL_GPIO_WritePin( LED1_PORT, LED1_B_PIN, GPIO_PIN_RESET ); // Turn On

HAL_Delay( pdMS_TO_TICKS( 500 ) );

HAL_GPIO_WritePin( LED1_PORT, LED1_B_PIN, GPIO_PIN_SET ); // Turn off

HAL_GPIO_WritePin( LED1_PORT, LED1_G_PIN, GPIO_PIN_RESET ); // Turn On

}

//-----------------------------------------------

  1. include “stm32l4xx.h�?

/**

* @brief Brown-out levels available

*/

typedef enum {

BOR_Level_0 = OB_BOR_LEVEL_0, /*!< Disable brown-out detection */

BOR_Level_1 = OB_BOR_LEVEL_1, /*!< Set brown out detection level 1 */

BOR_Level_2 = OB_BOR_LEVEL_2, /*!< Set brown out detection level 2 */

BOR_Level_3 = OB_BOR_LEVEL_3, /*!< Set brown out detection level 3 */

BOR_Level_4 = OB_BOR_LEVEL_4

} BOR_Level_t;

BOR_Result_t BOR_Set(BOR_Level_t BORValue)

{

HAL_StatusTypeDef status;

FLASH_OBProgramInitTypeDef FLASH_Handle;

/* Check current BOR value */

if ((BOR_Get() & 0x0F00) != BORValue) // Bit 8 to bit 10 for BOR value.

{

/* Set new value */

/* Select the desired V(BOR) Level */

FLASH_Handle.OptionType = OPTIONBYTE_USER;

FLASH_Handle.USERConfig = (uint32_t)BORValue;

//FLASH_Handle.USERType = OB_USER_BOR_LEV;

HAL_FLASH_Unlock();

/* Unlocks the option bytes block access */

HAL_FLASH_OB_Unlock();

/* Set value */

HAL_FLASHEx_OBProgram(&FLASH_Handle);

/* Launch the option byte loading */

status = HAL_FLASH_OB_Launch();

/* Lock access to registers */

HAL_FLASH_OB_Lock();

HAL_FLASH_Lock();

/* Check success */

if (status != HAL_OK)

{

/* Return error */

return BOR_Result_Error;

}

}

/* Return OK */

return BOR_Result_Ok;

}

The issue started when I debugging the code and when the HAL_FLASH_OB_Launch function was called.

I believe what corrupted the flash was this statement

"FLASH_Handle.USERConfig = (uint32_t)BORValue;" instead I should have used

""FLASH_Handle.USERConfig = FLASH_Handle.USERConfig | (uint32_t)BORValue;

Please if someone could point out where I am going wrong in the code as well as how I can reprogram the chip as currently its not responding and its not working. I have ST-LINK/V2 for programming and debugging the device and I am using Ac6 STM32 system workbench tool chain with Eclipse IDE. I tried erasing the flash and reprogram it but still the same error of program failed. Please help.

1 ACCEPTED SOLUTION

Accepted Solutions
Sanket
Associate II

I solved the problem of reprogramming the chip by using ST link utility. With the help of ST link software I was able to connect, read the option bytes which were corrupted and set them accordingly. After that used the same software to erase the complete chip and program the code with the help of bin file.

View solution in original post

3 REPLIES 3
S.Ma
Principal

Change boot0 pin level and power cycle. Chip will go to system start code listening to usart and other serial interfaces, hence a working code which the stlink can intercept and take control. Typucally, low power modes and clock stop mode makes debugging logically more challenging.

Thank you for your answer, since I am new to this how can I change the boot0 pin level ? Is there any options in the Eclipse IDE that i need to change for this to take effect?

Sanket
Associate II

I solved the problem of reprogramming the chip by using ST link utility. With the help of ST link software I was able to connect, read the option bytes which were corrupted and set them accordingly. After that used the same software to erase the complete chip and program the code with the help of bin file.