aboutsummaryrefslogtreecommitdiff
path: root/tmk_core
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-11-19 18:41:02 +0000
committerGitHub <noreply@github.com>2021-11-19 10:41:02 -0800
commit2728603fe6d73e805a539d337fd01051c46ca806 (patch)
tree5c83ffc7efa112da870bd5d8502a9d91d4792f35 /tmk_core
parent43b9e23bae12916d5161f03700c9bfe46737324b (diff)
downloadqmk_firmware-2728603fe6d73e805a539d337fd01051c46ca806.tar.gz
qmk_firmware-2728603fe6d73e805a539d337fd01051c46ca806.zip
Move tmk_core/common/<plat> (#13918)
Diffstat (limited to 'tmk_core')
-rw-r--r--tmk_core/arm_atsam.mk67
-rw-r--r--tmk_core/avr.mk179
-rw-r--r--tmk_core/chibios.mk436
-rw-r--r--tmk_core/common.mk16
-rw-r--r--tmk_core/common/arm_atsam/_timer.h19
-rw-r--r--tmk_core/common/arm_atsam/_wait.h22
-rw-r--r--tmk_core/common/arm_atsam/atomic_util.h37
-rw-r--r--tmk_core/common/arm_atsam/bootloader.c57
-rw-r--r--tmk_core/common/arm_atsam/eeprom.c184
-rw-r--r--tmk_core/common/arm_atsam/gpio.h77
-rw-r--r--tmk_core/common/arm_atsam/pin_defs.h84
-rw-r--r--tmk_core/common/arm_atsam/platform.c21
-rw-r--r--tmk_core/common/arm_atsam/platform_deps.h18
-rw-r--r--tmk_core/common/arm_atsam/suspend.c77
-rw-r--r--tmk_core/common/arm_atsam/timer.c19
-rw-r--r--tmk_core/common/atomic_util.h32
-rw-r--r--tmk_core/common/avr/_print.h33
-rw-r--r--tmk_core/common/avr/_timer.h19
-rw-r--r--tmk_core/common/avr/_wait.h49
-rw-r--r--tmk_core/common/avr/atomic_util.h22
-rw-r--r--tmk_core/common/avr/bootloader.c293
-rw-r--r--tmk_core/common/avr/bootloader_size.c21
-rw-r--r--tmk_core/common/avr/gpio.h49
-rw-r--r--tmk_core/common/avr/pin_defs.h128
-rw-r--r--tmk_core/common/avr/platform.c21
-rw-r--r--tmk_core/common/avr/platform_deps.h20
-rw-r--r--tmk_core/common/avr/printf.c20
-rw-r--r--tmk_core/common/avr/printf.mk2
-rw-r--r--tmk_core/common/avr/sleep_led.c124
-rw-r--r--tmk_core/common/avr/suspend.c152
-rw-r--r--tmk_core/common/avr/timer.c133
-rw-r--r--tmk_core/common/avr/timer_avr.h39
-rw-r--r--tmk_core/common/avr/xprintf.S498
-rw-r--r--tmk_core/common/avr/xprintf.h103
-rw-r--r--tmk_core/common/bootloader.h21
-rw-r--r--tmk_core/common/chibios/_timer.h19
-rw-r--r--tmk_core/common/chibios/_wait.c89
-rw-r--r--tmk_core/common/chibios/_wait.h60
-rw-r--r--tmk_core/common/chibios/atomic_util.h37
-rw-r--r--tmk_core/common/chibios/bootloader.c145
-rw-r--r--tmk_core/common/chibios/chibios_config.h78
-rw-r--r--tmk_core/common/chibios/eeprom_stm32.c687
-rw-r--r--tmk_core/common/chibios/eeprom_stm32.h33
-rw-r--r--tmk_core/common/chibios/eeprom_stm32_defs.h74
-rw-r--r--tmk_core/common/chibios/eeprom_teensy.c795
-rw-r--r--tmk_core/common/chibios/flash_stm32.c208
-rw-r--r--tmk_core/common/chibios/flash_stm32.h44
-rw-r--r--tmk_core/common/chibios/gd32v_compatibility.h120
-rw-r--r--tmk_core/common/chibios/gpio.h50
-rw-r--r--tmk_core/common/chibios/pin_defs.h323
-rw-r--r--tmk_core/common/chibios/platform.c22
-rw-r--r--tmk_core/common/chibios/platform_deps.h19
-rw-r--r--tmk_core/common/chibios/sleep_led.c192
-rw-r--r--tmk_core/common/chibios/suspend.c92
-rw-r--r--tmk_core/common/chibios/syscall-fallbacks.c110
-rw-r--r--tmk_core/common/chibios/timer.c47
-rw-r--r--tmk_core/common/chibios/wait.c41
-rw-r--r--tmk_core/common/eeprom.h21
-rw-r--r--tmk_core/common/gpio.h22
-rw-r--r--tmk_core/common/pin_defs.h23
-rw-r--r--tmk_core/common/progmem.h19
-rw-r--r--tmk_core/common/sleep_led.h17
-rw-r--r--tmk_core/common/suspend.h20
-rw-r--r--tmk_core/common/test/_wait.h22
-rw-r--r--tmk_core/common/test/bootloader.c19
-rw-r--r--tmk_core/common/test/eeprom.c95
-rw-r--r--tmk_core/common/test/eeprom_stm32_tests.cpp438
-rw-r--r--tmk_core/common/test/flash_stm32_mock.c49
-rw-r--r--tmk_core/common/test/hal.h18
-rw-r--r--tmk_core/common/test/platform.c21
-rw-r--r--tmk_core/common/test/platform.h18
-rw-r--r--tmk_core/common/test/platform_deps.h18
-rw-r--r--tmk_core/common/test/rules.mk24
-rw-r--r--tmk_core/common/test/suspend.c15
-rw-r--r--tmk_core/common/test/testlist.mk1
-rw-r--r--tmk_core/common/test/timer.c33
-rw-r--r--tmk_core/common/timer.h67
-rw-r--r--tmk_core/common/wait.h30
-rw-r--r--tmk_core/native.mk34
-rw-r--r--tmk_core/protocol.mk2
-rw-r--r--tmk_core/rules.mk2
81 files changed, 1 insertions, 7404 deletions
diff --git a/tmk_core/arm_atsam.mk b/tmk_core/arm_atsam.mk
deleted file mode 100644
index b49bf764d..000000000
--- a/tmk_core/arm_atsam.mk
+++ /dev/null
@@ -1,67 +0,0 @@
1# Hey Emacs, this is a -*- makefile -*-
2##############################################################################
3# Compiler settings
4#
5CC = $(CC_PREFIX) arm-none-eabi-gcc
6OBJCOPY = arm-none-eabi-objcopy
7OBJDUMP = arm-none-eabi-objdump
8SIZE = arm-none-eabi-size
9AR = arm-none-eabi-ar
10NM = arm-none-eabi-nm
11HEX = $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature
12EEP = $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT)
13BIN =
14
15COMMON_VPATH += $(LIB_PATH)/arm_atsam/packs/atmel/SAMD51_DFP/1.0.70/include
16COMMON_VPATH += $(LIB_PATH)/arm_atsam/packs/arm/cmsis/5.0.1/CMSIS/Include
17
18COMPILEFLAGS += -funsigned-char
19COMPILEFLAGS += -funsigned-bitfields
20COMPILEFLAGS += -ffunction-sections
21COMPILEFLAGS += -fshort-enums
22COMPILEFLAGS += -fno-inline-small-functions
23COMPILEFLAGS += -fno-strict-aliasing
24COMPILEFLAGS += -mfloat-abi=hard
25COMPILEFLAGS += -mfpu=fpv4-sp-d16
26COMPILEFLAGS += -mthumb
27
28#ALLOW_WARNINGS = yes
29
30CFLAGS += $(COMPILEFLAGS)
31
32CXXFLAGS += $(COMPILEFLAGS)
33CXXFLAGS += -fno-exceptions -std=c++11
34
35LDFLAGS +=-Wl,--gc-sections
36LDFLAGS += -Wl,-Map="%OUT%%PROJ_NAME%.map"
37LDFLAGS += -Wl,--start-group
38LDFLAGS += -Wl,--end-group
39LDFLAGS += --specs=rdimon.specs
40LDFLAGS += -T$(LIB_PATH)/arm_atsam/packs/atmel/SAMD51_DFP/1.0.70/gcc/gcc/samd51j18a_flash.ld
41
42OPT_DEFS += -DPROTOCOL_ARM_ATSAM
43
44MCUFLAGS = -mcpu=$(MCU)
45MCUFLAGS += -D__$(ARM_ATSAM)__
46
47# List any extra directories to look for libraries here.
48# Each directory must be seperated by a space.
49# Use forward slashes for directory separators.
50# For a directory that has spaces, enclose it in quotes.
51EXTRALIBDIRS =
52
53cpfirmware: warn-arm_atsam
54.INTERMEDIATE: warn-arm_atsam
55warn-arm_atsam: $(FIRMWARE_FORMAT)
56 $(info @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@)
57 $(info This MCU support package has a lack of support from the upstream provider (Massdrop).)
58 $(info There are currently questions about valid licensing, and at this stage it's likely)
59 $(info their boards and supporting code will be removed from QMK in the near future. Please)
60 $(info contact Massdrop for support, and encourage them to align their future board design)
61 $(info choices to gain proper license compatibility with QMK.)
62 $(info @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@)
63
64# Convert hex to bin.
65bin: $(BUILD_DIR)/$(TARGET).hex
66 $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
67 $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
diff --git a/tmk_core/avr.mk b/tmk_core/avr.mk
deleted file mode 100644
index c52262273..000000000
--- a/tmk_core/avr.mk
+++ /dev/null
@@ -1,179 +0,0 @@
1# Hey Emacs, this is a -*- makefile -*-
2##############################################################################
3# Compiler settings
4#
5CC = $(CC_PREFIX) avr-gcc
6OBJCOPY = avr-objcopy
7OBJDUMP = avr-objdump
8SIZE = avr-size
9AR = avr-ar
10NM = avr-nm
11HEX = $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature
12EEP = $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT)
13BIN =
14
15COMPILEFLAGS += -funsigned-char
16COMPILEFLAGS += -funsigned-bitfields
17COMPILEFLAGS += -ffunction-sections
18COMPILEFLAGS += -fdata-sections
19COMPILEFLAGS += -fpack-struct
20COMPILEFLAGS += -fshort-enums
21
22ASFLAGS += $(AVR_ASFLAGS)
23
24CFLAGS += $(COMPILEFLAGS) $(AVR_CFLAGS)
25CFLAGS += -fno-inline-small-functions
26CFLAGS += -fno-strict-aliasing
27
28CXXFLAGS += $(COMPILEFLAGS)
29CXXFLAGS += -fno-exceptions -std=c++11
30
31LDFLAGS +=-Wl,--gc-sections
32
33OPT_DEFS += -DF_CPU=$(F_CPU)UL
34
35MCUFLAGS = -mmcu=$(MCU)
36
37# List any extra directories to look for libraries here.
38# Each directory must be seperated by a space.
39# Use forward slashes for directory separators.
40# For a directory that has spaces, enclose it in quotes.
41EXTRALIBDIRS =
42
43
44#---------------- External Memory Options ----------------
45
46# 64 KB of external RAM, starting after internal RAM (ATmega128!),
47# used for variables (.data/.bss) and heap (malloc()).
48#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
49
50# 64 KB of external RAM, starting after internal RAM (ATmega128!),
51# only used for heap (malloc()).
52#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
53
54EXTMEMOPTS =
55
56#---------------- Debugging Options ----------------
57
58# Debugging format.
59# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
60# AVR Studio 4.10 requires dwarf-2.
61# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
62DEBUG = dwarf-2
63
64# For simulavr only - target MCU frequency.
65DEBUG_MFREQ = $(F_CPU)
66
67# Set the DEBUG_UI to either gdb or insight.
68# DEBUG_UI = gdb
69DEBUG_UI = insight
70
71# Set the debugging back-end to either avarice, simulavr.
72DEBUG_BACKEND = avarice
73#DEBUG_BACKEND = simulavr
74
75# GDB Init Filename.
76GDBINIT_FILE = __avr_gdbinit
77
78# When using avarice settings for the JTAG
79JTAG_DEV = /dev/com1
80
81# Debugging port used to communicate between GDB / avarice / simulavr.
82DEBUG_PORT = 4242
83
84# Debugging host used to communicate between GDB / avarice / simulavr, normally
85# just set to localhost unless doing some sort of crazy debugging when
86# avarice is running on a different computer.
87DEBUG_HOST = localhost
88
89#============================================================================
90
91# Convert hex to bin.
92bin: $(BUILD_DIR)/$(TARGET).hex
93ifeq ($(BOOTLOADER),lufa-ms)
94 $(eval BIN_PADDING=$(shell n=`expr 32768 - $(BOOTLOADER_SIZE)` && echo $$(($$n)) || echo 0))
95 $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin --pad-to $(BIN_PADDING)
96else
97 $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
98endif
99 $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
100
101# copy bin to FLASH.bin
102flashbin: bin
103 $(COPY) $(BUILD_DIR)/$(TARGET).bin FLASH.bin;
104
105# Generate avr-gdb config/init file which does the following:
106# define the reset signal, load the target file, connect to target, and set
107# a breakpoint at main().
108gdb-config:
109 @$(REMOVE) $(GDBINIT_FILE)
110 @echo define reset >> $(GDBINIT_FILE)
111 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
112 @echo end >> $(GDBINIT_FILE)
113 @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
114 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
115ifeq ($(DEBUG_BACKEND),simulavr)
116 @echo load >> $(GDBINIT_FILE)
117endif
118 @echo break main >> $(GDBINIT_FILE)
119
120debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
121ifeq ($(DEBUG_BACKEND), avarice)
122 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
123 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
124 $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
125 @$(WINSHELL) /c pause
126
127else
128 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
129 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
130endif
131 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
132
133
134
135
136# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
137COFFCONVERT = $(OBJCOPY) --debugging
138COFFCONVERT += --change-section-address .data-0x800000
139COFFCONVERT += --change-section-address .bss-0x800000
140COFFCONVERT += --change-section-address .noinit-0x800000
141COFFCONVERT += --change-section-address .eeprom-0x810000
142
143
144
145coff: $(BUILD_DIR)/$(TARGET).elf
146 @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
147 $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
148
149
150extcoff: $(BUILD_DIR)/$(TARGET).elf
151 @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
152 $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof
153
154ifeq ($(strip $(BOOTLOADER)), qmk-dfu)
155QMK_BOOTLOADER_TYPE = DFU
156else ifeq ($(strip $(BOOTLOADER)), qmk-hid)
157QMK_BOOTLOADER_TYPE = HID
158endif
159
160bootloader:
161ifeq ($(strip $(QMK_BOOTLOADER_TYPE)),)
162 $(error Please set BOOTLOADER to "qmk-dfu" or "qmk-hid" first!)
163else
164 make -C lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/ clean
165 $(QMK_BIN) generate-dfu-header --quiet --keyboard $(KEYBOARD) --output lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/Keyboard.h
166 $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) tmk_core/common/avr/bootloader_size.c 2> /dev/null | sed -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0))
167 $(eval PROGRAM_SIZE_KB=$(shell n=`expr $(MAX_SIZE) / 1024` && echo $$(($$n)) || echo 0))
168 $(eval BOOT_SECTION_SIZE_KB=$(shell n=`expr $(BOOTLOADER_SIZE) / 1024` && echo $$(($$n)) || echo 0))
169 $(eval FLASH_SIZE_KB=$(shell n=`expr $(PROGRAM_SIZE_KB) + $(BOOT_SECTION_SIZE_KB)` && echo $$(($$n)) || echo 0))
170 make -C lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/ MCU=$(MCU) ARCH=$(ARCH) F_CPU=$(F_CPU) FLASH_SIZE_KB=$(FLASH_SIZE_KB) BOOT_SECTION_SIZE_KB=$(BOOT_SECTION_SIZE_KB)
171 printf "Bootloader$(QMK_BOOTLOADER_TYPE).hex copied to $(TARGET)_bootloader.hex\n"
172 cp lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/Bootloader$(QMK_BOOTLOADER_TYPE).hex $(TARGET)_bootloader.hex
173endif
174
175production: $(BUILD_DIR)/$(TARGET).hex bootloader cpfirmware
176 @cat $(BUILD_DIR)/$(TARGET).hex | awk '/^:00000001FF/ == 0' > $(TARGET)_production.hex
177 @cat $(TARGET)_bootloader.hex >> $(TARGET)_production.hex
178 echo "File sizes:"
179 $(SIZE) $(TARGET).hex $(TARGET)_bootloader.hex $(TARGET)_production.hex
diff --git a/tmk_core/chibios.mk b/tmk_core/chibios.mk
deleted file mode 100644
index 6b298732c..000000000
--- a/tmk_core/chibios.mk
+++ /dev/null
@@ -1,436 +0,0 @@
1# Hey Emacs, this is a -*- makefile -*-
2##############################################################################
3# Architecture or project specific options
4#
5
6# Stack size to be allocated to the Cortex-M process stack. This stack is
7# the stack used by the main() thread.
8ifeq ($(USE_PROCESS_STACKSIZE),)
9 USE_PROCESS_STACKSIZE = 0x800
10endif
11
12# Stack size to the allocated to the Cortex-M main/exceptions stack. This
13# stack is used for processing interrupts and exceptions.
14ifeq ($(USE_EXCEPTIONS_STACKSIZE),)
15 USE_EXCEPTIONS_STACKSIZE = 0x400
16endif
17
18#
19# Architecture or project specific options
20##############################################################################
21
22##############################################################################
23# Project, sources and paths
24#
25
26# Imported source files and paths
27OPT_OS = chibios
28CHIBIOS = $(TOP_DIR)/lib/chibios
29CHIBIOS_CONTRIB = $(TOP_DIR)/lib/chibios-contrib
30
31#
32# Startup, Port and Platform support selection
33##############################################################################
34
35ifeq ($(strip $(MCU)), risc-v)
36 # RISC-V Support
37 # As of 7.4.2021 there is only one supported RISC-V platform in Chibios-Contrib,
38 # therefore all required settings are hard-coded
39 STARTUP_MK = $(CHIBIOS_CONTRIB)/os/common/startup/RISCV-ECLIC/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
40 PORT_V = $(CHIBIOS_CONTRIB)/os/common/ports/RISCV-ECLIC/compilers/GCC/mk/port.mk
41 RULESPATH = $(CHIBIOS_CONTRIB)/os/common/startup/RISCV-ECLIC/compilers/GCC
42 PLATFORM_MK = $(CHIBIOS_CONTRIB)/os/hal/ports/GD/GD32VF103/platform.mk
43else
44 # ARM Support
45 CHIBIOS_PORT ?=
46 ifeq ("$(CHIBIOS_PORT)","")
47 CHIBIOS_PORT = ARMv$(ARMV)-M
48 endif
49
50 # Startup files. Try a few different locations, for compability with old versions and
51 # for things hardware in the contrib repository
52 STARTUP_MK = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
53 ifeq ("$(wildcard $(STARTUP_MK))","")
54 STARTUP_MK = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
55 ifeq ("$(wildcard $(STARTUP_MK))","")
56 STARTUP_MK = $(CHIBIOS_CONTRIB)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
57 endif
58 endif
59
60 # Port files. Try a few different locations, for compability with old versions and
61 # for things hardware in the contrib repository
62 PORT_V = $(CHIBIOS)/os/common/ports/$(CHIBIOS_PORT)/compilers/GCC/mk/port.mk
63 ifeq ("$(wildcard $(PORT_V))","")
64 PORT_V = $(CHIBIOS)/os/rt/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
65 ifeq ("$(wildcard $(PORT_V))","")
66 PORT_V = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
67 endif
68 endif
69
70 # Rules location. Try a few different locations, for compability with old versions and
71 # for things hardware in the contrib repository
72 RULESPATH = $(CHIBIOS)/os/common/ports/$(CHIBIOS_PORT)/compilers/GCC
73 ifeq ("$(wildcard $(RULESPATH)/rules.mk)","")
74 RULESPATH = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC
75 ifeq ("$(wildcard $(RULESPATH)/rules.mk)","")
76 RULESPATH = $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC
77 endif
78 endif
79endif
80
81ifeq ("$(PLATFORM_NAME)","")
82 PLATFORM_NAME = platform
83endif
84
85ifeq ("$(wildcard $(PLATFORM_MK))","")
86 PLATFORM_MK = $(CHIBIOS)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
87 ifeq ("$(wildcard $(PLATFORM_MK))","")
88 PLATFORM_MK = $(CHIBIOS_CONTRIB)/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)/$(PLATFORM_NAME).mk
89 endif
90endif
91
92include $(STARTUP_MK)
93include $(PORT_V)
94include $(PLATFORM_MK)
95
96#
97# Board support selection.
98##############################################################################
99
100BOARD_MK :=
101
102ifneq ("$(wildcard $(KEYBOARD_PATH_5)/boards/$(BOARD)/board.mk)","")
103 BOARD_PATH = $(KEYBOARD_PATH_5)
104 BOARD_MK += $(KEYBOARD_PATH_5)/boards/$(BOARD)/board.mk
105else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/boards/$(BOARD)/board.mk)","")
106 BOARD_PATH = $(KEYBOARD_PATH_4)
107 BOARD_MK += $(KEYBOARD_PATH_4)/boards/$(BOARD)/board.mk
108else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/boards/$(BOARD)/board.mk)","")
109 BOARD_PATH = $(KEYBOARD_PATH_3)
110 BOARD_MK += $(KEYBOARD_PATH_3)/boards/$(BOARD)/board.mk
111else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/boards/$(BOARD)/board.mk)","")
112 BOARD_PATH = $(KEYBOARD_PATH_2)
113 BOARD_MK += $(KEYBOARD_PATH_2)/boards/$(BOARD)/board.mk
114else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/boards/$(BOARD)/board.mk)","")
115 BOARD_PATH = $(KEYBOARD_PATH_1)
116 BOARD_MK += $(KEYBOARD_PATH_1)/boards/$(BOARD)/board.mk
117else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/board/board.mk)","")
118 BOARD_PATH = $(TOP_DIR)/platforms/chibios/boards/$(BOARD)
119 BOARD_MK += $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/board/board.mk
120 KEYBOARD_PATHS += $(BOARD_PATH)/configs
121 ifneq ("$(wildcard $(BOARD_PATH)/rules.mk)","")
122 include $(BOARD_PATH)/rules.mk
123 endif
124endif
125
126ifeq ("$(wildcard $(BOARD_MK))","")
127 BOARD_MK = $(CHIBIOS)/os/hal/boards/$(BOARD)/board.mk
128 ifeq ("$(wildcard $(BOARD_MK))","")
129 BOARD_MK = $(CHIBIOS_CONTRIB)/os/hal/boards/$(BOARD)/board.mk
130 endif
131endif
132
133include $(BOARD_MK)
134
135#
136# Bootloader selection.
137##############################################################################
138
139# Set bootloader address if supplied.
140ifdef STM32_BOOTLOADER_ADDRESS
141 OPT_DEFS += -DSTM32_BOOTLOADER_ADDRESS=$(STM32_BOOTLOADER_ADDRESS)
142endif
143
144# Work out if we need to set up the include for the bootloader definitions
145ifneq ("$(wildcard $(KEYBOARD_PATH_5)/bootloader_defs.h)","")
146 OPT_DEFS += -include $(KEYBOARD_PATH_5)/bootloader_defs.h
147else ifneq ("$(wildcard $(KEYBOARD_PATH_5)/boards/$(BOARD)/bootloader_defs.h)","")
148 OPT_DEFS += -include $(KEYBOARD_PATH_5)/boards/$(BOARD)/bootloader_defs.h
149else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/bootloader_defs.h)","")
150 OPT_DEFS += -include $(KEYBOARD_PATH_4)/bootloader_defs.h
151else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/boards/$(BOARD)/bootloader_defs.h)","")
152 OPT_DEFS += -include $(KEYBOARD_PATH_4)/boards/$(BOARD)/bootloader_defs.h
153else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/bootloader_defs.h)","")
154 OPT_DEFS += -include $(KEYBOARD_PATH_3)/bootloader_defs.h
155else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/boards/$(BOARD)/bootloader_defs.h)","")
156 OPT_DEFS += -include $(KEYBOARD_PATH_3)/boards/$(BOARD)/bootloader_defs.h
157else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/bootloader_defs.h)","")
158 OPT_DEFS += -include $(KEYBOARD_PATH_2)/bootloader_defs.h
159else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/boards/$(BOARD)/bootloader_defs.h)","")
160 OPT_DEFS += -include $(KEYBOARD_PATH_2)/boards/$(BOARD)/bootloader_defs.h
161else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/bootloader_defs.h)","")
162 OPT_DEFS += -include $(KEYBOARD_PATH_1)/bootloader_defs.h
163else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/boards/$(BOARD)/bootloader_defs.h)","")
164 OPT_DEFS += -include $(KEYBOARD_PATH_1)/boards/$(BOARD)/bootloader_defs.h
165else ifneq ("$(wildcard $(BOARD_PATH)/configs/bootloader_defs.h)","")
166 OPT_DEFS += -include $(BOARD_PATH)/configs/bootloader_defs.h
167endif
168
169#
170# ChibiOS config selection.
171##############################################################################
172
173# Work out the config file directories
174ifneq ("$(wildcard $(KEYBOARD_PATH_5)/chconf.h)","")
175 CHCONFDIR = $(KEYBOARD_PATH_5)
176else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/chconf.h)","")
177 CHCONFDIR = $(KEYBOARD_PATH_4)
178else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/chconf.h)","")
179 CHCONFDIR = $(KEYBOARD_PATH_3)
180else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/chconf.h)","")
181 CHCONFDIR = $(KEYBOARD_PATH_2)
182else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/chconf.h)","")
183 CHCONFDIR = $(KEYBOARD_PATH_1)
184else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs/chconf.h)","")
185 CHCONFDIR = $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs
186else ifneq ("$(wildcard $(TOP_DIR)/platforms/boards/chibios/common/configs/chconf.h)","")
187 CHCONFDIR = $(TOP_DIR)/platforms/chibios/boards/common/configs
188endif
189
190#
191# HAL config selection.
192##############################################################################
193
194ifneq ("$(wildcard $(KEYBOARD_PATH_5)/halconf.h)","")
195 HALCONFDIR = $(KEYBOARD_PATH_5)
196else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/halconf.h)","")
197 HALCONFDIR = $(KEYBOARD_PATH_4)
198else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/halconf.h)","")
199 HALCONFDIR = $(KEYBOARD_PATH_3)
200else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/halconf.h)","")
201 HALCONFDIR = $(KEYBOARD_PATH_2)
202else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/halconf.h)","")
203 HALCONFDIR = $(KEYBOARD_PATH_1)
204else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs/halconf.h)","")
205 HALCONFDIR = $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs
206else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/common/configs/halconf.h)","")
207 HALCONFDIR = $(TOP_DIR)/platforms/chibios/boards/common/configs
208endif
209
210#
211# Linker script selection.
212##############################################################################
213
214ifneq ("$(wildcard $(KEYBOARD_PATH_5)/ld/$(MCU_LDSCRIPT).ld)","")
215 LDSCRIPT = $(KEYBOARD_PATH_5)/ld/$(MCU_LDSCRIPT).ld
216else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/ld/$(MCU_LDSCRIPT).ld)","")
217 LDSCRIPT = $(KEYBOARD_PATH_4)/ld/$(MCU_LDSCRIPT).ld
218else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/ld/$(MCU_LDSCRIPT).ld)","")
219 LDSCRIPT = $(KEYBOARD_PATH_3)/ld/$(MCU_LDSCRIPT).ld
220else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/ld/$(MCU_LDSCRIPT).ld)","")
221 LDSCRIPT = $(KEYBOARD_PATH_2)/ld/$(MCU_LDSCRIPT).ld
222else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/ld/$(MCU_LDSCRIPT).ld)","")
223 LDSCRIPT = $(KEYBOARD_PATH_1)/ld/$(MCU_LDSCRIPT).ld
224else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/ld/$(MCU_LDSCRIPT).ld)","")
225 LDFLAGS += -L$(TOP_DIR)/platforms/chibios/boards/$(BOARD)/ld
226 LDSCRIPT = $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/ld/$(MCU_LDSCRIPT).ld
227else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/common/ld/$(MCU_LDSCRIPT).ld)","")
228 LDSCRIPT = $(TOP_DIR)/platforms/chibios/boards/common/ld/$(MCU_LDSCRIPT).ld
229else ifneq ("$(wildcard $(STARTUPLD_CONTRIB)/$(MCU_LDSCRIPT).ld)","")
230 LDSCRIPT = $(STARTUPLD_CONTRIB)/$(MCU_LDSCRIPT).ld
231 USE_CHIBIOS_CONTRIB = yes
232else
233 LDSCRIPT = $(STARTUPLD)/$(MCU_LDSCRIPT).ld
234endif
235
236#
237# Include ChibiOS makefiles.
238##############################################################################
239
240# HAL-OSAL files (optional).
241include $(CHIBIOS)/os/hal/hal.mk
242-include $(CHIBIOS)/os/hal/osal/rt/osal.mk # ChibiOS <= 19.x
243-include $(CHIBIOS)/os/hal/osal/rt-nil/osal.mk # ChibiOS >= 20.x
244# RTOS files (optional).
245include $(CHIBIOS)/os/rt/rt.mk
246# Other files (optional).
247include $(CHIBIOS)/os/hal/lib/streams/streams.mk
248
249PLATFORM_SRC = \
250 $(STARTUPSRC) \
251 $(KERNSRC) \
252 $(PORTSRC) \
253 $(OSALSRC) \
254 $(HALSRC) \
255 $(PLATFORMSRC) \
256 $(BOARDSRC) \
257 $(STREAMSSRC) \
258 $(CHIBIOS)/os/various/syscalls.c \
259 $(PLATFORM_COMMON_DIR)/syscall-fallbacks.c \
260 $(PLATFORM_COMMON_DIR)/wait.c
261
262# Ensure the ASM files are not subjected to LTO -- it'll strip out interrupt handlers otherwise.
263QUANTUM_LIB_SRC += $(STARTUPASM) $(PORTASM) $(OSALASM) $(PLATFORMASM)
264
265PLATFORM_SRC := $(patsubst $(TOP_DIR)/%,%,$(PLATFORM_SRC))
266
267EXTRAINCDIRS += $(CHIBIOS)/os/license $(CHIBIOS)/os/oslib/include \
268 $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs \
269 $(TOP_DIR)/platforms/chibios/boards/common/configs \
270 $(HALCONFDIR) $(CHCONFDIR) \
271 $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
272 $(HALINC) $(PLATFORMINC) $(BOARDINC) $(TESTINC) \
273 $(STREAMSINC) $(CHIBIOS)/os/various $(COMMON_VPATH)
274
275#
276# ChibiOS-Contrib
277##############################################################################
278
279# Work out if we're using ChibiOS-Contrib by checking if halconf_community.h exists
280ifneq ("$(wildcard $(KEYBOARD_PATH_5)/halconf_community.h)","")
281 USE_CHIBIOS_CONTRIB = yes
282else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/halconf_community.h)","")
283 USE_CHIBIOS_CONTRIB = yes
284else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/halconf_community.h)","")
285 USE_CHIBIOS_CONTRIB = yes
286else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/halconf_community.h)","")
287 USE_CHIBIOS_CONTRIB = yes
288else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/halconf_community.h)","")
289 USE_CHIBIOS_CONTRIB = yes
290else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/boards/$(BOARD)/configs/halconf_community.h)","")
291 USE_CHIBIOS_CONTRIB = yes
292endif
293
294ifeq ($(strip $(USE_CHIBIOS_CONTRIB)),yes)
295 include $(CHIBIOS_CONTRIB)/os/hal/hal.mk
296 PLATFORM_SRC += $(PLATFORMSRC_CONTRIB) $(HALSRC_CONTRIB)
297 EXTRAINCDIRS += $(PLATFORMINC_CONTRIB) $(HALINC_CONTRIB) $(CHIBIOS_CONTRIB)/os/various
298endif
299
300#
301# Project, sources and paths
302##############################################################################
303
304##############################################################################
305# Injected configs
306#
307ifneq ("$(wildcard $(BOARD_PATH)/configs/config.h)","")
308 CONFIG_H += $(BOARD_PATH)/configs/config.h
309endif
310ifneq ("$(wildcard $(BOARD_PATH)/configs/post_config.h)","")
311 POST_CONFIG_H += $(BOARD_PATH)/configs/post_config.h
312endif
313
314##############################################################################
315# Compiler and Linker configuration
316#
317
318# Use defined stack sizes of the main thread in linker scripts
319LDSYMBOLS =--defsym=__process_stack_size__=$(USE_PROCESS_STACKSIZE),--defsym=__main_stack_size__=$(USE_EXCEPTIONS_STACKSIZE)
320
321# Shared Compiler flags for all toolchains
322SHARED_CFLAGS = -fomit-frame-pointer \
323 -ffunction-sections \
324 -fdata-sections \
325 -fno-common \
326 -fshort-wchar
327
328# Shared Linker flags for all toolchains
329SHARED_LDFLAGS = -T $(LDSCRIPT) \
330 -Wl,$(LDSYMBOLS) \
331 -Wl,--gc-sections \
332 -nostartfiles
333
334ifeq ($(strip $(MCU)), risc-v)
335 # RISC-V toolchain specific configuration
336 # Find suitable GCC compiler
337 ifeq ($(strip $(TOOLCHAIN)),)
338 ifneq ($(shell which riscv32-unknown-elf-gcc 2>/dev/null),)
339 TOOLCHAIN = riscv32-unknown-elf-
340 else
341 ifneq ($(shell which riscv64-unknown-elf-gcc 2>/dev/null),)
342 TOOLCHAIN = riscv64-unknown-elf-
343 else
344 $(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!")
345 endif
346 endif
347 endif
348
349 # Default to compiling with picolibc for RISC-V targets if available,
350 # which is available by default on current (bullseye) debian based systems.
351 ifeq ($(shell $(TOOLCHAIN)gcc --specs=picolibc.specs -E - 2>/dev/null >/dev/null </dev/null ; echo $$?),0)
352 # Toolchain specific Compiler flags
353 # Note that we still link with our own linker script
354 # by providing it via the -T flag above.
355 TOOLCHAIN_CFLAGS = --specs=picolibc.specs
356
357 # Tell QMK that we are compiling with picolibc.
358 OPT_DEFS += -DUSE_PICOLIBC
359 endif
360
361 # MCU architecture flags
362 MCUFLAGS = -march=$(MCU_ARCH) \
363 -mabi=$(MCU_ABI) \
364 -mcmodel=$(MCU_CMODEL) \
365 -mstrict-align
366else
367 # ARM toolchain specific configuration
368 TOOLCHAIN ?= arm-none-eabi-
369
370 # Toolchain specific Linker flags
371 TOOLCHAIN_LDFLAGS = -Wl,--no-wchar-size-warning \
372 --specs=nano.specs
373
374 # MCU architecture flags
375 MCUFLAGS = -mcpu=$(MCU) \
376 -mthumb -DTHUMB_PRESENT \
377 -mno-thumb-interwork -DTHUMB_NO_INTERWORKING \
378 -mno-unaligned-access
379
380 # Some ARM cores like the M4 and M7 have floating point units which can be enabled
381 USE_FPU ?= no
382
383 ifneq ($(USE_FPU),no)
384 OPT_DEFS += -DCORTEX_USE_FPU=TRUE
385
386 # Default is single precision floats
387 USE_FPU_OPT ?= -mfloat-abi=hard \
388 -mfpu=fpv4-sp-d16 \
389 -fsingle-precision-constant
390
391 MCUFLAGS += $(USE_FPU_OPT)
392 else
393 OPT_DEFS += -DCORTEX_USE_FPU=FALSE
394 endif
395endif
396
397# Assembler flags
398ASFLAGS += $(SHARED_ASFLAGS) $(TOOLCHAIN_ASFLAGS)
399
400# C Compiler flags
401CFLAGS += $(SHARED_CFLAGS) $(TOOLCHAIN_CFLAGS)
402
403# C++ Compiler flags
404CXXFLAGS += $(CFLAGS) $(SHARED_CXXFLAGS) $(TOOLCHAIN_CXXFLAGS) -fno-rtti
405
406# Linker flags
407LDFLAGS += $(SHARED_LDFLAGS) $(TOOLCHAIN_LDFLAGS) $(MCUFLAGS)
408
409# Tell QMK that we are hosting it on ChibiOS.
410OPT_DEFS += -DPROTOCOL_CHIBIOS
411
412# Workaround to stop ChibiOS from complaining about new GCC -- it's been fixed for 7/8/9 already
413OPT_DEFS += -DPORT_IGNORE_GCC_VERSION_CHECK=1
414
415# Construct GCC toolchain
416CC = $(CC_PREFIX) $(TOOLCHAIN)gcc
417OBJCOPY = $(TOOLCHAIN)objcopy
418OBJDUMP = $(TOOLCHAIN)objdump
419SIZE = $(TOOLCHAIN)size
420AR = $(TOOLCHAIN)ar
421NM = $(TOOLCHAIN)nm
422HEX = $(OBJCOPY) -O $(FORMAT)
423EEP =
424BIN = $(OBJCOPY) -O binary
425
426##############################################################################
427# Make targets
428#
429
430DEBUG = gdb
431
432# List any extra directories to look for libraries here.
433EXTRALIBDIRS = $(RULESPATH)/ld
434
435bin: $(BUILD_DIR)/$(TARGET).bin sizeafter
436 $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
diff --git a/tmk_core/common.mk b/tmk_core/common.mk
deleted file mode 100644
index 555b942c9..000000000
--- a/tmk_core/common.mk
+++ /dev/null
@@ -1,16 +0,0 @@
1COMMON_DIR = common
2PLATFORM_COMMON_DIR = $(COMMON_DIR)/$(PLATFORM_KEY)
3
4TMK_COMMON_SRC += \
5 $(PLATFORM_COMMON_DIR)/platform.c \
6 $(PLATFORM_COMMON_DIR)/suspend.c \
7 $(PLATFORM_COMMON_DIR)/timer.c \
8 $(PLATFORM_COMMON_DIR)/bootloader.c \
9
10# Use platform provided print if it exists
11-include $(TMK_PATH)/$(PLATFORM_COMMON_DIR)/printf.mk
12
13# Search Path
14VPATH += $(TMK_PATH)/$(COMMON_DIR)
15VPATH += $(TMK_PATH)/$(PLATFORM_COMMON_DIR)
16VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)
diff --git a/tmk_core/common/arm_atsam/_timer.h b/tmk_core/common/arm_atsam/_timer.h
deleted file mode 100644
index 77402b612..000000000
--- a/tmk_core/common/arm_atsam/_timer.h
+++ /dev/null
@@ -1,19 +0,0 @@
1/* Copyright 2021 Simon Arlott
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// The platform is 32-bit, so prefer 32-bit timers to avoid overflow
19#define FAST_TIMER_T_SIZE 32
diff --git a/tmk_core/common/arm_atsam/_wait.h b/tmk_core/common/arm_atsam/_wait.h
deleted file mode 100644
index 41b686b56..000000000
--- a/tmk_core/common/arm_atsam/_wait.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include "clks.h"
19
20#define wait_ms(ms) CLK_delay_ms(ms)
21#define wait_us(us) CLK_delay_us(us)
22#define waitInputPinDelay()
diff --git a/tmk_core/common/arm_atsam/atomic_util.h b/tmk_core/common/arm_atsam/atomic_util.h
deleted file mode 100644
index 848542d23..000000000
--- a/tmk_core/common/arm_atsam/atomic_util.h
+++ /dev/null
@@ -1,37 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include "samd51j18a.h"
19
20static __inline__ uint8_t __interrupt_disable__(void) {
21 __disable_irq();
22
23 return 1;
24}
25
26static __inline__ void __interrupt_enable__(const uint8_t *__s) {
27 __enable_irq();
28
29 __asm__ volatile("" ::: "memory");
30 (void)__s;
31}
32
33#define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0)
34#define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0
35
36#define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented")
37#define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
diff --git a/tmk_core/common/arm_atsam/bootloader.c b/tmk_core/common/arm_atsam/bootloader.c
deleted file mode 100644
index 9015b00aa..000000000
--- a/tmk_core/common/arm_atsam/bootloader.c
+++ /dev/null
@@ -1,57 +0,0 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "bootloader.h"
18#include "samd51j18a.h"
19#include "md_bootloader.h"
20
21// Set watchdog timer to reset. Directs the bootloader to stay in programming mode.
22void bootloader_jump(void) {
23#ifdef KEYBOARD_massdrop_ctrl
24 // CTRL keyboards released with bootloader version below must use RAM method. Otherwise use WDT method.
25 uint8_t ver_ram_method[] = "v2.18Jun 22 2018 17:28:08"; // The version to match (NULL terminated by compiler)
26 uint8_t *ver_check = ver_ram_method; // Pointer to version match string for traversal
27 uint8_t *ver_rom = (uint8_t *)0x21A0; // Pointer to address in ROM where this specific bootloader version would exist
28
29 while (*ver_check && *ver_rom == *ver_check) { // While there are check version characters to match and bootloader's version matches check's version
30 ver_check++; // Move check version pointer to next character
31 ver_rom++; // Move ROM version pointer to next character
32 }
33
34 if (!*ver_check) { // If check version pointer is NULL, all characters have matched
35 *MAGIC_ADDR = BOOTLOADER_MAGIC; // Set magic number into RAM
36 NVIC_SystemReset(); // Perform system reset
37 while (1) {
38 } // Won't get here
39 }
40#endif
41
42 WDT->CTRLA.bit.ENABLE = 0;
43 while (WDT->SYNCBUSY.bit.ENABLE) {
44 }
45 while (WDT->CTRLA.bit.ENABLE) {
46 }
47 WDT->CONFIG.bit.WINDOW = 0;
48 WDT->CONFIG.bit.PER = 0;
49 WDT->EWCTRL.bit.EWOFFSET = 0;
50 WDT->CTRLA.bit.ENABLE = 1;
51 while (WDT->SYNCBUSY.bit.ENABLE) {
52 }
53 while (!WDT->CTRLA.bit.ENABLE) {
54 }
55 while (1) {
56 } // Wait on timeout
57}
diff --git a/tmk_core/common/arm_atsam/eeprom.c b/tmk_core/common/arm_atsam/eeprom.c
deleted file mode 100644
index ff1a69262..000000000
--- a/tmk_core/common/arm_atsam/eeprom.c
+++ /dev/null
@@ -1,184 +0,0 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include "eeprom.h"
17#include "debug.h"
18#include "samd51j18a.h"
19#include "core_cm4.h"
20#include "component/nvmctrl.h"
21
22#ifndef EEPROM_SIZE
23# include "eeconfig.h"
24# define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
25#endif
26
27#ifndef MAX
28# define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
29#endif
30
31#ifndef BUSY_RETRIES
32# define BUSY_RETRIES 10000
33#endif
34
35// #define DEBUG_EEPROM_OUTPUT
36
37/*
38 * Debug print utils
39 */
40#if defined(DEBUG_EEPROM_OUTPUT)
41# define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__);
42#else /* NO_DEBUG */
43# define eeprom_printf(fmt, ...)
44#endif /* NO_DEBUG */
45
46__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE] = {0};
47volatile uint8_t * SmartEEPROM8 = (uint8_t *)SEEPROM_ADDR;
48
49static inline bool eeprom_is_busy(void) {
50 int timeout = BUSY_RETRIES;
51 while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
52 ;
53
54 return NVMCTRL->SEESTAT.bit.BUSY;
55}
56
57static uint32_t get_virtual_eeprom_size(void) {
58 // clang-format off
59 static const uint32_t VIRTUAL_EEPROM_MAP[11][8] = {
60 /* 4 8 16 32 64 128 256 512 */
61 /* 0*/ { 0, 0, 0, 0, 0, 0, 0, 0 },
62 /* 1*/ { 512, 1024, 2048, 4096, 4096, 4096, 4096, 4096 },
63 /* 2*/ { 512, 1024, 2048, 4096, 8192, 8192, 8192, 8192 },
64 /* 3*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 },
65 /* 4*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 },
66 /* 5*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
67 /* 6*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
68 /* 7*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
69 /* 8*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
70 /* 9*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 },
71 /*10*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 },
72 };
73 // clang-format on
74
75 static uint32_t virtual_eeprom_size = UINT32_MAX;
76 if (virtual_eeprom_size == UINT32_MAX) {
77 virtual_eeprom_size = VIRTUAL_EEPROM_MAP[NVMCTRL->SEESTAT.bit.PSZ][NVMCTRL->SEESTAT.bit.SBLK];
78 }
79 // eeprom_printf("get_virtual_eeprom_size:: %d:%d:%d\n", NVMCTRL->SEESTAT.bit.PSZ, NVMCTRL->SEESTAT.bit.SBLK, virtual_eeprom_size);
80 return virtual_eeprom_size;
81}
82
83uint8_t eeprom_read_byte(const uint8_t *addr) {
84 uintptr_t offset = (uintptr_t)addr;
85 if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) {
86 eeprom_printf("eeprom_read_byte:: out of bounds\n");
87 return 0x0;
88 }
89
90 if (get_virtual_eeprom_size() == 0) {
91 return buffer[offset];
92 }
93
94 if (eeprom_is_busy()) {
95 eeprom_printf("eeprom_write_byte:: timeout\n");
96 return 0x0;
97 }
98
99 return SmartEEPROM8[offset];
100}
101
102void eeprom_write_byte(uint8_t *addr, uint8_t value) {
103 uintptr_t offset = (uintptr_t)addr;
104 if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) {
105 eeprom_printf("eeprom_write_byte:: out of bounds\n");
106 return;
107 }
108
109 if (get_virtual_eeprom_size() == 0) {
110 buffer[offset] = value;
111 return;
112 }
113
114 if (eeprom_is_busy()) {
115 eeprom_printf("eeprom_write_byte:: timeout\n");
116 return;
117 }
118
119 SmartEEPROM8[offset] = value;
120}
121
122uint16_t eeprom_read_word(const uint16_t *addr) {
123 const uint8_t *p = (const uint8_t *)addr;
124 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
125}
126
127uint32_t eeprom_read_dword(const uint32_t *addr) {
128 const uint8_t *p = (const uint8_t *)addr;
129 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
130}
131
132void eeprom_read_block(void *buf, const void *addr, size_t len) {
133 const uint8_t *p = (const uint8_t *)addr;
134 uint8_t * dest = (uint8_t *)buf;
135 while (len--) {
136 *dest++ = eeprom_read_byte(p++);
137 }
138}
139
140void eeprom_write_word(uint16_t *addr, uint16_t value) {
141 uint8_t *p = (uint8_t *)addr;
142 eeprom_write_byte(p++, value);
143 eeprom_write_byte(p, value >> 8);
144}
145
146void eeprom_write_dword(uint32_t *addr, uint32_t value) {
147 uint8_t *p = (uint8_t *)addr;
148 eeprom_write_byte(p++, value);
149 eeprom_write_byte(p++, value >> 8);
150 eeprom_write_byte(p++, value >> 16);
151 eeprom_write_byte(p, value >> 24);
152}
153
154void eeprom_write_block(const void *buf, void *addr, size_t len) {
155 uint8_t * p = (uint8_t *)addr;
156 const uint8_t *src = (const uint8_t *)buf;
157 while (len--) {
158 eeprom_write_byte(p++, *src++);
159 }
160}
161
162void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); }
163
164void eeprom_update_word(uint16_t *addr, uint16_t value) {
165 uint8_t *p = (uint8_t *)addr;
166 eeprom_write_byte(p++, value);
167 eeprom_write_byte(p, value >> 8);
168}
169
170void eeprom_update_dword(uint32_t *addr, uint32_t value) {
171 uint8_t *p = (uint8_t *)addr;
172 eeprom_write_byte(p++, value);
173 eeprom_write_byte(p++, value >> 8);
174 eeprom_write_byte(p++, value >> 16);
175 eeprom_write_byte(p, value >> 24);
176}
177
178void eeprom_update_block(const void *buf, void *addr, size_t len) {
179 uint8_t * p = (uint8_t *)addr;
180 const uint8_t *src = (const uint8_t *)buf;
181 while (len--) {
182 eeprom_write_byte(p++, *src++);
183 }
184}
diff --git a/tmk_core/common/arm_atsam/gpio.h b/tmk_core/common/arm_atsam/gpio.h
deleted file mode 100644
index 915ed0ef4..000000000
--- a/tmk_core/common/arm_atsam/gpio.h
+++ /dev/null
@@ -1,77 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include "stdint.h"
19#include "samd51j18a.h"
20
21#include "pin_defs.h"
22
23typedef uint8_t pin_t;
24
25#define SAMD_PORT(pin) ((pin & 0x20) >> 5)
26#define SAMD_PIN(pin) (pin & 0x1f)
27#define SAMD_PIN_MASK(pin) (1 << (pin & 0x1f))
28
29#define setPinInput(pin) \
30 do { \
31 PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.INEN = 1; \
32 PORT->Group[SAMD_PORT(pin)].DIRCLR.reg = SAMD_PIN_MASK(pin); \
33 } while (0)
34
35#define setPinInputHigh(pin) \
36 do { \
37 PORT->Group[SAMD_PORT(pin)].DIRCLR.reg = SAMD_PIN_MASK(pin); \
38 PORT->Group[SAMD_PORT(pin)].OUTSET.reg = SAMD_PIN_MASK(pin); \
39 PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.INEN = 1; \
40 PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.PULLEN = 1; \
41 } while (0)
42
43#define setPinInputLow(pin) \
44 do { \
45 PORT->Group[SAMD_PORT(pin)].DIRCLR.reg = SAMD_PIN_MASK(pin); \
46 PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \
47 PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.INEN = 1; \
48 PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.PULLEN = 1; \
49 } while (0)
50
51#define setPinOutput(pin) \
52 do { \
53 PORT->Group[SAMD_PORT(pin)].DIRSET.reg = SAMD_PIN_MASK(pin); \
54 PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \
55 } while (0)
56
57#define writePinHigh(pin) \
58 do { \
59 PORT->Group[SAMD_PORT(pin)].OUTSET.reg = SAMD_PIN_MASK(pin); \
60 } while (0)
61
62#define writePinLow(pin) \
63 do { \
64 PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \
65 } while (0)
66
67#define writePin(pin, level) \
68 do { \
69 if (level) \
70 PORT->Group[SAMD_PORT(pin)].OUTSET.reg = SAMD_PIN_MASK(pin); \
71 else \
72 PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \
73 } while (0)
74
75#define readPin(pin) ((PORT->Group[SAMD_PORT(pin)].IN.reg & SAMD_PIN_MASK(pin)) != 0)
76
77#define togglePin(pin) (PORT->Group[SAMD_PORT(pin)].OUTTGL.reg = SAMD_PIN_MASK(pin))
diff --git a/tmk_core/common/arm_atsam/pin_defs.h b/tmk_core/common/arm_atsam/pin_defs.h
deleted file mode 100644
index 5b50b2391..000000000
--- a/tmk_core/common/arm_atsam/pin_defs.h
+++ /dev/null
@@ -1,84 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include "samd51j18a.h"
19
20#define A00 PIN_PA00
21#define A01 PIN_PA01
22#define A02 PIN_PA02
23#define A03 PIN_PA03
24#define A04 PIN_PA04
25#define A05 PIN_PA05
26#define A06 PIN_PA06
27#define A07 PIN_PA07
28#define A08 PIN_PA08
29#define A09 PIN_PA09
30#define A10 PIN_PA10
31#define A11 PIN_PA11
32#define A12 PIN_PA12
33#define A13 PIN_PA13
34#define A14 PIN_PA14
35#define A15 PIN_PA15
36#define A16 PIN_PA16
37#define A17 PIN_PA17
38#define A18 PIN_PA18
39#define A19 PIN_PA19
40#define A20 PIN_PA20
41#define A21 PIN_PA21
42#define A22 PIN_PA22
43#define A23 PIN_PA23
44#define A24 PIN_PA24
45#define A25 PIN_PA25
46#define A26 PIN_PA26
47#define A27 PIN_PA27
48#define A28 PIN_PA28
49#define A29 PIN_PA29
50#define A30 PIN_PA30
51#define A31 PIN_PA31
52
53#define B00 PIN_PB00
54#define B01 PIN_PB01
55#define B02 PIN_PB02
56#define B03 PIN_PB03
57#define B04 PIN_PB04
58#define B05 PIN_PB05
59#define B06 PIN_PB06
60#define B07 PIN_PB07
61#define B08 PIN_PB08
62#define B09 PIN_PB09
63#define B10 PIN_PB10
64#define B11 PIN_PB11
65#define B12 PIN_PB12
66#define B13 PIN_PB13
67#define B14 PIN_PB14
68#define B15 PIN_PB15
69#define B16 PIN_PB16
70#define B17 PIN_PB17
71#define B18 PIN_PB18
72#define B19 PIN_PB19
73#define B20 PIN_PB20
74#define B21 PIN_PB21
75#define B22 PIN_PB22
76#define B23 PIN_PB23
77#define B24 PIN_PB24
78#define B25 PIN_PB25
79#define B26 PIN_PB26
80#define B27 PIN_PB27
81#define B28 PIN_PB28
82#define B29 PIN_PB29
83#define B30 PIN_PB30
84#define B31 PIN_PB31
diff --git a/tmk_core/common/arm_atsam/platform.c b/tmk_core/common/arm_atsam/platform.c
deleted file mode 100644
index 3e35b4fe4..000000000
--- a/tmk_core/common/arm_atsam/platform.c
+++ /dev/null
@@ -1,21 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "platform_deps.h"
18
19void platform_setup(void) {
20 // do nothing
21}
diff --git a/tmk_core/common/arm_atsam/platform_deps.h b/tmk_core/common/arm_atsam/platform_deps.h
deleted file mode 100644
index f296d1d53..000000000
--- a/tmk_core/common/arm_atsam/platform_deps.h
+++ /dev/null
@@ -1,18 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// here just to please the build
diff --git a/tmk_core/common/arm_atsam/suspend.c b/tmk_core/common/arm_atsam/suspend.c
deleted file mode 100644
index e51426128..000000000
--- a/tmk_core/common/arm_atsam/suspend.c
+++ /dev/null
@@ -1,77 +0,0 @@
1#include "matrix.h"
2#include "i2c_master.h"
3#include "md_rgb_matrix.h"
4#include "suspend.h"
5
6/** \brief Suspend idle
7 *
8 * FIXME: needs doc
9 */
10void suspend_idle(uint8_t time) { /* Note: Not used anywhere currently */
11}
12
13/** \brief Run user level Power down
14 *
15 * FIXME: needs doc
16 */
17__attribute__((weak)) void suspend_power_down_user(void) {}
18
19/** \brief Run keyboard level Power down
20 *
21 * FIXME: needs doc
22 */
23__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }
24
25/** \brief Suspend power down
26 *
27 * FIXME: needs doc
28 */
29void suspend_power_down(void) {
30#ifdef RGB_MATRIX_ENABLE
31 I2C3733_Control_Set(0); // Disable LED driver
32#endif
33
34 suspend_power_down_kb();
35}
36
37__attribute__((weak)) void matrix_power_up(void) {}
38__attribute__((weak)) void matrix_power_down(void) {}
39bool suspend_wakeup_condition(void) {
40 matrix_power_up();
41 matrix_scan();
42 matrix_power_down();
43 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
44 if (matrix_get_row(r)) return true;
45 }
46 return false;
47}
48
49/** \brief run user level code immediately after wakeup
50 *
51 * FIXME: needs doc
52 */
53__attribute__((weak)) void suspend_wakeup_init_user(void) {}
54
55/** \brief run keyboard level code immediately after wakeup
56 *
57 * FIXME: needs doc
58 */
59__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }
60
61/** \brief run immediately after wakeup
62 *
63 * FIXME: needs doc
64 */
65void suspend_wakeup_init(void) {
66#ifdef RGB_MATRIX_ENABLE
67# ifdef USE_MASSDROP_CONFIGURATOR
68 if (led_enabled) {
69 I2C3733_Control_Set(1);
70 }
71# else
72 I2C3733_Control_Set(1);
73# endif
74#endif
75
76 suspend_wakeup_init_kb();
77}
diff --git a/tmk_core/common/arm_atsam/timer.c b/tmk_core/common/arm_atsam/timer.c
deleted file mode 100644
index b835dd5e7..000000000
--- a/tmk_core/common/arm_atsam/timer.c
+++ /dev/null
@@ -1,19 +0,0 @@
1#include "samd51j18a.h"
2#include "timer.h"
3#include "tmk_core/protocol/arm_atsam/clks.h"
4
5void set_time(uint64_t tset) { ms_clk = tset; }
6
7void timer_init(void) { timer_clear(); }
8
9uint16_t timer_read(void) { return (uint16_t)ms_clk; }
10
11uint32_t timer_read32(void) { return (uint32_t)ms_clk; }
12
13uint64_t timer_read64(void) { return ms_clk; }
14
15uint16_t timer_elapsed(uint16_t tlast) { return TIMER_DIFF_16(timer_read(), tlast); }
16
17uint32_t timer_elapsed32(uint32_t tlast) { return TIMER_DIFF_32(timer_read32(), tlast); }
18
19void timer_clear(void) { set_time(0); }
diff --git a/tmk_core/common/atomic_util.h b/tmk_core/common/atomic_util.h
deleted file mode 100644
index 2c95302a1..000000000
--- a/tmk_core/common/atomic_util.h
+++ /dev/null
@@ -1,32 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// Macro to help make GPIO and other controls atomic.
19
20#ifndef IGNORE_ATOMIC_BLOCK
21# if __has_include_next("atomic_util.h")
22# include_next "atomic_util.h" /* Include the platforms atomic.h */
23# else
24# define ATOMIC_BLOCK _Static_assert(0, "ATOMIC_BLOCK not implemented")
25# define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented")
26# define ATOMIC_BLOCK_FORCEON _Static_assert(0, "ATOMIC_BLOCK_FORCEON not implemented")
27# endif
28#else /* do nothing atomic macro */
29# define ATOMIC_BLOCK for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0)
30# define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK
31# define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK
32#endif
diff --git a/tmk_core/common/avr/_print.h b/tmk_core/common/avr/_print.h
deleted file mode 100644
index 5c1fdd26d..000000000
--- a/tmk_core/common/avr/_print.h
+++ /dev/null
@@ -1,33 +0,0 @@
1/* Copyright 2012 Jun Wako <wakojun@gmail.com> */
2/* Very basic print functions, intended to be used with usb_debug_only.c
3 * http://www.pjrc.com/teensy/
4 * Copyright (c) 2008 PJRC.COM, LLC
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#pragma once
25
26#include "avr/xprintf.h"
27
28// Create user & normal print defines
29#define print(s) xputs(PSTR(s))
30#define println(s) xputs(PSTR(s "\r\n"))
31#define uprint(s) xputs(PSTR(s))
32#define uprintln(s) xputs(PSTR(s "\r\n"))
33#define uprintf(fmt, ...) __xprintf(PSTR(fmt), ##__VA_ARGS__) \ No newline at end of file
diff --git a/tmk_core/common/avr/_timer.h b/tmk_core/common/avr/_timer.h
deleted file mode 100644
index b81e0f68b..000000000
--- a/tmk_core/common/avr/_timer.h
+++ /dev/null
@@ -1,19 +0,0 @@
1/* Copyright 2021 Simon Arlott
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// The platform is 8-bit, so prefer 16-bit timers to reduce code size
19#define FAST_TIMER_T_SIZE 16
diff --git a/tmk_core/common/avr/_wait.h b/tmk_core/common/avr/_wait.h
deleted file mode 100644
index 683db6ae5..000000000
--- a/tmk_core/common/avr/_wait.h
+++ /dev/null
@@ -1,49 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <util/delay.h>
19
20#define wait_ms(ms) \
21 do { \
22 if (__builtin_constant_p(ms)) { \
23 _delay_ms(ms); \
24 } else { \
25 for (uint16_t i = ms; i > 0; i--) { \
26 _delay_ms(1); \
27 } \
28 } \
29 } while (0)
30#define wait_us(us) \
31 do { \
32 if (__builtin_constant_p(us)) { \
33 _delay_us(us); \
34 } else { \
35 for (uint16_t i = us; i > 0; i--) { \
36 _delay_us(1); \
37 } \
38 } \
39 } while (0)
40#define wait_cpuclock(n) __builtin_avr_delay_cycles(n)
41#define CPU_CLOCK F_CPU
42
43/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal.
44 * But here's more margin to make it two clocks. */
45#ifndef GPIO_INPUT_PIN_DELAY
46# define GPIO_INPUT_PIN_DELAY 2
47#endif
48
49#define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
diff --git a/tmk_core/common/avr/atomic_util.h b/tmk_core/common/avr/atomic_util.h
deleted file mode 100644
index 7c5d2e7dc..000000000
--- a/tmk_core/common/avr/atomic_util.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18/* atomic macro for AVR */
19#include <util/atomic.h>
20
21#define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
22#define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c
deleted file mode 100644
index c0272903b..000000000
--- a/tmk_core/common/avr/bootloader.c
+++ /dev/null
@@ -1,293 +0,0 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <avr/io.h>
4#include <avr/eeprom.h>
5#include <avr/interrupt.h>
6#include <avr/wdt.h>
7#include <util/delay.h>
8#include "bootloader.h"
9#include <avr/boot.h>
10
11#ifdef PROTOCOL_LUFA
12# include <LUFA/Drivers/USB/USB.h>
13#endif
14
15/** \brief Bootloader Size in *bytes*
16 *
17 * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet.
18 * Note that 'Word'(2 bytes) size and address are used in datasheet while TMK uses 'Byte'.
19 *
20 * Size of Bootloaders in bytes:
21 * Atmel DFU loader(ATmega32U4) 4096
22 * Atmel DFU loader(AT90USB128) 8192
23 * LUFA bootloader(ATmega32U4) 4096
24 * Arduino Caterina(ATmega32U4) 4096
25 * USBaspLoader(ATmega***) 2048
26 * Teensy halfKay(ATmega32U4) 512
27 * Teensy++ halfKay(AT90USB128) 1024
28 *
29 * AVR Boot section is located at the end of Flash memory like the followings.
30 *
31 * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
32 * 0x0000 +---------------+ 0x00000 +---------------+
33 * | | | |
34 * | | | |
35 * | Application | | Application |
36 * | | | |
37 * = = = =
38 * | | 32KB-4KB | | 128KB-8KB
39 * 0x7000 +---------------+ 0x1E000 +---------------+
40 * | Bootloader | 4KB | Bootloader | 8KB
41 * 0x7FFF +---------------+ 0x1FFFF +---------------+
42 *
43 *
44 * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
45 * 0x0000 +---------------+ 0x00000 +---------------+
46 * | | | |
47 * | | | |
48 * | Application | | Application |
49 * | | | |
50 * = = = =
51 * | | 32KB-512B | | 128KB-1KB
52 * 0x7E00 +---------------+ 0x1FC00 +---------------+
53 * | Bootloader | 512B | Bootloader | 1KB
54 * 0x7FFF +---------------+ 0x1FFFF +---------------+
55 */
56#define FLASH_SIZE (FLASHEND + 1L)
57
58#if !defined(BOOTLOADER_SIZE)
59uint16_t bootloader_start;
60#endif
61
62// compatibility between ATMega8 and ATMega88
63#if !defined(MCUCSR)
64# if defined(MCUSR)
65# define MCUCSR MCUSR
66# endif
67#endif
68
69/** \brief Entering the Bootloader via Software
70 *
71 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
72 */
73#define BOOTLOADER_RESET_KEY 0xB007B007
74uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;")));
75
76/** \brief initialize MCU status by watchdog reset
77 *
78 * FIXME: needs doc
79 */
80__attribute__((weak)) void bootloader_jump(void) {
81#if !defined(BOOTLOADER_SIZE)
82 uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
83
84 if (high_fuse & ~(FUSE_BOOTSZ0 & FUSE_BOOTSZ1)) {
85 bootloader_start = (FLASH_SIZE - 512) >> 1;
86 } else if (high_fuse & ~(FUSE_BOOTSZ1)) {
87 bootloader_start = (FLASH_SIZE - 1024) >> 1;
88 } else if (high_fuse & ~(FUSE_BOOTSZ0)) {
89 bootloader_start = (FLASH_SIZE - 2048) >> 1;
90 } else {
91 bootloader_start = (FLASH_SIZE - 4096) >> 1;
92 }
93#endif
94
95 // Something like this might work, but it compiled larger than the block above
96 // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1));
97
98#if defined(BOOTLOADER_HALFKAY)
99 // http://www.pjrc.com/teensy/jump_to_bootloader.html
100 cli();
101 // disable watchdog, if enabled (it's not)
102 // disable all peripherals
103 // a shutdown call might make sense here
104 UDCON = 1;
105 USBCON = (1 << FRZCLK); // disable USB
106 UCSR1B = 0;
107 _delay_ms(5);
108# if defined(__AVR_AT90USB162__) // Teensy 1.0
109 EIMSK = 0;
110 PCICR = 0;
111 SPCR = 0;
112 ACSR = 0;
113 EECR = 0;
114 TIMSK0 = 0;
115 TIMSK1 = 0;
116 UCSR1B = 0;
117 DDRB = 0;
118 DDRC = 0;
119 DDRD = 0;
120 PORTB = 0;
121 PORTC = 0;
122 PORTD = 0;
123 asm volatile("jmp 0x3E00");
124# elif defined(__AVR_ATmega32U4__) // Teensy 2.0
125 EIMSK = 0;
126 PCICR = 0;
127 SPCR = 0;
128 ACSR = 0;
129 EECR = 0;
130 ADCSRA = 0;
131 TIMSK0 = 0;
132 TIMSK1 = 0;
133 TIMSK3 = 0;
134 TIMSK4 = 0;
135 UCSR1B = 0;
136 TWCR = 0;
137 DDRB = 0;
138 DDRC = 0;
139 DDRD = 0;
140 DDRE = 0;
141 DDRF = 0;
142 TWCR = 0;
143 PORTB = 0;
144 PORTC = 0;
145 PORTD = 0;
146 PORTE = 0;
147 PORTF = 0;
148 asm volatile("jmp 0x7E00");
149# elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
150 EIMSK = 0;
151 PCICR = 0;
152 SPCR = 0;
153 ACSR = 0;
154 EECR = 0;
155 ADCSRA = 0;
156 TIMSK0 = 0;
157 TIMSK1 = 0;
158 TIMSK2 = 0;
159 TIMSK3 = 0;
160 UCSR1B = 0;
161 TWCR = 0;
162 DDRA = 0;
163 DDRB = 0;
164 DDRC = 0;
165 DDRD = 0;
166 DDRE = 0;
167 DDRF = 0;
168 PORTA = 0;
169 PORTB = 0;
170 PORTC = 0;
171 PORTD = 0;
172 PORTE = 0;
173 PORTF = 0;
174 asm volatile("jmp 0xFC00");
175# elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
176 EIMSK = 0;
177 PCICR = 0;
178 SPCR = 0;
179 ACSR = 0;
180 EECR = 0;
181 ADCSRA = 0;
182 TIMSK0 = 0;
183 TIMSK1 = 0;
184 TIMSK2 = 0;
185 TIMSK3 = 0;
186 UCSR1B = 0;
187 TWCR = 0;
188 DDRA = 0;
189 DDRB = 0;
190 DDRC = 0;
191 DDRD = 0;
192 DDRE = 0;
193 DDRF = 0;
194 PORTA = 0;
195 PORTB = 0;
196 PORTC = 0;
197 PORTD = 0;
198 PORTE = 0;
199 PORTF = 0;
200 asm volatile("jmp 0x1FC00");
201# endif
202
203#elif defined(BOOTLOADER_CATERINA)
204 // this block may be optional
205 // TODO: figure it out
206
207 uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
208
209 // Value used by Caterina bootloader use to determine whether to run the
210 // sketch or the bootloader programmer.
211 uint16_t bootKey = 0x7777;
212
213 *bootKeyPtr = bootKey;
214
215 // setup watchdog timeout
216 wdt_enable(WDTO_60MS);
217
218 while (1) {
219 } // wait for watchdog timer to trigger
220
221#elif defined(BOOTLOADER_USBASP)
222 // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c
223 wdt_enable(WDTO_15MS);
224 wdt_reset();
225 asm volatile("cli \n\t"
226 "ldi r29 , %[ramendhi] \n\t"
227 "ldi r28 , %[ramendlo] \n\t"
228# if (FLASHEND > 131071)
229 "ldi r18 , %[bootaddrhi] \n\t"
230 "st Y+, r18 \n\t"
231# endif
232 "ldi r18 , %[bootaddrme] \n\t"
233 "st Y+, r18 \n\t"
234 "ldi r18 , %[bootaddrlo] \n\t"
235 "st Y+, r18 \n\t"
236 "out %[mcucsrio], __zero_reg__ \n\t"
237 "bootloader_startup_loop%=: \n\t"
238 "rjmp bootloader_startup_loop%= \n\t"
239 :
240 : [mcucsrio] "I"(_SFR_IO_ADDR(MCUCSR)),
241# if (FLASHEND > 131071)
242 [ramendhi] "M"(((RAMEND - 2) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 2) >> 0) & 0xff), [bootaddrhi] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff),
243# else
244 [ramendhi] "M"(((RAMEND - 1) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 1) >> 0) & 0xff),
245# endif
246 [bootaddrme] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [bootaddrlo] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff));
247
248#else // Assume remaining boards are DFU, even if the flag isn't set
249
250# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined(__AVR_ATtiny85__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though?
251 UDCON = 1;
252 USBCON = (1 << FRZCLK); // disable USB
253 UCSR1B = 0;
254 _delay_ms(5); // 5 seems to work fine
255# endif
256
257# ifdef BOOTLOADER_BOOTLOADHID
258 // force bootloadHID to stay in bootloader mode, so that it waits
259 // for a new firmware to be flashed
260 eeprom_write_byte((uint8_t *)1, 0x00);
261# endif
262
263 // watchdog reset
264 reset_key = BOOTLOADER_RESET_KEY;
265 wdt_enable(WDTO_250MS);
266 for (;;)
267 ;
268#endif
269}
270
271/* this runs before main() */
272void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3")));
273void bootloader_jump_after_watchdog_reset(void) {
274#ifndef BOOTLOADER_HALFKAY
275 if ((MCUCSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
276 reset_key = 0;
277
278 // My custom USBasploader requires this to come up.
279 MCUCSR = 0;
280
281 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
282 MCUCSR &= ~(1 << WDRF);
283 wdt_disable();
284
285// This is compled into 'icall', address should be in word unit, not byte.
286# ifdef BOOTLOADER_SIZE
287 ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
288# else
289 asm("ijmp" ::"z"(bootloader_start));
290# endif
291 }
292#endif
293}
diff --git a/tmk_core/common/avr/bootloader_size.c b/tmk_core/common/avr/bootloader_size.c
deleted file mode 100644
index a029f9321..000000000
--- a/tmk_core/common/avr/bootloader_size.c
+++ /dev/null
@@ -1,21 +0,0 @@
1// Copyright 2017 Jack Humbert
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#include <avr/io.h>
17#include <avr/boot.h>
18
19// clang-format off
20// this is not valid C - it's for computing the size available on the chip
21AVR_SIZE: FLASHEND + 1 - BOOTLOADER_SIZE
diff --git a/tmk_core/common/avr/gpio.h b/tmk_core/common/avr/gpio.h
deleted file mode 100644
index e9be68491..000000000
--- a/tmk_core/common/avr/gpio.h
+++ /dev/null
@@ -1,49 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <avr/io.h>
19#include "pin_defs.h"
20
21typedef uint8_t pin_t;
22
23/* Operation of GPIO by pin. */
24
25#define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
26#define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
27#define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
28#define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
29
30#define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
31#define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
32#define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
33
34#define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
35
36#define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF))
37
38/* Operation of GPIO by port. */
39
40typedef uint8_t port_data_t;
41
42#define readPort(port) PINx_ADDRESS(port)
43
44#define setPortBitInput(port, bit) (DDRx_ADDRESS(port) &= ~_BV((bit)&0xF), PORTx_ADDRESS(port) &= ~_BV((bit)&0xF))
45#define setPortBitInputHigh(port, bit) (DDRx_ADDRESS(port) &= ~_BV((bit)&0xF), PORTx_ADDRESS(port) |= _BV((bit)&0xF))
46#define setPortBitOutput(port, bit) (DDRx_ADDRESS(port) |= _BV((bit)&0xF))
47
48#define writePortBitLow(port, bit) (PORTx_ADDRESS(port) &= ~_BV((bit)&0xF))
49#define writePortBitHigh(port, bit) (PORTx_ADDRESS(port) |= _BV((bit)&0xF))
diff --git a/tmk_core/common/avr/pin_defs.h b/tmk_core/common/avr/pin_defs.h
deleted file mode 100644
index 23d948041..000000000
--- a/tmk_core/common/avr/pin_defs.h
+++ /dev/null
@@ -1,128 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <avr/io.h>
19
20#define PORT_SHIFTER 4 // this may be 4 for all AVR chips
21
22// If you want to add more to this list, reference the PINx definitions in these header
23// files: https://github.com/vancegroup-mirrors/avr-libc/tree/master/avr-libc/include/avr
24
25#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)
26# define ADDRESS_BASE 0x00
27# define PINB_ADDRESS 0x3
28# define PINC_ADDRESS 0x6
29# define PIND_ADDRESS 0x9
30# define PINE_ADDRESS 0xC
31# define PINF_ADDRESS 0xF
32#elif defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
33# define ADDRESS_BASE 0x00
34# define PINB_ADDRESS 0x3
35# define PINC_ADDRESS 0x6
36# define PIND_ADDRESS 0x9
37#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
38# define ADDRESS_BASE 0x00
39# define PINA_ADDRESS 0x0
40# define PINB_ADDRESS 0x3
41# define PINC_ADDRESS 0x6
42# define PIND_ADDRESS 0x9
43# define PINE_ADDRESS 0xC
44# define PINF_ADDRESS 0xF
45#elif defined(__AVR_ATmega32A__)
46# define ADDRESS_BASE 0x10
47# define PIND_ADDRESS 0x0
48# define PINC_ADDRESS 0x3
49# define PINB_ADDRESS 0x6
50# define PINA_ADDRESS 0x9
51#elif defined(__AVR_ATtiny85__)
52# define ADDRESS_BASE 0x10
53# define PINB_ADDRESS 0x6
54#else
55# error "Pins are not defined"
56#endif
57
58#define PINDEF(port, pin) ((PIN##port##_ADDRESS << PORT_SHIFTER) | pin)
59
60#define _PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + ((p) >> PORT_SHIFTER) + (offset))
61// Port X Input Pins Address
62#define PINx_ADDRESS(p) _PIN_ADDRESS(p, 0)
63// Port X Data Direction Register, 0:input 1:output
64#define DDRx_ADDRESS(p) _PIN_ADDRESS(p, 1)
65// Port X Data Register
66#define PORTx_ADDRESS(p) _PIN_ADDRESS(p, 2)
67
68/* I/O pins */
69#ifdef PORTA
70# define A0 PINDEF(A, 0)
71# define A1 PINDEF(A, 1)
72# define A2 PINDEF(A, 2)
73# define A3 PINDEF(A, 3)
74# define A4 PINDEF(A, 4)
75# define A5 PINDEF(A, 5)
76# define A6 PINDEF(A, 6)
77# define A7 PINDEF(A, 7)
78#endif
79#ifdef PORTB
80# define B0 PINDEF(B, 0)
81# define B1 PINDEF(B, 1)
82# define B2 PINDEF(B, 2)
83# define B3 PINDEF(B, 3)
84# define B4 PINDEF(B, 4)
85# define B5 PINDEF(B, 5)
86# define B6 PINDEF(B, 6)
87# define B7 PINDEF(B, 7)
88#endif
89#ifdef PORTC
90# define C0 PINDEF(C, 0)
91# define C1 PINDEF(C, 1)
92# define C2 PINDEF(C, 2)
93# define C3 PINDEF(C, 3)
94# define C4 PINDEF(C, 4)
95# define C5 PINDEF(C, 5)
96# define C6 PINDEF(C, 6)
97# define C7 PINDEF(C, 7)
98#endif
99#ifdef PORTD
100# define D0 PINDEF(D, 0)
101# define D1 PINDEF(D, 1)
102# define D2 PINDEF(D, 2)
103# define D3 PINDEF(D, 3)
104# define D4 PINDEF(D, 4)
105# define D5 PINDEF(D, 5)
106# define D6 PINDEF(D, 6)
107# define D7 PINDEF(D, 7)
108#endif
109#ifdef PORTE
110# define E0 PINDEF(E, 0)
111# define E1 PINDEF(E, 1)
112# define E2 PINDEF(E, 2)
113# define E3 PINDEF(E, 3)
114# define E4 PINDEF(E, 4)
115# define E5 PINDEF(E, 5)
116# define E6 PINDEF(E, 6)
117# define E7 PINDEF(E, 7)
118#endif
119#ifdef PORTF
120# define F0 PINDEF(F, 0)
121# define F1 PINDEF(F, 1)
122# define F2 PINDEF(F, 2)
123# define F3 PINDEF(F, 3)
124# define F4 PINDEF(F, 4)
125# define F5 PINDEF(F, 5)
126# define F6 PINDEF(F, 6)
127# define F7 PINDEF(F, 7)
128#endif
diff --git a/tmk_core/common/avr/platform.c b/tmk_core/common/avr/platform.c
deleted file mode 100644
index 3e35b4fe4..000000000
--- a/tmk_core/common/avr/platform.c
+++ /dev/null
@@ -1,21 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "platform_deps.h"
18
19void platform_setup(void) {
20 // do nothing
21}
diff --git a/tmk_core/common/avr/platform_deps.h b/tmk_core/common/avr/platform_deps.h
deleted file mode 100644
index 45d9dcebf..000000000
--- a/tmk_core/common/avr/platform_deps.h
+++ /dev/null
@@ -1,20 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <avr/pgmspace.h>
19#include <avr/io.h>
20#include <avr/interrupt.h>
diff --git a/tmk_core/common/avr/printf.c b/tmk_core/common/avr/printf.c
deleted file mode 100644
index 9ad7a3869..000000000
--- a/tmk_core/common/avr/printf.c
+++ /dev/null
@@ -1,20 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "xprintf.h"
18#include "sendchar.h"
19
20void print_set_sendchar(sendchar_func_t func) { xdev_out(func); }
diff --git a/tmk_core/common/avr/printf.mk b/tmk_core/common/avr/printf.mk
deleted file mode 100644
index 060ad88c5..000000000
--- a/tmk_core/common/avr/printf.mk
+++ /dev/null
@@ -1,2 +0,0 @@
1TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/xprintf.S
2TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c
diff --git a/tmk_core/common/avr/sleep_led.c b/tmk_core/common/avr/sleep_led.c
deleted file mode 100644
index 9a3b52abe..000000000
--- a/tmk_core/common/avr/sleep_led.c
+++ /dev/null
@@ -1,124 +0,0 @@
1#include <stdint.h>
2#include <avr/io.h>
3#include <avr/interrupt.h>
4#include <avr/pgmspace.h>
5#include "led.h"
6#include "sleep_led.h"
7
8#ifndef SLEEP_LED_TIMER
9# define SLEEP_LED_TIMER 1
10#endif
11
12#if SLEEP_LED_TIMER == 1
13# define TCCRxB TCCR1B
14# define TIMERx_COMPA_vect TIMER1_COMPA_vect
15# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
16# define TIMSKx TIMSK
17# else
18# define TIMSKx TIMSK1
19# endif
20# define OCIExA OCIE1A
21# define OCRxx OCR1A
22#elif SLEEP_LED_TIMER == 3
23# define TCCRxB TCCR3B
24# define TIMERx_COMPA_vect TIMER3_COMPA_vect
25# define TIMSKx TIMSK3
26# define OCIExA OCIE3A
27# define OCRxx OCR3A
28#else
29error("Invalid SLEEP_LED_TIMER config")
30#endif
31
32/* Software PWM
33 * ______ ______ __
34 * | ON |___OFF___| ON |___OFF___| ....
35 * |<-------------->|<-------------->|<- ....
36 * PWM period PWM period
37 *
38 * 256 interrupts/period[resolution]
39 * 64 periods/second[frequency]
40 * 256*64 interrupts/second
41 * F_CPU/(256*64) clocks/interrupt
42 */
43#define SLEEP_LED_TIMER_TOP F_CPU / (256 * 64)
44
45/** \brief Sleep LED initialization
46 *
47 * FIXME: needs doc
48 */
49void sleep_led_init(void) {
50 /* Timer1 setup */
51 /* CTC mode */
52 TCCRxB |= _BV(WGM12);
53 /* Clock selelct: clk/1 */
54 TCCRxB |= _BV(CS10);
55 /* Set TOP value */
56 uint8_t sreg = SREG;
57 cli();
58 OCRxx = SLEEP_LED_TIMER_TOP;
59 SREG = sreg;
60}
61
62/** \brief Sleep LED enable
63 *
64 * FIXME: needs doc
65 */
66void sleep_led_enable(void) {
67 /* Enable Compare Match Interrupt */
68 TIMSKx |= _BV(OCIExA);
69}
70
71/** \brief Sleep LED disable
72 *
73 * FIXME: needs doc
74 */
75void sleep_led_disable(void) {
76 /* Disable Compare Match Interrupt */
77 TIMSKx &= ~_BV(OCIExA);
78}
79
80/** \brief Sleep LED toggle
81 *
82 * FIXME: needs doc
83 */
84void sleep_led_toggle(void) {
85 /* Disable Compare Match Interrupt */
86 TIMSKx ^= _BV(OCIExA);
87}
88
89/** \brief Breathing Sleep LED brighness(PWM On period) table
90 *
91 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
92 *
93 * https://www.wolframalpha.com/input/?i=sin%28x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
94 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
95 */
96static const uint8_t breathing_table[64] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
97
98ISR(TIMERx_COMPA_vect) {
99 /* Software PWM
100 * timer:1111 1111 1111 1111
101 * \_____/\/ \_______/____ count(0-255)
102 * \ \______________ duration of step(4)
103 * \__________________ index of step table(0-63)
104 */
105 static union {
106 uint16_t row;
107 struct {
108 uint8_t count : 8;
109 uint8_t duration : 2;
110 uint8_t index : 6;
111 } pwm;
112 } timer = {.row = 0};
113
114 timer.row++;
115
116 // LED on
117 if (timer.pwm.count == 0) {
118 led_set(1 << USB_LED_CAPS_LOCK);
119 }
120 // LED off
121 if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) {
122 led_set(0);
123 }
124}
diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c
deleted file mode 100644
index b614746e6..000000000
--- a/tmk_core/common/avr/suspend.c
+++ /dev/null
@@ -1,152 +0,0 @@
1#include <stdbool.h>
2#include <avr/sleep.h>
3#include <avr/wdt.h>
4#include <avr/interrupt.h>
5#include "matrix.h"
6#include "action.h"
7#include "suspend.h"
8#include "timer.h"
9#include "led.h"
10#include "host.h"
11
12#ifdef PROTOCOL_LUFA
13# include "lufa.h"
14#endif
15#ifdef PROTOCOL_VUSB
16# include "vusb.h"
17#endif
18
19/** \brief Suspend idle
20 *
21 * FIXME: needs doc
22 */
23void suspend_idle(uint8_t time) {
24 cli();
25 set_sleep_mode(SLEEP_MODE_IDLE);
26 sleep_enable();
27 sei();
28 sleep_cpu();
29 sleep_disable();
30}
31
32// TODO: This needs some cleanup
33
34#if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect)
35
36// clang-format off
37#define wdt_intr_enable(value) \
38__asm__ __volatile__ ( \
39 "in __tmp_reg__,__SREG__" "\n\t" \
40 "cli" "\n\t" \
41 "wdr" "\n\t" \
42 "sts %0,%1" "\n\t" \
43 "out __SREG__,__tmp_reg__" "\n\t" \
44 "sts %0,%2" "\n\t" \
45 : /* no outputs */ \
46 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
47 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
48 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \
49 : "r0" \
50)
51// clang-format on
52
53/** \brief Power down MCU with watchdog timer
54 *
55 * wdto: watchdog timer timeout defined in <avr/wdt.h>
56 * WDTO_15MS
57 * WDTO_30MS
58 * WDTO_60MS
59 * WDTO_120MS
60 * WDTO_250MS
61 * WDTO_500MS
62 * WDTO_1S
63 * WDTO_2S
64 * WDTO_4S
65 * WDTO_8S
66 */
67static uint8_t wdt_timeout = 0;
68
69/** \brief Power down
70 *
71 * FIXME: needs doc
72 */
73static void power_down(uint8_t wdto) {
74 wdt_timeout = wdto;
75
76 // Watchdog Interrupt Mode
77 wdt_intr_enable(wdto);
78
79 // TODO: more power saving
80 // See PicoPower application note
81 // - I/O port input with pullup
82 // - prescale clock
83 // - BOD disable
84 // - Power Reduction Register PRR
85 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
86 sleep_enable();
87 sei();
88 sleep_cpu();
89 sleep_disable();
90
91 // Disable watchdog after sleep
92 wdt_disable();
93}
94#endif
95
96/** \brief Suspend power down
97 *
98 * FIXME: needs doc
99 */
100void suspend_power_down(void) {
101#ifdef PROTOCOL_LUFA
102 if (USB_DeviceState == DEVICE_STATE_Configured) return;
103#endif
104#ifdef PROTOCOL_VUSB
105 if (!vusb_suspended) return;
106#endif
107
108 suspend_power_down_quantum();
109
110#ifndef NO_SUSPEND_POWER_DOWN
111 // Enter sleep state if possible (ie, the MCU has a watchdog timeout interrupt)
112# if defined(WDT_vect)
113 power_down(WDTO_15MS);
114# endif
115#endif
116}
117
118__attribute__((weak)) void matrix_power_up(void) {}
119__attribute__((weak)) void matrix_power_down(void) {}
120bool suspend_wakeup_condition(void) {
121 matrix_power_up();
122 matrix_scan();
123 matrix_power_down();
124 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
125 if (matrix_get_row(r)) return true;
126 }
127 return false;
128}
129
130/** \brief run immediately after wakeup
131 *
132 * FIXME: needs doc
133 */
134void suspend_wakeup_init(void) {
135 // clear keyboard state
136 clear_keyboard();
137
138 suspend_wakeup_init_quantum();
139}
140
141#if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect)
142/* watchdog timeout */
143ISR(WDT_vect) {
144 // compensate timer for sleep
145 switch (wdt_timeout) {
146 case WDTO_15MS:
147 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)
148 break;
149 default:;
150 }
151}
152#endif
diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c
deleted file mode 100644
index c2e6c6e08..000000000
--- a/tmk_core/common/avr/timer.c
+++ /dev/null
@@ -1,133 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <avr/interrupt.h>
20#include <util/atomic.h>
21#include <stdint.h>
22#include "timer_avr.h"
23#include "timer.h"
24
25// counter resolution 1ms
26// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
27volatile uint32_t timer_count;
28
29/** \brief timer initialization
30 *
31 * FIXME: needs doc
32 */
33void timer_init(void) {
34#if TIMER_PRESCALER == 1
35 uint8_t prescaler = _BV(CS00);
36#elif TIMER_PRESCALER == 8
37 uint8_t prescaler = _BV(CS01);
38#elif TIMER_PRESCALER == 64
39 uint8_t prescaler = _BV(CS00) | _BV(CS01);
40#elif TIMER_PRESCALER == 256
41 uint8_t prescaler = _BV(CS02);
42#elif TIMER_PRESCALER == 1024
43 uint8_t prescaler = _BV(CS00) | _BV(CS02);
44#else
45# error "Timer prescaler value is not valid"
46#endif
47
48#if defined(__AVR_ATmega32A__)
49 // Timer0 CTC mode
50 TCCR0 = _BV(WGM01) | prescaler;
51
52 OCR0 = TIMER_RAW_TOP;
53 TIMSK = _BV(OCIE0);
54#elif defined(__AVR_ATtiny85__)
55 // Timer0 CTC mode
56 TCCR0A = _BV(WGM01);
57 TCCR0B = prescaler;
58
59 OCR0A = TIMER_RAW_TOP;
60 TIMSK = _BV(OCIE0A);
61#else
62 // Timer0 CTC mode
63 TCCR0A = _BV(WGM01);
64 TCCR0B = prescaler;
65
66 OCR0A = TIMER_RAW_TOP;
67 TIMSK0 = _BV(OCIE0A);
68#endif
69}
70
71/** \brief timer clear
72 *
73 * FIXME: needs doc
74 */
75inline void timer_clear(void) {
76 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; }
77}
78
79/** \brief timer read
80 *
81 * FIXME: needs doc
82 */
83inline uint16_t timer_read(void) {
84 uint32_t t;
85
86 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
87
88 return (t & 0xFFFF);
89}
90
91/** \brief timer read32
92 *
93 * FIXME: needs doc
94 */
95inline uint32_t timer_read32(void) {
96 uint32_t t;
97
98 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
99
100 return t;
101}
102
103/** \brief timer elapsed
104 *
105 * FIXME: needs doc
106 */
107inline uint16_t timer_elapsed(uint16_t last) {
108 uint32_t t;
109
110 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
111
112 return TIMER_DIFF_16((t & 0xFFFF), last);
113}
114
115/** \brief timer elapsed32
116 *
117 * FIXME: needs doc
118 */
119inline uint32_t timer_elapsed32(uint32_t last) {
120 uint32_t t;
121
122 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; }
123
124 return TIMER_DIFF_32(t, last);
125}
126
127// excecuted once per 1ms.(excess for just timer count?)
128#ifndef __AVR_ATmega32A__
129# define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
130#else
131# define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
132#endif
133ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; }
diff --git a/tmk_core/common/avr/timer_avr.h b/tmk_core/common/avr/timer_avr.h
deleted file mode 100644
index c1b726bd0..000000000
--- a/tmk_core/common/avr/timer_avr.h
+++ /dev/null
@@ -1,39 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20#include <stdint.h>
21
22#ifndef TIMER_PRESCALER
23# if F_CPU > 16000000
24# define TIMER_PRESCALER 256
25# elif F_CPU > 2000000
26# define TIMER_PRESCALER 64
27# elif F_CPU > 250000
28# define TIMER_PRESCALER 8
29# else
30# define TIMER_PRESCALER 1
31# endif
32#endif
33#define TIMER_RAW_FREQ (F_CPU / TIMER_PRESCALER)
34#define TIMER_RAW TCNT0
35#define TIMER_RAW_TOP (TIMER_RAW_FREQ / 1000)
36
37#if (TIMER_RAW_TOP > 255)
38# error "Timer0 can't count 1ms at this clock freq. Use larger prescaler."
39#endif
diff --git a/tmk_core/common/avr/xprintf.S b/tmk_core/common/avr/xprintf.S
deleted file mode 100644
index c5a414c35..000000000
--- a/tmk_core/common/avr/xprintf.S
+++ /dev/null
@@ -1,498 +0,0 @@
1;---------------------------------------------------------------------------;
2; Extended itoa, puts, printf and atoi (C)ChaN, 2011
3;---------------------------------------------------------------------------;
4
5 // Base size is 152 bytes
6#define CR_CRLF 0 // Convert \n to \r\n (+10 bytes)
7#define USE_XPRINTF 1 // Enable xprintf function (+194 bytes)
8#define USE_XSPRINTF 0 // Add xsprintf function (+78 bytes)
9#define USE_XFPRINTF 0 // Add xfprintf function (+54 bytes)
10#define USE_XATOI 0 // Enable xatoi function (+182 bytes)
11
12
13#if FLASHEND > 0x1FFFF
14#error xitoa module does not support 256K devices
15#endif
16
17.nolist
18#include <avr/io.h> // Include device specific definitions.
19.list
20
21#ifdef SPM_PAGESIZE // Recent devices have "lpm Rd,Z+" and "movw".
22.macro _LPMI reg
23 lpm \reg, Z+
24.endm
25.macro _MOVW dh,dl, sh,sl
26 movw \dl, \sl
27.endm
28#else // Earlier devices do not have "lpm Rd,Z+" nor "movw".
29.macro _LPMI reg
30 lpm
31 mov \reg, r0
32 adiw ZL, 1
33.endm
34.macro _MOVW dh,dl, sh,sl
35 mov \dl, \sl
36 mov \dh, \sh
37.endm
38#endif
39
40
41
42;---------------------------------------------------------------------------
43; Stub function to forward to user output function
44;
45;Prototype: void xputc (char chr // a character to be output
46; );
47;Size: 12/12 words
48
49.section .bss
50.global xfunc_out ; xfunc_out must be initialized before using this module.
51xfunc_out: .ds.w 1
52.section .text
53
54
55.func xputc
56.global xputc
57xputc:
58#if CR_CRLF
59 cpi r24, 10 ;LF --> CRLF
60 brne 1f ;
61 ldi r24, 13 ;
62 rcall 1f ;
63 ldi r24, 10 ;/
641:
65#endif
66 push ZH
67 push ZL
68 lds ZL, xfunc_out+0 ;Pointer to the registered output function.
69 lds ZH, xfunc_out+1 ;/
70 sbiw ZL, 0 ;Skip if null
71 breq 2f ;/
72 icall
732: pop ZL
74 pop ZH
75 ret
76.endfunc
77
78
79
80;---------------------------------------------------------------------------
81; Direct ROM string output
82;
83;Prototype: void xputs (const char *str_p // rom string to be output
84; );
85
86.func xputs
87.global xputs
88xputs:
89 _MOVW ZH,ZL, r25,r24 ; Z = pointer to rom string
901: _LPMI r24
91 cpi r24, 0
92 breq 2f
93 rcall xputc
94 rjmp 1b
952: ret
96.endfunc
97
98
99;---------------------------------------------------------------------------
100; Extended direct numeral string output (32bit version)
101;
102;Prototype: void xitoa (long value, // value to be output
103; char radix, // radix
104; char width); // minimum width
105;
106
107.func xitoa
108.global xitoa
109xitoa:
110 ;r25:r22 = value, r20 = base, r18 = digits
111 clr r31 ;r31 = stack level
112 ldi r30, ' ' ;r30 = sign
113 ldi r19, ' ' ;r19 = filler
114 sbrs r20, 7 ;When base indicates signd format and the value
115 rjmp 0f ;is minus, add a '-'.
116 neg r20 ;
117 sbrs r25, 7 ;
118 rjmp 0f ;
119 ldi r30, '-' ;
120 com r22 ;
121 com r23 ;
122 com r24 ;
123 com r25 ;
124 adc r22, r1 ;
125 adc r23, r1 ;
126 adc r24, r1 ;
127 adc r25, r1 ;/
1280: sbrs r18, 7 ;When digits indicates zero filled,
129 rjmp 1f ;filler is '0'.
130 neg r18 ;
131 ldi r19, '0' ;/
132 ;----- string conversion loop
1331: ldi r21, 32 ;r26 = r25:r22 % r20
134 clr r26 ;r25:r22 /= r20
1352: lsl r22 ;
136 rol r23 ;
137 rol r24 ;
138 rol r25 ;
139 rol r26 ;
140 cp r26, r20 ;
141 brcs 3f ;
142 sub r26, r20 ;
143 inc r22 ;
1443: dec r21 ;
145 brne 2b ;/
146 cpi r26, 10 ;r26 is a numeral digit '0'-'F'
147 brcs 4f ;
148 subi r26, -7 ;
1494: subi r26, -'0' ;/
150 push r26 ;Stack it
151 inc r31 ;/
152 cp r22, r1 ;Repeat until r25:r22 gets zero
153 cpc r23, r1 ;
154 cpc r24, r1 ;
155 cpc r25, r1 ;
156 brne 1b ;/
157
158 cpi r30, '-' ;Minus sign if needed
159 brne 5f ;
160 push r30 ;
161 inc r31 ;/
1625: cp r31, r18 ;Filler
163 brcc 6f ;
164 push r19 ;
165 inc r31 ;
166 rjmp 5b ;/
167
1686: pop r24 ;Flush stacked digits and exit
169 rcall xputc ;
170 dec r31 ;
171 brne 6b ;/
172
173 ret
174.endfunc
175
176
177
178;---------------------------------------------------------------------------;
179; Formatted string output (16/32bit version)
180;
181;Prototype:
182; void __xprintf (const char *format_p, ...);
183; void __xsprintf(char*, const char *format_p, ...);
184; void __xfprintf(void(*func)(char), const char *format_p, ...);
185;
186
187#if USE_XPRINTF
188
189.func xvprintf
190xvprintf:
191 ld ZL, Y+ ;Z = pointer to format string
192 ld ZH, Y+ ;/
193
1940: _LPMI r24 ;Get a format char
195 cpi r24, 0 ;End of format string?
196 breq 90f ;/
197 cpi r24, '%' ;Is format?
198 breq 20f ;/
1991: rcall xputc ;Put a normal character
200 rjmp 0b ;/
20190: ret
202
20320: ldi r18, 0 ;r18: digits
204 clt ;T: filler
205 _LPMI r21 ;Get flags
206 cpi r21, '%' ;Is a %?
207 breq 1b ;/
208 cpi r21, '0' ;Zero filled?
209 brne 23f ;
210 set ;/
21122: _LPMI r21 ;Get width
21223: cpi r21, '9'+1 ;
213 brcc 24f ;
214 subi r21, '0' ;
215 brcs 90b ;
216 lsl r18 ;
217 mov r0, r18 ;
218 lsl r18 ;
219 lsl r18 ;
220 add r18, r0 ;
221 add r18, r21 ;
222 rjmp 22b ;/
223
22424: brtc 25f ;get value (low word)
225 neg r18 ;
22625: ld r24, Y+ ;
227 ld r25, Y+ ;/
228 cpi r21, 'c' ;Is type character?
229 breq 1b ;/
230 cpi r21, 's' ;Is type RAM string?
231 breq 50f ;/
232 cpi r21, 'S' ;Is type ROM string?
233 breq 60f ;/
234 _MOVW r23,r22,r25,r24 ;r25:r22 = value
235 clr r24 ;
236 clr r25 ;
237 clt ;/
238 cpi r21, 'l' ;Is long int?
239 brne 26f ;
240 ld r24, Y+ ;get value (high word)
241 ld r25, Y+ ;
242 set ;
243 _LPMI r21 ;/
24426: cpi r21, 'd' ;Is type signed decimal?
245 brne 27f ;/
246 ldi r20, -10 ;
247 brts 40f ;
248 sbrs r23, 7 ;
249 rjmp 40f ;
250 ldi r24, -1 ;
251 ldi r25, -1 ;
252 rjmp 40f ;/
25327: cpi r21, 'u' ;Is type unsigned decimal?
254 ldi r20, 10 ;
255 breq 40f ;/
256 cpi r21, 'X' ;Is type hexdecimal?
257 ldi r20, 16 ;
258 breq 40f ;/
259 cpi r21, 'b' ;Is type binary?
260 ldi r20, 2 ;
261 breq 40f ;/
262 ret ;abort
26340: push ZH ;Output the value
264 push ZL ;
265 rcall xitoa ;
26642: pop ZL ;
267 pop ZH ;
268 rjmp 0b ;/
269
27050: push ZH ;Put a string on the RAM
271 push ZL
272 _MOVW ZH,ZL, r25,r24
27351: ld r24, Z+
274 cpi r24, 0
275 breq 42b
276 rcall xputc
277 rjmp 51b
278
27960: push ZH ;Put a string on the ROM
280 push ZL
281 rcall xputs
282 rjmp 42b
283.endfunc
284
285
286.func __xprintf
287.global __xprintf
288__xprintf:
289 push YH
290 push YL
291 in YL, _SFR_IO_ADDR(SPL)
292#ifdef SPH
293 in YH, _SFR_IO_ADDR(SPH)
294#else
295 clr YH
296#endif
297 adiw YL, 5 ;Y = pointer to arguments
298 rcall xvprintf
299 pop YL
300 pop YH
301 ret
302.endfunc
303
304
305#if USE_XSPRINTF
306
307.func __xsprintf
308putram:
309 _MOVW ZH,ZL, r15,r14
310 st Z+, r24
311 _MOVW r15,r14, ZH,ZL
312 ret
313.global __xsprintf
314__xsprintf:
315 push YH
316 push YL
317 in YL, _SFR_IO_ADDR(SPL)
318#ifdef SPH
319 in YH, _SFR_IO_ADDR(SPH)
320#else
321 clr YH
322#endif
323 adiw YL, 5 ;Y = pointer to arguments
324 lds ZL, xfunc_out+0 ;Save registered output function
325 lds ZH, xfunc_out+1 ;
326 push ZL ;
327 push ZH ;/
328 ldi ZL, lo8(pm(putram));Set local output function
329 ldi ZH, hi8(pm(putram));
330 sts xfunc_out+0, ZL ;
331 sts xfunc_out+1, ZH ;/
332 push r15 ;Initialize pointer to string buffer
333 push r14 ;
334 ld r14, Y+ ;
335 ld r15, Y+ ;/
336 rcall xvprintf
337 _MOVW ZH,ZL, r15,r14 ;Terminate string
338 st Z, r1 ;
339 pop r14 ;
340 pop r15 ;/
341 pop ZH ;Restore registered output function
342 pop ZL ;
343 sts xfunc_out+0, ZL ;
344 sts xfunc_out+1, ZH ;/
345 pop YL
346 pop YH
347 ret
348.endfunc
349#endif
350
351
352#if USE_XFPRINTF
353.func __xfprintf
354.global __xfprintf
355__xfprintf:
356 push YH
357 push YL
358 in YL, _SFR_IO_ADDR(SPL)
359#ifdef SPH
360 in YH, _SFR_IO_ADDR(SPH)
361#else
362 clr YH
363#endif
364 adiw YL, 5 ;Y = pointer to arguments
365 lds ZL, xfunc_out+0 ;Save registered output function
366 lds ZH, xfunc_out+1 ;
367 push ZL ;
368 push ZH ;/
369 ld ZL, Y+ ;Set output function
370 ld ZH, Y+ ;
371 sts xfunc_out+0, ZL ;
372 sts xfunc_out+1, ZH ;/
373 rcall xvprintf
374 pop ZH ;Restore registered output function
375 pop ZL ;
376 sts xfunc_out+0, ZL ;
377 sts xfunc_out+1, ZH ;/
378 pop YL
379 pop YH
380 ret
381.endfunc
382#endif
383
384#endif
385
386
387
388;---------------------------------------------------------------------------
389; Extended numeral string input
390;
391;Prototype:
392; char xatoi ( /* 1: Successful, 0: Failed */
393; const char **str, /* pointer to pointer to source string */
394; long *res /* result */
395; );
396;
397
398
399#if USE_XATOI
400.func xatoi
401.global xatoi
402xatoi:
403 _MOVW r1, r0, r23, r22
404 _MOVW XH, XL, r25, r24
405 ld ZL, X+
406 ld ZH, X+
407 clr r18 ;r21:r18 = 0;
408 clr r19 ;
409 clr r20 ;
410 clr r21 ;/
411 clt ;T = 0;
412
413 ldi r25, 10 ;r25 = 10;
414 rjmp 41f ;/
41540: adiw ZL, 1 ;Z++;
41641: ld r22, Z ;r22 = *Z;
417 cpi r22, ' ' ;if(r22 == ' ') continue
418 breq 40b ;/
419 brcs 70f ;if(r22 < ' ') error;
420 cpi r22, '-' ;if(r22 == '-') {
421 brne 42f ; T = 1;
422 set ; continue;
423 rjmp 40b ;}
42442: cpi r22, '9'+1 ;if(r22 > '9') error;
425 brcc 70f ;/
426 cpi r22, '0' ;if(r22 < '0') error;
427 brcs 70f ;/
428 brne 51f ;if(r22 > '0') cv_start;
429 ldi r25, 8 ;r25 = 8;
430 adiw ZL, 1 ;r22 = *(++Z);
431 ld r22, Z ;/
432 cpi r22, ' '+1 ;if(r22 <= ' ') exit;
433 brcs 80f ;/
434 cpi r22, 'b' ;if(r22 == 'b') {
435 brne 43f ; r25 = 2;
436 ldi r25, 2 ; cv_start;
437 rjmp 50f ;}
43843: cpi r22, 'x' ;if(r22 != 'x') error;
439 brne 51f ;/
440 ldi r25, 16 ;r25 = 16;
441
44250: adiw ZL, 1 ;Z++;
443 ld r22, Z ;r22 = *Z;
44451: cpi r22, ' '+1 ;if(r22 <= ' ') break;
445 brcs 80f ;/
446 cpi r22, 'a' ;if(r22 >= 'a') r22 =- 0x20;
447 brcs 52f ;
448 subi r22, 0x20 ;/
44952: subi r22, '0' ;if((r22 -= '0') < 0) error;
450 brcs 70f ;/
451 cpi r22, 10 ;if(r22 >= 10) {
452 brcs 53f ; r22 -= 7;
453 subi r22, 7 ; if(r22 < 10)
454 cpi r22, 10 ;
455 brcs 70f ;}
45653: cp r22, r25 ;if(r22 >= r25) error;
457 brcc 70f ;/
45860: ldi r24, 33 ;r21:r18 *= r25;
459 sub r23, r23 ;
46061: brcc 62f ;
461 add r23, r25 ;
46262: lsr r23 ;
463 ror r21 ;
464 ror r20 ;
465 ror r19 ;
466 ror r18 ;
467 dec r24 ;
468 brne 61b ;/
469 add r18, r22 ;r21:r18 += r22;
470 adc r19, r24 ;
471 adc r20, r24 ;
472 adc r21, r24 ;/
473 rjmp 50b ;repeat
474
47570: ldi r24, 0
476 rjmp 81f
47780: ldi r24, 1
47881: brtc 82f
479 clr r22
480 com r18
481 com r19
482 com r20
483 com r21
484 adc r18, r22
485 adc r19, r22
486 adc r20, r22
487 adc r21, r22
48882: st -X, ZH
489 st -X, ZL
490 _MOVW XH, XL, r1, r0
491 st X+, r18
492 st X+, r19
493 st X+, r20
494 st X+, r21
495 clr r1
496 ret
497.endfunc
498#endif
diff --git a/tmk_core/common/avr/xprintf.h b/tmk_core/common/avr/xprintf.h
deleted file mode 100644
index 80834f171..000000000
--- a/tmk_core/common/avr/xprintf.h
+++ /dev/null
@@ -1,103 +0,0 @@
1/*---------------------------------------------------------------------------
2 Extended itoa, puts and printf (C)ChaN, 2011
3-----------------------------------------------------------------------------*/
4
5#pragma once
6
7#include <inttypes.h>
8#include <avr/pgmspace.h>
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14extern void (*xfunc_out)(uint8_t);
15#define xdev_out(func) xfunc_out = (void (*)(uint8_t))(func)
16
17/* This is a pointer to user defined output function. It must be initialized
18 before using this modle.
19*/
20
21void xputc(char chr);
22
23/* This is a stub function to forward outputs to user defined output function.
24 All outputs from this module are output via this function.
25*/
26
27/*-----------------------------------------------------------------------------*/
28void xputs(const char *string_p);
29
30/* The string placed in the ROM is forwarded to xputc() directly.
31 */
32
33/*-----------------------------------------------------------------------------*/
34void xitoa(long value, char radix, char width);
35
36/* Extended itoa().
37
38 value radix width output
39 100 10 6 " 100"
40 100 10 -6 "000100"
41 100 10 0 "100"
42 4294967295 10 0 "4294967295"
43 4294967295 -10 0 "-1"
44 655360 16 -8 "000A0000"
45 1024 16 0 "400"
46 0x55 2 -8 "01010101"
47*/
48
49/*-----------------------------------------------------------------------------*/
50#define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__)
51#define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__)
52#define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__)
53
54void __xprintf(const char *format_p, ...); /* Send formatted string to the registered device */
55// void __xsprintf(char*, const char *format_p, ...); /* Put formatted string to the memory */
56// void __xfprintf(void(*func)(uint8_t), const char *format_p, ...); /* Send formatted string to the specified device */
57
58/* Format string is placed in the ROM. The format flags is similar to printf().
59
60 %[flag][width][size]type
61
62 flag
63 A '0' means filled with '0' when output is shorter than width.
64 ' ' is used in default. This is effective only numeral type.
65 width
66 Minimum width in decimal number. This is effective only numeral type.
67 Default width is zero.
68 size
69 A 'l' means the argument is long(32bit). Default is short(16bit).
70 This is effective only numeral type.
71 type
72 'c' : Character, argument is the value
73 's' : String placed on the RAM, argument is the pointer
74 'S' : String placed on the ROM, argument is the pointer
75 'd' : Signed decimal, argument is the value
76 'u' : Unsigned decimal, argument is the value
77 'X' : Hexdecimal, argument is the value
78 'b' : Binary, argument is the value
79 '%' : '%'
80
81*/
82
83/*-----------------------------------------------------------------------------*/
84char xatoi(char **str, long *ret);
85
86/* Get value of the numeral string.
87
88 str
89 Pointer to pointer to source string
90
91 "0b11001010" binary
92 "0377" octal
93 "0xff800" hexdecimal
94 "1250000" decimal
95 "-25000" decimal
96
97 ret
98 Pointer to return value
99*/
100
101#ifdef __cplusplus
102}
103#endif
diff --git a/tmk_core/common/bootloader.h b/tmk_core/common/bootloader.h
deleted file mode 100644
index 25ebd9528..000000000
--- a/tmk_core/common/bootloader.h
+++ /dev/null
@@ -1,21 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20/* give code for your bootloader to come up if needed */
21void bootloader_jump(void);
diff --git a/tmk_core/common/chibios/_timer.h b/tmk_core/common/chibios/_timer.h
deleted file mode 100644
index 77402b612..000000000
--- a/tmk_core/common/chibios/_timer.h
+++ /dev/null
@@ -1,19 +0,0 @@
1/* Copyright 2021 Simon Arlott
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// The platform is 32-bit, so prefer 32-bit timers to avoid overflow
19#define FAST_TIMER_T_SIZE 32
diff --git a/tmk_core/common/chibios/_wait.c b/tmk_core/common/chibios/_wait.c
deleted file mode 100644
index 1fbea2dd5..000000000
--- a/tmk_core/common/chibios/_wait.c
+++ /dev/null
@@ -1,89 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef __OPTIMIZE__
18# pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed"
19#endif
20
21#define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t"
22
23__attribute__((always_inline)) static inline void wait_cpuclock(unsigned int n) { /* n: 1..135 */
24 /* The argument n must be a constant expression.
25 * That way, compiler optimization will remove unnecessary code. */
26 if (n < 1) {
27 return;
28 }
29 if (n > 8) {
30 unsigned int n8 = n / 8;
31 n = n - n8 * 8;
32 switch (n8) {
33 case 16:
34 asm volatile(CLOCK_DELAY_NOP8::: "memory");
35 case 15:
36 asm volatile(CLOCK_DELAY_NOP8::: "memory");
37 case 14:
38 asm volatile(CLOCK_DELAY_NOP8::: "memory");
39 case 13:
40 asm volatile(CLOCK_DELAY_NOP8::: "memory");
41 case 12:
42 asm volatile(CLOCK_DELAY_NOP8::: "memory");
43 case 11:
44 asm volatile(CLOCK_DELAY_NOP8::: "memory");
45 case 10:
46 asm volatile(CLOCK_DELAY_NOP8::: "memory");
47 case 9:
48 asm volatile(CLOCK_DELAY_NOP8::: "memory");
49 case 8:
50 asm volatile(CLOCK_DELAY_NOP8::: "memory");
51 case 7:
52 asm volatile(CLOCK_DELAY_NOP8::: "memory");
53 case 6:
54 asm volatile(CLOCK_DELAY_NOP8::: "memory");
55 case 5:
56 asm volatile(CLOCK_DELAY_NOP8::: "memory");
57 case 4:
58 asm volatile(CLOCK_DELAY_NOP8::: "memory");
59 case 3:
60 asm volatile(CLOCK_DELAY_NOP8::: "memory");
61 case 2:
62 asm volatile(CLOCK_DELAY_NOP8::: "memory");
63 case 1:
64 asm volatile(CLOCK_DELAY_NOP8::: "memory");
65 case 0:
66 break;
67 }
68 }
69 switch (n) {
70 case 8:
71 asm volatile("nop" ::: "memory");
72 case 7:
73 asm volatile("nop" ::: "memory");
74 case 6:
75 asm volatile("nop" ::: "memory");
76 case 5:
77 asm volatile("nop" ::: "memory");
78 case 4:
79 asm volatile("nop" ::: "memory");
80 case 3:
81 asm volatile("nop" ::: "memory");
82 case 2:
83 asm volatile("nop" ::: "memory");
84 case 1:
85 asm volatile("nop" ::: "memory");
86 case 0:
87 break;
88 }
89}
diff --git a/tmk_core/common/chibios/_wait.h b/tmk_core/common/chibios/_wait.h
deleted file mode 100644
index 2f36c64a2..000000000
--- a/tmk_core/common/chibios/_wait.h
+++ /dev/null
@@ -1,60 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <ch.h>
19#include <hal.h>
20
21/* chThdSleepX of zero maps to infinite - so we map to a tiny delay to still yield */
22#define wait_ms(ms) \
23 do { \
24 if (ms != 0) { \
25 chThdSleepMilliseconds(ms); \
26 } else { \
27 chThdSleepMicroseconds(1); \
28 } \
29 } while (0)
30
31#ifdef WAIT_US_TIMER
32void wait_us(uint16_t duration);
33#else
34# define wait_us(us) \
35 do { \
36 if (us != 0) { \
37 chThdSleepMicroseconds(us); \
38 } else { \
39 chThdSleepMicroseconds(1); \
40 } \
41 } while (0)
42#endif
43
44#include "_wait.c"
45
46/* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus
47 * to which the GPIO is connected.
48 * The connected buses differ depending on the various series of MCUs.
49 * And since the instruction execution clock of the CPU and the bus clock of GPIO are different,
50 * there is a delay of several clocks to read the change of the input signal.
51 *
52 * Define this delay with the GPIO_INPUT_PIN_DELAY macro.
53 * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used.
54 * (A fairly large value of 0.25 microseconds is set.)
55 */
56#ifndef GPIO_INPUT_PIN_DELAY
57# define GPIO_INPUT_PIN_DELAY (CPU_CLOCK / 1000000L / 4)
58#endif
59
60#define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
diff --git a/tmk_core/common/chibios/atomic_util.h b/tmk_core/common/chibios/atomic_util.h
deleted file mode 100644
index 897504515..000000000
--- a/tmk_core/common/chibios/atomic_util.h
+++ /dev/null
@@ -1,37 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <ch.h>
19
20static __inline__ uint8_t __interrupt_disable__(void) {
21 chSysLock();
22
23 return 1;
24}
25
26static __inline__ void __interrupt_enable__(const uint8_t *__s) {
27 chSysUnlock();
28
29 __asm__ volatile("" ::: "memory");
30 (void)__s;
31}
32
33#define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0)
34#define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0
35
36#define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented")
37#define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
diff --git a/tmk_core/common/chibios/bootloader.c b/tmk_core/common/chibios/bootloader.c
deleted file mode 100644
index 5cadadeee..000000000
--- a/tmk_core/common/chibios/bootloader.c
+++ /dev/null
@@ -1,145 +0,0 @@
1#include "bootloader.h"
2
3#include <ch.h>
4#include <hal.h>
5#include "wait.h"
6
7/* This code should be checked whether it runs correctly on platforms */
8#define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
9#define BOOTLOADER_MAGIC 0xDEADBEEF
10#define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4)
11
12#ifndef STM32_BOOTLOADER_DUAL_BANK
13# define STM32_BOOTLOADER_DUAL_BANK FALSE
14#endif
15
16#ifdef BOOTLOADER_TINYUF2
17
18# define DBL_TAP_MAGIC 0xf01669ef // From tinyuf2's board_api.h
19
20// defined by linker script
21extern uint32_t _board_dfu_dbl_tap[];
22# define DBL_TAP_REG _board_dfu_dbl_tap[0]
23
24void bootloader_jump(void) {
25 DBL_TAP_REG = DBL_TAP_MAGIC;
26 NVIC_SystemReset();
27}
28
29void enter_bootloader_mode_if_requested(void) { /* not needed, no two-stage reset */
30}
31
32#elif STM32_BOOTLOADER_DUAL_BANK
33
34// Need pin definitions
35# include "config_common.h"
36
37# ifndef STM32_BOOTLOADER_DUAL_BANK_GPIO
38# error "No STM32_BOOTLOADER_DUAL_BANK_GPIO defined, don't know which pin to toggle"
39# endif
40
41# ifndef STM32_BOOTLOADER_DUAL_BANK_POLARITY
42# define STM32_BOOTLOADER_DUAL_BANK_POLARITY 0
43# endif
44
45# ifndef STM32_BOOTLOADER_DUAL_BANK_DELAY
46# define STM32_BOOTLOADER_DUAL_BANK_DELAY 100000
47# endif
48
49extern uint32_t __ram0_end__;
50
51__attribute__((weak)) void bootloader_jump(void) {
52 // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash
53 // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do
54 // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to
55 // BOOT0's RC charging circuit, lets it charge the capacitor, and issue a system reset. See the QMK discord
56 // #hardware channel pins for an example circuit.
57 palSetPadMode(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_MODE_OUTPUT_PUSHPULL);
58# if STM32_BOOTLOADER_DUAL_BANK_POLARITY
59 palSetPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
60# else
61 palClearPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
62# endif
63
64 // Wait for a while for the capacitor to charge
65 wait_ms(100);
66
67 // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high
68 NVIC_SystemReset();
69}
70
71void enter_bootloader_mode_if_requested(void) {} // not needed at all, but if anybody attempts to invoke it....
72
73#elif defined(STM32_BOOTLOADER_ADDRESS) // STM32_BOOTLOADER_DUAL_BANK
74
75extern uint32_t __ram0_end__;
76
77__attribute__((weak)) void bootloader_jump(void) {
78 *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
79 NVIC_SystemReset();
80}
81
82void enter_bootloader_mode_if_requested(void) {
83 unsigned long *check = MAGIC_ADDR;
84 if (*check == BOOTLOADER_MAGIC) {
85 *check = 0;
86 __set_CONTROL(0);
87 __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS);
88 __enable_irq();
89
90 typedef void (*BootJump_t)(void);
91 BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4);
92 boot_jump();
93 while (1)
94 ;
95 }
96}
97
98#elif defined(GD32VF103)
99
100# define DBGMCU_KEY_UNLOCK 0x4B5A6978
101# define DBGMCU_CMD_RESET 0x1
102
103__IO uint32_t *DBGMCU_KEY = (uint32_t *)DBGMCU_BASE + 0x0CU;
104__IO uint32_t *DBGMCU_CMD = (uint32_t *)DBGMCU_BASE + 0x08U;
105
106__attribute__((weak)) void bootloader_jump(void) {
107 /* The MTIMER unit of the GD32VF103 doesn't have the MSFRST
108 * register to generate a software reset request.
109 * BUT instead two undocumented registers in the debug peripheral
110 * that allow issueing a software reset. WHO would need the MSFRST
111 * register anyway? Source:
112 * https://github.com/esmil/gd32vf103inator/blob/master/include/gd32vf103/dbg.h */
113 *DBGMCU_KEY = DBGMCU_KEY_UNLOCK;
114 *DBGMCU_CMD = DBGMCU_CMD_RESET;
115}
116
117void enter_bootloader_mode_if_requested(void) { /* Jumping to bootloader is not possible from user code. */
118}
119
120#elif defined(KL2x) || defined(K20x) || defined(MK66F18) || defined(MIMXRT1062) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS
121/* Kinetis */
122
123# if defined(BOOTLOADER_KIIBOHD)
124/* Kiibohd Bootloader (MCHCK and Infinity KB) */
125# define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000
126const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
127__attribute__((weak)) void bootloader_jump(void) {
128 void *volatile vbat = (void *)VBAT;
129 __builtin_memcpy(vbat, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic));
130 // request reset
131 SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk;
132}
133
134# else /* defined(BOOTLOADER_KIIBOHD) */
135/* Default for Kinetis - expecting an ARM Teensy */
136# include "wait.h"
137__attribute__((weak)) void bootloader_jump(void) {
138 wait_ms(100);
139 __BKPT(0);
140}
141# endif /* defined(BOOTLOADER_KIIBOHD) */
142
143#else /* neither STM32 nor KINETIS */
144__attribute__((weak)) void bootloader_jump(void) {}
145#endif
diff --git a/tmk_core/common/chibios/chibios_config.h b/tmk_core/common/chibios/chibios_config.h
deleted file mode 100644
index ad2f808a9..000000000
--- a/tmk_core/common/chibios/chibios_config.h
+++ /dev/null
@@ -1,78 +0,0 @@
1/* Copyright 2019
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#ifndef USB_VBUS_PIN
19# define SPLIT_USB_DETECT // Force this on when dedicated pin is not used
20#endif
21
22// STM32 compatibility
23#if defined(MCU_STM32)
24# define CPU_CLOCK STM32_SYSCLK
25
26# if defined(STM32F1XX)
27# define USE_GPIOV1
28# define PAL_MODE_ALTERNATE_OPENDRAIN PAL_MODE_STM32_ALTERNATE_OPENDRAIN
29# define PAL_MODE_ALTERNATE_PUSHPULL PAL_MODE_STM32_ALTERNATE_PUSHPULL
30# else
31# define PAL_OUTPUT_TYPE_OPENDRAIN PAL_STM32_OTYPE_OPENDRAIN
32# define PAL_OUTPUT_TYPE_PUSHPULL PAL_STM32_OTYPE_PUSHPULL
33# define PAL_OUTPUT_SPEED_HIGHEST PAL_STM32_OSPEED_HIGHEST
34# define PAL_PUPDR_FLOATING PAL_STM32_PUPDR_FLOATING
35# endif
36
37# if defined(STM32F1XX) || defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32L1XX)
38# define USE_I2CV1
39# endif
40#endif
41
42// GD32 compatibility
43#if defined(MCU_GD32V)
44# define CPU_CLOCK GD32_SYSCLK
45
46# if defined(GD32VF103)
47# define USE_GPIOV1
48# define USE_I2CV1
49# define PAL_MODE_ALTERNATE_OPENDRAIN PAL_MODE_GD32_ALTERNATE_OPENDRAIN
50# define PAL_MODE_ALTERNATE_PUSHPULL PAL_MODE_GD32_ALTERNATE_PUSHPULL
51# endif
52#endif
53
54#if defined(GD32VF103)
55/* This chip has the same API as STM32F103, but uses different names for literally the same thing.
56 * As of 4.7.2021 QMK is tailored to use STM32 defines/names, for compatibility sake
57 * we just redefine the GD32 names. */
58# include "gd32v_compatibility.h"
59#endif
60
61// teensy compatibility
62#if defined(MCU_KINETIS)
63# define CPU_CLOCK KINETIS_SYSCLK_FREQUENCY
64
65# if defined(K20x) || defined(KL2x)
66# define USE_I2CV1
67# define USE_I2CV1_CONTRIB // for some reason a bunch of ChibiOS-Contrib boards only have clock_speed
68# define USE_GPIOV1
69# endif
70#endif
71
72#if defined(HT32)
73# define CPU_CLOCK HT32_CK_SYS_FREQUENCY
74# define PAL_MODE_ALTERNATE PAL_HT32_MODE_AF
75# define PAL_OUTPUT_TYPE_OPENDRAIN (PAL_HT32_MODE_OD | PAL_HT32_MODE_DIR)
76# define PAL_OUTPUT_TYPE_PUSHPULL PAL_HT32_MODE_DIR
77# define PAL_OUTPUT_SPEED_HIGHEST 0
78#endif
diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c
deleted file mode 100644
index acc6a4851..000000000
--- a/tmk_core/common/chibios/eeprom_stm32.c
+++ /dev/null
@@ -1,687 +0,0 @@
1/*
2 * This software is experimental and a work in progress.
3 * Under no circumstances should these files be used in relation to any critical system(s).
4 * Use of these files is at your own risk.
5 *
6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
7 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
8 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
9 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
11 * DEALINGS IN THE SOFTWARE.
12 *
13 * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by
14 * Artur F.
15 *
16 * Modifications for QMK and STM32F303 by Yiancar
17 * Modifications to add flash wear leveling by Ilya Zhuravlev
18 * Modifications to increase flash density by Don Kjer
19 */
20
21#include <stdio.h>
22#include <stdbool.h>
23#include "util.h"
24#include "debug.h"
25#include "eeprom_stm32.h"
26#include "flash_stm32.h"
27
28/*
29 * We emulate eeprom by writing a snapshot compacted view of eeprom contents,
30 * followed by a write log of any change since that snapshot:
31 *
32 * === SIMULATED EEPROM CONTENTS ===
33 *
34 * ┌─ Compacted ┬ Write Log ─┐
35 * │............│[BYTE][BYTE]│
36 * │FFFF....FFFF│[WRD0][WRD1]│
37 * │FFFFFFFFFFFF│[WORD][NEXT]│
38 * │....FFFFFFFF│[BYTE][WRD0]│
39 * ├────────────┼────────────┤
40 * └──PAGE_BASE │ │
41 * PAGE_LAST─┴─WRITE_BASE │
42 * WRITE_LAST ┘
43 *
44 * Compacted contents are the 1's complement of the actual EEPROM contents.
45 * e.g. An 'FFFF' represents a '0000' value.
46 *
47 * The size of the 'compacted' area is equal to the size of the 'emulated' eeprom.
48 * The size of the compacted-area and write log are configurable, and the combined
49 * size of Compacted + WriteLog is a multiple FEE_PAGE_SIZE, which is MCU dependent.
50 * Simulated Eeprom contents are located at the end of available flash space.
51 *
52 * The following configuration defines can be set:
53 *
54 * FEE_PAGE_COUNT # Total number of pages to use for eeprom simulation (Compact + Write log)
55 * FEE_DENSITY_BYTES # Size of simulated eeprom. (Defaults to half the space allocated by FEE_PAGE_COUNT)
56 * NOTE: The current implementation does not include page swapping,
57 * and FEE_DENSITY_BYTES will consume that amount of RAM as a cached view of actual EEPROM contents.
58 *
59 * The maximum size of FEE_DENSITY_BYTES is currently 16384. The write log size equals
60 * FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES.
61 * The larger the write log, the less frequently the compacted area needs to be rewritten.
62 *
63 *
64 * *** General Algorithm ***
65 *
66 * During initialization:
67 * The contents of the Compacted-flash area are loaded and the 1's complement value
68 * is cached into memory (e.g. 0xFFFF in Flash represents 0x0000 in cache).
69 * Write log entries are processed until a 0xFFFF is reached.
70 * Each log entry updates a byte or word in the cache.
71 *
72 * During reads:
73 * EEPROM contents are given back directly from the cache in memory.
74 *
75 * During writes:
76 * The contents of the cache is updated first.
77 * If the Compacted-flash area corresponding to the write address is unprogrammed, the 1's complement of the value is written directly into Compacted-flash
78 * Otherwise:
79 * If the write log is full, erase both the Compacted-flash area and the Write log, then write cached contents to the Compacted-flash area.
80 * Otherwise a Write log entry is constructed and appended to the next free position in the Write log.
81 *
82 *
83 * *** Write Log Structure ***
84 *
85 * Write log entries allow for optimized byte writes to addresses below 128. Writing 0 or 1 words are also optimized when word-aligned.
86 *
87 * === WRITE LOG ENTRY FORMATS ===
88 *
89 * ╔═══ Byte-Entry ══╗
90 * ║0XXXXXXX║YYYYYYYY║
91 * ║ └──┬──┘║└──┬───┘║
92 * ║ Address║ Value ║
93 * ╚════════╩════════╝
94 * 0 <= Address < 0x80 (128)
95 *
96 * ╔ Word-Encoded 0 ╗
97 * ║100XXXXXXXXXXXXX║
98 * ║ │└─────┬─────┘║
99 * ║ │Address >> 1 ║
100 * ║ └── Value: 0 ║
101 * ╚════════════════╝
102 * 0 <= Address <= 0x3FFE (16382)
103 *
104 * ╔ Word-Encoded 1 ╗
105 * ║101XXXXXXXXXXXXX║
106 * ║ │└─────┬─────┘║
107 * ║ │Address >> 1 ║
108 * ║ └── Value: 1 ║
109 * ╚════════════════╝
110 * 0 <= Address <= 0x3FFE (16382)
111 *
112 * ╔═══ Reserved ═══╗
113 * ║110XXXXXXXXXXXXX║
114 * ╚════════════════╝
115 *
116 * ╔═══════════ Word-Next ═══════════╗
117 * ║111XXXXXXXXXXXXX║YYYYYYYYYYYYYYYY║
118 * ║ └─────┬─────┘║└───────┬──────┘║
119 * ║(Address-128)>>1║ ~Value ║
120 * ╚════════════════╩════════════════╝
121 * ( 0 <= Address < 0x0080 (128): Reserved)
122 * 0x80 <= Address <= 0x3FFE (16382)
123 *
124 * Write Log entry ranges:
125 * 0x0000 ... 0x7FFF - Byte-Entry; address is (Entry & 0x7F00) >> 4; value is (Entry & 0xFF)
126 * 0x8000 ... 0x9FFF - Word-Encoded 0; address is (Entry & 0x1FFF) << 1; value is 0
127 * 0xA000 ... 0xBFFF - Word-Encoded 1; address is (Entry & 0x1FFF) << 1; value is 1
128 * 0xC000 ... 0xDFFF - Reserved
129 * 0xE000 ... 0xFFBF - Word-Next; address is (Entry & 0x1FFF) << 1 + 0x80; value is ~(Next_Entry)
130 * 0xFFC0 ... 0xFFFE - Reserved
131 * 0xFFFF - Unprogrammed
132 *
133 */
134
135#include "eeprom_stm32_defs.h"
136#if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT) || !defined(FEE_MCU_FLASH_SIZE) || !defined(FEE_PAGE_BASE_ADDRESS)
137# error "not implemented."
138#endif
139
140/* These bits are used for optimizing encoding of bytes, 0 and 1 */
141#define FEE_WORD_ENCODING 0x8000
142#define FEE_VALUE_NEXT 0x6000
143#define FEE_VALUE_RESERVED 0x4000
144#define FEE_VALUE_ENCODED 0x2000
145#define FEE_BYTE_RANGE 0x80
146
147/* Addressable range 16KByte: 0 <-> (0x1FFF << 1) */
148#define FEE_ADDRESS_MAX_SIZE 0x4000
149
150/* Flash word value after erase */
151#define FEE_EMPTY_WORD ((uint16_t)0xFFFF)
152
153/* Size of combined compacted eeprom and write log pages */
154#define FEE_DENSITY_MAX_SIZE (FEE_PAGE_COUNT * FEE_PAGE_SIZE)
155
156#ifndef FEE_MCU_FLASH_SIZE_IGNORE_CHECK /* *TODO: Get rid of this check */
157# if FEE_DENSITY_MAX_SIZE > (FEE_MCU_FLASH_SIZE * 1024)
158# pragma message STR(FEE_DENSITY_MAX_SIZE) " > " STR(FEE_MCU_FLASH_SIZE * 1024)
159# error emulated eeprom: FEE_DENSITY_MAX_SIZE is greater than available flash size
160# endif
161#endif
162
163/* Size of emulated eeprom */
164#ifdef FEE_DENSITY_BYTES
165# if (FEE_DENSITY_BYTES > FEE_DENSITY_MAX_SIZE)
166# pragma message STR(FEE_DENSITY_BYTES) " > " STR(FEE_DENSITY_MAX_SIZE)
167# error emulated eeprom: FEE_DENSITY_BYTES exceeds FEE_DENSITY_MAX_SIZE
168# endif
169# if (FEE_DENSITY_BYTES == FEE_DENSITY_MAX_SIZE)
170# pragma message STR(FEE_DENSITY_BYTES) " == " STR(FEE_DENSITY_MAX_SIZE)
171# warning emulated eeprom: FEE_DENSITY_BYTES leaves no room for a write log. This will greatly increase the flash wear rate!
172# endif
173# if FEE_DENSITY_BYTES > FEE_ADDRESS_MAX_SIZE
174# pragma message STR(FEE_DENSITY_BYTES) " > " STR(FEE_ADDRESS_MAX_SIZE)
175# error emulated eeprom: FEE_DENSITY_BYTES is greater than FEE_ADDRESS_MAX_SIZE allows
176# endif
177# if ((FEE_DENSITY_BYTES) % 2) == 1
178# error emulated eeprom: FEE_DENSITY_BYTES must be even
179# endif
180#else
181/* Default to half of allocated space used for emulated eeprom, half for write log */
182# define FEE_DENSITY_BYTES (FEE_PAGE_COUNT * FEE_PAGE_SIZE / 2)
183#endif
184
185/* Size of write log */
186#ifdef FEE_WRITE_LOG_BYTES
187# if ((FEE_DENSITY_BYTES + FEE_WRITE_LOG_BYTES) > FEE_DENSITY_MAX_SIZE)
188# pragma message STR(FEE_DENSITY_BYTES) " + " STR(FEE_WRITE_LOG_BYTES) " > " STR(FEE_DENSITY_MAX_SIZE)
189# error emulated eeprom: FEE_WRITE_LOG_BYTES exceeds remaining FEE_DENSITY_MAX_SIZE
190# endif
191# if ((FEE_WRITE_LOG_BYTES) % 2) == 1
192# error emulated eeprom: FEE_WRITE_LOG_BYTES must be even
193# endif
194#else
195/* Default to use all remaining space */
196# define FEE_WRITE_LOG_BYTES (FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES)
197#endif
198
199/* Start of the emulated eeprom compacted flash area */
200#define FEE_COMPACTED_BASE_ADDRESS FEE_PAGE_BASE_ADDRESS
201/* End of the emulated eeprom compacted flash area */
202#define FEE_COMPACTED_LAST_ADDRESS (FEE_COMPACTED_BASE_ADDRESS + FEE_DENSITY_BYTES)
203/* Start of the emulated eeprom write log */
204#define FEE_WRITE_LOG_BASE_ADDRESS FEE_COMPACTED_LAST_ADDRESS
205/* End of the emulated eeprom write log */
206#define FEE_WRITE_LOG_LAST_ADDRESS (FEE_WRITE_LOG_BASE_ADDRESS + FEE_WRITE_LOG_BYTES)
207
208#if defined(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) && (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR >= FEE_DENSITY_BYTES)
209# error emulated eeprom: DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is greater than the FEE_DENSITY_BYTES available
210#endif
211
212/* In-memory contents of emulated eeprom for faster access */
213/* *TODO: Implement page swapping */
214static uint16_t WordBuf[FEE_DENSITY_BYTES / 2];
215static uint8_t *DataBuf = (uint8_t *)WordBuf;
216
217/* Pointer to the first available slot within the write log */
218static uint16_t *empty_slot;
219
220// #define DEBUG_EEPROM_OUTPUT
221
222/*
223 * Debug print utils
224 */
225
226#if defined(DEBUG_EEPROM_OUTPUT)
227
228# define debug_eeprom debug_enable
229# define eeprom_println(s) println(s)
230# define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__);
231
232#else /* NO_DEBUG */
233
234# define debug_eeprom false
235# define eeprom_println(s)
236# define eeprom_printf(fmt, ...)
237
238#endif /* NO_DEBUG */
239
240void print_eeprom(void) {
241#ifndef NO_DEBUG
242 int empty_rows = 0;
243 for (uint16_t i = 0; i < FEE_DENSITY_BYTES; i++) {
244 if (i % 16 == 0) {
245 if (i >= FEE_DENSITY_BYTES - 16) {
246 /* Make sure we display the last row */
247 empty_rows = 0;
248 }
249 /* Check if this row is uninitialized */
250 ++empty_rows;
251 for (uint16_t j = 0; j < 16; j++) {
252 if (DataBuf[i + j]) {
253 empty_rows = 0;
254 break;
255 }
256 }
257 if (empty_rows > 1) {
258 /* Repeat empty row */
259 if (empty_rows == 2) {
260 /* Only display the first repeat empty row */
261 println("*");
262 }
263 i += 15;
264 continue;
265 }
266 xprintf("%04x", i);
267 }
268 if (i % 8 == 0) print(" ");
269
270 xprintf(" %02x", DataBuf[i]);
271 if ((i + 1) % 16 == 0) {
272 println("");
273 }
274 }
275#endif
276}
277
278uint16_t EEPROM_Init(void) {
279 /* Load emulated eeprom contents from compacted flash into memory */
280 uint16_t *src = (uint16_t *)FEE_COMPACTED_BASE_ADDRESS;
281 uint16_t *dest = (uint16_t *)DataBuf;
282 for (; src < (uint16_t *)FEE_COMPACTED_LAST_ADDRESS; ++src, ++dest) {
283 *dest = ~*src;
284 }
285
286 if (debug_eeprom) {
287 println("EEPROM_Init Compacted Pages:");
288 print_eeprom();
289 println("EEPROM_Init Write Log:");
290 }
291
292 /* Replay write log */
293 uint16_t *log_addr;
294 for (log_addr = (uint16_t *)FEE_WRITE_LOG_BASE_ADDRESS; log_addr < (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS; ++log_addr) {
295 uint16_t address = *log_addr;
296 if (address == FEE_EMPTY_WORD) {
297 break;
298 }
299 /* Check for lowest 128-bytes optimization */
300 if (!(address & FEE_WORD_ENCODING)) {
301 uint8_t bvalue = (uint8_t)address;
302 address >>= 8;
303 DataBuf[address] = bvalue;
304 eeprom_printf("DataBuf[0x%02x] = 0x%02x;\n", address, bvalue);
305 } else {
306 uint16_t wvalue;
307 /* Check if value is in next word */
308 if ((address & FEE_VALUE_NEXT) == FEE_VALUE_NEXT) {
309 /* Read value from next word */
310 if (++log_addr >= (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS) {
311 break;
312 }
313 wvalue = ~*log_addr;
314 if (!wvalue) {
315 eeprom_printf("Incomplete write at log_addr: 0x%04x;\n", (uint32_t)log_addr);
316 /* Possibly incomplete write. Ignore and continue */
317 continue;
318 }
319 address &= 0x1FFF;
320 address <<= 1;
321 /* Writes to addresses less than 128 are byte log entries */
322 address += FEE_BYTE_RANGE;
323 } else {
324 /* Reserved for future use */
325 if (address & FEE_VALUE_RESERVED) {
326 eeprom_printf("Reserved encoded value at log_addr: 0x%04x;\n", (uint32_t)log_addr);
327 continue;
328 }
329 /* Optimization for 0 or 1 values. */
330 wvalue = (address & FEE_VALUE_ENCODED) >> 13;
331 address &= 0x1FFF;
332 address <<= 1;
333 }
334 if (address < FEE_DENSITY_BYTES) {
335 eeprom_printf("DataBuf[0x%04x] = 0x%04x;\n", address, wvalue);
336 *(uint16_t *)(&DataBuf[address]) = wvalue;
337 } else {
338 eeprom_printf("DataBuf[0x%04x] cannot be set to 0x%04x [BAD ADDRESS]\n", address, wvalue);
339 }
340 }
341 }
342
343 empty_slot = log_addr;
344
345 if (debug_eeprom) {
346 println("EEPROM_Init Final DataBuf:");
347 print_eeprom();
348 }
349
350 return FEE_DENSITY_BYTES;
351}
352
353/* Clear flash contents (doesn't touch in-memory DataBuf) */
354static void eeprom_clear(void) {
355 FLASH_Unlock();
356
357 for (uint16_t page_num = 0; page_num < FEE_PAGE_COUNT; ++page_num) {
358 eeprom_printf("FLASH_ErasePage(0x%04x)\n", (uint32_t)(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)));
359 FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE));
360 }
361
362 FLASH_Lock();
363
364 empty_slot = (uint16_t *)FEE_WRITE_LOG_BASE_ADDRESS;
365 eeprom_printf("eeprom_clear empty_slot: 0x%08x\n", (uint32_t)empty_slot);
366}
367
368/* Erase emulated eeprom */
369void EEPROM_Erase(void) {
370 eeprom_println("EEPROM_Erase");
371 /* Erase compacted pages and write log */
372 eeprom_clear();
373 /* re-initialize to reset DataBuf */
374 EEPROM_Init();
375}
376
377/* Compact write log */
378static uint8_t eeprom_compact(void) {
379 /* Erase compacted pages and write log */
380 eeprom_clear();
381
382 FLASH_Unlock();
383
384 FLASH_Status final_status = FLASH_COMPLETE;
385
386 /* Write emulated eeprom contents from memory to compacted flash */
387 uint16_t *src = (uint16_t *)DataBuf;
388 uintptr_t dest = FEE_COMPACTED_BASE_ADDRESS;
389 uint16_t value;
390 for (; dest < FEE_COMPACTED_LAST_ADDRESS; ++src, dest += 2) {
391 value = *src;
392 if (value) {
393 eeprom_printf("FLASH_ProgramHalfWord(0x%04x, 0x%04x)\n", (uint32_t)dest, ~value);
394 FLASH_Status status = FLASH_ProgramHalfWord(dest, ~value);
395 if (status != FLASH_COMPLETE) final_status = status;
396 }
397 }
398
399 FLASH_Lock();
400
401 if (debug_eeprom) {
402 println("eeprom_compacted:");
403 print_eeprom();
404 }
405
406 return final_status;
407}
408
409static uint8_t eeprom_write_direct_entry(uint16_t Address) {
410 /* Check if we can just write this directly to the compacted flash area */
411 uintptr_t directAddress = FEE_COMPACTED_BASE_ADDRESS + (Address & 0xFFFE);
412 if (*(uint16_t *)directAddress == FEE_EMPTY_WORD) {
413 /* Write the value directly to the compacted area without a log entry */
414 uint16_t value = ~*(uint16_t *)(&DataBuf[Address & 0xFFFE]);
415 /* Early exit if a write isn't needed */
416 if (value == FEE_EMPTY_WORD) return FLASH_COMPLETE;
417
418 FLASH_Unlock();
419
420 eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x) [DIRECT]\n", (uint32_t)directAddress, value);
421 FLASH_Status status = FLASH_ProgramHalfWord(directAddress, value);
422
423 FLASH_Lock();
424 return status;
425 }
426 return 0;
427}
428
429static uint8_t eeprom_write_log_word_entry(uint16_t Address) {
430 FLASH_Status final_status = FLASH_COMPLETE;
431
432 uint16_t value = *(uint16_t *)(&DataBuf[Address]);
433 eeprom_printf("eeprom_write_log_word_entry(0x%04x): 0x%04x\n", Address, value);
434
435 /* MSB signifies the lowest 128-byte optimization is not in effect */
436 uint16_t encoding = FEE_WORD_ENCODING;
437 uint8_t entry_size;
438 if (value <= 1) {
439 encoding |= value << 13;
440 entry_size = 2;
441 } else {
442 encoding |= FEE_VALUE_NEXT;
443 entry_size = 4;
444 /* Writes to addresses less than 128 are byte log entries */
445 Address -= FEE_BYTE_RANGE;
446 }
447
448 /* if we can't find an empty spot, we must compact emulated eeprom */
449 if (empty_slot > (uint16_t *)(FEE_WRITE_LOG_LAST_ADDRESS - entry_size)) {
450 /* compact the write log into the compacted flash area */
451 return eeprom_compact();
452 }
453
454 /* Word log writes should be word-aligned. Take back a bit */
455 Address >>= 1;
456 Address |= encoding;
457
458 /* ok we found a place let's write our data */
459 FLASH_Unlock();
460
461 /* address */
462 eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, Address);
463 final_status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, Address);
464
465 /* value */
466 if (encoding == (FEE_WORD_ENCODING | FEE_VALUE_NEXT)) {
467 eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, ~value);
468 FLASH_Status status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, ~value);
469 if (status != FLASH_COMPLETE) final_status = status;
470 }
471
472 FLASH_Lock();
473
474 return final_status;
475}
476
477static uint8_t eeprom_write_log_byte_entry(uint16_t Address) {
478 eeprom_printf("eeprom_write_log_byte_entry(0x%04x): 0x%02x\n", Address, DataBuf[Address]);
479
480 /* if couldn't find an empty spot, we must compact emulated eeprom */
481 if (empty_slot >= (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS) {
482 /* compact the write log into the compacted flash area */
483 return eeprom_compact();
484 }
485
486 /* ok we found a place let's write our data */
487 FLASH_Unlock();
488
489 /* Pack address and value into the same word */
490 uint16_t value = (Address << 8) | DataBuf[Address];
491
492 /* write to flash */
493 eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, value);
494 FLASH_Status status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, value);
495
496 FLASH_Lock();
497
498 return status;
499}
500
501uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) {
502 /* if the address is out-of-bounds, do nothing */
503 if (Address >= FEE_DENSITY_BYTES) {
504 eeprom_printf("EEPROM_WriteDataByte(0x%04x, 0x%02x) [BAD ADDRESS]\n", Address, DataByte);
505 return FLASH_BAD_ADDRESS;
506 }
507
508 /* if the value is the same, don't bother writing it */
509 if (DataBuf[Address] == DataByte) {
510 eeprom_printf("EEPROM_WriteDataByte(0x%04x, 0x%02x) [SKIP SAME]\n", Address, DataByte);
511 return 0;
512 }
513
514 /* keep DataBuf cache in sync */
515 DataBuf[Address] = DataByte;
516 eeprom_printf("EEPROM_WriteDataByte DataBuf[0x%04x] = 0x%02x\n", Address, DataBuf[Address]);
517
518 /* perform the write into flash memory */
519 /* First, attempt to write directly into the compacted flash area */
520 FLASH_Status status = eeprom_write_direct_entry(Address);
521 if (!status) {
522 /* Otherwise append to the write log */
523 if (Address < FEE_BYTE_RANGE) {
524 status = eeprom_write_log_byte_entry(Address);
525 } else {
526 status = eeprom_write_log_word_entry(Address & 0xFFFE);
527 }
528 }
529 if (status != 0 && status != FLASH_COMPLETE) {
530 eeprom_printf("EEPROM_WriteDataByte [STATUS == %d]\n", status);
531 }
532 return status;
533}
534
535uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord) {
536 /* if the address is out-of-bounds, do nothing */
537 if (Address >= FEE_DENSITY_BYTES) {
538 eeprom_printf("EEPROM_WriteDataWord(0x%04x, 0x%04x) [BAD ADDRESS]\n", Address, DataWord);
539 return FLASH_BAD_ADDRESS;
540 }
541
542 /* Check for word alignment */
543 FLASH_Status final_status = FLASH_COMPLETE;
544 if (Address % 2) {
545 final_status = EEPROM_WriteDataByte(Address, DataWord);
546 FLASH_Status status = EEPROM_WriteDataByte(Address + 1, DataWord >> 8);
547 if (status != FLASH_COMPLETE) final_status = status;
548 if (final_status != 0 && final_status != FLASH_COMPLETE) {
549 eeprom_printf("EEPROM_WriteDataWord [STATUS == %d]\n", final_status);
550 }
551 return final_status;
552 }
553
554 /* if the value is the same, don't bother writing it */
555 uint16_t oldValue = *(uint16_t *)(&DataBuf[Address]);
556 if (oldValue == DataWord) {
557 eeprom_printf("EEPROM_WriteDataWord(0x%04x, 0x%04x) [SKIP SAME]\n", Address, DataWord);
558 return 0;
559 }
560
561 /* keep DataBuf cache in sync */
562 *(uint16_t *)(&DataBuf[Address]) = DataWord;
563 eeprom_printf("EEPROM_WriteDataWord DataBuf[0x%04x] = 0x%04x\n", Address, *(uint16_t *)(&DataBuf[Address]));
564
565 /* perform the write into flash memory */
566 /* First, attempt to write directly into the compacted flash area */
567 final_status = eeprom_write_direct_entry(Address);
568 if (!final_status) {
569 /* Otherwise append to the write log */
570 /* Check if we need to fall back to byte write */
571 if (Address < FEE_BYTE_RANGE) {
572 final_status = FLASH_COMPLETE;
573 /* Only write a byte if it has changed */
574 if ((uint8_t)oldValue != (uint8_t)DataWord) {
575 final_status = eeprom_write_log_byte_entry(Address);
576 }
577 FLASH_Status status = FLASH_COMPLETE;
578 /* Only write a byte if it has changed */
579 if ((oldValue >> 8) != (DataWord >> 8)) {
580 status = eeprom_write_log_byte_entry(Address + 1);
581 }
582 if (status != FLASH_COMPLETE) final_status = status;
583 } else {
584 final_status = eeprom_write_log_word_entry(Address);
585 }
586 }
587 if (final_status != 0 && final_status != FLASH_COMPLETE) {
588 eeprom_printf("EEPROM_WriteDataWord [STATUS == %d]\n", final_status);
589 }
590 return final_status;
591}
592
593uint8_t EEPROM_ReadDataByte(uint16_t Address) {
594 uint8_t DataByte = 0xFF;
595
596 if (Address < FEE_DENSITY_BYTES) {
597 DataByte = DataBuf[Address];
598 }
599
600 eeprom_printf("EEPROM_ReadDataByte(0x%04x): 0x%02x\n", Address, DataByte);
601
602 return DataByte;
603}
604
605uint16_t EEPROM_ReadDataWord(uint16_t Address) {
606 uint16_t DataWord = 0xFFFF;
607
608 if (Address < FEE_DENSITY_BYTES - 1) {
609 /* Check word alignment */
610 if (Address % 2) {
611 DataWord = DataBuf[Address] | (DataBuf[Address + 1] << 8);
612 } else {
613 DataWord = *(uint16_t *)(&DataBuf[Address]);
614 }
615 }
616
617 eeprom_printf("EEPROM_ReadDataWord(0x%04x): 0x%04x\n", Address, DataWord);
618
619 return DataWord;
620}
621
622/*****************************************************************************
623 * Bind to eeprom_driver.c
624 *******************************************************************************/
625void eeprom_driver_init(void) { EEPROM_Init(); }
626
627void eeprom_driver_erase(void) { EEPROM_Erase(); }
628
629void eeprom_read_block(void *buf, const void *addr, size_t len) {
630 const uint8_t *src = (const uint8_t *)addr;
631 uint8_t * dest = (uint8_t *)buf;
632
633 /* Check word alignment */
634 if (len && (uintptr_t)src % 2) {
635 /* Read the unaligned first byte */
636 *dest++ = EEPROM_ReadDataByte((const uintptr_t)src++);
637 --len;
638 }
639
640 uint16_t value;
641 bool aligned = ((uintptr_t)dest % 2 == 0);
642 while (len > 1) {
643 value = EEPROM_ReadDataWord((const uintptr_t)((uint16_t *)src));
644 if (aligned) {
645 *(uint16_t *)dest = value;
646 dest += 2;
647 } else {
648 *dest++ = value;
649 *dest++ = value >> 8;
650 }
651 src += 2;
652 len -= 2;
653 }
654 if (len) {
655 *dest = EEPROM_ReadDataByte((const uintptr_t)src);
656 }
657}
658
659void eeprom_write_block(const void *buf, void *addr, size_t len) {
660 uint8_t * dest = (uint8_t *)addr;
661 const uint8_t *src = (const uint8_t *)buf;
662
663 /* Check word alignment */
664 if (len && (uintptr_t)dest % 2) {
665 /* Write the unaligned first byte */
666 EEPROM_WriteDataByte((uintptr_t)dest++, *src++);
667 --len;
668 }
669
670 uint16_t value;
671 bool aligned = ((uintptr_t)src % 2 == 0);
672 while (len > 1) {
673 if (aligned) {
674 value = *(uint16_t *)src;
675 } else {
676 value = *(uint8_t *)src | (*(uint8_t *)(src + 1) << 8);
677 }
678 EEPROM_WriteDataWord((uintptr_t)((uint16_t *)dest), value);
679 dest += 2;
680 src += 2;
681 len -= 2;
682 }
683
684 if (len) {
685 EEPROM_WriteDataByte((uintptr_t)dest, *src);
686 }
687}
diff --git a/tmk_core/common/chibios/eeprom_stm32.h b/tmk_core/common/chibios/eeprom_stm32.h
deleted file mode 100644
index 8fcfb556b..000000000
--- a/tmk_core/common/chibios/eeprom_stm32.h
+++ /dev/null
@@ -1,33 +0,0 @@
1/*
2 * This software is experimental and a work in progress.
3 * Under no circumstances should these files be used in relation to any critical system(s).
4 * Use of these files is at your own risk.
5 *
6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
7 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
8 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
9 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
11 * DEALINGS IN THE SOFTWARE.
12 *
13 * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by
14 * Artur F.
15 *
16 * Modifications for QMK and STM32F303 by Yiancar
17 *
18 * This library assumes 8-bit data locations. To add a new MCU, please provide the flash
19 * page size and the total flash size in Kb. The number of available pages must be a multiple
20 * of 2. Only half of the pages account for the total EEPROM size.
21 * This library also assumes that the pages are not used by the firmware.
22 */
23
24#pragma once
25
26uint16_t EEPROM_Init(void);
27void EEPROM_Erase(void);
28uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte);
29uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord);
30uint8_t EEPROM_ReadDataByte(uint16_t Address);
31uint16_t EEPROM_ReadDataWord(uint16_t Address);
32
33void print_eeprom(void);
diff --git a/tmk_core/common/chibios/eeprom_stm32_defs.h b/tmk_core/common/chibios/eeprom_stm32_defs.h
deleted file mode 100644
index 66904f247..000000000
--- a/tmk_core/common/chibios/eeprom_stm32_defs.h
+++ /dev/null
@@ -1,74 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <hal.h>
19
20#if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT)
21# if defined(STM32F103xB) || defined(STM32F042x6) || defined(GD32VF103C8) || defined(GD32VF103CB)
22# ifndef FEE_PAGE_SIZE
23# define FEE_PAGE_SIZE 0x400 // Page size = 1KByte
24# endif
25# ifndef FEE_PAGE_COUNT
26# define FEE_PAGE_COUNT 2 // How many pages are used
27# endif
28# elif defined(STM32F103xE) || defined(STM32F303xC) || defined(STM32F072xB) || defined(STM32F070xB)
29# ifndef FEE_PAGE_SIZE
30# define FEE_PAGE_SIZE 0x800 // Page size = 2KByte
31# endif
32# ifndef FEE_PAGE_COUNT
33# define FEE_PAGE_COUNT 4 // How many pages are used
34# endif
35# elif defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F405xG) || defined(STM32F411xE)
36# ifndef FEE_PAGE_SIZE
37# define FEE_PAGE_SIZE 0x4000 // Page size = 16KByte
38# endif
39# ifndef FEE_PAGE_COUNT
40# define FEE_PAGE_COUNT 1 // How many pages are used
41# endif
42# endif
43#endif
44
45#if !defined(FEE_MCU_FLASH_SIZE)
46# if defined(STM32F042x6)
47# define FEE_MCU_FLASH_SIZE 32 // Size in Kb
48# elif defined(GD32VF103C8)
49# define FEE_MCU_FLASH_SIZE 64 // Size in Kb
50# elif defined(STM32F103xB) || defined(STM32F072xB) || defined(STM32F070xB) || defined(GD32VF103CB)
51# define FEE_MCU_FLASH_SIZE 128 // Size in Kb
52# elif defined(STM32F303xC) || defined(STM32F401xC)
53# define FEE_MCU_FLASH_SIZE 256 // Size in Kb
54# elif defined(STM32F103xE) || defined(STM32F401xE) || defined(STM32F411xE)
55# define FEE_MCU_FLASH_SIZE 512 // Size in Kb
56# elif defined(STM32F405xG)
57# define FEE_MCU_FLASH_SIZE 1024 // Size in Kb
58# endif
59#endif
60
61/* Start of the emulated eeprom */
62#if !defined(FEE_PAGE_BASE_ADDRESS)
63# if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F405xG) || defined(STM32F411xE)
64# ifndef FEE_PAGE_BASE_ADDRESS
65# define FEE_PAGE_BASE_ADDRESS 0x08004000 // bodge to force 2nd 16k page
66# endif
67# else
68# ifndef FEE_FLASH_BASE
69# define FEE_FLASH_BASE 0x8000000
70# endif
71/* Default to end of flash */
72# define FEE_PAGE_BASE_ADDRESS ((uintptr_t)(FEE_FLASH_BASE) + FEE_MCU_FLASH_SIZE * 1024 - (FEE_PAGE_COUNT * FEE_PAGE_SIZE))
73# endif
74#endif
diff --git a/tmk_core/common/chibios/eeprom_teensy.c b/tmk_core/common/chibios/eeprom_teensy.c
deleted file mode 100644
index 97da6f9e1..000000000
--- a/tmk_core/common/chibios/eeprom_teensy.c
+++ /dev/null
@@ -1,795 +0,0 @@
1#include <ch.h>
2#include <hal.h>
3
4#include "eeconfig.h"
5
6/*************************************/
7/* Hardware backend */
8/* */
9/* Code from PJRC/Teensyduino */
10/*************************************/
11
12/* Teensyduino Core Library
13 * http://www.pjrc.com/teensy/
14 * Copyright (c) 2013 PJRC.COM, LLC.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining
17 * a copy of this software and associated documentation files (the
18 * "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
23 *
24 * 1. The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
26 *
27 * 2. If the Software is incorporated into a build system that allows
28 * selection among a list of target devices, then similar target
29 * devices manufactured by PJRC.COM must be included in the list of
30 * target devices and selectable in the same manner.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
36 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
37 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
38 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 * SOFTWARE.
40 */
41
42#define SMC_PMSTAT_RUN ((uint8_t)0x01)
43#define SMC_PMSTAT_HSRUN ((uint8_t)0x80)
44
45#define F_CPU KINETIS_SYSCLK_FREQUENCY
46
47static inline int kinetis_hsrun_disable(void) {
48#if defined(MK66F18)
49 if (SMC->PMSTAT == SMC_PMSTAT_HSRUN) {
50// First, reduce the CPU clock speed, but do not change
51// the peripheral speed (F_BUS). Serial1 & Serial2 baud
52// rates will be impacted, but most other peripherals
53// will continue functioning at the same speed.
54# if F_CPU == 256000000 && F_BUS == 64000000
55 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // TODO: TEST
56# elif F_CPU == 256000000 && F_BUS == 128000000
57 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // TODO: TEST
58# elif F_CPU == 240000000 && F_BUS == 60000000
59 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
60# elif F_CPU == 240000000 && F_BUS == 80000000
61 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
62# elif F_CPU == 240000000 && F_BUS == 120000000
63 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
64# elif F_CPU == 216000000 && F_BUS == 54000000
65 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
66# elif F_CPU == 216000000 && F_BUS == 72000000
67 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
68# elif F_CPU == 216000000 && F_BUS == 108000000
69 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
70# elif F_CPU == 192000000 && F_BUS == 48000000
71 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
72# elif F_CPU == 192000000 && F_BUS == 64000000
73 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
74# elif F_CPU == 192000000 && F_BUS == 96000000
75 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
76# elif F_CPU == 180000000 && F_BUS == 60000000
77 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
78# elif F_CPU == 180000000 && F_BUS == 90000000
79 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
80# elif F_CPU == 168000000 && F_BUS == 56000000
81 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5); // ok
82# elif F_CPU == 144000000 && F_BUS == 48000000
83 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5); // ok
84# elif F_CPU == 144000000 && F_BUS == 72000000
85 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 5); // ok
86# elif F_CPU == 120000000 && F_BUS == 60000000
87 SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(KINETIS_CLKDIV1_OUTDIV1 - 1) | SIM_CLKDIV1_OUTDIV2(KINETIS_CLKDIV1_OUTDIV2 - 1) |
88# if defined(MK66F18)
89 SIM_CLKDIV1_OUTDIV3(KINETIS_CLKDIV1_OUTDIV3 - 1) |
90# endif
91 SIM_CLKDIV1_OUTDIV4(KINETIS_CLKDIV1_OUTDIV4 - 1);
92# else
93 return 0;
94# endif
95 // Then turn off HSRUN mode
96 SMC->PMCTRL = SMC_PMCTRL_RUNM_SET(0);
97 while (SMC->PMSTAT == SMC_PMSTAT_HSRUN)
98 ; // wait
99 return 1;
100 }
101#endif
102 return 0;
103}
104
105static inline int kinetis_hsrun_enable(void) {
106#if defined(MK66F18)
107 if (SMC->PMSTAT == SMC_PMSTAT_RUN) {
108 // Turn HSRUN mode on
109 SMC->PMCTRL = SMC_PMCTRL_RUNM_SET(3);
110 while (SMC->PMSTAT != SMC_PMSTAT_HSRUN) {
111 ;
112 } // wait
113// Then configure clock for full speed
114# if F_CPU == 256000000 && F_BUS == 64000000
115 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
116# elif F_CPU == 256000000 && F_BUS == 128000000
117 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
118# elif F_CPU == 240000000 && F_BUS == 60000000
119 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
120# elif F_CPU == 240000000 && F_BUS == 80000000
121 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
122# elif F_CPU == 240000000 && F_BUS == 120000000
123 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
124# elif F_CPU == 216000000 && F_BUS == 54000000
125 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
126# elif F_CPU == 216000000 && F_BUS == 72000000
127 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
128# elif F_CPU == 216000000 && F_BUS == 108000000
129 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
130# elif F_CPU == 192000000 && F_BUS == 48000000
131 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 6);
132# elif F_CPU == 192000000 && F_BUS == 64000000
133 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
134# elif F_CPU == 192000000 && F_BUS == 96000000
135 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
136# elif F_CPU == 180000000 && F_BUS == 60000000
137 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
138# elif F_CPU == 180000000 && F_BUS == 90000000
139 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
140# elif F_CPU == 168000000 && F_BUS == 56000000
141 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 5);
142# elif F_CPU == 144000000 && F_BUS == 48000000
143 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 4);
144# elif F_CPU == 144000000 && F_BUS == 72000000
145 SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 4);
146# elif F_CPU == 120000000 && F_BUS == 60000000
147 SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(KINETIS_CLKDIV1_OUTDIV1 - 1) | SIM_CLKDIV1_OUTDIV2(KINETIS_CLKDIV1_OUTDIV2 - 1) |
148# if defined(MK66F18)
149 SIM_CLKDIV1_OUTDIV3(KINETIS_CLKDIV1_OUTDIV3 - 1) |
150# endif
151 SIM_CLKDIV1_OUTDIV4(KINETIS_CLKDIV1_OUTDIV4 - 1);
152# else
153 return 0;
154# endif
155 return 1;
156 }
157#endif
158 return 0;
159}
160
161#if defined(K20x) || defined(MK66F18) /* chip selection */
162/* Teensy 3.0, 3.1, 3.2; mchck; infinity keyboard */
163
164// The EEPROM is really RAM with a hardware-based backup system to
165// flash memory. Selecting a smaller size EEPROM allows more wear
166// leveling, for higher write endurance. If you edit this file,
167// set this to the smallest size your application can use. Also,
168// due to Freescale's implementation, writing 16 or 32 bit words
169// (aligned to 2 or 4 byte boundaries) has twice the endurance
170// compared to writing 8 bit bytes.
171//
172# ifndef EEPROM_SIZE
173# define EEPROM_SIZE 32
174# endif
175
176/*
177 ^^^ Here be dragons:
178 NXP AppNote AN4282 section 3.1 states that partitioning must only be done once.
179 Once EEPROM partitioning is done, the size is locked to this initial configuration.
180 Attempts to modify the EEPROM_SIZE setting may brick your board.
181*/
182
183// Writing unaligned 16 or 32 bit data is handled automatically when
184// this is defined, but at a cost of extra code size. Without this,
185// any unaligned write will cause a hard fault exception! If you're
186// absolutely sure all 16 and 32 bit writes will be aligned, you can
187// remove the extra unnecessary code.
188//
189# define HANDLE_UNALIGNED_WRITES
190
191# if defined(K20x)
192# define EEPROM_MAX 2048
193# define EEPARTITION 0x03 // all 32K dataflash for EEPROM, none for Data
194# define EEESPLIT 0x30 // must be 0x30 on these chips
195# elif defined(MK66F18)
196# define EEPROM_MAX 4096
197# define EEPARTITION 0x05 // 128K dataflash for EEPROM, 128K for Data
198# define EEESPLIT 0x10 // best endurance: 0x00 = first 12%, 0x10 = first 25%, 0x30 = all equal
199# endif
200
201// Minimum EEPROM Endurance
202// ------------------------
203# if (EEPROM_SIZE == 4096)
204# define EEESIZE 0x02
205# elif (EEPROM_SIZE == 2048) // 35000 writes/byte or 70000 writes/word
206# define EEESIZE 0x03
207# elif (EEPROM_SIZE == 1024) // 75000 writes/byte or 150000 writes/word
208# define EEESIZE 0x04
209# elif (EEPROM_SIZE == 512) // 155000 writes/byte or 310000 writes/word
210# define EEESIZE 0x05
211# elif (EEPROM_SIZE == 256) // 315000 writes/byte or 630000 writes/word
212# define EEESIZE 0x06
213# elif (EEPROM_SIZE == 128) // 635000 writes/byte or 1270000 writes/word
214# define EEESIZE 0x07
215# elif (EEPROM_SIZE == 64) // 1275000 writes/byte or 2550000 writes/word
216# define EEESIZE 0x08
217# elif (EEPROM_SIZE == 32) // 2555000 writes/byte or 5110000 writes/word
218# define EEESIZE 0x09
219# endif
220
221/** \brief eeprom initialization
222 *
223 * FIXME: needs doc
224 */
225void eeprom_initialize(void) {
226 uint32_t count = 0;
227 uint16_t do_flash_cmd[] = {0xf06f, 0x037f, 0x7003, 0x7803, 0xf013, 0x0f80, 0xd0fb, 0x4770};
228 uint8_t status;
229
230 if (FTFL->FCNFG & FTFL_FCNFG_RAMRDY) {
231 uint8_t stat = FTFL->FSTAT & 0x70;
232 if (stat) FTFL->FSTAT = stat;
233
234 // FlexRAM is configured as traditional RAM
235 // We need to reconfigure for EEPROM usage
236 kinetis_hsrun_disable();
237 FTFL->FCCOB0 = 0x80; // PGMPART = Program Partition Command
238 FTFL->FCCOB3 = 0;
239 FTFL->FCCOB4 = EEESPLIT | EEESIZE;
240 FTFL->FCCOB5 = EEPARTITION;
241 __disable_irq();
242 // do_flash_cmd() must execute from RAM. Luckily the C syntax is simple...
243 (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFL->FSTAT));
244 __enable_irq();
245 kinetis_hsrun_enable();
246 status = FTFL->FSTAT;
247 if (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL)) {
248 FTFL->FSTAT = (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL));
249 return; // error
250 }
251 }
252 // wait for eeprom to become ready (is this really necessary?)
253 while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
254 if (++count > 200000) break;
255 }
256}
257
258# define FlexRAM ((volatile uint8_t *)0x14000000)
259
260/** \brief eeprom read byte
261 *
262 * FIXME: needs doc
263 */
264uint8_t eeprom_read_byte(const uint8_t *addr) {
265 uint32_t offset = (uint32_t)addr;
266 if (offset >= EEPROM_SIZE) return 0;
267 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
268 return FlexRAM[offset];
269}
270
271/** \brief eeprom read word
272 *
273 * FIXME: needs doc
274 */
275uint16_t eeprom_read_word(const uint16_t *addr) {
276 uint32_t offset = (uint32_t)addr;
277 if (offset >= EEPROM_SIZE - 1) return 0;
278 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
279 return *(uint16_t *)(&FlexRAM[offset]);
280}
281
282/** \brief eeprom read dword
283 *
284 * FIXME: needs doc
285 */
286uint32_t eeprom_read_dword(const uint32_t *addr) {
287 uint32_t offset = (uint32_t)addr;
288 if (offset >= EEPROM_SIZE - 3) return 0;
289 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
290 return *(uint32_t *)(&FlexRAM[offset]);
291}
292
293/** \brief eeprom read block
294 *
295 * FIXME: needs doc
296 */
297void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
298 uint32_t offset = (uint32_t)addr;
299 uint8_t *dest = (uint8_t *)buf;
300 uint32_t end = offset + len;
301
302 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
303 if (end > EEPROM_SIZE) end = EEPROM_SIZE;
304 while (offset < end) {
305 *dest++ = FlexRAM[offset++];
306 }
307}
308
309/** \brief eeprom is ready
310 *
311 * FIXME: needs doc
312 */
313int eeprom_is_ready(void) { return (FTFL->FCNFG & FTFL_FCNFG_EEERDY) ? 1 : 0; }
314
315/** \brief flexram wait
316 *
317 * FIXME: needs doc
318 */
319static void flexram_wait(void) {
320 while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) {
321 // TODO: timeout
322 }
323}
324
325/** \brief eeprom_write_byte
326 *
327 * FIXME: needs doc
328 */
329void eeprom_write_byte(uint8_t *addr, uint8_t value) {
330 uint32_t offset = (uint32_t)addr;
331
332 if (offset >= EEPROM_SIZE) return;
333 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
334 if (FlexRAM[offset] != value) {
335 kinetis_hsrun_disable();
336 uint8_t stat = FTFL->FSTAT & 0x70;
337 if (stat) FTFL->FSTAT = stat;
338 FlexRAM[offset] = value;
339 flexram_wait();
340 kinetis_hsrun_enable();
341 }
342}
343
344/** \brief eeprom write word
345 *
346 * FIXME: needs doc
347 */
348void eeprom_write_word(uint16_t *addr, uint16_t value) {
349 uint32_t offset = (uint32_t)addr;
350
351 if (offset >= EEPROM_SIZE - 1) return;
352 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
353# ifdef HANDLE_UNALIGNED_WRITES
354 if ((offset & 1) == 0) {
355# endif
356 if (*(uint16_t *)(&FlexRAM[offset]) != value) {
357 kinetis_hsrun_disable();
358 uint8_t stat = FTFL->FSTAT & 0x70;
359 if (stat) FTFL->FSTAT = stat;
360 *(uint16_t *)(&FlexRAM[offset]) = value;
361 flexram_wait();
362 kinetis_hsrun_enable();
363 }
364# ifdef HANDLE_UNALIGNED_WRITES
365 } else {
366 if (FlexRAM[offset] != value) {
367 kinetis_hsrun_disable();
368 uint8_t stat = FTFL->FSTAT & 0x70;
369 if (stat) FTFL->FSTAT = stat;
370 FlexRAM[offset] = value;
371 flexram_wait();
372 kinetis_hsrun_enable();
373 }
374 if (FlexRAM[offset + 1] != (value >> 8)) {
375 kinetis_hsrun_disable();
376 uint8_t stat = FTFL->FSTAT & 0x70;
377 if (stat) FTFL->FSTAT = stat;
378 FlexRAM[offset + 1] = value >> 8;
379 flexram_wait();
380 kinetis_hsrun_enable();
381 }
382 }
383# endif
384}
385
386/** \brief eeprom write dword
387 *
388 * FIXME: needs doc
389 */
390void eeprom_write_dword(uint32_t *addr, uint32_t value) {
391 uint32_t offset = (uint32_t)addr;
392
393 if (offset >= EEPROM_SIZE - 3) return;
394 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
395# ifdef HANDLE_UNALIGNED_WRITES
396 switch (offset & 3) {
397 case 0:
398# endif
399 if (*(uint32_t *)(&FlexRAM[offset]) != value) {
400 kinetis_hsrun_disable();
401 uint8_t stat = FTFL->FSTAT & 0x70;
402 if (stat) FTFL->FSTAT = stat;
403 *(uint32_t *)(&FlexRAM[offset]) = value;
404 flexram_wait();
405 kinetis_hsrun_enable();
406 }
407 return;
408# ifdef HANDLE_UNALIGNED_WRITES
409 case 2:
410 if (*(uint16_t *)(&FlexRAM[offset]) != value) {
411 kinetis_hsrun_disable();
412 uint8_t stat = FTFL->FSTAT & 0x70;
413 if (stat) FTFL->FSTAT = stat;
414 *(uint16_t *)(&FlexRAM[offset]) = value;
415 flexram_wait();
416 kinetis_hsrun_enable();
417 }
418 if (*(uint16_t *)(&FlexRAM[offset + 2]) != (value >> 16)) {
419 kinetis_hsrun_disable();
420 uint8_t stat = FTFL->FSTAT & 0x70;
421 if (stat) FTFL->FSTAT = stat;
422 *(uint16_t *)(&FlexRAM[offset + 2]) = value >> 16;
423 flexram_wait();
424 kinetis_hsrun_enable();
425 }
426 return;
427 default:
428 if (FlexRAM[offset] != value) {
429 kinetis_hsrun_disable();
430 uint8_t stat = FTFL->FSTAT & 0x70;
431 if (stat) FTFL->FSTAT = stat;
432 FlexRAM[offset] = value;
433 flexram_wait();
434 kinetis_hsrun_enable();
435 }
436 if (*(uint16_t *)(&FlexRAM[offset + 1]) != (value >> 8)) {
437 kinetis_hsrun_disable();
438 uint8_t stat = FTFL->FSTAT & 0x70;
439 if (stat) FTFL->FSTAT = stat;
440 *(uint16_t *)(&FlexRAM[offset + 1]) = value >> 8;
441 flexram_wait();
442 kinetis_hsrun_enable();
443 }
444 if (FlexRAM[offset + 3] != (value >> 24)) {
445 kinetis_hsrun_disable();
446 uint8_t stat = FTFL->FSTAT & 0x70;
447 if (stat) FTFL->FSTAT = stat;
448 FlexRAM[offset + 3] = value >> 24;
449 flexram_wait();
450 kinetis_hsrun_enable();
451 }
452 }
453# endif
454}
455
456/** \brief eeprom write block
457 *
458 * FIXME: needs doc
459 */
460void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
461 uint32_t offset = (uint32_t)addr;
462 const uint8_t *src = (const uint8_t *)buf;
463
464 if (offset >= EEPROM_SIZE) return;
465 if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize();
466 if (len >= EEPROM_SIZE) len = EEPROM_SIZE;
467 if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset;
468 kinetis_hsrun_disable();
469 while (len > 0) {
470 uint32_t lsb = offset & 3;
471 if (lsb == 0 && len >= 4) {
472 // write aligned 32 bits
473 uint32_t val32;
474 val32 = *src++;
475 val32 |= (*src++ << 8);
476 val32 |= (*src++ << 16);
477 val32 |= (*src++ << 24);
478 if (*(uint32_t *)(&FlexRAM[offset]) != val32) {
479 uint8_t stat = FTFL->FSTAT & 0x70;
480 if (stat) FTFL->FSTAT = stat;
481 *(uint32_t *)(&FlexRAM[offset]) = val32;
482 flexram_wait();
483 }
484 offset += 4;
485 len -= 4;
486 } else if ((lsb == 0 || lsb == 2) && len >= 2) {
487 // write aligned 16 bits
488 uint16_t val16;
489 val16 = *src++;
490 val16 |= (*src++ << 8);
491 if (*(uint16_t *)(&FlexRAM[offset]) != val16) {
492 uint8_t stat = FTFL->FSTAT & 0x70;
493 if (stat) FTFL->FSTAT = stat;
494 *(uint16_t *)(&FlexRAM[offset]) = val16;
495 flexram_wait();
496 }
497 offset += 2;
498 len -= 2;
499 } else {
500 // write 8 bits
501 uint8_t val8 = *src++;
502 if (FlexRAM[offset] != val8) {
503 uint8_t stat = FTFL->FSTAT & 0x70;
504 if (stat) FTFL->FSTAT = stat;
505 FlexRAM[offset] = val8;
506 flexram_wait();
507 }
508 offset++;
509 len--;
510 }
511 }
512 kinetis_hsrun_enable();
513}
514
515/*
516void do_flash_cmd(volatile uint8_t *fstat)
517{
518 *fstat = 0x80;
519 while ((*fstat & 0x80) == 0) ; // wait
520}
52100000000 <do_flash_cmd>:
522 0: f06f 037f mvn.w r3, #127 ; 0x7f
523 4: 7003 strb r3, [r0, #0]
524 6: 7803 ldrb r3, [r0, #0]
525 8: f013 0f80 tst.w r3, #128 ; 0x80
526 c: d0fb beq.n 6 <do_flash_cmd+0x6>
527 e: 4770 bx lr
528*/
529
530#elif defined(KL2x) /* chip selection */
531/* Teensy LC (emulated) */
532
533# define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
534
535extern uint32_t __eeprom_workarea_start__;
536extern uint32_t __eeprom_workarea_end__;
537
538# define EEPROM_SIZE 128
539
540static uint32_t flashend = 0;
541
542void eeprom_initialize(void) {
543 const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
544
545 do {
546 if (*p++ == 0xFFFF) {
547 flashend = (uint32_t)(p - 2);
548 return;
549 }
550 } while (p < (uint16_t *)SYMVAL(__eeprom_workarea_end__));
551 flashend = (uint32_t)(p - 1);
552}
553
554uint8_t eeprom_read_byte(const uint8_t *addr) {
555 uint32_t offset = (uint32_t)addr;
556 const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__);
557 const uint16_t *end = (const uint16_t *)((uint32_t)flashend);
558 uint16_t val;
559 uint8_t data = 0xFF;
560
561 if (!end) {
562 eeprom_initialize();
563 end = (const uint16_t *)((uint32_t)flashend);
564 }
565 if (offset < EEPROM_SIZE) {
566 while (p <= end) {
567 val = *p++;
568 if ((val & 255) == offset) data = val >> 8;
569 }
570 }
571 return data;
572}
573
574static void flash_write(const uint16_t *code, uint32_t addr, uint32_t data) {
575 // with great power comes great responsibility....
576 uint32_t stat;
577 *(uint32_t *)&(FTFA->FCCOB3) = 0x06000000 | (addr & 0x00FFFFFC);
578 *(uint32_t *)&(FTFA->FCCOB7) = data;
579 __disable_irq();
580 (*((void (*)(volatile uint8_t *))((uint32_t)code | 1)))(&(FTFA->FSTAT));
581 __enable_irq();
582 stat = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
583 if (stat) {
584 FTFA->FSTAT = stat;
585 }
586 MCM->PLACR |= MCM_PLACR_CFCC;
587}
588
589void eeprom_write_byte(uint8_t *addr, uint8_t data) {
590 uint32_t offset = (uint32_t)addr;
591 const uint16_t *p, *end = (const uint16_t *)((uint32_t)flashend);
592 uint32_t i, val, flashaddr;
593 uint16_t do_flash_cmd[] = {0x2380, 0x7003, 0x7803, 0xb25b, 0x2b00, 0xdafb, 0x4770};
594 uint8_t buf[EEPROM_SIZE];
595
596 if (offset >= EEPROM_SIZE) return;
597 if (!end) {
598 eeprom_initialize();
599 end = (const uint16_t *)((uint32_t)flashend);
600 }
601 if (++end < (uint16_t *)SYMVAL(__eeprom_workarea_end__)) {
602 val = (data << 8) | offset;
603 flashaddr = (uint32_t)end;
604 flashend = flashaddr;
605 if ((flashaddr & 2) == 0) {
606 val |= 0xFFFF0000;
607 } else {
608 val <<= 16;
609 val |= 0x0000FFFF;
610 }
611 flash_write(do_flash_cmd, flashaddr, val);
612 } else {
613 for (i = 0; i < EEPROM_SIZE; i++) {
614 buf[i] = 0xFF;
615 }
616 val = 0;
617 for (p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); p < (uint16_t *)SYMVAL(__eeprom_workarea_end__); p++) {
618 val = *p;
619 if ((val & 255) < EEPROM_SIZE) {
620 buf[val & 255] = val >> 8;
621 }
622 }
623 buf[offset] = data;
624 for (flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); flashaddr < (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_end__); flashaddr += 1024) {
625 *(uint32_t *)&(FTFA->FCCOB3) = 0x09000000 | flashaddr;
626 __disable_irq();
627 (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFA->FSTAT));
628 __enable_irq();
629 val = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL);
630 ;
631 if (val) FTFA->FSTAT = val;
632 MCM->PLACR |= MCM_PLACR_CFCC;
633 }
634 flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__);
635 for (i = 0; i < EEPROM_SIZE; i++) {
636 if (buf[i] == 0xFF) continue;
637 if ((flashaddr & 2) == 0) {
638 val = (buf[i] << 8) | i;
639 } else {
640 val = val | (buf[i] << 24) | (i << 16);
641 flash_write(do_flash_cmd, flashaddr, val);
642 }
643 flashaddr += 2;
644 }
645 flashend = flashaddr;
646 if ((flashaddr & 2)) {
647 val |= 0xFFFF0000;
648 flash_write(do_flash_cmd, flashaddr, val);
649 }
650 }
651}
652
653/*
654void do_flash_cmd(volatile uint8_t *fstat)
655{
656 *fstat = 0x80;
657 while ((*fstat & 0x80) == 0) ; // wait
658}
65900000000 <do_flash_cmd>:
660 0: 2380 movs r3, #128 ; 0x80
661 2: 7003 strb r3, [r0, #0]
662 4: 7803 ldrb r3, [r0, #0]
663 6: b25b sxtb r3, r3
664 8: 2b00 cmp r3, #0
665 a: dafb bge.n 4 <do_flash_cmd+0x4>
666 c: 4770 bx lr
667*/
668
669uint16_t eeprom_read_word(const uint16_t *addr) {
670 const uint8_t *p = (const uint8_t *)addr;
671 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
672}
673
674uint32_t eeprom_read_dword(const uint32_t *addr) {
675 const uint8_t *p = (const uint8_t *)addr;
676 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
677}
678
679void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
680 const uint8_t *p = (const uint8_t *)addr;
681 uint8_t * dest = (uint8_t *)buf;
682 while (len--) {
683 *dest++ = eeprom_read_byte(p++);
684 }
685}
686
687int eeprom_is_ready(void) { return 1; }
688
689void eeprom_write_word(uint16_t *addr, uint16_t value) {
690 uint8_t *p = (uint8_t *)addr;
691 eeprom_write_byte(p++, value);
692 eeprom_write_byte(p, value >> 8);
693}
694
695void eeprom_write_dword(uint32_t *addr, uint32_t value) {
696 uint8_t *p = (uint8_t *)addr;
697 eeprom_write_byte(p++, value);
698 eeprom_write_byte(p++, value >> 8);
699 eeprom_write_byte(p++, value >> 16);
700 eeprom_write_byte(p, value >> 24);
701}
702
703void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
704 uint8_t * p = (uint8_t *)addr;
705 const uint8_t *src = (const uint8_t *)buf;
706 while (len--) {
707 eeprom_write_byte(p++, *src++);
708 }
709}
710
711#else
712// No EEPROM supported, so emulate it
713
714# ifndef EEPROM_SIZE
715# include "eeconfig.h"
716# define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO
717# endif
718__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE];
719
720uint8_t eeprom_read_byte(const uint8_t *addr) {
721 uint32_t offset = (uint32_t)addr;
722 return buffer[offset];
723}
724
725void eeprom_write_byte(uint8_t *addr, uint8_t value) {
726 uint32_t offset = (uint32_t)addr;
727 buffer[offset] = value;
728}
729
730uint16_t eeprom_read_word(const uint16_t *addr) {
731 const uint8_t *p = (const uint8_t *)addr;
732 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
733}
734
735uint32_t eeprom_read_dword(const uint32_t *addr) {
736 const uint8_t *p = (const uint8_t *)addr;
737 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
738}
739
740void eeprom_read_block(void *buf, const void *addr, size_t len) {
741 const uint8_t *p = (const uint8_t *)addr;
742 uint8_t * dest = (uint8_t *)buf;
743 while (len--) {
744 *dest++ = eeprom_read_byte(p++);
745 }
746}
747
748void eeprom_write_word(uint16_t *addr, uint16_t value) {
749 uint8_t *p = (uint8_t *)addr;
750 eeprom_write_byte(p++, value);
751 eeprom_write_byte(p, value >> 8);
752}
753
754void eeprom_write_dword(uint32_t *addr, uint32_t value) {
755 uint8_t *p = (uint8_t *)addr;
756 eeprom_write_byte(p++, value);
757 eeprom_write_byte(p++, value >> 8);
758 eeprom_write_byte(p++, value >> 16);
759 eeprom_write_byte(p, value >> 24);
760}
761
762void eeprom_write_block(const void *buf, void *addr, size_t len) {
763 uint8_t * p = (uint8_t *)addr;
764 const uint8_t *src = (const uint8_t *)buf;
765 while (len--) {
766 eeprom_write_byte(p++, *src++);
767 }
768}
769
770#endif /* chip selection */
771// The update functions just calls write for now, but could probably be optimized
772
773void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); }
774
775void eeprom_update_word(uint16_t *addr, uint16_t value) {
776 uint8_t *p = (uint8_t *)addr;
777 eeprom_write_byte(p++, value);
778 eeprom_write_byte(p, value >> 8);
779}
780
781void eeprom_update_dword(uint32_t *addr, uint32_t value) {
782 uint8_t *p = (uint8_t *)addr;
783 eeprom_write_byte(p++, value);
784 eeprom_write_byte(p++, value >> 8);
785 eeprom_write_byte(p++, value >> 16);
786 eeprom_write_byte(p, value >> 24);
787}
788
789void eeprom_update_block(const void *buf, void *addr, size_t len) {
790 uint8_t * p = (uint8_t *)addr;
791 const uint8_t *src = (const uint8_t *)buf;
792 while (len--) {
793 eeprom_write_byte(p++, *src++);
794 }
795}
diff --git a/tmk_core/common/chibios/flash_stm32.c b/tmk_core/common/chibios/flash_stm32.c
deleted file mode 100644
index 72c41b8b7..000000000
--- a/tmk_core/common/chibios/flash_stm32.c
+++ /dev/null
@@ -1,208 +0,0 @@
1/*
2 * This software is experimental and a work in progress.
3 * Under no circumstances should these files be used in relation to any critical system(s).
4 * Use of these files is at your own risk.
5 *
6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
7 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
8 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
9 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
11 * DEALINGS IN THE SOFTWARE.
12 *
13 * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
14 * https://github.com/leaflabs/libmaple
15 *
16 * Modifications for QMK and STM32F303 by Yiancar
17 */
18
19#include <hal.h>
20#include "flash_stm32.h"
21
22#if defined(STM32F1XX)
23# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
24#endif
25
26#if defined(MCU_GD32V)
27/* GigaDevice GD32VF103 is a STM32F103 clone at heart. */
28# include "gd32v_compatibility.h"
29#endif
30
31#if defined(STM32F4XX)
32# define FLASH_SR_PGERR (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR)
33
34# define FLASH_KEY1 0x45670123U
35# define FLASH_KEY2 0xCDEF89ABU
36
37static uint8_t ADDR2PAGE(uint32_t Page_Address) {
38 switch (Page_Address) {
39 case 0x08000000 ... 0x08003FFF:
40 return 0;
41 case 0x08004000 ... 0x08007FFF:
42 return 1;
43 case 0x08008000 ... 0x0800BFFF:
44 return 2;
45 case 0x0800C000 ... 0x0800FFFF:
46 return 3;
47 }
48
49 // TODO: bad times...
50 return 7;
51}
52#endif
53
54/* Delay definition */
55#define EraseTimeout ((uint32_t)0x00000FFF)
56#define ProgramTimeout ((uint32_t)0x0000001F)
57
58#define ASSERT(exp) (void)((0))
59
60/**
61 * @brief Inserts a time delay.
62 * @param None
63 * @retval None
64 */
65static void delay(void) {
66 __IO uint32_t i = 0;
67 for (i = 0xFF; i != 0; i--) {
68 }
69}
70
71/**
72 * @brief Returns the FLASH Status.
73 * @param None
74 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
75 * FLASH_ERROR_WRP or FLASH_COMPLETE
76 */
77FLASH_Status FLASH_GetStatus(void) {
78 if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY;
79
80 if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG;
81
82 if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP;
83
84#if defined(FLASH_OBR_OPTERR)
85 if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT;
86#endif
87
88 return FLASH_COMPLETE;
89}
90
91/**
92 * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
93 * @param Timeout: FLASH progamming Timeout
94 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
95 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
96 */
97FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) {
98 FLASH_Status status;
99
100 /* Check for the Flash Status */
101 status = FLASH_GetStatus();
102 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
103 while ((status == FLASH_BUSY) && (Timeout != 0x00)) {
104 delay();
105 status = FLASH_GetStatus();
106 Timeout--;
107 }
108 if (Timeout == 0) status = FLASH_TIMEOUT;
109 /* Return the operation status */
110 return status;
111}
112
113/**
114 * @brief Erases a specified FLASH page.
115 * @param Page_Address: The page address to be erased.
116 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
117 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
118 */
119FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
120 FLASH_Status status = FLASH_COMPLETE;
121 /* Check the parameters */
122 ASSERT(IS_FLASH_ADDRESS(Page_Address));
123 /* Wait for last operation to be completed */
124 status = FLASH_WaitForLastOperation(EraseTimeout);
125
126 if (status == FLASH_COMPLETE) {
127 /* if the previous operation is completed, proceed to erase the page */
128#if defined(FLASH_CR_SNB)
129 FLASH->CR &= ~FLASH_CR_SNB;
130 FLASH->CR |= FLASH_CR_SER | (ADDR2PAGE(Page_Address) << FLASH_CR_SNB_Pos);
131#else
132 FLASH->CR |= FLASH_CR_PER;
133 FLASH->AR = Page_Address;
134#endif
135 FLASH->CR |= FLASH_CR_STRT;
136
137 /* Wait for last operation to be completed */
138 status = FLASH_WaitForLastOperation(EraseTimeout);
139 if (status != FLASH_TIMEOUT) {
140 /* if the erase operation is completed, disable the configured Bits */
141#if defined(FLASH_CR_SNB)
142 FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB);
143#else
144 FLASH->CR &= ~FLASH_CR_PER;
145#endif
146 }
147 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
148 }
149 /* Return the Erase Status */
150 return status;
151}
152
153/**
154 * @brief Programs a half word at a specified address.
155 * @param Address: specifies the address to be programmed.
156 * @param Data: specifies the data to be programmed.
157 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
158 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
159 */
160FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
161 FLASH_Status status = FLASH_BAD_ADDRESS;
162
163 if (IS_FLASH_ADDRESS(Address)) {
164 /* Wait for last operation to be completed */
165 status = FLASH_WaitForLastOperation(ProgramTimeout);
166 if (status == FLASH_COMPLETE) {
167 /* if the previous operation is completed, proceed to program the new data */
168
169#if defined(FLASH_CR_PSIZE)
170 FLASH->CR &= ~FLASH_CR_PSIZE;
171 FLASH->CR |= FLASH_CR_PSIZE_0;
172#endif
173 FLASH->CR |= FLASH_CR_PG;
174 *(__IO uint16_t*)Address = Data;
175 /* Wait for last operation to be completed */
176 status = FLASH_WaitForLastOperation(ProgramTimeout);
177 if (status != FLASH_TIMEOUT) {
178 /* if the program operation is completed, disable the PG Bit */
179 FLASH->CR &= ~FLASH_CR_PG;
180 }
181 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
182 }
183 }
184 return status;
185}
186
187/**
188 * @brief Unlocks the FLASH Program Erase Controller.
189 * @param None
190 * @retval None
191 */
192void FLASH_Unlock(void) {
193 if (FLASH->CR & FLASH_CR_LOCK) {
194 /* Authorize the FPEC Access */
195 FLASH->KEYR = FLASH_KEY1;
196 FLASH->KEYR = FLASH_KEY2;
197 }
198}
199
200/**
201 * @brief Locks the FLASH Program Erase Controller.
202 * @param None
203 * @retval None
204 */
205void FLASH_Lock(void) {
206 /* Set the Lock Bit to lock the FPEC and the FCR */
207 FLASH->CR |= FLASH_CR_LOCK;
208}
diff --git a/tmk_core/common/chibios/flash_stm32.h b/tmk_core/common/chibios/flash_stm32.h
deleted file mode 100644
index 6c66642ec..000000000
--- a/tmk_core/common/chibios/flash_stm32.h
+++ /dev/null
@@ -1,44 +0,0 @@
1/*
2 * This software is experimental and a work in progress.
3 * Under no circumstances should these files be used in relation to any critical system(s).
4 * Use of these files is at your own risk.
5 *
6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
7 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
8 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
9 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
11 * DEALINGS IN THE SOFTWARE.
12 *
13 * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
14 * https://github.com/leaflabs/libmaple
15 *
16 * Modifications for QMK and STM32F303 by Yiancar
17 */
18
19#pragma once
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25#include <stdint.h>
26
27#ifdef FLASH_STM32_MOCKED
28extern uint8_t FlashBuf[MOCK_FLASH_SIZE];
29#endif
30
31typedef enum { FLASH_BUSY = 1, FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_ERROR_OPT, FLASH_COMPLETE, FLASH_TIMEOUT, FLASH_BAD_ADDRESS } FLASH_Status;
32
33#define IS_FLASH_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x0807FFFF))
34
35FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
36FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
37FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
38
39void FLASH_Unlock(void);
40void FLASH_Lock(void);
41
42#ifdef __cplusplus
43}
44#endif
diff --git a/tmk_core/common/chibios/gd32v_compatibility.h b/tmk_core/common/chibios/gd32v_compatibility.h
deleted file mode 100644
index f4dcfd8c5..000000000
--- a/tmk_core/common/chibios/gd32v_compatibility.h
+++ /dev/null
@@ -1,120 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19/* GD32VF103 has the same API as STM32F103, but uses different names for literally the same thing.
20 * As of 23.7.2021 QMK is tailored to use STM32 defines/names, for compatibility sake
21 * we just redefine the GD32 names. */
22
23/* Close your eyes kids. */
24#define MCU_STM32
25
26/* AFIO redefines */
27#define MAPR PCF0
28#define AFIO_MAPR_USART1_REMAP AFIO_PCF0_USART0_REMAP
29#define AFIO_MAPR_USART2_REMAP AFIO_PCF0_USART1_REMAP
30#define AFIO_MAPR_USART3_REMAP_PARTIALREMAP AFIO_PCF0_USART2_REMAP_PARTIALREMAP
31#define AFIO_MAPR_USART3_REMAP_FULLREMAP AFIO_PCF0_USART2_REMAP_FULLREMAP
32
33/* DMA redefines. */
34#define STM32_DMA_STREAM(stream) GD32_DMA_STREAM(stream)
35#define STM32_DMA_STREAM_ID(peripheral, channel) GD32_DMA_STREAM_ID(peripheral - 1, channel - 1)
36#define STM32_DMA_CR_DIR_M2P GD32_DMA_CTL_DIR_M2P
37#define STM32_DMA_CR_PSIZE_WORD GD32_DMA_CTL_PWIDTH_WORD
38#define STM32_DMA_CR_MSIZE_WORD GD32_DMA_CTL_MWIDTH_WORD
39#define STM32_DMA_CR_MINC GD32_DMA_CTL_MNAGA
40#define STM32_DMA_CR_CIRC GD32_DMA_CTL_CMEN
41#define STM32_DMA_CR_PL GD32_DMA_CTL_PRIO
42#define STM32_DMA_CR_CHSEL GD32_DMA_CTL_CHSEL
43#define cr1 ctl0
44#define cr2 ctl1
45#define cr3 ctl2
46#define dier dmainten
47
48/* ADC redefines */
49#if HAL_USE_ADC
50# define STM32_ADC_USE_ADC1 GD32_ADC_USE_ADC0
51
52# define smpr1 sampt0
53# define smpr2 sampt1
54# define sqr1 rsq0
55# define sqr2 rsq1
56# define sqr3 rsq2
57
58# define ADC_SMPR2_SMP_AN0 ADC_SAMPT1_SMP_SPT0
59# define ADC_SMPR2_SMP_AN1 ADC_SAMPT1_SMP_SPT1
60# define ADC_SMPR2_SMP_AN2 ADC_SAMPT1_SMP_SPT2
61# define ADC_SMPR2_SMP_AN3 ADC_SAMPT1_SMP_SPT3
62# define ADC_SMPR2_SMP_AN4 ADC_SAMPT1_SMP_SPT4
63# define ADC_SMPR2_SMP_AN5 ADC_SAMPT1_SMP_SPT5
64# define ADC_SMPR2_SMP_AN6 ADC_SAMPT1_SMP_SPT6
65# define ADC_SMPR2_SMP_AN7 ADC_SAMPT1_SMP_SPT7
66# define ADC_SMPR2_SMP_AN8 ADC_SAMPT1_SMP_SPT8
67# define ADC_SMPR2_SMP_AN9 ADC_SAMPT1_SMP_SPT9
68
69# define ADC_SMPR1_SMP_AN10 ADC_SAMPT0_SMP_SPT10
70# define ADC_SMPR1_SMP_AN11 ADC_SAMPT0_SMP_SPT11
71# define ADC_SMPR1_SMP_AN12 ADC_SAMPT0_SMP_SPT12
72# define ADC_SMPR1_SMP_AN13 ADC_SAMPT0_SMP_SPT13
73# define ADC_SMPR1_SMP_AN14 ADC_SAMPT0_SMP_SPT14
74# define ADC_SMPR1_SMP_AN15 ADC_SAMPT0_SMP_SPT15
75
76# define ADC_SQR3_SQ1_N ADC_RSQ2_RSQ1_N
77#endif
78
79/* FLASH redefines */
80#if defined(EEPROM_ENABLE)
81# define SR STAT
82# define FLASH_SR_BSY FLASH_STAT_BUSY
83# define FLASH_SR_PGERR FLASH_STAT_PGERR
84# define FLASH_SR_EOP FLASH_STAT_ENDF
85# define FLASH_SR_WRPRTERR FLASH_STAT_WPERR
86# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
87# define FLASH_OBR_OPTERR FLASH_OBSTAT_OBERR
88# define AR ADDR
89# define CR CTL
90# define FLASH_CR_PER FLASH_CTL_PER
91# define FLASH_CR_STRT FLASH_CTL_START
92# define FLASH_CR_LOCK FLASH_CTL_LK
93# define FLASH_CR_PG FLASH_CTL_PG
94# define KEYR KEY
95#endif
96
97/* Serial USART redefines. */
98#if HAL_USE_SERIAL
99# if !defined(SERIAL_USART_CR1)
100# define SERIAL_USART_CR1 (USART_CTL0_PCEN | USART_CTL0_PM | USART_CTL0_WL) // parity enable, odd parity, 9 bit length
101# endif
102# if !defined(SERIAL_USART_CR2)
103# define SERIAL_USART_CR2 (USART_CTL1_STB_1) // 2 stop bits
104# endif
105# if !defined(SERIAL_USART_CR3)
106# define SERIAL_USART_CR3 0x0
107# endif
108# define USART_CR3_HDSEL USART_CTL2_HDEN
109# define CCR CHCV
110#endif
111
112/* SPI redefines. */
113#if HAL_USE_SPI
114# define SPI_CR1_LSBFIRST SPI_CTL0_LF
115# define SPI_CR1_CPHA SPI_CTL0_CKPH
116# define SPI_CR1_CPOL SPI_CTL0_CKPL
117# define SPI_CR1_BR_0 SPI_CTL0_PSC_0
118# define SPI_CR1_BR_1 SPI_CTL0_PSC_1
119# define SPI_CR1_BR_2 SPI_CTL0_PSC_2
120#endif
diff --git a/tmk_core/common/chibios/gpio.h b/tmk_core/common/chibios/gpio.h
deleted file mode 100644
index 4d057f1ca..000000000
--- a/tmk_core/common/chibios/gpio.h
+++ /dev/null
@@ -1,50 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <hal.h>
19#include "pin_defs.h"
20
21typedef ioline_t pin_t;
22
23/* Operation of GPIO by pin. */
24
25#define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
26#define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
27#define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
28#define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
29
30#define writePinHigh(pin) palSetLine(pin)
31#define writePinLow(pin) palClearLine(pin)
32#define writePin(pin, level) ((level) ? (writePinHigh(pin)) : (writePinLow(pin)))
33
34#define readPin(pin) palReadLine(pin)
35
36#define togglePin(pin) palToggleLine(pin)
37
38/* Operation of GPIO by port. */
39
40typedef uint16_t port_data_t;
41
42#define readPort(pin) palReadPort(PAL_PORT(pin))
43
44#define setPortBitInput(pin, bit) palSetPadMode(PAL_PORT(pin), bit, PAL_MODE_INPUT)
45#define setPortBitInputHigh(pin, bit) palSetPadMode(PAL_PORT(pin), bit, PAL_MODE_INPUT_PULLUP)
46#define setPortBitInputLow(pin, bit) palSetPadMode(PAL_PORT(pin), bit, PAL_MODE_INPUT_PULLDOWN)
47#define setPortBitOutput(pin, bit) palSetPadMode(PAL_PORT(pin), bit, PAL_MODE_OUTPUT_PUSHPULL)
48
49#define writePortBitLow(pin, bit) palClearLine(PAL_LINE(PAL_PORT(pin), bit))
50#define writePortBitHigh(pin, bit) palSetLine(PAL_LINE(PAL_PORT(pin), bit))
diff --git a/tmk_core/common/chibios/pin_defs.h b/tmk_core/common/chibios/pin_defs.h
deleted file mode 100644
index c03f8de0c..000000000
--- a/tmk_core/common/chibios/pin_defs.h
+++ /dev/null
@@ -1,323 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// Defines mapping for Proton C replacement
19#ifdef CONVERT_TO_PROTON_C
20// Left side (front)
21# define D3 PAL_LINE(GPIOA, 9)
22# define D2 PAL_LINE(GPIOA, 10)
23// GND
24// GND
25# define D1 PAL_LINE(GPIOB, 7)
26# define D0 PAL_LINE(GPIOB, 6)
27# define D4 PAL_LINE(GPIOB, 5)
28# define C6 PAL_LINE(GPIOB, 4)
29# define D7 PAL_LINE(GPIOB, 3)
30# define E6 PAL_LINE(GPIOB, 2)
31# define B4 PAL_LINE(GPIOB, 1)
32# define B5 PAL_LINE(GPIOB, 0)
33
34// Right side (front)
35// RAW
36// GND
37// RESET
38// VCC
39# define F4 PAL_LINE(GPIOA, 2)
40# define F5 PAL_LINE(GPIOA, 1)
41# define F6 PAL_LINE(GPIOA, 0)
42# define F7 PAL_LINE(GPIOB, 8)
43# define B1 PAL_LINE(GPIOB, 13)
44# define B3 PAL_LINE(GPIOB, 14)
45# define B2 PAL_LINE(GPIOB, 15)
46# define B6 PAL_LINE(GPIOB, 9)
47
48// LEDs (only D5/C13 uses an actual LED)
49# ifdef CONVERT_TO_PROTON_C_RXLED
50# define D5 PAL_LINE(GPIOC, 14)
51# define B0 PAL_LINE(GPIOC, 13)
52# else
53# define D5 PAL_LINE(GPIOC, 13)
54# define B0 PAL_LINE(GPIOC, 14)
55# endif
56#else
57# define A0 PAL_LINE(GPIOA, 0)
58# define A1 PAL_LINE(GPIOA, 1)
59# define A2 PAL_LINE(GPIOA, 2)
60# define A3 PAL_LINE(GPIOA, 3)
61# define A4 PAL_LINE(GPIOA, 4)
62# define A5 PAL_LINE(GPIOA, 5)
63# define A6 PAL_LINE(GPIOA, 6)
64# define A7 PAL_LINE(GPIOA, 7)
65# define A8 PAL_LINE(GPIOA, 8)
66# define A9 PAL_LINE(GPIOA, 9)
67# define A10 PAL_LINE(GPIOA, 10)
68# define A11 PAL_LINE(GPIOA, 11)
69# define A12 PAL_LINE(GPIOA, 12)
70# define A13 PAL_LINE(GPIOA, 13)
71# define A14 PAL_LINE(GPIOA, 14)
72# define A15 PAL_LINE(GPIOA, 15)
73# define A16 PAL_LINE(GPIOA, 16)
74# define A17 PAL_LINE(GPIOA, 17)
75# define A18 PAL_LINE(GPIOA, 18)
76# define A19 PAL_LINE(GPIOA, 19)
77# define A20 PAL_LINE(GPIOA, 20)
78# define A21 PAL_LINE(GPIOA, 21)
79# define A22 PAL_LINE(GPIOA, 22)
80# define A23 PAL_LINE(GPIOA, 23)
81# define A24 PAL_LINE(GPIOA, 24)
82# define A25 PAL_LINE(GPIOA, 25)
83# define A26 PAL_LINE(GPIOA, 26)
84# define A27 PAL_LINE(GPIOA, 27)
85# define A28 PAL_LINE(GPIOA, 28)
86# define A29 PAL_LINE(GPIOA, 29)
87# define A30 PAL_LINE(GPIOA, 30)
88# define A31 PAL_LINE(GPIOA, 31)
89# define A32 PAL_LINE(GPIOA, 32)
90# define B0 PAL_LINE(GPIOB, 0)
91# define B1 PAL_LINE(GPIOB, 1)
92# define B2 PAL_LINE(GPIOB, 2)
93# define B3 PAL_LINE(GPIOB, 3)
94# define B4 PAL_LINE(GPIOB, 4)
95# define B5 PAL_LINE(GPIOB, 5)
96# define B6 PAL_LINE(GPIOB, 6)
97# define B7 PAL_LINE(GPIOB, 7)
98# define B8 PAL_LINE(GPIOB, 8)
99# define B9 PAL_LINE(GPIOB, 9)
100# define B10 PAL_LINE(GPIOB, 10)
101# define B11 PAL_LINE(GPIOB, 11)
102# define B12 PAL_LINE(GPIOB, 12)
103# define B13 PAL_LINE(GPIOB, 13)
104# define B14 PAL_LINE(GPIOB, 14)
105# define B15 PAL_LINE(GPIOB, 15)
106# define B16 PAL_LINE(GPIOB, 16)
107# define B17 PAL_LINE(GPIOB, 17)
108# define B18 PAL_LINE(GPIOB, 18)
109# define B19 PAL_LINE(GPIOB, 19)
110# define B20 PAL_LINE(GPIOB, 20)
111# define B21 PAL_LINE(GPIOB, 21)
112# define B22 PAL_LINE(GPIOB, 22)
113# define B23 PAL_LINE(GPIOB, 23)
114# define B24 PAL_LINE(GPIOB, 24)
115# define B25 PAL_LINE(GPIOB, 25)
116# define B26 PAL_LINE(GPIOB, 26)
117# define B27 PAL_LINE(GPIOB, 27)
118# define B28 PAL_LINE(GPIOB, 28)
119# define B29 PAL_LINE(GPIOB, 29)
120# define B30 PAL_LINE(GPIOB, 30)
121# define B31 PAL_LINE(GPIOB, 31)
122# define B32 PAL_LINE(GPIOB, 32)
123# define C0 PAL_LINE(GPIOC, 0)
124# define C1 PAL_LINE(GPIOC, 1)
125# define C2 PAL_LINE(GPIOC, 2)
126# define C3 PAL_LINE(GPIOC, 3)
127# define C4 PAL_LINE(GPIOC, 4)
128# define C5 PAL_LINE(GPIOC, 5)
129# define C6 PAL_LINE(GPIOC, 6)
130# define C7 PAL_LINE(GPIOC, 7)
131# define C8 PAL_LINE(GPIOC, 8)
132# define C9 PAL_LINE(GPIOC, 9)
133# define C10 PAL_LINE(GPIOC, 10)
134# define C11 PAL_LINE(GPIOC, 11)
135# define C12 PAL_LINE(GPIOC, 12)
136# define C13 PAL_LINE(GPIOC, 13)
137# define C14 PAL_LINE(GPIOC, 14)
138# define C15 PAL_LINE(GPIOC, 15)
139# define C16 PAL_LINE(GPIOC, 16)
140# define C17 PAL_LINE(GPIOC, 17)
141# define C18 PAL_LINE(GPIOC, 18)
142# define C19 PAL_LINE(GPIOC, 19)
143# define C20 PAL_LINE(GPIOC, 20)
144# define C21 PAL_LINE(GPIOC, 21)
145# define C22 PAL_LINE(GPIOC, 22)
146# define C23 PAL_LINE(GPIOC, 23)
147# define C24 PAL_LINE(GPIOC, 24)
148# define C25 PAL_LINE(GPIOC, 25)
149# define C26 PAL_LINE(GPIOC, 26)
150# define C27 PAL_LINE(GPIOC, 27)
151# define C28 PAL_LINE(GPIOC, 28)
152# define C29 PAL_LINE(GPIOC, 29)
153# define C30 PAL_LINE(GPIOC, 30)
154# define C31 PAL_LINE(GPIOC, 31)
155# define C32 PAL_LINE(GPIOC, 32)
156# define D0 PAL_LINE(GPIOD, 0)
157# define D1 PAL_LINE(GPIOD, 1)
158# define D2 PAL_LINE(GPIOD, 2)
159# define D3 PAL_LINE(GPIOD, 3)
160# define D4 PAL_LINE(GPIOD, 4)
161# define D5 PAL_LINE(GPIOD, 5)
162# define D6 PAL_LINE(GPIOD, 6)
163# define D7 PAL_LINE(GPIOD, 7)
164# define D8 PAL_LINE(GPIOD, 8)
165# define D9 PAL_LINE(GPIOD, 9)
166# define D10 PAL_LINE(GPIOD, 10)
167# define D11 PAL_LINE(GPIOD, 11)
168# define D12 PAL_LINE(GPIOD, 12)
169# define D13 PAL_LINE(GPIOD, 13)
170# define D14 PAL_LINE(GPIOD, 14)
171# define D15 PAL_LINE(GPIOD, 15)
172# define D16 PAL_LINE(GPIOD, 16)
173# define D17 PAL_LINE(GPIOD, 17)
174# define D18 PAL_LINE(GPIOD, 18)
175# define D19 PAL_LINE(GPIOD, 19)
176# define D20 PAL_LINE(GPIOD, 20)
177# define D21 PAL_LINE(GPIOD, 21)
178# define D22 PAL_LINE(GPIOD, 22)
179# define D23 PAL_LINE(GPIOD, 23)
180# define D24 PAL_LINE(GPIOD, 24)
181# define D25 PAL_LINE(GPIOD, 25)
182# define D26 PAL_LINE(GPIOD, 26)
183# define D27 PAL_LINE(GPIOD, 27)
184# define D28 PAL_LINE(GPIOD, 28)
185# define D29 PAL_LINE(GPIOD, 29)
186# define D30 PAL_LINE(GPIOD, 30)
187# define D31 PAL_LINE(GPIOD, 31)
188# define D32 PAL_LINE(GPIOD, 32)
189# define E0 PAL_LINE(GPIOE, 0)
190# define E1 PAL_LINE(GPIOE, 1)
191# define E2 PAL_LINE(GPIOE, 2)
192# define E3 PAL_LINE(GPIOE, 3)
193# define E4 PAL_LINE(GPIOE, 4)
194# define E5 PAL_LINE(GPIOE, 5)
195# define E6 PAL_LINE(GPIOE, 6)
196# define E7 PAL_LINE(GPIOE, 7)
197# define E8 PAL_LINE(GPIOE, 8)
198# define E9 PAL_LINE(GPIOE, 9)
199# define E10 PAL_LINE(GPIOE, 10)
200# define E11 PAL_LINE(GPIOE, 11)
201# define E12 PAL_LINE(GPIOE, 12)
202# define E13 PAL_LINE(GPIOE, 13)
203# define E14 PAL_LINE(GPIOE, 14)
204# define E15 PAL_LINE(GPIOE, 15)
205# define E16 PAL_LINE(GPIOE, 16)
206# define E17 PAL_LINE(GPIOE, 17)
207# define E18 PAL_LINE(GPIOE, 18)
208# define E19 PAL_LINE(GPIOE, 19)
209# define E20 PAL_LINE(GPIOE, 20)
210# define E21 PAL_LINE(GPIOE, 21)
211# define E22 PAL_LINE(GPIOE, 22)
212# define E23 PAL_LINE(GPIOE, 23)
213# define E24 PAL_LINE(GPIOE, 24)
214# define E25 PAL_LINE(GPIOE, 25)
215# define E26 PAL_LINE(GPIOE, 26)
216# define E27 PAL_LINE(GPIOE, 27)
217# define E28 PAL_LINE(GPIOE, 28)
218# define E29 PAL_LINE(GPIOE, 29)
219# define E30 PAL_LINE(GPIOE, 30)
220# define E31 PAL_LINE(GPIOE, 31)
221# define E32 PAL_LINE(GPIOE, 32)
222# define F0 PAL_LINE(GPIOF, 0)
223# define F1 PAL_LINE(GPIOF, 1)
224# define F2 PAL_LINE(GPIOF, 2)
225# define F3 PAL_LINE(GPIOF, 3)
226# define F4 PAL_LINE(GPIOF, 4)
227# define F5 PAL_LINE(GPIOF, 5)
228# define F6 PAL_LINE(GPIOF, 6)
229# define F7 PAL_LINE(GPIOF, 7)
230# define F8 PAL_LINE(GPIOF, 8)
231# define F9 PAL_LINE(GPIOF, 9)
232# define F10 PAL_LINE(GPIOF, 10)
233# define F11 PAL_LINE(GPIOF, 11)
234# define F12 PAL_LINE(GPIOF, 12)
235# define F13 PAL_LINE(GPIOF, 13)
236# define F14 PAL_LINE(GPIOF, 14)
237# define F15 PAL_LINE(GPIOF, 15)
238# define G0 PAL_LINE(GPIOG, 0)
239# define G1 PAL_LINE(GPIOG, 1)
240# define G2 PAL_LINE(GPIOG, 2)
241# define G3 PAL_LINE(GPIOG, 3)
242# define G4 PAL_LINE(GPIOG, 4)
243# define G5 PAL_LINE(GPIOG, 5)
244# define G6 PAL_LINE(GPIOG, 6)
245# define G7 PAL_LINE(GPIOG, 7)
246# define G8 PAL_LINE(GPIOG, 8)
247# define G9 PAL_LINE(GPIOG, 9)
248# define G10 PAL_LINE(GPIOG, 10)
249# define G11 PAL_LINE(GPIOG, 11)
250# define G12 PAL_LINE(GPIOG, 12)
251# define G13 PAL_LINE(GPIOG, 13)
252# define G14 PAL_LINE(GPIOG, 14)
253# define G15 PAL_LINE(GPIOG, 15)
254# define H0 PAL_LINE(GPIOH, 0)
255# define H1 PAL_LINE(GPIOH, 1)
256# define H2 PAL_LINE(GPIOH, 2)
257# define H3 PAL_LINE(GPIOH, 3)
258# define H4 PAL_LINE(GPIOH, 4)
259# define H5 PAL_LINE(GPIOH, 5)
260# define H6 PAL_LINE(GPIOH, 6)
261# define H7 PAL_LINE(GPIOH, 7)
262# define H8 PAL_LINE(GPIOH, 8)
263# define H9 PAL_LINE(GPIOH, 9)
264# define H10 PAL_LINE(GPIOH, 10)
265# define H11 PAL_LINE(GPIOH, 11)
266# define H12 PAL_LINE(GPIOH, 12)
267# define H13 PAL_LINE(GPIOH, 13)
268# define H14 PAL_LINE(GPIOH, 14)
269# define H15 PAL_LINE(GPIOH, 15)
270# define I0 PAL_LINE(GPIOI, 0)
271# define I1 PAL_LINE(GPIOI, 1)
272# define I2 PAL_LINE(GPIOI, 2)
273# define I3 PAL_LINE(GPIOI, 3)
274# define I4 PAL_LINE(GPIOI, 4)
275# define I5 PAL_LINE(GPIOI, 5)
276# define I6 PAL_LINE(GPIOI, 6)
277# define I7 PAL_LINE(GPIOI, 7)
278# define I8 PAL_LINE(GPIOI, 8)
279# define I9 PAL_LINE(GPIOI, 9)
280# define I10 PAL_LINE(GPIOI, 10)
281# define I11 PAL_LINE(GPIOI, 11)
282# define I12 PAL_LINE(GPIOI, 12)
283# define I13 PAL_LINE(GPIOI, 13)
284# define I14 PAL_LINE(GPIOI, 14)
285# define I15 PAL_LINE(GPIOI, 15)
286# define J0 PAL_LINE(GPIOJ, 0)
287# define J1 PAL_LINE(GPIOJ, 1)
288# define J2 PAL_LINE(GPIOJ, 2)
289# define J3 PAL_LINE(GPIOJ, 3)
290# define J4 PAL_LINE(GPIOJ, 4)
291# define J5 PAL_LINE(GPIOJ, 5)
292# define J6 PAL_LINE(GPIOJ, 6)
293# define J7 PAL_LINE(GPIOJ, 7)
294# define J8 PAL_LINE(GPIOJ, 8)
295# define J9 PAL_LINE(GPIOJ, 9)
296# define J10 PAL_LINE(GPIOJ, 10)
297# define J11 PAL_LINE(GPIOJ, 11)
298# define J12 PAL_LINE(GPIOJ, 12)
299# define J13 PAL_LINE(GPIOJ, 13)
300# define J14 PAL_LINE(GPIOJ, 14)
301# define J15 PAL_LINE(GPIOJ, 15)
302// Keyboards can `#define KEYBOARD_REQUIRES_GPIOK` if they need to access GPIO-K pins. These conflict with a whole
303// bunch of layout definitions, so it's intentionally left out unless absolutely required -- in that case, the
304// keyboard designer should use a different symbol when defining their layout macros.
305# ifdef KEYBOARD_REQUIRES_GPIOK
306# define K0 PAL_LINE(GPIOK, 0)
307# define K1 PAL_LINE(GPIOK, 1)
308# define K2 PAL_LINE(GPIOK, 2)
309# define K3 PAL_LINE(GPIOK, 3)
310# define K4 PAL_LINE(GPIOK, 4)
311# define K5 PAL_LINE(GPIOK, 5)
312# define K6 PAL_LINE(GPIOK, 6)
313# define K7 PAL_LINE(GPIOK, 7)
314# define K8 PAL_LINE(GPIOK, 8)
315# define K9 PAL_LINE(GPIOK, 9)
316# define K10 PAL_LINE(GPIOK, 10)
317# define K11 PAL_LINE(GPIOK, 11)
318# define K12 PAL_LINE(GPIOK, 12)
319# define K13 PAL_LINE(GPIOK, 13)
320# define K14 PAL_LINE(GPIOK, 14)
321# define K15 PAL_LINE(GPIOK, 15)
322# endif
323#endif
diff --git a/tmk_core/common/chibios/platform.c b/tmk_core/common/chibios/platform.c
deleted file mode 100644
index d4a229f27..000000000
--- a/tmk_core/common/chibios/platform.c
+++ /dev/null
@@ -1,22 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "platform_deps.h"
18
19void platform_setup(void) {
20 halInit();
21 chSysInit();
22} \ No newline at end of file
diff --git a/tmk_core/common/chibios/platform_deps.h b/tmk_core/common/chibios/platform_deps.h
deleted file mode 100644
index 8243dcec5..000000000
--- a/tmk_core/common/chibios/platform_deps.h
+++ /dev/null
@@ -1,19 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <hal.h>
19#include "chibios_config.h"
diff --git a/tmk_core/common/chibios/sleep_led.c b/tmk_core/common/chibios/sleep_led.c
deleted file mode 100644
index 477056a45..000000000
--- a/tmk_core/common/chibios/sleep_led.c
+++ /dev/null
@@ -1,192 +0,0 @@
1#include <ch.h>
2#include <hal.h>
3
4#include "led.h"
5#include "sleep_led.h"
6
7/* All right, we go the "software" way: timer, toggle LED in interrupt.
8 * Based on hasu's code for AVRs.
9 * Use LP timer on Kinetises, TIM14 on STM32F0.
10 */
11
12#ifndef SLEEP_LED_GPT_DRIVER
13# if defined(STM32F0XX)
14# define SLEEP_LED_GPT_DRIVER GPTD14
15# endif
16#endif
17
18#if defined(KL2x) || defined(K20x) || defined(SLEEP_LED_GPT_DRIVER) /* common parts for timers/interrupts */
19
20/* Breathing Sleep LED brighness(PWM On period) table
21 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
22 *
23 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
24 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
25 */
26static const uint8_t breathing_table[64] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
27
28void sleep_led_timer_callback(void) {
29 /* Software PWM
30 * timer:1111 1111 1111 1111
31 * \_____/\/ \_______/____ count(0-255)
32 * \ \______________ duration of step(4)
33 * \__________________ index of step table(0-63)
34 */
35
36 // this works for cca 65536 irqs/sec
37 static union {
38 uint16_t row;
39 struct {
40 uint8_t count : 8;
41 uint8_t duration : 2;
42 uint8_t index : 6;
43 } pwm;
44 } timer = {.row = 0};
45
46 timer.row++;
47
48 // LED on
49 if (timer.pwm.count == 0) {
50 led_set(1 << USB_LED_CAPS_LOCK);
51 }
52 // LED off
53 if (timer.pwm.count == breathing_table[timer.pwm.index]) {
54 led_set(0);
55 }
56}
57
58#endif /* common parts for known platforms */
59
60#if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */
61
62/* Use Low Power Timer (LPTMR) */
63# define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR
64# define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF
65
66/* LPTMR clock options */
67# define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */
68# define LPTMR_CLOCK_LPO 1 /* 1kHz clock */
69# define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */
70# define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */
71
72/* Work around inconsistencies in Freescale naming */
73# if !defined(SIM_SCGC5_LPTMR)
74# define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER
75# endif
76
77/* interrupt handler */
78OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
79 OSAL_IRQ_PROLOGUE();
80
81 sleep_led_timer_callback();
82
83 /* Reset the counter */
84 RESET_COUNTER;
85
86 OSAL_IRQ_EPILOGUE();
87}
88
89/* Initialise the timer */
90void sleep_led_init(void) {
91 /* Make sure the clock to the LPTMR is enabled */
92 SIM->SCGC5 |= SIM_SCGC5_LPTMR;
93 /* Reset LPTMR settings */
94 LPTMR0->CSR = 0;
95 /* Set the compare value */
96 LPTMR0->CMR = 0; // trigger on counter value (i.e. every time)
97
98/* Set up clock source and prescaler */
99/* Software PWM
100 * ______ ______ __
101 * | ON |___OFF___| ON |___OFF___| ....
102 * |<-------------->|<-------------->|<- ....
103 * PWM period PWM period
104 *
105 * R interrupts/period[resolution]
106 * F periods/second[frequency]
107 * R * F interrupts/second
108 */
109
110/* === OPTION 1 === */
111# if 0
112 // 1kHz LPO
113 // No prescaler => 1024 irqs/sec
114 // Note: this is too slow for a smooth breathe
115 LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_LPO)|LPTMRx_PSR_PBYP;
116# endif /* OPTION 1 */
117
118/* === OPTION 2 === */
119# if 1
120 // nMHz IRC (n=4 on KL25Z, KL26Z and K20x; n=2 or 8 on KL27Z)
121 MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock
122# if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others
123 MCG->MC |= MCG_MC_LIRC_DIV2_DIV2;
124# endif /* KL27 */
125 MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock
126 // to work in stop mode, also MCG_C1_IREFSTEN
127 // Divide 4MHz by 2^N (N=6) => 62500 irqs/sec =>
128 // => approx F=61, R=256, duration = 4
129 LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_MCGIRCLK) | LPTMRx_PSR_PRESCALE(6);
130# endif /* OPTION 2 */
131
132/* === OPTION 3 === */
133# if 0
134 // OSC output (external crystal), usually 8MHz or 16MHz
135 OSC0->CR |= OSC_CR_ERCLKEN; // enable ext ref clock
136 // to work in stop mode, also OSC_CR_EREFSTEN
137 // Divide by 2^N
138 LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_OSCERCLK)|LPTMRx_PSR_PRESCALE(7);
139# endif /* OPTION 3 */
140 /* === END OPTIONS === */
141
142 /* Interrupt on TCF set (compare flag) */
143 nvicEnableVector(LPTMR0_IRQn, 2); // vector, priority
144 LPTMR0->CSR |= LPTMRx_CSR_TIE;
145}
146
147void sleep_led_enable(void) {
148 /* Enable the timer */
149 LPTMR0->CSR |= LPTMRx_CSR_TEN;
150}
151
152void sleep_led_disable(void) {
153 /* Disable the timer */
154 LPTMR0->CSR &= ~LPTMRx_CSR_TEN;
155}
156
157void sleep_led_toggle(void) {
158 /* Toggle the timer */
159 LPTMR0->CSR ^= LPTMRx_CSR_TEN;
160}
161
162#elif defined(SLEEP_LED_GPT_DRIVER)
163
164static void gptTimerCallback(GPTDriver *gptp) {
165 (void)gptp;
166 sleep_led_timer_callback();
167}
168
169static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0};
170
171/* Initialise the timer */
172void sleep_led_init(void) { gptStart(&SLEEP_LED_GPT_DRIVER, &gptcfg); }
173
174void sleep_led_enable(void) { gptStartContinuous(&SLEEP_LED_GPT_DRIVER, gptcfg.frequency / 0xFFFF); }
175
176void sleep_led_disable(void) { gptStopTimer(&SLEEP_LED_GPT_DRIVER); }
177
178void sleep_led_toggle(void) { (SLEEP_LED_GPT_DRIVER.state == GPT_READY) ? sleep_led_enable() : sleep_led_disable(); }
179
180#else /* platform selection: not on familiar chips */
181
182void sleep_led_init(void) {}
183
184void sleep_led_enable(void) { led_set(1 << USB_LED_CAPS_LOCK); }
185
186void sleep_led_disable(void) { led_set(0); }
187
188void sleep_led_toggle(void) {
189 // not implemented
190}
191
192#endif /* platform selection */
diff --git a/tmk_core/common/chibios/suspend.c b/tmk_core/common/chibios/suspend.c
deleted file mode 100644
index 9310a9992..000000000
--- a/tmk_core/common/chibios/suspend.c
+++ /dev/null
@@ -1,92 +0,0 @@
1/* TODO */
2
3#include <ch.h>
4#include <hal.h>
5
6#include "matrix.h"
7#include "action.h"
8#include "action_util.h"
9#include "mousekey.h"
10#include "programmable_button.h"
11#include "host.h"
12#include "suspend.h"
13#include "led.h"
14#include "wait.h"
15
16/** \brief suspend idle
17 *
18 * FIXME: needs doc
19 */
20void suspend_idle(uint8_t time) {
21 // TODO: this is not used anywhere - what units is 'time' in?
22 wait_ms(time);
23}
24
25/** \brief suspend power down
26 *
27 * FIXME: needs doc
28 */
29void suspend_power_down(void) {
30 suspend_power_down_quantum();
31 // on AVR, this enables the watchdog for 15ms (max), and goes to
32 // SLEEP_MODE_PWR_DOWN
33
34 wait_ms(17);
35}
36
37/** \brief suspend wakeup condition
38 *
39 * FIXME: needs doc
40 */
41__attribute__((weak)) void matrix_power_up(void) {}
42__attribute__((weak)) void matrix_power_down(void) {}
43bool suspend_wakeup_condition(void) {
44 matrix_power_up();
45 matrix_scan();
46 matrix_power_down();
47 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
48 if (matrix_get_row(r)) return true;
49 }
50 return false;
51}
52
53/** \brief run user level code immediately after wakeup
54 *
55 * FIXME: needs doc
56 */
57__attribute__((weak)) void suspend_wakeup_init_user(void) {}
58
59/** \brief run keyboard level code immediately after wakeup
60 *
61 * FIXME: needs doc
62 */
63__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }
64
65/** \brief suspend wakeup condition
66 *
67 * run immediately after wakeup
68 * FIXME: needs doc
69 */
70void suspend_wakeup_init(void) {
71 // clear keyboard state
72 // need to do it manually, because we're running from ISR
73 // and clear_keyboard() calls print
74 // so only clear the variables in memory
75 // the reports will be sent from main.c afterwards
76 // or if the PC asks for GET_REPORT
77 clear_mods();
78 clear_weak_mods();
79 clear_keys();
80#ifdef MOUSEKEY_ENABLE
81 mousekey_clear();
82#endif /* MOUSEKEY_ENABLE */
83#ifdef PROGRAMMABLE_BUTTON_ENABLE
84 programmable_button_clear();
85#endif /* PROGRAMMABLE_BUTTON_ENABLE */
86#ifdef EXTRAKEY_ENABLE
87 host_system_send(0);
88 host_consumer_send(0);
89#endif /* EXTRAKEY_ENABLE */
90
91 suspend_wakeup_init_quantum();
92}
diff --git a/tmk_core/common/chibios/syscall-fallbacks.c b/tmk_core/common/chibios/syscall-fallbacks.c
deleted file mode 100644
index 4569879c7..000000000
--- a/tmk_core/common/chibios/syscall-fallbacks.c
+++ /dev/null
@@ -1,110 +0,0 @@
1/* Copyright 2021 Nick Brassel, QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <errno.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20
21/* To compile the ChibiOS syscall stubs with picolibc
22 * the _reent struct has to be defined. */
23#if defined(USE_PICOLIBC)
24struct _reent;
25#endif
26
27#pragma GCC diagnostic ignored "-Wmissing-prototypes"
28
29__attribute__((weak, used)) int _open_r(struct _reent *r, const char *path, int flag, int m) {
30 __errno_r(r) = ENOENT;
31 return -1;
32}
33
34__attribute__((weak, used)) int _lseek_r(struct _reent *r, int file, int ptr, int dir) {
35 __errno_r(r) = EBADF;
36 return -1;
37}
38
39__attribute__((weak, used)) int _read_r(struct _reent *r, int file, char *ptr, int len) {
40 __errno_r(r) = EBADF;
41 return -1;
42}
43
44__attribute__((weak, used)) int _write_r(struct _reent *r, int file, char *ptr, int len) {
45 __errno_r(r) = EBADF;
46 return -1;
47}
48
49__attribute__((weak, used)) int _close_r(struct _reent *r, int file) {
50 __errno_r(r) = EBADF;
51 return -1;
52}
53
54__attribute__((weak, used)) int _link_r(struct _reent *r, const char *oldpath, const char *newpath) {
55 __errno_r(r) = EPERM;
56 return -1;
57}
58
59__attribute__((weak, used)) int _unlink_r(struct _reent *r, const char *path) {
60 __errno_r(r) = EPERM;
61 return -1;
62}
63
64__attribute__((weak, used)) clock_t _times_r(struct _reent *r, void *t) {
65 __errno_r(r) = EFAULT;
66 return -1;
67}
68
69__attribute__((weak, used)) int _fstat_r(struct _reent *r, int file, struct stat *st) {
70 __errno_r(r) = EBADF;
71 return -1;
72}
73
74__attribute__((weak, used)) int _isatty_r(struct _reent *r, int fd) {
75 __errno_r(r) = EBADF;
76 return 0;
77}
78
79__attribute__((weak, used)) caddr_t _sbrk_r(struct _reent *r, int incr) {
80 __errno_r(r) = ENOMEM;
81 return (caddr_t)-1;
82}
83
84__attribute__((weak, used)) int _kill(int pid, int sig) {
85 errno = EPERM;
86 return -1;
87}
88
89__attribute__((weak, used)) pid_t _getpid(void) { return 1; }
90
91__attribute__((weak, used)) void _fini(void) { return; }
92
93__attribute__((weak, used, noreturn)) void _exit(int i) {
94 while (1)
95 ;
96}
97
98__attribute__((weak, used)) int _gettimeofday_r(struct _reent *r, struct timeval *t, void *tzp) {
99 __errno_r(r) = EPERM;
100 return -1;
101}
102
103__attribute__((weak, used)) void *__dso_handle;
104
105__attribute__((weak, used)) void __cxa_pure_virtual(void) {
106 while (1)
107 ;
108}
109
110#pragma GCC diagnostic pop
diff --git a/tmk_core/common/chibios/timer.c b/tmk_core/common/chibios/timer.c
deleted file mode 100644
index 9f664e1f7..000000000
--- a/tmk_core/common/chibios/timer.c
+++ /dev/null
@@ -1,47 +0,0 @@
1#include <ch.h>
2
3#include "timer.h"
4
5static uint32_t reset_point = 0;
6#if CH_CFG_ST_RESOLUTION < 32
7static uint32_t last_systime = 0;
8static uint32_t overflow = 0;
9#endif
10
11void timer_init(void) { timer_clear(); }
12
13void timer_clear(void) {
14 reset_point = (uint32_t)chVTGetSystemTime();
15#if CH_CFG_ST_RESOLUTION < 32
16 last_systime = reset_point;
17 overflow = 0;
18#endif
19}
20
21uint16_t timer_read(void) { return (uint16_t)timer_read32(); }
22
23uint32_t timer_read32(void) {
24 uint32_t systime = (uint32_t)chVTGetSystemTime();
25
26#if CH_CFG_ST_RESOLUTION < 32
27 // If/when we need to support 64-bit chips, this may need to be modified to match the native bit-ness of the MCU.
28 // At this point, the only SysTick resolution allowed other than 32 is 16 bit.
29 // In the 16-bit case, at:
30 // - CH_CFG_ST_FREQUENCY = 100000, overflow will occur every ~0.65 seconds
31 // - CH_CFG_ST_FREQUENCY = 10000, overflow will occur every ~6.5 seconds
32 // - CH_CFG_ST_FREQUENCY = 1000, overflow will occur every ~65 seconds
33 // With this implementation, as long as we ensure a timer read happens at least once during the overflow period, timing should be accurate.
34 if (systime < last_systime) {
35 overflow += ((uint32_t)1) << CH_CFG_ST_RESOLUTION;
36 }
37
38 last_systime = systime;
39 return (uint32_t)TIME_I2MS(systime - reset_point + overflow);
40#else
41 return (uint32_t)TIME_I2MS(systime - reset_point);
42#endif
43}
44
45uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); }
46
47uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); }
diff --git a/tmk_core/common/chibios/wait.c b/tmk_core/common/chibios/wait.c
deleted file mode 100644
index 56fd6ffce..000000000
--- a/tmk_core/common/chibios/wait.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <ch.h>
18#include <hal.h>
19
20#include "_wait.h"
21
22#ifdef WAIT_US_TIMER
23void wait_us(uint16_t duration) {
24 static const GPTConfig gpt_cfg = {1000000, NULL, 0, 0}; /* 1MHz timer, no callback */
25
26 if (duration == 0) {
27 duration = 1;
28 }
29
30 /*
31 * Only use this timer on the main thread;
32 * other threads need to use their own timer.
33 */
34 if (chThdGetSelfX() == &ch.mainthread && duration < (1ULL << (sizeof(gptcnt_t) * 8))) {
35 gptStart(&WAIT_US_TIMER, &gpt_cfg);
36 gptPolledDelay(&WAIT_US_TIMER, duration);
37 } else {
38 chThdSleepMicroseconds(duration);
39 }
40}
41#endif
diff --git a/tmk_core/common/eeprom.h b/tmk_core/common/eeprom.h
deleted file mode 100644
index f5b3f0ad5..000000000
--- a/tmk_core/common/eeprom.h
+++ /dev/null
@@ -1,21 +0,0 @@
1#pragma once
2
3#if defined(__AVR__) && !defined(EEPROM_DRIVER)
4# include <avr/eeprom.h>
5#else
6# include <stdint.h>
7# include <stdlib.h>
8
9uint8_t eeprom_read_byte(const uint8_t *__p);
10uint16_t eeprom_read_word(const uint16_t *__p);
11uint32_t eeprom_read_dword(const uint32_t *__p);
12void eeprom_read_block(void *__dst, const void *__src, size_t __n);
13void eeprom_write_byte(uint8_t *__p, uint8_t __value);
14void eeprom_write_word(uint16_t *__p, uint16_t __value);
15void eeprom_write_dword(uint32_t *__p, uint32_t __value);
16void eeprom_write_block(const void *__src, void *__dst, size_t __n);
17void eeprom_update_byte(uint8_t *__p, uint8_t __value);
18void eeprom_update_word(uint16_t *__p, uint16_t __value);
19void eeprom_update_dword(uint32_t *__p, uint32_t __value);
20void eeprom_update_block(const void *__src, void *__dst, size_t __n);
21#endif
diff --git a/tmk_core/common/gpio.h b/tmk_core/common/gpio.h
deleted file mode 100644
index b47f6f8e4..000000000
--- a/tmk_core/common/gpio.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include "pin_defs.h"
19
20#if __has_include_next("gpio.h")
21# include_next "gpio.h" /* Include the platforms gpio.h */
22#endif \ No newline at end of file
diff --git a/tmk_core/common/pin_defs.h b/tmk_core/common/pin_defs.h
deleted file mode 100644
index ea730138f..000000000
--- a/tmk_core/common/pin_defs.h
+++ /dev/null
@@ -1,23 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// useful for direct pin mapping
19#define NO_PIN (pin_t)(~0)
20
21#if __has_include_next("pin_defs.h")
22# include_next "pin_defs.h" /* Include the platforms pin_defs.h */
23#endif
diff --git a/tmk_core/common/progmem.h b/tmk_core/common/progmem.h
deleted file mode 100644
index a70d8e299..000000000
--- a/tmk_core/common/progmem.h
+++ /dev/null
@@ -1,19 +0,0 @@
1#pragma once
2
3#if defined(__AVR__)
4# include <avr/pgmspace.h>
5#else
6# include <string.h>
7# define PROGMEM
8# define __flash
9# define PSTR(x) x
10# define PGM_P const char*
11# define memcpy_P(dest, src, n) memcpy(dest, src, n)
12# define pgm_read_byte(address_short) *((uint8_t*)(address_short))
13# define pgm_read_word(address_short) *((uint16_t*)(address_short))
14# define pgm_read_dword(address_short) *((uint32_t*)(address_short))
15# define pgm_read_ptr(address_short) *((void**)(address_short))
16# define strcmp_P(s1, s2) strcmp(s1, s2)
17# define strcpy_P(dest, src) strcpy(dest, src)
18# define strlen_P(src) strlen(src)
19#endif
diff --git a/tmk_core/common/sleep_led.h b/tmk_core/common/sleep_led.h
deleted file mode 100644
index 38f80a660..000000000
--- a/tmk_core/common/sleep_led.h
+++ /dev/null
@@ -1,17 +0,0 @@
1#pragma once
2
3#ifdef SLEEP_LED_ENABLE
4
5void sleep_led_init(void);
6void sleep_led_enable(void);
7void sleep_led_disable(void);
8void sleep_led_toggle(void);
9
10#else
11
12# define sleep_led_init()
13# define sleep_led_enable()
14# define sleep_led_disable()
15# define sleep_led_toggle()
16
17#endif
diff --git a/tmk_core/common/suspend.h b/tmk_core/common/suspend.h
deleted file mode 100644
index 081735f90..000000000
--- a/tmk_core/common/suspend.h
+++ /dev/null
@@ -1,20 +0,0 @@
1#pragma once
2
3#include <stdint.h>
4#include <stdbool.h>
5
6void suspend_idle(uint8_t timeout);
7void suspend_power_down(void);
8bool suspend_wakeup_condition(void);
9void suspend_wakeup_init(void);
10
11void suspend_wakeup_init_user(void);
12void suspend_wakeup_init_kb(void);
13void suspend_wakeup_init_quantum(void);
14void suspend_power_down_user(void);
15void suspend_power_down_kb(void);
16void suspend_power_down_quantum(void);
17
18#ifndef USB_SUSPEND_WAKEUP_DELAY
19# define USB_SUSPEND_WAKEUP_DELAY 0
20#endif
diff --git a/tmk_core/common/test/_wait.h b/tmk_core/common/test/_wait.h
deleted file mode 100644
index 4e22f593b..000000000
--- a/tmk_core/common/test/_wait.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <inttypes.h>
19
20void wait_ms(uint32_t ms);
21#define wait_us(us) wait_ms(us / 1000)
22#define waitInputPinDelay()
diff --git a/tmk_core/common/test/bootloader.c b/tmk_core/common/test/bootloader.c
deleted file mode 100644
index 5155d9ff0..000000000
--- a/tmk_core/common/test/bootloader.c
+++ /dev/null
@@ -1,19 +0,0 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "bootloader.h"
18
19void bootloader_jump(void) {}
diff --git a/tmk_core/common/test/eeprom.c b/tmk_core/common/test/eeprom.c
deleted file mode 100644
index 5c8e69dae..000000000
--- a/tmk_core/common/test/eeprom.c
+++ /dev/null
@@ -1,95 +0,0 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "eeprom.h"
18
19#define EEPROM_SIZE 32
20
21static uint8_t buffer[EEPROM_SIZE];
22
23uint8_t eeprom_read_byte(const uint8_t *addr) {
24 uintptr_t offset = (uintptr_t)addr;
25 return buffer[offset];
26}
27
28void eeprom_write_byte(uint8_t *addr, uint8_t value) {
29 uintptr_t offset = (uintptr_t)addr;
30 buffer[offset] = value;
31}
32
33uint16_t eeprom_read_word(const uint16_t *addr) {
34 const uint8_t *p = (const uint8_t *)addr;
35 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
36}
37
38uint32_t eeprom_read_dword(const uint32_t *addr) {
39 const uint8_t *p = (const uint8_t *)addr;
40 return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
41}
42
43void eeprom_read_block(void *buf, const void *addr, size_t len) {
44 const uint8_t *p = (const uint8_t *)addr;
45 uint8_t * dest = (uint8_t *)buf;
46 while (len--) {
47 *dest++ = eeprom_read_byte(p++);
48 }
49}
50
51void eeprom_write_word(uint16_t *addr, uint16_t value) {
52 uint8_t *p = (uint8_t *)addr;
53 eeprom_write_byte(p++, value);
54 eeprom_write_byte(p, value >> 8);
55}
56
57void eeprom_write_dword(uint32_t *addr, uint32_t value) {
58 uint8_t *p = (uint8_t *)addr;
59 eeprom_write_byte(p++, value);
60 eeprom_write_byte(p++, value >> 8);
61 eeprom_write_byte(p++, value >> 16);
62 eeprom_write_byte(p, value >> 24);
63}
64
65void eeprom_write_block(const void *buf, void *addr, size_t len) {
66 uint8_t * p = (uint8_t *)addr;
67 const uint8_t *src = (const uint8_t *)buf;
68 while (len--) {
69 eeprom_write_byte(p++, *src++);
70 }
71}
72
73void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); }
74
75void eeprom_update_word(uint16_t *addr, uint16_t value) {
76 uint8_t *p = (uint8_t *)addr;
77 eeprom_write_byte(p++, value);
78 eeprom_write_byte(p, value >> 8);
79}
80
81void eeprom_update_dword(uint32_t *addr, uint32_t value) {
82 uint8_t *p = (uint8_t *)addr;
83 eeprom_write_byte(p++, value);
84 eeprom_write_byte(p++, value >> 8);
85 eeprom_write_byte(p++, value >> 16);
86 eeprom_write_byte(p, value >> 24);
87}
88
89void eeprom_update_block(const void *buf, void *addr, size_t len) {
90 uint8_t * p = (uint8_t *)addr;
91 const uint8_t *src = (const uint8_t *)buf;
92 while (len--) {
93 eeprom_write_byte(p++, *src++);
94 }
95}
diff --git a/tmk_core/common/test/eeprom_stm32_tests.cpp b/tmk_core/common/test/eeprom_stm32_tests.cpp
deleted file mode 100644
index 5bc8d8790..000000000
--- a/tmk_core/common/test/eeprom_stm32_tests.cpp
+++ /dev/null
@@ -1,438 +0,0 @@
1/* Copyright 2021 by Don Kjer
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "gtest/gtest.h"
18
19extern "C" {
20#include "flash_stm32.h"
21#include "eeprom_stm32.h"
22#include "eeprom.h"
23}
24
25/* Mock Flash Parameters:
26 *
27 * === Large Layout ===
28 * flash size: 65536
29 * page size: 2048
30 * density pages: 16
31 * Simulated EEPROM size: 16384
32 *
33 * FlashBuf Layout:
34 * [Unused | Compact | Write Log ]
35 * [0......|32768......|49152......65535]
36 *
37 * === Tiny Layout ===
38 * flash size: 1024
39 * page size: 512
40 * density pages: 1
41 * Simulated EEPROM size: 256
42 *
43 * FlashBuf Layout:
44 * [Unused | Compact | Write Log ]
45 * [0......|512......|768......1023]
46 *
47 */
48
49#define EEPROM_SIZE (FEE_PAGE_SIZE * FEE_PAGE_COUNT / 2)
50#define LOG_SIZE EEPROM_SIZE
51#define LOG_BASE (MOCK_FLASH_SIZE - LOG_SIZE)
52#define EEPROM_BASE (LOG_BASE - EEPROM_SIZE)
53
54/* Log encoding helpers */
55#define BYTE_VALUE(addr, value) (((addr) << 8) | (value))
56#define WORD_ZERO(addr) (0x8000 | ((addr) >> 1))
57#define WORD_ONE(addr) (0xA000 | ((addr) >> 1))
58#define WORD_NEXT(addr) (0xE000 | (((addr)-0x80) >> 1))
59
60class EepromStm32Test : public testing::Test {
61 public:
62 EepromStm32Test() {}
63 ~EepromStm32Test() {}
64
65 protected:
66 void SetUp() override { EEPROM_Erase(); }
67
68 void TearDown() override {
69#ifdef EEPROM_DEBUG
70 dumpEepromDataBuf();
71#endif
72 }
73};
74
75TEST_F(EepromStm32Test, TestErase) {
76 EEPROM_WriteDataByte(0, 0x42);
77 EEPROM_Erase();
78 EXPECT_EQ(EEPROM_ReadDataByte(0), 0);
79 EXPECT_EQ(EEPROM_ReadDataByte(1), 0);
80}
81
82TEST_F(EepromStm32Test, TestReadGarbage) {
83 uint8_t garbage = 0x3c;
84 for (int i = 0; i < MOCK_FLASH_SIZE; ++i) {
85 garbage ^= 0xa3;
86 garbage += i;
87 FlashBuf[i] = garbage;
88 }
89 EEPROM_Init(); // Just verify we don't crash
90}
91
92TEST_F(EepromStm32Test, TestWriteBadAddress) {
93 EXPECT_EQ(EEPROM_WriteDataByte(EEPROM_SIZE, 0x42), FLASH_BAD_ADDRESS);
94 EXPECT_EQ(EEPROM_WriteDataWord(EEPROM_SIZE - 1, 0xbeef), FLASH_BAD_ADDRESS);
95 EXPECT_EQ(EEPROM_WriteDataWord(EEPROM_SIZE, 0xbeef), FLASH_BAD_ADDRESS);
96}
97
98TEST_F(EepromStm32Test, TestReadBadAddress) {
99 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE), 0xFF);
100 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 1), 0xFFFF);
101 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE), 0xFFFF);
102 EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 4)), 0);
103 EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 3)), 0xFF000000);
104 EXPECT_EQ(eeprom_read_dword((uint32_t*)EEPROM_SIZE), 0xFFFFFFFF);
105}
106
107TEST_F(EepromStm32Test, TestReadByte) {
108 /* Direct compacted-area baseline: Address < 0x80 */
109 FlashBuf[EEPROM_BASE + 2] = ~0xef;
110 FlashBuf[EEPROM_BASE + 3] = ~0xbe;
111 /* Direct compacted-area baseline: Address >= 0x80 */
112 FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2] = ~0x78;
113 FlashBuf[EEPROM_BASE + EEPROM_SIZE - 1] = ~0x56;
114 /* Check values */
115 EEPROM_Init();
116 EXPECT_EQ(EEPROM_ReadDataByte(2), 0xef);
117 EXPECT_EQ(EEPROM_ReadDataByte(3), 0xbe);
118 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0x78);
119 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x56);
120 /* Write Log byte value */
121 FlashBuf[LOG_BASE] = 0x65;
122 FlashBuf[LOG_BASE + 1] = 3;
123 /* Write Log word value */
124 *(uint16_t*)&FlashBuf[LOG_BASE + 2] = WORD_NEXT(EEPROM_SIZE - 2);
125 *(uint16_t*)&FlashBuf[LOG_BASE + 4] = ~0x9abc;
126 /* Check values */
127 EEPROM_Init();
128 EXPECT_EQ(EEPROM_ReadDataByte(2), 0xef);
129 EXPECT_EQ(EEPROM_ReadDataByte(3), 0x65);
130 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0xbc);
131 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x9a);
132}
133
134TEST_F(EepromStm32Test, TestWriteByte) {
135 /* Direct compacted-area baseline: Address < 0x80 */
136 EEPROM_WriteDataByte(2, 0xef);
137 EEPROM_WriteDataByte(3, 0xbe);
138 /* Direct compacted-area baseline: Address >= 0x80 */
139 EEPROM_WriteDataByte(EEPROM_SIZE - 2, 0x78);
140 EEPROM_WriteDataByte(EEPROM_SIZE - 1, 0x56);
141 /* Check values */
142 /* First write in each aligned word should have been direct */
143 EXPECT_EQ(FlashBuf[EEPROM_BASE + 2], (uint8_t)~0xef);
144 EXPECT_EQ(FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2], (uint8_t)~0x78);
145
146 /* Second write per aligned word requires a log entry */
147 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], BYTE_VALUE(3, 0xbe));
148 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 2], WORD_NEXT(EEPROM_SIZE - 1));
149 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 4], (uint16_t)~0x5678);
150}
151
152TEST_F(EepromStm32Test, TestByteRoundTrip) {
153 /* Direct compacted-area: Address < 0x80 */
154 EEPROM_WriteDataWord(0, 0xdead);
155 EEPROM_WriteDataByte(2, 0xef);
156 EEPROM_WriteDataByte(3, 0xbe);
157 /* Direct compacted-area: Address >= 0x80 */
158 EEPROM_WriteDataByte(EEPROM_SIZE - 2, 0x78);
159 EEPROM_WriteDataByte(EEPROM_SIZE - 1, 0x56);
160 /* Check values */
161 EEPROM_Init();
162 EXPECT_EQ(EEPROM_ReadDataByte(0), 0xad);
163 EXPECT_EQ(EEPROM_ReadDataByte(1), 0xde);
164 EXPECT_EQ(EEPROM_ReadDataByte(2), 0xef);
165 EXPECT_EQ(EEPROM_ReadDataByte(3), 0xbe);
166 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0x78);
167 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x56);
168 /* Write log entries */
169 EEPROM_WriteDataByte(2, 0x80);
170 EEPROM_WriteDataByte(EEPROM_SIZE - 2, 0x3c);
171 /* Check values */
172 EEPROM_Init();
173 EXPECT_EQ(EEPROM_ReadDataByte(2), 0x80);
174 EXPECT_EQ(EEPROM_ReadDataByte(3), 0xbe);
175 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0x3c);
176 EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x56);
177}
178
179TEST_F(EepromStm32Test, TestReadWord) {
180 /* Direct compacted-area baseline: Address < 0x80 */
181 FlashBuf[EEPROM_BASE + 0] = ~0xad;
182 FlashBuf[EEPROM_BASE + 1] = ~0xde;
183 /* Direct compacted-area baseline: Address >= 0x80 */
184 FlashBuf[EEPROM_BASE + 200] = ~0xcd;
185 FlashBuf[EEPROM_BASE + 201] = ~0xab;
186 FlashBuf[EEPROM_BASE + EEPROM_SIZE - 4] = ~0x34;
187 FlashBuf[EEPROM_BASE + EEPROM_SIZE - 3] = ~0x12;
188 FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2] = ~0x78;
189 FlashBuf[EEPROM_BASE + EEPROM_SIZE - 1] = ~0x56;
190 /* Check values */
191 EEPROM_Init();
192 EXPECT_EQ(EEPROM_ReadDataWord(0), 0xdead);
193 EXPECT_EQ(EEPROM_ReadDataWord(200), 0xabcd);
194 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 0x1234);
195 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 0x5678);
196 /* Write Log word zero-encoded */
197 *(uint16_t*)&FlashBuf[LOG_BASE] = WORD_ZERO(200);
198 /* Write Log word one-encoded */
199 *(uint16_t*)&FlashBuf[LOG_BASE + 2] = WORD_ONE(EEPROM_SIZE - 4);
200 /* Write Log word value */
201 *(uint16_t*)&FlashBuf[LOG_BASE + 4] = WORD_NEXT(EEPROM_SIZE - 2);
202 *(uint16_t*)&FlashBuf[LOG_BASE + 6] = ~0x9abc;
203 /* Check values */
204 EEPROM_Init();
205 EXPECT_EQ(EEPROM_ReadDataWord(200), 0);
206 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 1);
207 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 0x9abc);
208}
209
210TEST_F(EepromStm32Test, TestWriteWord) {
211 /* Direct compacted-area: Address < 0x80 */
212 EEPROM_WriteDataWord(0, 0xdead); // Aligned
213 EEPROM_WriteDataWord(3, 0xbeef); // Unaligned
214 /* Direct compacted-area: Address >= 0x80 */
215 EEPROM_WriteDataWord(200, 0xabcd); // Aligned
216 EEPROM_WriteDataWord(203, 0x9876); // Unaligned
217 EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0x1234);
218 EEPROM_WriteDataWord(EEPROM_SIZE - 2, 0x5678);
219 /* Write Log word zero-encoded */
220 EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0);
221 /* Write Log word one-encoded */
222 EEPROM_WriteDataWord(EEPROM_SIZE - 2, 1);
223 /* Write Log word value aligned */
224 EEPROM_WriteDataWord(200, 0x4321); // Aligned
225 /* Write Log word value unaligned */
226 EEPROM_WriteDataByte(202, 0x3c); // Set neighboring byte
227 EEPROM_WriteDataWord(203, 0xcdef); // Unaligned
228 /* Check values */
229 /* Direct compacted-area */
230 EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE], (uint16_t)~0xdead);
231 EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + 3], (uint16_t)~0xbeef);
232 EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + 200], (uint16_t)~0xabcd);
233 EXPECT_EQ(FlashBuf[EEPROM_BASE + 203], (uint8_t)~0x76);
234 EXPECT_EQ(FlashBuf[EEPROM_BASE + 204], (uint8_t)~0x98);
235 EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + EEPROM_SIZE - 4], (uint16_t)~0x1234);
236 EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2], (uint16_t)~0x5678);
237 /* Write Log word zero-encoded */
238 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], WORD_ZERO(EEPROM_SIZE - 4));
239 /* Write Log word one-encoded */
240 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 2], WORD_ONE(EEPROM_SIZE - 2));
241 /* Write Log word value aligned */
242 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 4], WORD_NEXT(200));
243 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 6], (uint16_t)~0x4321);
244 /* Write Log word value unaligned */
245 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 8], WORD_NEXT(202));
246 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 10], (uint16_t)~0x763c);
247 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 12], WORD_NEXT(202));
248 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 14], (uint16_t)~0xef3c);
249 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 16], WORD_NEXT(204));
250 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 18], (uint16_t)~0x00cd);
251}
252
253TEST_F(EepromStm32Test, TestWordRoundTrip) {
254 /* Direct compacted-area: Address < 0x80 */
255 EEPROM_WriteDataWord(0, 0xdead); // Aligned
256 EEPROM_WriteDataWord(3, 0xbeef); // Unaligned
257 /* Direct compacted-area: Address >= 0x80 */
258 EEPROM_WriteDataWord(200, 0xabcd); // Aligned
259 EEPROM_WriteDataWord(203, 0x9876); // Unaligned
260 EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0x1234);
261 EEPROM_WriteDataWord(EEPROM_SIZE - 2, 0x5678);
262 /* Check values */
263 EEPROM_Init();
264 EXPECT_EQ(EEPROM_ReadDataWord(0), 0xdead);
265 EXPECT_EQ(EEPROM_ReadDataWord(3), 0xbeef);
266 EXPECT_EQ(EEPROM_ReadDataWord(200), 0xabcd);
267 EXPECT_EQ(EEPROM_ReadDataWord(203), 0x9876);
268 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 0x1234);
269 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 0x5678);
270
271 /* Write Log word zero-encoded */
272 EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0);
273 /* Write Log word one-encoded */
274 EEPROM_WriteDataWord(EEPROM_SIZE - 2, 1);
275 /* Write Log word value aligned */
276 EEPROM_WriteDataWord(200, 0x4321); // Aligned
277 /* Write Log word value unaligned */
278 EEPROM_WriteDataByte(202, 0x3c); // Set neighboring byte
279 EEPROM_WriteDataWord(203, 0xcdef); // Unaligned
280 /* Check values */
281 EEPROM_Init();
282 EXPECT_EQ(EEPROM_ReadDataWord(200), 0x4321);
283 EXPECT_EQ(EEPROM_ReadDataByte(202), 0x3c);
284 EXPECT_EQ(EEPROM_ReadDataWord(203), 0xcdef);
285 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 0);
286 EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 1);
287}
288
289TEST_F(EepromStm32Test, TestByteWordBoundary) {
290 /* Direct compacted-area write */
291 EEPROM_WriteDataWord(0x7e, 0xdead);
292 EEPROM_WriteDataWord(0x80, 0xbeef);
293 /* Byte log entry */
294 EEPROM_WriteDataByte(0x7f, 0x3c);
295 /* Word log entry */
296 EEPROM_WriteDataByte(0x80, 0x18);
297 /* Check values */
298 EEPROM_Init();
299 EXPECT_EQ(EEPROM_ReadDataWord(0x7e), 0x3cad);
300 EXPECT_EQ(EEPROM_ReadDataWord(0x80), 0xbe18);
301 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], BYTE_VALUE(0x7f, 0x3c));
302 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 2], WORD_NEXT(0x80));
303 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 4], (uint16_t)~0xbe18);
304 /* Byte log entries */
305 EEPROM_WriteDataWord(0x7e, 0xcafe);
306 /* Check values */
307 EEPROM_Init();
308 EXPECT_EQ(EEPROM_ReadDataWord(0x7e), 0xcafe);
309 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 6], BYTE_VALUE(0x7e, 0xfe));
310 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 8], BYTE_VALUE(0x7f, 0xca));
311 /* Byte and Word log entries */
312 EEPROM_WriteDataWord(0x7f, 0xba5e);
313 /* Check values */
314 EEPROM_Init();
315 EXPECT_EQ(EEPROM_ReadDataWord(0x7f), 0xba5e);
316 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 10], BYTE_VALUE(0x7f, 0x5e));
317 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 12], WORD_NEXT(0x80));
318 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 14], (uint16_t)~0xbeba);
319 /* Word log entry */
320 EEPROM_WriteDataWord(0x80, 0xf00d);
321 /* Check values */
322 EEPROM_Init();
323 EXPECT_EQ(EEPROM_ReadDataWord(0x80), 0xf00d);
324 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 16], WORD_NEXT(0x80));
325 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 18], (uint16_t)~0xf00d);
326}
327
328TEST_F(EepromStm32Test, TestDWordRoundTrip) {
329 /* Direct compacted-area: Address < 0x80 */
330 eeprom_write_dword((uint32_t*)0, 0xdeadbeef); // Aligned
331 eeprom_write_dword((uint32_t*)9, 0x12345678); // Unaligned
332 /* Direct compacted-area: Address >= 0x80 */
333 eeprom_write_dword((uint32_t*)200, 0xfacef00d);
334 eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 4), 0xba5eba11); // Aligned
335 eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 9), 0xcafed00d); // Unaligned
336 /* Check direct values */
337 EEPROM_Init();
338 EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdeadbeef);
339 EXPECT_EQ(eeprom_read_dword((uint32_t*)9), 0x12345678);
340 EXPECT_EQ(eeprom_read_dword((uint32_t*)200), 0xfacef00d);
341 EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 4)), 0xba5eba11); // Aligned
342 EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 9)), 0xcafed00d); // Unaligned
343 /* Write Log byte encoded */
344 eeprom_write_dword((uint32_t*)0, 0xdecafbad);
345 eeprom_write_dword((uint32_t*)9, 0x87654321);
346 /* Write Log word encoded */
347 eeprom_write_dword((uint32_t*)200, 1);
348 /* Write Log word value aligned */
349 eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 4), 0xdeadc0de); // Aligned
350 eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 9), 0x6789abcd); // Unaligned
351 /* Check log values */
352 EEPROM_Init();
353 EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdecafbad);
354 EXPECT_EQ(eeprom_read_dword((uint32_t*)9), 0x87654321);
355 EXPECT_EQ(eeprom_read_dword((uint32_t*)200), 1);
356 EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 4)), 0xdeadc0de); // Aligned
357 EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 9)), 0x6789abcd); // Unaligned
358}
359
360TEST_F(EepromStm32Test, TestBlockRoundTrip) {
361 char src0[] = "0123456789abcdef";
362 void* src1 = (void*)&src0[1];
363 /* Various alignments of src & dst, Address < 0x80 */
364 eeprom_write_block(src0, (void*)0, sizeof(src0));
365 eeprom_write_block(src0, (void*)21, sizeof(src0));
366 eeprom_write_block(src1, (void*)40, sizeof(src0) - 1);
367 eeprom_write_block(src1, (void*)61, sizeof(src0) - 1);
368 /* Various alignments of src & dst, Address >= 0x80 */
369 eeprom_write_block(src0, (void*)140, sizeof(src0));
370 eeprom_write_block(src0, (void*)161, sizeof(src0));
371 eeprom_write_block(src1, (void*)180, sizeof(src0) - 1);
372 eeprom_write_block(src1, (void*)201, sizeof(src0) - 1);
373
374 /* Check values */
375 EEPROM_Init();
376
377 char dstBuf[256] = {0};
378 char* dst0a = (char*)dstBuf;
379 char* dst0b = (char*)&dstBuf[20];
380 char* dst1a = (char*)&dstBuf[41];
381 char* dst1b = (char*)&dstBuf[61];
382 char* dst0c = (char*)&dstBuf[80];
383 char* dst0d = (char*)&dstBuf[100];
384 char* dst1c = (char*)&dstBuf[121];
385 char* dst1d = (char*)&dstBuf[141];
386 eeprom_read_block((void*)dst0a, (void*)0, sizeof(src0));
387 eeprom_read_block((void*)dst0b, (void*)21, sizeof(src0));
388 eeprom_read_block((void*)dst1a, (void*)40, sizeof(src0) - 1);
389 eeprom_read_block((void*)dst1b, (void*)61, sizeof(src0) - 1);
390 eeprom_read_block((void*)dst0c, (void*)140, sizeof(src0));
391 eeprom_read_block((void*)dst0d, (void*)161, sizeof(src0));
392 eeprom_read_block((void*)dst1c, (void*)180, sizeof(src0) - 1);
393 eeprom_read_block((void*)dst1d, (void*)201, sizeof(src0) - 1);
394 EXPECT_EQ(strcmp((char*)src0, dst0a), 0);
395 EXPECT_EQ(strcmp((char*)src0, dst0b), 0);
396 EXPECT_EQ(strcmp((char*)src0, dst0c), 0);
397 EXPECT_EQ(strcmp((char*)src0, dst0d), 0);
398 EXPECT_EQ(strcmp((char*)src1, dst1a), 0);
399 EXPECT_EQ(strcmp((char*)src1, dst1b), 0);
400 EXPECT_EQ(strcmp((char*)src1, dst1c), 0);
401 EXPECT_EQ(strcmp((char*)src1, dst1d), 0);
402}
403
404TEST_F(EepromStm32Test, TestCompaction) {
405 /* Direct writes */
406 eeprom_write_dword((uint32_t*)0, 0xdeadbeef);
407 eeprom_write_byte((uint8_t*)4, 0x3c);
408 eeprom_write_word((uint16_t*)6, 0xd00d);
409 eeprom_write_dword((uint32_t*)150, 0xcafef00d);
410 eeprom_write_dword((uint32_t*)200, 0x12345678);
411 /* Fill write log entries */
412 uint32_t i;
413 uint32_t val = 0xd8453c6b;
414 for (i = 0; i < (LOG_SIZE / (sizeof(uint32_t) * 2)); i++) {
415 val ^= 0x593ca5b3;
416 val += i;
417 eeprom_write_dword((uint32_t*)200, val);
418 }
419 /* Check values pre-compaction */
420 EEPROM_Init();
421 EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdeadbeef);
422 EXPECT_EQ(eeprom_read_byte((uint8_t*)4), 0x3c);
423 EXPECT_EQ(eeprom_read_word((uint16_t*)6), 0xd00d);
424 EXPECT_EQ(eeprom_read_dword((uint32_t*)150), 0xcafef00d);
425 EXPECT_EQ(eeprom_read_dword((uint32_t*)200), val);
426 EXPECT_NE(*(uint16_t*)&FlashBuf[LOG_BASE], 0xFFFF);
427 EXPECT_NE(*(uint16_t*)&FlashBuf[LOG_BASE + LOG_SIZE - 2], 0xFFFF);
428 /* Run compaction */
429 eeprom_write_byte((uint8_t*)4, 0x1f);
430 EEPROM_Init();
431 EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdeadbeef);
432 EXPECT_EQ(eeprom_read_byte((uint8_t*)4), 0x1f);
433 EXPECT_EQ(eeprom_read_word((uint16_t*)6), 0xd00d);
434 EXPECT_EQ(eeprom_read_dword((uint32_t*)150), 0xcafef00d);
435 EXPECT_EQ(eeprom_read_dword((uint32_t*)200), val);
436 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], 0xFFFF);
437 EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + LOG_SIZE - 2], 0xFFFF);
438}
diff --git a/tmk_core/common/test/flash_stm32_mock.c b/tmk_core/common/test/flash_stm32_mock.c
deleted file mode 100644
index 222a004bc..000000000
--- a/tmk_core/common/test/flash_stm32_mock.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* Copyright 2021 by Don Kjer
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <string.h>
18#include <stdbool.h>
19#include "flash_stm32.h"
20
21uint8_t FlashBuf[MOCK_FLASH_SIZE] = {0};
22
23static bool flash_locked = true;
24
25FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
26 if (flash_locked) return FLASH_ERROR_WRP;
27 Page_Address -= (uintptr_t)FlashBuf;
28 Page_Address -= (Page_Address % FEE_PAGE_SIZE);
29 if (Page_Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS;
30 memset(&FlashBuf[Page_Address], '\xff', FEE_PAGE_SIZE);
31 return FLASH_COMPLETE;
32}
33
34FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
35 if (flash_locked) return FLASH_ERROR_WRP;
36 Address -= (uintptr_t)FlashBuf;
37 if (Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS;
38 uint16_t oldData = *(uint16_t*)&FlashBuf[Address];
39 if (oldData == 0xFFFF || Data == 0) {
40 *(uint16_t*)&FlashBuf[Address] = Data;
41 return FLASH_COMPLETE;
42 } else {
43 return FLASH_ERROR_PG;
44 }
45}
46
47FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { return FLASH_COMPLETE; }
48void FLASH_Unlock(void) { flash_locked = false; }
49void FLASH_Lock(void) { flash_locked = true; }
diff --git a/tmk_core/common/test/hal.h b/tmk_core/common/test/hal.h
deleted file mode 100644
index 2d268ad54..000000000
--- a/tmk_core/common/test/hal.h
+++ /dev/null
@@ -1,18 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// Just here to please eeprom tests
diff --git a/tmk_core/common/test/platform.c b/tmk_core/common/test/platform.c
deleted file mode 100644
index 8ddceeda8..000000000
--- a/tmk_core/common/test/platform.c
+++ /dev/null
@@ -1,21 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "platform_deps.h"
18
19void platform_setup(void) {
20 // do nothing
21} \ No newline at end of file
diff --git a/tmk_core/common/test/platform.h b/tmk_core/common/test/platform.h
deleted file mode 100644
index f296d1d53..000000000
--- a/tmk_core/common/test/platform.h
+++ /dev/null
@@ -1,18 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// here just to please the build
diff --git a/tmk_core/common/test/platform_deps.h b/tmk_core/common/test/platform_deps.h
deleted file mode 100644
index f296d1d53..000000000
--- a/tmk_core/common/test/platform_deps.h
+++ /dev/null
@@ -1,18 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18// here just to please the build
diff --git a/tmk_core/common/test/rules.mk b/tmk_core/common/test/rules.mk
deleted file mode 100644
index 73d2302da..000000000
--- a/tmk_core/common/test/rules.mk
+++ /dev/null
@@ -1,24 +0,0 @@
1eeprom_stm32_DEFS := -DFLASH_STM32_MOCKED -DNO_PRINT -DFEE_FLASH_BASE=FlashBuf
2eeprom_stm32_tiny_DEFS := $(eeprom_stm32_DEFS) \
3 -DFEE_MCU_FLASH_SIZE=1 \
4 -DMOCK_FLASH_SIZE=1024 \
5 -DFEE_PAGE_SIZE=512 \
6 -DFEE_PAGE_COUNT=1
7eeprom_stm32_large_DEFS := $(eeprom_stm32_DEFS) \
8 -DFEE_MCU_FLASH_SIZE=64 \
9 -DMOCK_FLASH_SIZE=65536 \
10 -DFEE_PAGE_SIZE=2048 \
11 -DFEE_PAGE_COUNT=16
12
13eeprom_stm32_INC := \
14 $(TMK_PATH)/common/chibios/
15eeprom_stm32_tiny_INC := $(eeprom_stm32_INC)
16eeprom_stm32_large_INC := $(eeprom_stm32_INC)
17
18eeprom_stm32_SRC := \
19 $(TOP_DIR)/drivers/eeprom/eeprom_driver.c \
20 $(TMK_PATH)/common/test/eeprom_stm32_tests.cpp \
21 $(TMK_PATH)/common/test/flash_stm32_mock.c \
22 $(TMK_PATH)/common/chibios/eeprom_stm32.c
23eeprom_stm32_tiny_SRC := $(eeprom_stm32_SRC)
24eeprom_stm32_large_SRC := $(eeprom_stm32_SRC)
diff --git a/tmk_core/common/test/suspend.c b/tmk_core/common/test/suspend.c
deleted file mode 100644
index 76b705967..000000000
--- a/tmk_core/common/test/suspend.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
diff --git a/tmk_core/common/test/testlist.mk b/tmk_core/common/test/testlist.mk
deleted file mode 100644
index 51a9638bb..000000000
--- a/tmk_core/common/test/testlist.mk
+++ /dev/null
@@ -1 +0,0 @@
1TEST_LIST += eeprom_stm32_tiny eeprom_stm32_large
diff --git a/tmk_core/common/test/timer.c b/tmk_core/common/test/timer.c
deleted file mode 100644
index 61c3a0020..000000000
--- a/tmk_core/common/test/timer.c
+++ /dev/null
@@ -1,33 +0,0 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "timer.h"
18
19static uint32_t current_time = 0;
20
21void timer_init(void) { current_time = 0; }
22
23void timer_clear(void) { current_time = 0; }
24
25uint16_t timer_read(void) { return current_time & 0xFFFF; }
26uint32_t timer_read32(void) { return current_time; }
27uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); }
28uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); }
29
30void set_time(uint32_t t) { current_time = t; }
31void advance_time(uint32_t ms) { current_time += ms; }
32
33void wait_ms(uint32_t ms) { advance_time(ms); }
diff --git a/tmk_core/common/timer.h b/tmk_core/common/timer.h
deleted file mode 100644
index 02e39e79e..000000000
--- a/tmk_core/common/timer.h
+++ /dev/null
@@ -1,67 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3Copyright 2021 Simon Arlott
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#if __has_include_next("_timer.h")
22# include_next "_timer.h" /* Include the platform's _timer.h */
23#endif
24
25#include <stdint.h>
26
27#define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a) - (b))) : ((max == UINT16_MAX) ? ((uint16_t)((a) - (b))) : ((max == UINT32_MAX) ? ((uint32_t)((a) - (b))) : ((a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a)))))
28#define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX)
29#define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX)
30#define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX)
31#define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b)
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37extern volatile uint32_t timer_count;
38
39void timer_init(void);
40void timer_clear(void);
41uint16_t timer_read(void);
42uint32_t timer_read32(void);
43uint16_t timer_elapsed(uint16_t last);
44uint32_t timer_elapsed32(uint32_t last);
45
46// Utility functions to check if a future time has expired & autmatically handle time wrapping if checked / reset frequently (half of max value)
47#define timer_expired(current, future) ((uint16_t)(current - future) < UINT16_MAX / 2)
48#define timer_expired32(current, future) ((uint32_t)(current - future) < UINT32_MAX / 2)
49
50// Use an appropriate timer integer size based on architecture (16-bit will overflow sooner)
51#if FAST_TIMER_T_SIZE < 32
52# define TIMER_DIFF_FAST(a, b) TIMER_DIFF_16(a, b)
53# define timer_expired_fast(current, future) timer_expired(current, future)
54typedef uint16_t fast_timer_t;
55fast_timer_t inline timer_read_fast(void) { return timer_read(); }
56fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed(last); }
57#else
58# define TIMER_DIFF_FAST(a, b) TIMER_DIFF_32(a, b)
59# define timer_expired_fast(current, future) timer_expired32(current, future)
60typedef uint32_t fast_timer_t;
61fast_timer_t inline timer_read_fast(void) { return timer_read32(); }
62fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed32(last); }
63#endif
64
65#ifdef __cplusplus
66}
67#endif
diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h
deleted file mode 100644
index cf7180fb0..000000000
--- a/tmk_core/common/wait.h
+++ /dev/null
@@ -1,30 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <inttypes.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#if __has_include_next("_wait.h")
25# include_next "_wait.h" /* Include the platforms _wait.h */
26#endif
27
28#ifdef __cplusplus
29}
30#endif
diff --git a/tmk_core/native.mk b/tmk_core/native.mk
deleted file mode 100644
index eb2424ec5..000000000
--- a/tmk_core/native.mk
+++ /dev/null
@@ -1,34 +0,0 @@
1SYSTEM_TYPE := $(shell gcc -dumpmachine)
2GCC_VERSION := $(shell gcc --version 2>/dev/null)
3
4CC = $(CC_PREFIX) gcc
5OBJCOPY =
6OBJDUMP =
7SIZE =
8AR =
9NM =
10HEX =
11EEP =
12BIN =
13
14
15COMPILEFLAGS += -funsigned-char
16ifeq ($(findstring clang, ${GCC_VERSION}),)
17COMPILEFLAGS += -funsigned-bitfields
18endif
19COMPILEFLAGS += -ffunction-sections
20COMPILEFLAGS += -fdata-sections
21COMPILEFLAGS += -fshort-enums
22ifneq ($(findstring mingw, ${SYSTEM_TYPE}),)
23COMPILEFLAGS += -mno-ms-bitfields
24endif
25
26CFLAGS += $(COMPILEFLAGS)
27ifeq ($(findstring clang, ${GCC_VERSION}),)
28CFLAGS += -fno-inline-small-functions
29endif
30CFLAGS += -fno-strict-aliasing
31
32CXXFLAGS += $(COMPILEFLAGS)
33CXXFLAGS += -fno-exceptions
34CXXFLAGS += -std=gnu++11
diff --git a/tmk_core/protocol.mk b/tmk_core/protocol.mk
index a8723e6b2..31a6de76f 100644
--- a/tmk_core/protocol.mk
+++ b/tmk_core/protocol.mk
@@ -1,5 +1,3 @@
1PROTOCOL_DIR = protocol
2
3TMK_COMMON_SRC += \ 1TMK_COMMON_SRC += \
4 $(PROTOCOL_DIR)/host.c \ 2 $(PROTOCOL_DIR)/host.c \
5 $(PROTOCOL_DIR)/report.c \ 3 $(PROTOCOL_DIR)/report.c \
diff --git a/tmk_core/rules.mk b/tmk_core/rules.mk
index e657af3f4..6767a8e4a 100644
--- a/tmk_core/rules.mk
+++ b/tmk_core/rules.mk
@@ -479,7 +479,7 @@ ifeq ($(findstring avr-gcc,$(CC)),avr-gcc)
479SIZE_MARGIN = 1024 479SIZE_MARGIN = 1024
480 480
481check-size: 481check-size:
482 $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) tmk_core/common/avr/bootloader_size.c 2> /dev/null | sed -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0)) 482 $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) platforms/avr/bootloader_size.c 2> /dev/null | sed -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0))
483 $(eval CURRENT_SIZE=$(shell if [ -f $(BUILD_DIR)/$(TARGET).hex ]; then $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex | $(AWK) 'NR==2 {print $$4}'; else printf 0; fi)) 483 $(eval CURRENT_SIZE=$(shell if [ -f $(BUILD_DIR)/$(TARGET).hex ]; then $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex | $(AWK) 'NR==2 {print $$4}'; else printf 0; fi))
484 $(eval FREE_SIZE=$(shell expr $(MAX_SIZE) - $(CURRENT_SIZE))) 484 $(eval FREE_SIZE=$(shell expr $(MAX_SIZE) - $(CURRENT_SIZE)))
485 $(eval OVER_SIZE=$(shell expr $(CURRENT_SIZE) - $(MAX_SIZE))) 485 $(eval OVER_SIZE=$(shell expr $(CURRENT_SIZE) - $(MAX_SIZE)))