Unable to upload code to stm32f401cdu6

I am trying to upload the code blackpill based on stm43f401cdu6.

Here is the error that I got when try to upload using st link.

Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.8% (used 7624 bytes from 98304 bytes)
Flash: [==        ]  21.2% (used 83232 bytes from 393216 bytes)
Configuring upload protocol...
AVAILABLE: dfu, jlink, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\STM32F401CD_blackpill_stlink\firmware.elf
xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev-00378-ge5be992df (2020-06-26-09:29)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

embedded:startup.tcl:26: Error: Can't find scripts/interface/stlink.cfg
in procedure 'script' 
at file "embedded:startup.tcl", line 26
*** [upload] Error 1
================================================= [FAILED] Took 26.38 seconds =================================================

Environment                   Status    Duration
----------------------------  --------  ------------
STM32F401CD_blackpill_stlink  FAILED    00:00:26.379
============================================ 1 failed, 0 succeeded in 00:00:26.379 ============================================ 
1 Like

AFAIK, the black pill uses DFU for uploading - it does not have an ST-Link on board.
I suggest trying upload_protocol = dfu in platformio.ini.
You’ll need to put the board in “DFU mode” (a specific way of pressing the on-board buttons).

I am using external stlink.

I have another project which upload perfectly fine using stlink.

I am trying to add blackpill to Marlin (3D printer firmware).

ini file with which it uploads

[env:blackpill_f401cc]
platform = ststm32
board = blackpill_f401cc
framework = arduino

upload_protocol = stlink

With which it does not upload

[common_stm32]
platform         = ststm32@~12.1
board_build.core = stm32
build_flags      = ${common.build_flags}
                   -std=gnu++14 -DHAL_STM32
                   -DUSBCON -DUSBD_USE_CDC
                   -DTIM_IRQ_PRIO=13
                   -DADC_RESOLUTION=12
build_unflags    = -std=gnu++11
build_src_filter = ${common.default_src_filter} +<src/HAL/STM32> +<src/HAL/shared/backtrace>
extra_scripts    = ${common.extra_scripts}
                   pre:buildroot/share/PlatformIO/scripts/stm32_serialbuffer.py


[env:STM32F401CD_blackpill_stlink]
platform = ${common_stm32.platform}
extends = common_stm32
board = marlin_custom_blackpill
board_build.mcu = stm32f401cdu6
board_build.f_cpu = 84000000L
build_flags = ${common_stm32.build_flags} 
              -DHSE_VALUE=25000000
              -DHAL_PCD_MODULE_ENABLED
              -DHAL_UART_MODULE_ENABLED
              -DUSE_FULL_LL_DRIVER

monitor_speed               = 115200
upload_protocol             = stlink

Here is the information about the custom board added for the marlin

{
  "build": {
    "core": "stm32",
    "cpu": "cortex-m4",
    "extra_flags": "-DSTM32F401xE -DSTM32F401xx",
    "f_cpu": "84000000L",
    "hwids": [
      [
        "0x1EAF",
        "0x0003"
      ],
      [
        "0x0483",
        "0x3748"
      ]
    ],
    "ldscript": "ldscript.ld",
    "mcu": "stm32f401cdu6",
    "variant": "marlin_custom_blackpill"
  },
  "debug": {
    "jlink_device": "STM32F401CD",
    "openocd_target": "stm32f4x",
    "svd_path": "STM32F40x.svd",
    "tools": {
      "stlink": {
        "server": {
          "arguments": [
            "-f",
            "scripts/interface/stlink.cfg",
            "-c",
            "transport select hla_swd",
            "-f",
            "scripts/target/stm32f4x.cfg",
            "-c",
            "reset_config none"
          ],
          "executable": "bin/openocd",
          "package": "tool-openocd"
        }
      }
    }
  },
  "frameworks": [
    "arduino",
    "stm32cube"
  ],
  "name": "STM32F401CD (96k RAM. 384k Flash)",
  "upload": {
    "maximum_ram_size": 98304,
    "maximum_size": 393216,
    "protocol": "stlink",
    "protocols": [
      "stlink",
      "dfu",
      "jlink"
    ]
  },
  "url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f401cd.html",
  "vendor": "Generic"
}

After updating the json file it worked fine

here is the updated file for reference

{
  "build": {
    "core": "stm32",
    "cpu": "cortex-m4",
    "extra_flags": "-DSTM32F401xC -DSTM32F401xx",
    "f_cpu": "84000000L",
    "hwids": [
      [
        "0x1EAF",
        "0x0003"
      ],
      [
        "0x0483",
        "0x3748"
      ]
    ],
    "ldscript": "ldscript.ld",
    "mcu": "stm32f401cdu6",
    "variant": "marlin_custom_blackpill"
  },
  "debug": {
    "jlink_device": "STM32F401CD",
    "openocd_target": "stm32f4x",
    "svd_path": "STM32F40x.svd"
  },
  "frameworks": [
    "arduino",
    "stm32cube"
  ],
  "name": "STM32F401CD (96k RAM. 384k Flash)",
  "upload": {
    "maximum_ram_size": 98304,
    "maximum_size": 393216,
    "protocol": "stlink",
    "protocols": [
      "stlink",
      "dfu",
      "jlink"
    ]
  },
  "url": "https://www.st.com/en/microcontrollers-microprocessors/stm32f401cd.html",
  "vendor": "Generic"
}