summaryrefslogtreecommitdiff
path: root/tmk_core
diff options
context:
space:
mode:
authorStefan Kerkmann <karlk90@pm.me>2021-10-18 07:23:20 +0200
committerGitHub <noreply@github.com>2021-10-18 16:23:20 +1100
commite50867d52d9c255570630a23514842749b45e4c1 (patch)
treee448e47262668e21084fb7439a38b0322136bb89 /tmk_core
parent7e3ff206b8600265ea086040b397f1a600c4a7f3 (diff)
[Core] Add support for RISC-V builds and GD32VF103 MCU (#12508)
* Add support for RISC-V builds and GD32VF103 MCU * Add toolchain selection in chibios.mk based on the mcu selected in mcu_selection.mk * Reorder and added comments to chibios.mk to have a streamlined makefile * Add GD32VF103 mcu to possible targets for QMK. * Add STM32 compatibility for GD32VF103 MCU, this is hacky but more efficent then rewriting every driver. * Add GigaDevice DFU bootloader as flash target, please note that dfu-util of at least version 0.10 is needed. * Add analog driver compatibility * Add apa102 bitbang driver compatibility * Add ws2812 bitbang driver compatibility * Add eeprom in flash emulation compatibility * Allow faster re-builds with ccache * Add SiPeed Longan Nano to platform files * Add SiPeed Longan Nano Onekeys * Make quine compatible with other bootloaders * Support builds with picolibc * Add risc-v toolchain to arch and debian/ubuntu scripts
Diffstat (limited to 'tmk_core')
-rw-r--r--tmk_core/chibios.mk310
-rw-r--r--tmk_core/common/chibios/bootloader.c22
-rw-r--r--tmk_core/common/chibios/chibios_config.h19
-rw-r--r--tmk_core/common/chibios/eeprom_stm32_defs.h6
-rw-r--r--tmk_core/common/chibios/flash_stm32.c5
-rw-r--r--tmk_core/common/chibios/gd32v_compatibility.h120
-rw-r--r--tmk_core/common/chibios/syscall-fallbacks.c6
7 files changed, 377 insertions, 111 deletions
diff --git a/tmk_core/chibios.mk b/tmk_core/chibios.mk
index 18839710be..cae840fa4f 100644
--- a/tmk_core/chibios.mk
+++ b/tmk_core/chibios.mk
@@ -27,29 +27,62 @@ endif
OPT_OS = chibios
CHIBIOS = $(TOP_DIR)/lib/chibios
CHIBIOS_CONTRIB = $(TOP_DIR)/lib/chibios-contrib
-# Startup files. Try a few different locations, for compability with old versions and
-# for things hardware in the contrib repository
-STARTUP_MK = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
-ifeq ("$(wildcard $(STARTUP_MK))","")
- STARTUP_MK = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
- ifeq ("$(wildcard $(STARTUP_MK))","")
- STARTUP_MK = $(CHIBIOS_CONTRIB)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
- endif
+
+#
+# Startup, Port and Platform support selection
+##############################################################################
+
+ifeq ($(strip $(MCU)), risc-v)
+ # RISC-V Support
+ # As of 7.4.2021 there is only one supported RISC-V platform in Chibios-Contrib,
+ # therefore all required settings are hard-coded
+ STARTUP_MK = $(CHIBIOS_CONTRIB)/os/common/startup/RISCV-ECLIC/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
+ PORT_V = $(CHIBIOS_CONTRIB)/os/common/ports/RISCV-ECLIC/compilers/GCC/mk/port.mk
+ RULESPATH = $(CHIBIOS_CONTRIB)/os/common/startup/RISCV-ECLIC/compilers/GCC
+ PLATFORM_MK = $(CHIBIOS_CONTRIB)/os/hal/ports/GD/GD32VF103/platform.mk
+else
+ # ARM Support
+ # Startup files. Try a few different locations, for compability with old versions and
+ # for things hardware in the contrib repository
+ STARTUP_MK = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
+ ifeq ("$(wildcard $(STARTUP_MK))","")
+ STARTUP_MK = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
+ ifeq ("$(wildcard $(STARTUP_MK))","")
+ STARTUP_MK = $(CHIBIOS_CONTRIB)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
+ endif
+ endif
+
+ # Compability with old version
+ PORT_V = $(CHIBIOS)/os/rt/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
+ ifeq ("$(wildcard $(PORT_V))","")
+ PORT_V = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
+ endif
+
+ RULESPATH = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC
+ ifeq ("$(wildcard $(RULESPATH)/rules.mk)","")
+ RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC
+ endif
endif
-include $(STARTUP_MK)
-# HAL-OSAL files (optional).
-include $(CHIBIOS)/os/hal/hal.mk
ifeq ("$(PLATFORM_NAME)","")
- PLATFORM_NAME = platform
+ PLATFORM_NAME = platform
endif
-PLATFORM_MK = $(CHIBIOS)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
ifeq ("$(wildcard $(PLATFORM_MK))","")
-PLATFORM_MK = $(CHIBIOS_CONTRIB)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
+ PLATFORM_MK = $(CHIBIOS)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
+ ifeq ("$(wildcard $(PLATFORM_MK))","")
+ PLATFORM_MK = $(CHIBIOS_CONTRIB)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
+ endif
endif
+
+include $(STARTUP_MK)
+include $(PORT_V)
include $(PLATFORM_MK)
+#
+# Board support selection.
+##############################################################################
+
BOARD_MK :=
ifneq ("$(wildcard $(KEYBOARD_PATH_5)/boards/$(BOARD)/board.mk)","")
@@ -77,13 +110,19 @@ else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/board/board
endif
ifeq ("$(wildcard $(BOARD_MK))","")
- BOARD_MK = $(CHIBIOS)/os/hal/boards/$(BOARD)/board.mk
- ifeq ("$(wildcard $(BOARD_MK))","")
- BOARD_MK = $(CHIBIOS_CONTRIB)/os/hal/boards/$(BOARD)/board.mk
- endif
+ BOARD_MK = $(CHIBIOS)/os/hal/boards/$(BOARD)/board.mk
+ ifeq ("$(wildcard $(BOARD_MK))","")
+ BOARD_MK = $(CHIBIOS_CONTRIB)/os/hal/boards/$(BOARD)/board.mk
+ endif
endif
-# Bootloader address
+include $(BOARD_MK)
+
+#
+# Bootloader selection.
+##############################################################################
+
+# Set bootloader address if supplied.
ifdef STM32_BOOTLOADER_ADDRESS
OPT_DEFS += -DSTM32_BOOTLOADER_ADDRESS=$(STM32_BOOTLOADER_ADDRESS)
endif
@@ -113,6 +152,10 @@ else ifneq ("$(wildcard $(BOARD_PATH)/configs/bootloader_defs.h)","")
OPT_DEFS += -include $(BOARD_PATH)/configs/bootloader_defs.h
endif
+#
+# ChibiOS config selection.
+##############################################################################
+
# Work out the config file directories
ifneq ("$(wildcard $(KEYBOARD_PATH_5)/chconf.h)","")
CHCONFDIR = $(KEYBOARD_PATH_5)
@@ -130,6 +173,10 @@ else ifneq ("$(wildcard $(TOP_DIR)/platforms/boards/chibios/common/configs/chcon
CHCONFDIR = $(TOP_DIR)/platforms/chibios/boards/common/configs
endif
+#
+# HAL config selection.
+##############################################################################
+
ifneq ("$(wildcard $(KEYBOARD_PATH_5)/halconf.h)","")
HALCONFDIR = $(KEYBOARD_PATH_5)
else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/halconf.h)","")
@@ -146,40 +193,10 @@ else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/common/configs/halco
HALCONFDIR = $(TOP_DIR)/platforms/chibios/boards/common/configs
endif
-# HAL-OSAL files (optional).
-include $(CHIBIOS)/os/hal/hal.mk
-
-ifeq ("$(PLATFORM_NAME)","")
- PLATFORM_NAME = platform
-endif
-
-PLATFORM_MK = $(CHIBIOS)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
-ifeq ("$(wildcard $(PLATFORM_MK))","")
-PLATFORM_MK = $(CHIBIOS_CONTRIB)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
-endif
-include $(PLATFORM_MK)
-
-
-include $(BOARD_MK)
--include $(CHIBIOS)/os/hal/osal/rt/osal.mk # ChibiOS <= 19.x
--include $(CHIBIOS)/os/hal/osal/rt-nil/osal.mk # ChibiOS >= 20.x
-# RTOS files (optional).
-include $(CHIBIOS)/os/rt/rt.mk
-# Compability with old version
-PORT_V = $(CHIBIOS)/os/rt/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
-ifeq ("$(wildcard $(PORT_V))","")
-PORT_V = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
-endif
-include $(PORT_V)
-# Other files (optional).
-include $(CHIBIOS)/os/hal/lib/streams/streams.mk
-
-RULESPATH = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC
-ifeq ("$(wildcard $(RULESPATH)/rules.mk)","")
-RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC
-endif
+#
+# Linker script selection.
+##############################################################################
-# Define linker script file here
ifneq ("$(wildcard $(KEYBOARD_PATH_5)/ld/$(MCU_LDSCRIPT).ld)","")
LDSCRIPT = $(KEYBOARD_PATH_5)/ld/$(MCU_LDSCRIPT).ld
else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/ld/$(MCU_LDSCRIPT).ld)","")
@@ -202,17 +219,30 @@ else
LDSCRIPT = $(STARTUPLD)/$(MCU_LDSCRIPT).ld
endif
+#
+# Include ChibiOS makefiles.
+##############################################################################
+
+# HAL-OSAL files (optional).
+include $(CHIBIOS)/os/hal/hal.mk
+-include $(CHIBIOS)/os/hal/osal/rt/osal.mk # ChibiOS <= 19.x
+-include $(CHIBIOS)/os/hal/osal/rt-nil/osal.mk # ChibiOS >= 20.x
+# RTOS files (optional).
+include $(CHIBIOS)/os/rt/rt.mk
+# Other files (optional).
+include $(CHIBIOS)/os/hal/lib/streams/streams.mk
+
CHIBISRC = $(STARTUPSRC) \
- $(KERNSRC) \
- $(PORTSRC) \
- $(OSALSRC) \
- $(HALSRC) \
- $(PLATFORMSRC) \
- $(BOARDSRC) \
- $(STREAMSSRC) \
- $(CHIBIOS)/os/various/syscalls.c \
- $(PLATFORM_COMMON_DIR)/syscall-fallbacks.c \
- $(PLATFORM_COMMON_DIR)/wait.c
+ $(KERNSRC) \
+ $(PORTSRC) \
+ $(OSALSRC) \
+ $(HALSRC) \
+ $(PLATFORMSRC) \
+ $(BOARDSRC) \
+ $(STREAMSSRC) \
+ $(CHIBIOS)/os/various/syscalls.c \
+ $(PLATFORM_COMMON_DIR)/syscall-fallbacks.c \
+ $(PLATFORM_COMMON_DIR)/wait.c
# Ensure the ASM files are not subjected to LTO -- it'll strip out interrupt handlers otherwise.
QUANTUM_LIB_SRC += $(STARTUPASM) $(PORTASM) $(OSALASM) $(PLATFORMASM)
@@ -247,9 +277,9 @@ else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs/hal
endif
ifeq ($(strip $(USE_CHIBIOS_CONTRIB)),yes)
- include $(CHIBIOS_CONTRIB)/os/hal/hal.mk
- CHIBISRC += $(PLATFORMSRC_CONTRIB) $(HALSRC_CONTRIB)
- EXTRAINCDIRS += $(PLATFORMINC_CONTRIB) $(HALINC_CONTRIB) $(CHIBIOS_CONTRIB)/os/various
+ include $(CHIBIOS_CONTRIB)/os/hal/hal.mk
+ CHIBISRC += $(PLATFORMSRC_CONTRIB) $(HALSRC_CONTRIB)
+ EXTRAINCDIRS += $(PLATFORMINC_CONTRIB) $(HALINC_CONTRIB) $(CHIBIOS_CONTRIB)/os/various
endif
#
@@ -267,61 +297,123 @@ ifneq ("$(wildcard $(BOARD_PATH)/configs/post_config.h)","")
endif
##############################################################################
-# Compiler settings
+# Compiler and Linker configuration
#
-CC = arm-none-eabi-gcc
-OBJCOPY = arm-none-eabi-objcopy
-OBJDUMP = arm-none-eabi-objdump
-SIZE = arm-none-eabi-size
-AR = arm-none-eabi-ar
-NM = arm-none-eabi-nm
-HEX = $(OBJCOPY) -O $(FORMAT)
-EEP =
-BIN = $(OBJCOPY) -O binary
-
-THUMBFLAGS = -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb -DTHUMB
-
-COMPILEFLAGS += -fomit-frame-pointer
-COMPILEFLAGS += -falign-functions=16
-COMPILEFLAGS += -ffunction-sections
-COMPILEFLAGS += -fdata-sections
-COMPILEFLAGS += -fno-common
-COMPILEFLAGS += -fshort-wchar
-COMPILEFLAGS += $(THUMBFLAGS)
-
-# FPU options default (Cortex-M4 and Cortex-M7 single precision).
-USE_FPU_OPT ?= -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant
-
-# FPU-related options
-USE_FPU ?= no
-ifneq ($(USE_FPU),no)
- COMPILEFLAGS += $(USE_FPU_OPT)
- OPT_DEFS += -DCORTEX_USE_FPU=TRUE
+
+# Use defined stack sizes of the main thread in linker scripts
+LDSYMBOLS =--defsym=__process_stack_size__=$(USE_PROCESS_STACKSIZE),--defsym=__main_stack_size__=$(USE_EXCEPTIONS_STACKSIZE)
+
+# Shared Compiler flags for all toolchains
+SHARED_CFLAGS = -fomit-frame-pointer \
+ -ffunction-sections \
+ -fdata-sections \
+ -fno-common \
+ -fshort-wchar
+
+# Shared Linker flags for all toolchains
+SHARED_LDFLAGS = -T $(LDSCRIPT) \
+ -Wl,$(LDSYMBOLS) \
+ -Wl,--gc-sections \
+ -nostartfiles
+
+ifeq ($(strip $(MCU)), risc-v)
+ # RISC-V toolchain specific configuration
+ # Find suitable GCC compiler
+ ifeq ($(strip $(TOOLCHAIN)),)
+ ifneq ($(shell which riscv32-unknown-elf-gcc 2>/dev/null),)
+ TOOLCHAIN = riscv32-unknown-elf-
+ else
+ ifneq ($(shell which riscv64-unknown-elf-gcc 2>/dev/null),)
+ TOOLCHAIN = riscv64-unknown-elf-
+ else
+ $(error "No RISC-V toolchain found. Can't find riscv32-unknown-elf-gcc or riscv64-unknown-elf-gcc found in your systems PATH variable. Please install a valid toolchain and make it accessible!")
+ endif
+ endif
+ endif
+
+ # Default to compiling with picolibc for RISC-V targets if available,
+ # which is available by default on current (bullseye) debian based systems.
+ ifeq ($(shell $(TOOLCHAIN)gcc --specs=picolibc.specs -E - 2>/dev/null >/dev/null </dev/null ; echo $$?),0)
+ # Toolchain specific Compiler flags
+ # Note that we still link with our own linker script
+ # by providing it via the -T flag above.
+ TOOLCHAIN_CFLAGS = --specs=picolibc.specs
+
+ # Tell QMK that we are compiling with picolibc.
+ OPT_DEFS += -DUSE_PICOLIBC
+ endif
+
+ # MCU architecture flags
+ MCUFLAGS = -march=$(MCU_ARCH) \
+ -mabi=$(MCU_ABI) \
+ -mcmodel=$(MCU_CMODEL) \
+ -mstrict-align
else
- OPT_DEFS += -DCORTEX_USE_FPU=FALSE
+ # ARM toolchain specific configuration
+ TOOLCHAIN ?= arm-none-eabi-
+
+ # Toolchain specific Linker flags
+ TOOLCHAIN_LDFLAGS = -Wl,--no-wchar-size-warning \
+ --specs=nano.specs
+
+ # MCU architecture flags
+ MCUFLAGS = -mcpu=$(MCU) \
+ -mthumb -DTHUMB_PRESENT \
+ -mno-thumb-interwork -DTHUMB_NO_INTERWORKING \
+ -mno-unaligned-access
+
+ # Some ARM cores like the M4 and M7 have floating point units which can be enabled
+ USE_FPU ?= no
+
+ ifneq ($(USE_FPU),no)
+ OPT_DEFS += -DCORTEX_USE_FPU=TRUE
+
+ # Default is single precision floats
+ USE_FPU_OPT ?= -mfloat-abi=hard \
+ -mfpu=fpv4-sp-d16 \
+ -fsingle-precision-constant
+
+ MCUFLAGS += $(USE_FPU_OPT)
+ else
+ OPT_DEFS += -DCORTEX_USE_FPU=FALSE
+ endif
endif
-CFLAGS += $(COMPILEFLAGS)
+# Assembler flags
+ASFLAGS += $(SHARED_ASFLAGS) $(TOOLCHAIN_ASFLAGS)
-ASFLAGS += $(THUMBFLAGS)
+# C Compiler flags
+CFLAGS += $(SHARED_CFLAGS) $(TOOLCHAIN_CFLAGS)
-CXXFLAGS += $(COMPILEFLAGS)
-CXXFLAGS += -fno-rtti
+# C++ Compiler flags
+CXXFLAGS += $(CFLAGS) $(SHARED_CXXFLAGS) $(TOOLCHAIN_CXXFLAGS) -fno-rtti
-LDFLAGS +=-Wl,--gc-sections
-LDFLAGS +=-Wl,--no-wchar-size-warning
-LDFLAGS += -mno-thumb-interwork -mthumb
-LDSYMBOLS =,--defsym=__process_stack_size__=$(USE_PROCESS_STACKSIZE)
-LDSYMBOLS :=$(LDSYMBOLS),--defsym=__main_stack_size__=$(USE_EXCEPTIONS_STACKSIZE)
-LDFLAGS += -Wl,--script=$(LDSCRIPT)$(LDSYMBOLS)
-LDFLAGS += --specs=nano.specs
+# Linker flags
+LDFLAGS += $(SHARED_LDFLAGS) $(TOOLCHAIN_LDFLAGS) $(MCUFLAGS)
+# Tell QMK that we are hosting it on ChibiOS.
OPT_DEFS += -DPROTOCOL_CHIBIOS
-# Workaround to stop ChibiOS from complaining about new GCC -- it's been fixed for 7/8/9 already
-OPT_DEFS += -DPORT_IGNORE_GCC_VERSION_CHECK=1
+# Speed up recompilations by opt-in usage of ccache
+USE_CCACHE ?= no
+ifneq ($(USE_CCACHE),no)
+ CC_PREFIX ?= ccache
+endif
+
+# Construct GCC toolchain
+CC = $(CC_PREFIX) $(TOOLCHAIN)gcc
+OBJCOPY = $(TOOLCHAIN)objcopy
+OBJDUMP = $(TOOLCHAIN)objdump
+SIZE = $(TOOLCHAIN)size
+AR = $(TOOLCHAIN)ar
+NM = $(TOOLCHAIN)nm
+HEX = $(OBJCOPY) -O $(FORMAT)
+EEP =
+BIN = $(OBJCOPY) -O binary
-MCUFLAGS = -mcpu=$(MCU)
+##############################################################################
+# Make targets
+#
DEBUG = gdb
diff --git a/tmk_core/common/chibios/bootloader.c b/tmk_core/common/chibios/bootloader.c
index f9514ee5f3..5cadadeeeb 100644
--- a/tmk_core/common/chibios/bootloader.c
+++ b/tmk_core/common/chibios/bootloader.c
@@ -95,6 +95,28 @@ void enter_bootloader_mode_if_requested(void) {
}
}
+#elif defined(GD32VF103)
+
+# define DBGMCU_KEY_UNLOCK 0x4B5A6978
+# define DBGMCU_CMD_RESET 0x1
+
+__IO uint32_t *DBGMCU_KEY = (uint32_t *)DBGMCU_BASE + 0x0CU;
+__IO uint32_t *DBGMCU_CMD = (uint32_t *)DBGMCU_BASE + 0x08U;
+
+__attribute__((weak)) void bootloader_jump(void) {
+ /* The MTIMER unit of the GD32VF103 doesn't have the MSFRST
+ * register to generate a software reset request.
+ * BUT instead two undocumented registers in the debug peripheral
+ * that allow issueing a software reset. WHO would need the MSFRST
+ * register anyway? Source:
+ * https://github.com/esmil/gd32vf103inator/blob/master/include/gd32vf103/dbg.h */
+ *DBGMCU_KEY = DBGMCU_KEY_UNLOCK;
+ *DBGMCU_CMD = DBGMCU_CMD_RESET;
+}
+
+void enter_bootloader_mode_if_requested(void) { /* Jumping to bootloader is not possible from user code. */
+}
+
#elif defined(KL2x) || defined(K20x) || defined(MK66F18) || defined(MIMXRT1062) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS
/* Kinetis */
diff --git a/tmk_core/common/chibios/chibios_config.h b/tmk_core/common/chibios/chibios_config.h
index f065948028..ad2f808a95 100644
--- a/tmk_core/common/chibios/chibios_config.h
+++ b/tmk_core/common/chibios/chibios_config.h
@@ -39,6 +39,25 @@
# endif
#endif
+// GD32 compatibility
+#if defined(MCU_GD32V)
+# define CPU_CLOCK GD32_SYSCLK
+
+# if defined(GD32VF103)
+# define USE_GPIOV1
+# define USE_I2CV1
+# define PAL_MODE_ALTERNATE_OPENDRAIN PAL_MODE_GD32_ALTERNATE_OPENDRAIN
+# define PAL_MODE_ALTERNATE_PUSHPULL PAL_MODE_GD32_ALTERNATE_PUSHPULL
+# endif
+#endif
+
+#if defined(GD32VF103)
+/* This chip has the same API as STM32F103, but uses different names for literally the same thing.
+ * As of 4.7.2021 QMK is tailored to use STM32 defines/names, for compatibility sake
+ * we just redefine the GD32 names. */
+# include "gd32v_compatibility.h"
+#endif
+
// teensy compatibility
#if defined(MCU_KINETIS)
# define CPU_CLOCK KINETIS_SYSCLK_FREQUENCY
diff --git a/tmk_core/common/chibios/eeprom_stm32_defs.h b/tmk_core/common/chibios/eeprom_stm32_defs.h
index 3499796264..66904f247f 100644
--- a/tmk_core/common/chibios/eeprom_stm32_defs.h
+++ b/tmk_core/common/chibios/eeprom_stm32_defs.h
@@ -18,7 +18,7 @@
#include <hal.h>
#if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT)
-# if defined(STM32F103xB) || defined(STM32F042x6)
+# if defined(STM32F103xB) || defined(STM32F042x6) || defined(GD32VF103C8) || defined(GD32VF103CB)
# ifndef FEE_PAGE_SIZE
# define FEE_PAGE_SIZE 0x400 // Page size = 1KByte
# endif
@@ -45,7 +45,9 @@
#if !defined(FEE_MCU_FLASH_SIZE)
# if defined(STM32F042x6)
# define FEE_MCU_FLASH_SIZE 32 // Size in Kb
-# elif defined(STM32F103xB) || defined(STM32F072xB) || defined(STM32F070xB)
+# elif defined(GD32VF103C8)
+# define FEE_MCU_FLASH_SIZE 64 // Size in Kb
+# elif defined(STM32F103xB) || defined(STM32F072xB) || defined(STM32F070xB) || defined(GD32VF103CB)
# define FEE_MCU_FLASH_SIZE 128 // Size in Kb
# elif defined(STM32F303xC) || defined(STM32F401xC)
# define FEE_MCU_FLASH_SIZE 256 // Size in Kb
diff --git a/tmk_core/common/chibios/flash_stm32.c b/tmk_core/common/chibios/flash_stm32.c
index 35fb0eebdb..72c41b8b78 100644
--- a/tmk_core/common/chibios/flash_stm32.c
+++ b/tmk_core/common/chibios/flash_stm32.c
@@ -23,6 +23,11 @@
# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
#endif
+#if defined(MCU_GD32V)
+/* GigaDevice GD32VF103 is a STM32F103 clone at heart. */
+# include "gd32v_compatibility.h"
+#endif
+
#if defined(STM32F4XX)
# define FLASH_SR_PGERR (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR)
diff --git a/tmk_core/common/chibios/gd32v_compatibility.h b/tmk_core/common/chibios/gd32v_compatibility.h
new file mode 100644
index 0000000000..f4dcfd8c55
--- /dev/null
+++ b/tmk_core/common/chibios/gd32v_compatibility.h
@@ -0,0 +1,120 @@
+/* Copyright 2021 QMK
+ *
+ * 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 2 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/>.
+ */
+
+#pragma once
+
+/* GD32VF103 has the same API as STM32F103, but uses different names for literally the same thing.
+ * As of 23.7.2021 QMK is tailored to use STM32 defines/names, for compatibility sake
+ * we just redefine the GD32 names. */
+
+/* Close your eyes kids. */
+#define MCU_STM32
+
+/* AFIO redefines */
+#define MAPR PCF0
+#define AFIO_MAPR_USART1_REMAP AFIO_PCF0_USART0_REMAP
+#define AFIO_MAPR_USART2_REMAP AFIO_PCF0_USART1_REMAP
+#define AFIO_MAPR_USART3_REMAP_PARTIALREMAP AFIO_PCF0_USART2_REMAP_PARTIALREMAP
+#define AFIO_MAPR_USART3_REMAP_FULLREMAP AFIO_PCF0_USART2_REMAP_FULLREMAP
+
+/* DMA redefines. */
+#define STM32_DMA_STREAM(stream) GD32_DMA_STREAM(stream)
+#define STM32_DMA_STREAM_ID(peripheral, channel) GD32_DMA_STREAM_ID(peripheral - 1, channel - 1)
+#define STM32_DMA_CR_DIR_M2P GD32_DMA_CTL_DIR_M2P
+#define STM32_DMA_CR_PSIZE_WORD GD32_DMA_CTL_PWIDTH_WORD
+#define STM32_DMA_CR_MSIZE_WORD GD32_DMA_CTL_MWIDTH_WORD
+#define STM32_DMA_CR_MINC GD32_DMA_CTL_MNAGA
+#define STM32_DMA_CR_CIRC GD32_DMA_CTL_CMEN
+#define STM32_DMA_CR_PL GD32_DMA_CTL_PRIO
+#define STM32_DMA_CR_CHSEL GD32_DMA_CTL_CHSEL
+#define cr1 ctl0
+#define cr2 ctl1
+#define cr3 ctl2
+#define dier dmainten
+
+/* ADC redefines */
+#if HAL_USE_ADC
+# define STM32_ADC_USE_ADC1 GD32_ADC_USE_ADC0
+
+# define smpr1 sampt0
+# define smpr2 sampt1
+# define sqr1 rsq0
+# define sqr2 rsq1
+# define sqr3 rsq2
+
+# define ADC_SMPR2_SMP_AN0 ADC_SAMPT1_SMP_SPT0
+# define ADC_SMPR2_SMP_AN1 ADC_SAMPT1_SMP_SPT1
+# define ADC_SMPR2_SMP_AN2 ADC_SAMPT1_SMP_SPT2
+# define ADC_SMPR2_SMP_AN3 ADC_SAMPT1_SMP_SPT3
+# define ADC_SMPR2_SMP_AN4 ADC_SAMPT1_SMP_SPT4
+# define ADC_SMPR2_SMP_AN5 ADC_SAMPT1_SMP_SPT5
+# define ADC_SMPR2_SMP_AN6 ADC_SAMPT1_SMP_SPT6
+# define ADC_SMPR2_SMP_AN7 ADC_SAMPT1_SMP_SPT7
+# define ADC_SMPR2_SMP_AN8 ADC_SAMPT1_SMP_SPT8
+# define ADC_SMPR2_SMP_AN9 ADC_SAMPT1_SMP_SPT9
+
+# define ADC_SMPR1_SMP_AN10 ADC_SAMPT0_SMP_SPT10
+# define ADC_SMPR1_SMP_AN11 ADC_SAMPT0_SMP_SPT11
+# define ADC_SMPR1_SMP_AN12 ADC_SAMPT0_SMP_SPT12
+# define ADC_SMPR1_SMP_AN13 ADC_SAMPT0_SMP_SPT13
+# define ADC_SMPR1_SMP_AN14 ADC_SAMPT0_SMP_SPT14
+# define ADC_SMPR1_SMP_AN15 ADC_SAMPT0_SMP_SPT15
+
+# define ADC_SQR3_SQ1_N ADC_RSQ2_RSQ1_N
+#endif
+
+/* FLASH redefines */
+#if defined(EEPROM_ENABLE)
+# define SR STAT
+# define FLASH_SR_BSY FLASH_STAT_BUSY
+# define FLASH_SR_PGERR FLASH_STAT_PGERR
+# define FLASH_SR_EOP FLASH_STAT_ENDF
+# define FLASH_SR_WRPRTERR FLASH_STAT_WPERR
+# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
+# define FLASH_OBR_OPTERR FLASH_OBSTAT_OBERR
+# define AR ADDR
+# define CR CTL
+# define FLASH_CR_PER FLASH_CTL_PER
+# define FLASH_CR_STRT FLASH_CTL_START
+# define FLASH_CR_LOCK FLASH_CTL_LK
+# define FLASH_CR_PG FLASH_CTL_PG
+# define KEYR KEY
+#endif
+
+/* Serial USART redefines. */
+#if HAL_USE_SERIAL
+# if !defined(SERIAL_USART_CR1)
+# define SERIAL_USART_CR1 (USART_CTL0_PCEN | USART_CTL0_PM | USART_CTL0_WL) // parity enable, odd parity, 9 bit length
+# endif
+# if !defined(SERIAL_USART_CR2)
+# define SERIAL_USART_CR2 (USART_CTL1_STB_1) // 2 stop bits
+# endif
+# if !defined(SERIAL_USART_CR3)
+# define SERIAL_USART_CR3 0x0
+# endif
+# define USART_CR3_HDSEL USART_CTL2_HDEN
+# define CCR CHCV
+#endif
+
+/* SPI redefines. */
+#if HAL_USE_SPI
+# define SPI_CR1_LSBFIRST SPI_CTL0_LF
+# define SPI_CR1_CPHA SPI_CTL0_CKPH
+# define SPI_CR1_CPOL SPI_CTL0_CKPL
+# define SPI_CR1_BR_0 SPI_CTL0_PSC_0
+# define SPI_CR1_BR_1 SPI_CTL0_PSC_1
+# define SPI_CR1_BR_2 SPI_CTL0_PSC_2
+#endif
diff --git a/tmk_core/common/chibios/syscall-fallbacks.c b/tmk_core/common/chibios/syscall-fallbacks.c
index 739017ae1d..4569879c7c 100644
--- a/tmk_core/common/chibios/syscall-fallbacks.c
+++ b/tmk_core/common/chibios/syscall-fallbacks.c
@@ -18,6 +18,12 @@
#include <sys/stat.h>
#include <sys/types.h>
+/* To compile the ChibiOS syscall stubs with picolibc
+ * the _reent struct has to be defined. */
+#if defined(USE_PICOLIBC)
+struct _reent;
+#endif
+
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
__attribute__((weak, used)) int _open_r(struct _reent *r, const char *path, int flag, int m) {