STM32G4 flash download error "write into gap from 0x08010000 to 0x0803ffff"

fail to download code to STM32G474CBT6 flash. yet download code to G474RE successfully.
Error: write into gap from 0x08010000 to 0x0803ffff

infos:

Building in release mode
Checking size .pio\build\genericSTM32G474CB\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [===       ]  28.5% (used 37316 bytes from 131072 bytes)
Flash: [=====     ]  54.7% (used 71636 bytes from 131072 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\genericSTM32G474CB\firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

hla_swd
[stm32g4x.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080002b0 msp: 0x2000d278
** Programming Started **
Warn : Adding extra erase range, 0x080119c8 .. 0x080407ff
Error: write into gap from 0x08010000 to 0x0803ffff
Error: error writing to flash at address 0x08000000 at offset 0x00000000
embedded:startup.tcl:1516: Error: ** Programming Failed **
in procedure 'program' 
in procedure 'program_error' called at file "embedded:startup.tcl", line 1581
at file "embedded:startup.tcl", line 1516
*** [upload] Error 1

here I use STM32G474CBT6 with my own enviroment: env:genericSTM32G474CB.
I add my own board named “genericSTM32G474CB.json” at C:\Users\xxx.platformio\platforms\ststm32\boards

{
  "build": {
    "core": "stm32",
    "cpu": "cortex-m4",
    "extra_flags": "-DSTM32G4xx -DSTM32G474xx",
    "f_cpu": "170000000L",
    "mcu": "stm32g474cbt6",
    "product_line": "STM32G474xx",
    "variant": "STM32G4xx/G473R(B-C-E)T_G474R(B-C-E)T_G483RET_G484RET"
  },
  "connectivity": [
    "can"
  ],
  "debug": {
    "default_tools": [
      "stlink"
    ],
    "jlink_device": "STM32G474CB",
    "onboard_tools": [
      "stlink"
    ],
    "openocd_target": "stm32g4x",
    "svd_path": "STM32G474xx.svd"
  },
  "frameworks": [
    "arduino",
    "cmsis",
    "mbed",
    "libopencm3",
    "stm32cube",
    "zephyr"
  ],
  "name": "genericSTM32G474CB",
  "upload": {
    "maximum_ram_size": 131072,
    "maximum_size": 131072,
    "protocol": "stlink",
    "protocols": [
      "stlink",
      "jlink",
      "cmsis-dap",
      "blackmagic",
      "mbed"
    ]
  },
  "url": "https://www.st.com/en/evaluation-tools/nucleo-g474re.html",
  "vendor": "ST"
}

Is the flash unlocked? Try with https://www.st.com/en/development-tools/stm32cubeprog.html.

same issue here. Flash is unlocked .

Advanced Memory Usage is available via “PlatformIO Home > Project Inspect”
RAM: [=== ] 29.4% (used 38540 bytes from 131072 bytes)
Flash: [====== ] 56.6% (used 74196 bytes from 131072 bytes)
Configuring upload protocol…
AVAILABLE: blackmagic, cmsis-dap, jlink, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\CAN_Triple\firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
OpenOCD: Bug Reporting
debug_level: 1

hla_swd
adapter speed: 24000 kHz

[stm32g4x.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
[stm32g4x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
** Programming Started **
Warn : Adding extra erase range, 0x080123c8 … 0x080407ff
Error: write into gap from 0x08010000 to 0x0803ffff
Error: error writing to flash at address 0x08000000 at offset 0x00000000
embedded:startup.tcl:1516: Error: ** Programming Failed **
in procedure ‘program’
in procedure ‘program_error’ called at file “embedded:startup.tcl”, line 1581
at file “embedded:startup.tcl”, line 1516
*** [upload] Error 1

platformio.ini config

; PlatformIO Project Configuration File

;

; Build options: build flags, source filter

; Upload options: custom upload port, speed and extra flags

; Library options: dependencies, extra library storages

; Advanced options: extra scripting

;

; Please visit documentation for the other options and examples

; Redirecting...

[env:CAN_Triple]

platform = ststm32

platform_packages = framework-stm32cubeg4 @ 1.6.1

board = CAN_Triple

framework = stm32cube

;build_flags = -Wl,-u,_printf_float, -Wl,-u,_scanf_float

upload_protocol = stlink

debug_tool = stlink

debug_speed = 24000

monitor_speed = 7372800

;monitor_filters = log2file ; This exports message information into the Directory\logs\device-monitor-{YYMMDD-HHMMSS}.log

;monitor_filters = hexlify ; This sends message information to serial terminal in hexidecimal output

extra_scripts = pre:scripts/build_flags.py

UPdate : Flash Bank was set to Dual… setting to single bank with cubeprogrammer resolved this.

How exactly did you change the flash configuration of the device with cubeprogrammer? Is it with the option bytes?

What openocd_target did you set in your CAN_Triple.json file? Could you maybe switch to the single bank g4 config file there?

correct, options bytes > User Configuration > DBANK Unchecked…