Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New platform: WeActStudio.BluePill-Plus-GD32 #2071

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cross-file/bluepillplus.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This a cross-file for the bluepillplus probe, providing sane default options for it.

[binaries]
c = 'arm-none-eabi-gcc'
cpp = 'arm-none-eabi-g++'
ld = 'arm-none-eabi-gcc'
ar = 'arm-none-eabi-ar'
nm = 'arm-none-eabi-nm'
strip = 'arm-none-eabi-strip'
objcopy = 'arm-none-eabi-objcopy'
objdump = 'arm-none-eabi-objdump'
size = 'arm-none-eabi-size'

[host_machine]
system = 'bare-metal'
cpu_family = 'arm'
cpu = 'arm'
endian = 'little'

[project options]
probe = 'bluepillplus'
targets = 'cortexm,lpc,nrf,nxp,renesas,rp,sam,stm,ti'
rtt_support = false
bmd_bootloader = true
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ option(
'blackpill-f401ce',
'blackpill-f411ce',
'bluepill',
'bluepillplus',
'ctxlink',
'f072',
'f3',
Expand Down
98 changes: 98 additions & 0 deletions src/platforms/bluepillplus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Black Magic Debug Probe Firmware for WeAct Studio BluePill-Plus boards

This platform allows using various BluePill-Plus boards as a Black Magic Probe.

[BluePill-Plus](https://github.com/WeActStudio/BluePill-Plus) is based on STM32F103CB or GD32F303CC;

[BluePill-Plus-GD32](https://github.com/WeActStudio/WeActStudio.BluePill-Plus-GD32) is based on GD32F103CB. SoC runs at up to 108 MHz, but because of USB FS device clock restrictions 96 MHz is used.

[BluePill-Plus-APM32](https://github.com/WeActStudio/WeActStudio.BluePill-Plus-APM32) is based on APM32F103CB (untested).

[BluePill-Plus-CH32](https://github.com/WeActStudio/WeActStudio.BluePill-Plus-CH32) is based on CH32F103C8 (CM3) or CH32V103C8 or CH32V203C8 (RISC-V) and unsupported (flash too small, non-Cortex-M).

| SoC | Core |Clock, MHz|SRAM, KiB|Flash, KiB|
|-------------|------------|----------|---------|----------|
| STM32F103CB | Cortex-M3 | 72 | 20 | 128 (2WS)|
| GD32F103CB | Cortex-M3 | 96 | 20 | 128 (0WS)|
| GD32F303CC | Cortex-M4F | 120 | 48 | 256 (0WS)|
| APM32F103CB | Cortex-M3 | 96 | 20 | 128 (0WS)|

## Pinout

| Function | Pinout | Cluster |
| --------------- | ------ | --------- |
| TDI | PB6 | JTAG/SWD |
| TDO/TRACESWO | PB7 | JTAG/SWD |
| TCK/SWCLK | PB8 | JTAG/SWD |
| TMS/SWDIO | PB9 | JTAG/SWD |
| nRST | PA5 | JTAG/SWD |
| TRST (optional) | PA6 | JTAG/SWD |
| UART TX | PA2 | USB USART |
| UART RX | PA3 | USB USART |
| LED idle run | PB2 | LED |
| LED error | PB10 | LED |
| LED UART | PB11 | LED |
| User button KEY | PA0 | |

USB Full-Speed PHY is on PA11/PA12 with a fixed 1.5 kOhm external pull-up resistor.

SWJ-DP is on PA13/14/15/PB3/4, those pins are kept in default function (Inception debug possible).

TODO: add support for onboard SPI flash on PA4/5/6/7 (SPI1) and PB12/13/14/15 (SPI2).

## Instructions for build system

0. Clone the repo and libopencm3 submodule, install toolchains, meson, etc.

```sh
git clone https://github.com/blackmagic-debug/blackmagic.git --depth=2000
cd blackmagic
```

1. Create a build configuration for specific platform (WeActStudio BluePill-Plus) with specific options

```sh
meson setup build --cross-file=cross-file/bluepillplus.ini
```

2. Compile the firmware and bootloader

```sh
ninja -C build
ninja -C build boot-bin
```

3. Flash the BMD bootloader into first 8 KiB of memory if it's empty, using USART1, or only flash the BMD Firmware at an offset, using USB DFU.

For other firmware upgrade instructions see the [Firmware Upgrade](https://black-magic.org/upgrade.html) section.

### Using the BMD Bootloader
If you flashed the bootloader using the above instructions, it may be invoked using the following:
- Force the F1 into BMD bootloader mode:
- Hold down KEY
- Tap NRST
- Release KEY

The BMD bootloader also recognizes RCC Reset reason when nRST is pressed, and `dfu-util --detach` (implemented via magic flags in main SRAM).

Once activated the BMD bootloader may be used to flash the device using 'bmputil,' available [here](https://github.com/blackmagic-debug/bmputil).

## Performance

System clock is set to 72 MHz, expecting a 8 MHz HSE crystal. On GD32 chips, 96 and 120 MHz are also an option.

Because of low expectations to signal integrity or quality wiring, default SWD frequency is reduced to 2 MHz, but in practice 6 MHz is possible, JTAG is slower.

Aux serial runs via USART2 DMA on APB1 (Pclk = 36 MHz) at 549..2250000 baud.

TraceSWO Async capture runs via USART1 DMA on APB2 (Pclk = 72 MHz) at 1098..4500000 baud.

SPI ports are set to Pclk/8 each (use with `bmpflash`). As SPI1 pins may conflict with certain functions, and platform code does not bother restoring them, please soft-reset the probe when done with SPI operations.

## More details

* ST MaskROM is the read-only System Memory bootloader which starts up per BOOT0-triggered SYSCFG mem-remap. It talks USART AN2606 so you can use stm32flash etc. However, it does not implement USB DFU, which is why a BMD bootloader is provided here.
* BMD bootloader is the port of BMP bootloader which
a) has a fixed compatible PLL config;
b) understands buttons, drives LED, does not touch other GPIOs, talks USB DfuSe, ~~has MS OS descriptors for automatic driver installation on Windows~~, uses same libopencm3 code so you can verify hardware config via a smaller binary;
c) erases and writes to internal Flash ~2x faster than MaskROM USART at 460800 baud (verify?) without an external USB-UART bridge dongle.
29 changes: 29 additions & 0 deletions src/platforms/bluepillplus/gd32f303cc.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2025 1BitSquared <[email protected]>
* Written by ALTracer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* Define memory regions. */
MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 48K
}

/* Include the platform common linker script. */
INCLUDE ../common/blackmagic.ld
85 changes: 85 additions & 0 deletions src/platforms/bluepillplus/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# This file is part of the Black Magic Debug project.
#
# Copyright (C) 2024 1BitSquared <[email protected]>
# Written by ALTracer <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

probe_bluepillplus_includes = include_directories('.')

probe_bluepillplus_sources = files('platform.c')

probe_bluepillplus_dfu_sources = files('usbdfu.c')

probe_bluepillplus_args = [
'-DDFU_SERIAL_LENGTH=9',
'-DBLACKMAGIC',
]

trace_protocol = get_option('trace_protocol')
probe_bluepillplus_args += [f'-DSWO_ENCODING=@trace_protocol@']
probe_bluepillplus_dependencies = [platform_stm32_swo]
if trace_protocol in ['1', '3']
probe_bluepillplus_dependencies += platform_stm32_swo_manchester
endif
if trace_protocol in ['2', '3']
probe_bluepillplus_dependencies += platform_stm32_swo_uart
endif

probe_bluepillplus_common_link_args = [
'-L@0@'.format(meson.current_source_dir()),
'-T@0@'.format('stm32f103cb.ld'),
]

probe_bluepillplus_link_args = [
'-Wl,-Ttext=0x8002000',
]

probe_host = declare_dependency(
include_directories: probe_bluepillplus_includes,
sources: probe_bluepillplus_sources,
compile_args: probe_bluepillplus_args,
link_args: probe_bluepillplus_common_link_args + probe_bluepillplus_link_args,
dependencies: [platform_common, platform_stm32f1, probe_bluepillplus_dependencies],
)

probe_bootloader = declare_dependency(
include_directories: [platform_common_includes, probe_bluepillplus_includes],
sources: probe_bluepillplus_dfu_sources,
compile_args: probe_bluepillplus_args,
link_args: probe_bluepillplus_common_link_args,
dependencies: platform_stm32f1_dfu,
)

summary(
{
'Name': 'Black Magic Probe (BluePill-Plus)',
'Platform': 'STM32F1',
'Bootloader': 'Black Magic Debug Bootloader',
'Load Address': '0x8002000',
},
section: 'Probe',
)
Loading
Loading