12

I'm using MDK-Lite Version 5.23 with a STMicroelectronics STM32F072B-Disco "Discovery" board and I am trying to use the Flash Example provided by the Discovery samples.

I've used this board and toolchain for other examples and I have coded some SPI and GPIO work. The IDE works like a champ. However, for this particular project I can build the code and run it by downloading and using the reset button. I cannot use the debugger on the project as soon as I use the HAL_FLASHEx_Erase() routine. Once I execute that routine the IDE pops up a dialog "Cannot access target. Shutting down debug session."

For what it's worth, I know it's not a programming error because if I download the code and then execute the code by pressing the reset button it will work. I've used this same debugger with a TI board and it was able to program flash and execute flash routines as well. I'm pretty sure I'm not erasing the portion of flash where the code is stored, so it's not that.

If I step over this line in main.c

if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)

then it drops the debug session. If I instead step into the same line and then step over each of the calls in the flash erase routine, then it will work and eventually step out of the routine and I can debug the rest of the code.

Helmar
  • 8,450
  • 6
  • 36
  • 84
netskink
  • 321
  • 1
  • 7

1 Answers1

7

My guess is that this is a power-supply related at some level. Either the external supply, or on-board switching of power rails.

To clarify the scenario, debug works fine after a hardware reset, but when your target erases a block of flash, the debug connection is dropped?

Debug doesn't care about code running correctly - you can be in lockup state, and halting debug should still work. The only thing at the CPU side which locks out debug is a deadlocked AHB access. This means that the problem is either with the SWD interface between the STM32F7, and the on-board USB-SWD interface chip (also an STM32, I presume). This device has some on-board power rail switching which confused me the first time I used the board.

It's worth noting that flash erase will increase the current drain of the device - is your external PSU up to the job, and can you use an alternative?

Edit: Based on your feedback that stepping over the code in question causes the debugger to crash, whereas single-stepping does not, I think your issue is related to this question.

Step-over is implemented by using a breakpoint (and polling for halt state), whereas single-step is supported in hardware. This still doesn't explain why the debugger seems to be getting confused, but does allow for it being possible that the debugger is trying to access code (from flash) whilst the flash controller is active.

Based on these observations, I'd suggest you set a breakpoint after the erase, and try to avoid triggering this scenario.

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
Sean Houlihane
  • 10,524
  • 2
  • 26
  • 62