Setting _FLASH_SIZE in Debug upload (Bluepill)

Hi, I have a bluepill with a F103C8 uC that has 128K of usable flash (64K Reported), the upload and verify works fine with the following platformio.ini:

[env:testv2]
platform = ststm32@^14.0.0
board = bluepill_f103c8_128k
framework = arduino
upload_protocol = stlink
upload_flags = -c set FLASH_SIZE 0x20000
debug_tool = stlink

I also tested it by printing through the serial monitor more than 64K of flash text.

My problem is when uploading in Debug Mode, I managed to make it compile to 128K with the following line: “board_upload.maximum_size = 131072”, but when uploading it autodetects the Flash size as 64K: “Info : flash size = 64kbytes”, to make it upload I had to modify the file “.platformio/packages/tool-openocd/scripts/target/stm32f1x.cfg” by changing the autodetect flash size to the value required:

else {
# autodetect size it was 0
set _FLASH_SIZE 0x20000
}

My question is, is there any way to set this _FLASH_SIZE to the desired value, similar to the command “upload_flags = -c set FLASH_SIZE 0x20000” but for the Debug upload_flags?

Thanks, let me know if you need anything else.

Per

You should be able to create a (or locally modify the existing) board definition to inject -c "set FLASH_SIZE 0x20000" through the openocd_extra_args.

Per

you can see the board already has some stuff there for reset config. Does it work if you add to this array

"-c",
"set FLASH_SIZE 0x20000"

in your local <home path>/.platformio/platforms/ststm32/boards/bluepill_f103c8_128k.json file, then restart VSCode and retry debugging without any previous .cfg file modification?

A STM32F103C8 MCU with 128K is a STM32F103CB. Why not use on of the F103CB boards?

This might a case of the ID code being that of a C8 but internally really having 128KB (C8 come from the same die as CB and are just less tested), and then OpenOCD rejecting the upload. But yes, OP can give that a try too.

I tried modifying as requested (pretty sure):

"openocd_extra_args": [
  "-c",
  "reset_config none",
  "-c",
  "set FLASH_SIZE 0x20000"
],

and it is still autodetected as 64k and the upload fails.

just in case I also tried putting a random command and I get an invalid command error, so it is executing those commands from that file.

I noticed something, when the command above is present I get a random 0x20000 printed in the console:

xPack OpenOCD, x86_64 Open On-Chip Debugger 0.11.0-00155-ge392e485e (2021-03-15-16:43)
Licensed under GNU GPL v2
For bug reports, read
OpenOCD: Bug Reporting
hla_swd
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate

0x20000
Info : tcl server disabled
Info : telnet server disabled
Info : clock speed 1000 kHz
Info : STLINK V2J31S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.258614
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f1x.cpu on pipe
Info : accepting ‘gdb’ connection from pipe
Info : device id = 0x20036410
Info : flash size = 64kbytes

I tried with the following Board (genericSTM32F103CB) and it didn’t work, it autodetected the 64k of flash.

Thanks for the suggestion.

Hi! Were you able to get around the issue?
I’m stuck with the same problem and discovered that openocd_extra_args gets appended at the end of openocd command, after it processes the target/stm32.cfg file, so openocd still does flash autodetect. Interestingly, upload_flags are prepended to openocd command, so it works when uploading.

Was able to fix this by creating a boards folder in my project, creating a new board json using the contents of bluepill_f103c8_128k.json and changing "openocd_target": "stm32f1x", to "openocd_target": "../board/stm32f103c8_blue_pill",. On platformio.ini change the board parameter to the name of the new json.