Skip to main content
ABouk
Associate II
August 15, 2019
Question

Error: failed to erase memory

  • August 15, 2019
  • 17 replies
  • 20799 views

Good afternoon, I was trying to debug a program on my new stm32f103C8T6 chip using the st-link v2 programmer, but i always get the same error : Error: failed to erase memory

I have tried the procedure with multiple boards so i guess that the problem is software, here is the full console output :

STMicroelectronics ST-LINK GDB server. Version 5.2.3

Copyright (c) 2019, STMicroelectronics. All rights reserved.

Starting server with the following options:

    Persistent Mode      : Disabled

    Logging Level       : 1

    Listen Port Number     : 61234

    Status Refresh Delay    : 15s

    Verbose Mode        : Disabled

    SWD Debug         : Enabled

Waiting for debugger connection...

Debugger connected

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

            STM32CubeProgrammer v2.1.0          

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

Log output file:  /tmp/STM32CubeProgrammer_k3pQvR.log

ST-LINK SN : 33FF6D064E59343942520343

ST-LINK FW : V2J33S7

Voltage   : 3.20V

SWD freq  : 4000 KHz

Connect mode: Under Reset

Reset mode : Hardware reset

Device ID  : 0x410

Device name : STM32F101/F102/F103 Medium-density

Flash size : 63.999 MBytes

Device type : MCU

Device CPU : Cortex-M3

Memory Programming ...

Opening and parsing file: ST-LINK_GDB_server_JSWBJu.srec

 File     : ST-LINK_GDB_server_JSWBJu.srec

 Size     : 9716 Bytes

 Address    : 0x08000000 

Erasing memory corresponding to segment 0:

Erasing internal memory sectors [0 9]

Error: failed to erase memory

Error: failed to erase memory

Encountered Error when opening /Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.macos64_1.0.0.201904021149/tools/bin/STM32_Programmer_CLI

Error in STM32CubeProgrammer

Error! Failed to read target status 

Debugger connection lost.

Shutting down...

Thanks !

This topic has been closed for replies.

17 replies

Jack Peacock_2
Associate II
August 15, 2019

"Flash size : 63.999 MBytes"

WOW! Where do I get an STM32 with 64MB of internal flash????? And it's only medium density?

Looks like you're trying to erase far more memory than what's installed. You'll hit a hard fault at the end of real memory.

Jack Peacock

ABouk
ABoukAuthor
Associate II
August 15, 2019

It is written in the data sheet that the stm32f103c8t6 got 64/128 Kb flash I think i negliged the Mb unity, can you tell me how can i fix it ?

ABouk
ABoukAuthor
Associate II
August 15, 2019

When i write the .elf file in the flash, i get "stlink_fwrite_flash() == -1" ...

Can someone help me with those flash problems please ?

Bob S
Super User
August 15, 2019

You might try updating the STLinkV2 firmware. There is at least a version V2.J34M25 available. Also, have you checked the option bytes to see if any sectors/pages are write protected? Though I would expect an error message from STCubeProgrammer saying so if that were the case.

Is this a custom board, or one of the Nucleo/Discovery boards? I'm guessing from your "I have tried the procedure with multiple boards" comment that this is a custom board. If that is case, check the power supplies, bypass capacitors and reset circuit.

ABouk
ABoukAuthor
Associate II
August 15, 2019
  • I have updated my stink v2 firmware to  V2.J34M25, and no i didn't check that option, im getting the same error
  • Yes it's a custom board, the reset circuit is a pull-up 10k resistor + 100nf ceramic capacitor, and the power is provided by an ams1117-3.3v so it's safe.

I tried to use the st-flash command in terminal, here what i get :

0690X00000A9fWqQAJ.png

Bob S
Super User
August 15, 2019

Something is seriously messed up!

That terminal session shows that the programmer thinks there is 25616 KBytes of FLASH (as mentioned above). And apparently your program is 705348 bytes (688 KBytes). All targeted to a CPU that really only has 64KBytes of FLASH.

So first, find out why your program is so HUGE.

Maybe the ID value that is being returned isn't valid? Look in the reference manual this CPU and see what the ID is supposed to be. Look for the DBGMCU_IDCODE register.

Hmmmm... the FLASH size may be reported by the chip itself, if the debugger is reading the "Flash size data register". This should report 0x040 for 64KBytes. So maybe the debugger connection to the chip is flakey? Or the chips are somehow messed up.

I wonder if trying to erase pages way above the actual FLASH memory region has somehow broken the CPUs? I would hope this isn't the case.

And "power is provided by an ams1117-3.3v so it's safe". That isn't necessarily so. If you don't have all the power pins connected, or you have bad solder joints, or insufficient bypass capacitors, or the power supply feeding the 3.3V regulator is somehow current limiting. Check your schematic against the closest Nucleo/Discovery board that you can find.

ABouk
ABoukAuthor
Associate II
August 15, 2019

I have found a way to generate the bin file, it's only 4572 bytes but im getting the same problem ...

Im sure that the problem is software because i reviewed the pcb layout many times and tested solder points, resistors, capacitiors ...

Here is what i'm getting when i try to burn the 4.5 Kb in the chip's flash

0690X00000A9fdWQAR.png

Bob S
Super User
August 15, 2019

Have you tried to read the option bytes? You may need to go back to the CubeProgrammer to do that.

Maybe look at the SWD signals on a scope. Make sure they are clean, good edges and good signal levels.

The device ID value looks reasonable for your the F103 part. Though I would still be worried about the irrational value that both CubeProgrammer and st-flash are both reading for the FLASH size. It may be a case of both of them reading the (same) wrong memory address (kind of strange coincidence). Or it may be an indicator that the chips are really not behaving as they should and maybe the flash size register isn't the only thing that isn't working right.

ABouk
ABoukAuthor
Associate II
August 15, 2019

I have changed the wires for SW communication, still same result.

I don't think it's random value for the flash size, else i would have different value every time i reprogram the flash memory.

And I'm sorry but i don't know how to read the options bytes, still new in stm ...

Bob S
Super User
August 16, 2019

You are probably right about not flakey connection since the funny looking value is always the same. Do you get the same value from all of your boards?

Look in the reference manual for your CPU. Find the "flash size register" and see its address. For example, for the STM32L4xx, it is at 0x1fff 75e0. I would expect it is in a different location for your F103. Run your programmer software of choice and have it read memory from that address. See what it shows. Again, for my L433 it shows 0xffff 0100. The 16 MSBs are ignored because the size register is only 16 bits wide. 0x0100 = 256K Bytes of FLASH.

ABouk
ABoukAuthor
Associate II
August 16, 2019

@Bob S​ finally found the problem :

I went on Windows and attached the st link v2, started the program st-link and disabled 'read out protection' from configuration bits, once i did that, I could write freely any program on the flash using st-link command on mac OS and booting on System Memory (Boot<1:0> = 01).

Thanks all for your help !!0690X00000A9jl0QAB.png

Bob S
Super User
August 19, 2019

Don't forget to mark this as "answered' :)

AHofm
Visitor II
August 23, 2019

I got the same error with STM32L151

Waiting for debugger connection...

Debugger connected

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

                      STM32CubeProgrammer v2.1.0                 

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

Log output file:  C:\Users\Tonda\AppData\Local\Temp\STM32CubeProgrammer_a15552.log

ST-LINK SN : 0677FF564948897767025744

ST-LINK FW : V2J33M25

Voltage    : 3.24V

SWD freq   : 4000 KHz

Connect mode: Under Reset

Reset mode : Hardware reset

Device ID  : 0x429

Device name : STM32L100x6xxA/STM32L100x8xxA/STM32L100xBxxA/STM32L15xx6xxA/STM32L15xx8xxA/STM32L15xxBxxA

Flash size : 27.125 MBytes

Device type : MCU

Device CPU : Cortex-M3

Memory Programming ...

Opening and parsing file: ST-LINK_GDB_server_a15552.srec

 File         : ST-LINK_GDB_server_a15552.srec

 Size         : 88948 Bytes

 Address      : 0x08000000

Erasing memory corresponding to segment 0:

Error: Flash loader cannot be loaded

Error: failed to erase memory

Encountered Error when opening C:\ST\STM32CubeIDE_1.0.1\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.win32_1.0.0.201904021149\tools\bin\STM32_Programmer_CLI.exe

Error in STM32CubeProgrammer

Error! Failed to read target status

Debugger connection lost.

==============================

Another interesting thing...

1) I cannot erase the chip with STM32CubeProgrammer

11:02:40 : STM32CubeProgrammer API v2.1.0

11:02:42 : ST-LINK SN : 0677FF564948897767025744

11:02:42 : ST-LINK FW : V2J33M25

11:02:42 : Voltage : 3.24V

11:02:42 : SWD freq : 4000 KHz

11:02:42 : Connect mode: Under Reset

11:02:42 : Reset mode : Software reset

11:02:42 : Device ID : 0x429

11:02:42 : UPLOADING OPTION BYTES DATA ...

11:02:42 : Bank : 0x00

11:02:42 : Address : 0x40023c1c

11:02:42 : Size : 136 Bytes

11:02:42 : Bank : 0x01

11:02:42 : Address : 0x1ff80000

11:02:42 : Size : 136 Bytes

11:02:42 : UPLOADING ...

11:02:42 : Size : 1024 Bytes

11:02:42 : Address : 0x8000000

11:02:42 : Read progress:

11:02:42 : Data read successfully

11:02:42 : Time elapsed during the read operation is: 00:00:00.007

11:02:49 : MASS ERASE ... 

11:02:49 : Error: Mass erase operation failed.Please verify flash protection

2) but, I can erase it by STM32ST-LinkUtility

???