From a1cb0059816b1bd2a64ad5f9b13021165b763f2a Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 5 Apr 2023 12:59:16 -0400 Subject: [PATCH] storm commit, chibios exp --- CMakeLists.txt | 1 + chibios_mcu.json | 41 ++++ cmake/Findchibios.cmake | 229 +++++++++++++++++++++++ cmake/ParseMakefile.cmake | 44 +++++ cmake/features/eeprom.cmake | 54 ++++++ cmake/toolchains/arm-none-eabi.cmake | 12 -- platforms/chibios/CMakeLists.txt | 33 +++- quantum/CMakeLists.txt | 4 +- tmk_core/protocol/chibios/CMakeLists.txt | 13 +- 9 files changed, 408 insertions(+), 23 deletions(-) create mode 100644 chibios_mcu.json create mode 100644 cmake/ParseMakefile.cmake create mode 100644 cmake/features/eeprom.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 85e96ebab7b..b96b9a3ccc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,5 +101,6 @@ add_subdirectory(tmk_core/protocol) include(features/oled) include(features/backlight) +include(features/eeprom) endif() diff --git a/chibios_mcu.json b/chibios_mcu.json new file mode 100644 index 00000000000..96248049323 --- /dev/null +++ b/chibios_mcu.json @@ -0,0 +1,41 @@ +{ + "MKL26Z64" : { + "MCU" : "cortex-m0plus", + "ARMV": 6, + "MCU_FAMILY": "KINETIS", + "MCU_SERIES": "KL2x", + "MCU_LDSCRIPT": "MKL26Z64", + "MCU_STARTUP": "kl2x", + "BOARD": "PJRC_TEENSY_LC" + }, + "MK20DX128" : { + "MCU" : "cortex-m4", + "ARMV": 7, + "MCU_FAMILY": "KINETIS", + "MCU_SERIES": "K20x", + "MCU_LDSCRIPT": "MK20DX128", + "MCU_STARTUP": "k20x5", + "BOARD": "PJRC_TEENSY_3" + }, + "MK20DX256" : { + "MCU" : "cortex-m4", + "ARMV": 7, + "MCU_FAMILY": "KINETIS", + "MCU_SERIES": "K20x", + "MCU_LDSCRIPT": "MK20DX256", + "MCU_STARTUP": "k20x7", + "BOARD": "PJRC_TEENSY_3_1" + }, + "STM32F303" : { + "MCU" : "cortex-m4", + "ARMV": 7, + "MCU_FAMILY": "STM32", + "MCU_SERIES": "STM32F3xx", + "MCU_LDSCRIPT": "STM32F303xC", + "MCU_STARTUP": "stm32f3xx", + "BOARD": "GENERIC_STM32_F303XC", + "USE_FPU": true, + "UF2_FAMILY": "STM32F3", + "STM32_BOOTLOADER_ADDRESS": "0x1FFFD800" + } +} \ No newline at end of file diff --git a/cmake/Findchibios.cmake b/cmake/Findchibios.cmake index e69de29bb2d..dc1a663aa20 100644 --- a/cmake/Findchibios.cmake +++ b/cmake/Findchibios.cmake @@ -0,0 +1,229 @@ +include(ParseMakefile) + +set(MCU "cortex-m4") +set(ARMV 7) +set(MCU_FAMILY "STM32") +set(MCU_SERIES "STM32F3xx") +set(MCU_LDSCRIPT "STM32F303xC") +set(MCU_STARTUP "stm32f3xx") +set(BOARD "GENERIC_STM32_F303XC") +set(USE_FPU TRUE) +set(UF2_FAMILY "STM32F3") +set(STM32_BOOTLOADER_ADDRESS 0x1FFFD800) + +set(MCU_PORT_NAME ${MCU_FAMILY}) +set(MCU_ARCH ${MCU}) +set(CHIBIOS_PORT "ARMv${ARMV}-M") +set(PLATFORM_NAME platform) + +set(CHIBIOS ${CMAKE_SOURCE_DIR}/lib/chibios) +set(CHIBIOS_CONTRIB ${CMAKE_SOURCE_DIR}/lib/chibios-contrib) +set(EEPROM_DRIVER "wear_leveling" CACHE STRING "" FORCE) + +# target_compile_definitions(qmk PUBLIC + # __ARM_ARCH_7M__ + # STM32F303xC +# ) + +# F3 sources +set(CHIBIOS_PORT_SRCS + # RT + chcore.c + chcoreasm.S +) +foreach(SRC_FILE ${CHIBIOS_PORT_SRCS}) + set(CHIBIOS_F3_SRC_FILE SRC_FILE-NOTFOUND) + find_file(CHIBIOS_F3_SRC_FILE ${SRC_FILE} + PATHS + ${CHIBIOS}/os/common/ports/ARMv7-M/compilers/GCC + ${CHIBIOS}/os/common/ports/ARMv7-M + CMAKE_FIND_ROOT_PATH_BOTH + ) + + target_sources(qmk PUBLIC ${CHIBIOS_F3_SRC_FILE}) +endforeach() + +target_include_directories(qmk PUBLIC + ${CHIBIOS}/os/common/ports/ARMv7-M + ${CHIBIOS}/os/hal/boards/ST_STM32F3_DISCOVERY +) + +# F3 HAL specific +target_include_directories(qmk PUBLIC + ${CHIBIOS}/os/hal/ports/common/ARMCMx + ${CHIBIOS}/os/hal/include + ${CHIBIOS}/os/hal/ports/STM32/STM32F3xx + + ${CHIBIOS}/os/common/portability/GCC + ${CHIBIOS}/os/common/ports/ARM-common + ${CHIBIOS}/os/common/ports/ARMv7-M + ${CHIBIOS}/os/common/startup/ARMCMx/compilers/GCC + ${CHIBIOS}/os/common/startup/ARMCMx/devices/STM32F3xx + ${CHIBIOS}/os/common/ext/ARM/CMSIS/Core/Include + ${CHIBIOS}/os/common/ext/ST/STM32F3xx + + ${CHIBIOS}/os/hal/ports/STM32/LLD/ADCv3 + ${CHIBIOS}/os/hal/ports/STM32/LLD/CANv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/DACv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/DMAv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/EXTIv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/GPIOv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/I2Cv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/RTCv2 + # ${CHIBIOS}/os/hal/ports/STM32/LLD/SDIOv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/SPIv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/SYSTICKv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/TIMv1 + # ${CHIBIOS}/os/hal/ports/STM32/LLD/USART + ${CHIBIOS}/os/hal/ports/STM32/LLD/USARTv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/USBv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/xWDGv1 +) + +# source files for ChibiOS HAL +set(CHIBIOS_HAL_SRCS + + # startup code + crt1.c + vectors.S + crt0_v7m.S + + nvic.c + stm32_isr.c + + # HAL-OSAL files + hal.c + hal_st.c + + hal_buffers.c + hal_queues.c + hal_flash.c + hal_mmcsd.c + + hal_adc.c + hal_can.c + # hal_crypto.c + hal_dac.c + hal_efl.c + hal_gpt.c + hal_i2c.c + hal_i2s.c + hal_icu.c + hal_mac.c + hal_mmc_spi.c + hal_pal.c + hal_pwm.c + hal_rtc.c + hal_sdc.c + hal_serial.c + hal_serial_usb.c + hal_sio.c + hal_spi.c + hal_trng.c + hal_uart.c + hal_usb.c + hal_wdg.c + hal_wspi.c + + # LLD HAL files + hal_lld.c + + hal_adc_lld.c + hal_can_lld.c + hal_crypto_lld.c + hal_dac_lld.c + stm32_dma.c + stm32_exti.c + hal_pal_lld.c + hal_i2c_lld.c + hal_mac_lld.c + hal_usb_lld.c + hal_wspi_lld.c + hal_trng_lld.c + hal_rtc_lld.c + hal_sdc_lld.c + + hal_i2s_lld.c + hal_spi_v2_lld.c + + hal_st_lld.c + hal_gpt_lld.c + hal_icu_lld.c + hal_pwm_lld.c + + hal_serial_lld.c + hal_uart_lld.c + + hal_wdg_lld.c + + # OSAL + osal.c + + board.c +) + +foreach(SRC_FILE ${CHIBIOS_HAL_SRCS}) + + set(CHIBIOS_HAL_SRC_FILE SRC_FILE -NOTFOUND) + + find_file(CHIBIOS_HAL_SRC_FILE ${SRC_FILE} + PATHS + + ${CHIBIOS}/os/common/ports/ARMv7-M/compilers/GCC + ${CHIBIOS}/os/common/startup/ARMCMx/compilers/GCC + + ${CHIBIOS}/os/hal/src + ${CHIBIOS}/os/hal/ports/common/ARMCMx + + ${CHIBIOS}/os/hal/ports/STM32/STM32F3xx + + ${CHIBIOS}/os/hal/ports/STM32/STM32F3xx + ${CHIBIOS}/os/hal/ports/STM32/LLD/ADCv3 + ${CHIBIOS}/os/hal/ports/STM32/LLD/CANv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/DACv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/DMAv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/EXTIv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/GPIOv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/I2Cv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/RTCv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/SPIv2 + ${CHIBIOS}/os/hal/ports/STM32/LLD/SYSTICKv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/TIMv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/USARTv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/USBv1 + ${CHIBIOS}/os/hal/ports/STM32/LLD/xWDGv1 + + # ${OSHAL_PATH} + + # ${CHIBIOS_BOARD_DEFINITIONS_PATH} + + CMAKE_FIND_ROOT_PATH_BOTH + ) + if(EXISTS "${CHIBIOS_HAL_SRC_FILE}") + target_sources(qmk PUBLIC ${CHIBIOS_HAL_SRC_FILE}) + endif() +endforeach() + +# chibios +target_include_directories(qmk PUBLIC + # ${CHIBIOS}/os/hal/include + # ${CHIBIOS}/os/hal/osal/sb + # ${CHIBIOS}/os/common/portability/GCC + # ${CHIBIOS}/os/sb/user + # ${CHIBIOS}/os/sb/common + + ${CHIBIOS}/os + ${CHIBIOS}/os/license + ${CHIBIOS}/os/hal/ports/common/ARMCMx + ${CHIBIOS}/os/hal/include + ${CHIBIOS}/os/hal/osal/rt-nil + ${CHIBIOS}/os/oslib/include + ${CHIBIOS}/os/rt/include + ${CHIBIOS}/os/hal/ports/STM32/${MCU_SERIES} + ${CHIBIOS}/os/common/ports/ARMv7-M + ${CHIBIOS}/os/common/ports/ARMv7-M/compilers/GCC + ${CHIBIOS}/os/common/abstractions/cmsis_os + ${CHIBIOS}/os/common/startup/ARMCMx/compilers/GCC + ${CHIBIOS}/os/common/ext/CMSIS/include + ${CHIBIOS}/os/common/ext/CMSIS/ST/${MCU_SERIES} +) \ No newline at end of file diff --git a/cmake/ParseMakefile.cmake b/cmake/ParseMakefile.cmake new file mode 100644 index 00000000000..6dee007b13a --- /dev/null +++ b/cmake/ParseMakefile.cmake @@ -0,0 +1,44 @@ +# Simple CMake utility to read variables from MK files +# - Gets contents from given file (name or path) +# - Parses the assignment statements +# - Makes the same assignments in the PARENT_SCOPE + +if(POLICY CMP0007) + cmake_policy(SET CMP0007 NEW) +endif() + +function(ParseMakefile MKFile) + _ParseMakefile(${MKFile}) +endfunction() + +macro(_ParseMakefile MKFile) + file(READ "${MKFile}" FileContents) + string(REPLACE "\\\n" "" FileContents ${FileContents}) + string(REPLACE "\n" ";" FileLines ${FileContents}) + list(REMOVE_ITEM FileLines "") + foreach(line ${FileLines}) + # if(line MATCHES "$\\((.+)\\)") + # string(REPLACE "(${CMAKE_MATCH_1})" "{${CMAKE_MATCH_1}}" line ${line}) + # endif() + # if(line MATCHES "^include (.+)$") + # set(MAKE_CHILD ${CMAKE_MATCH_1}) + # message(STATUS "Reading ${MAKE_CHILD}") + # _ParseMakefile("${MAKE_CHILD}") + # else() + string(REPLACE "=" ";" line_split ${line}) + list(LENGTH line_split count) + if (count LESS 2) + message(STATUS "Skipping ${line}") + continue() + endif() + list(GET line_split -1 value) + string(STRIP ${value} value) + separate_arguments(value) + list(REMOVE_AT line_split -1) + foreach(var_name ${line_split}) + string(STRIP ${var_name} var_name) + set(${var_name} ${value} PARENT_SCOPE) + endforeach() + # endif() + endforeach() +endmacro() \ No newline at end of file diff --git a/cmake/features/eeprom.cmake b/cmake/features/eeprom.cmake new file mode 100644 index 00000000000..294abe5546e --- /dev/null +++ b/cmake/features/eeprom.cmake @@ -0,0 +1,54 @@ +set(EEPROM_DRIVER "vendor" CACHE STRING "EEPROM driver") +set_property(CACHE EEPROM_DRIVER PROPERTY STRINGS vendor custom transient i2c spi wear_leveling legacy_stm32_flash) +set(WEAR_LEVELING_DRIVER "none" CACHE STRING "EEPROM wear-leveling driver") +set_property(CACHE WEAR_LEVELING_DRIVER PROPERTY STRINGS custom embedded_flash spi_flash rp2040_flash legacy) + +if(${EEPROM_DRIVER} STREQUAL "wear_leveling") + target_compile_definitions(qmk PUBLIC + EEPROM_DRIVER + EEPROM_WEAR_LEVELING + ) + target_sources(qmk PUBLIC + drivers/eeprom/eeprom_driver.c + drivers/eeprom/eeprom_wear_leveling.c + ) + target_include_directories(qmk PUBLIC drivers/eeprom) +elseif(${EEPROM_DRIVER} STREQUAL "vendor") + target_compile_definitions(qmk PUBLIC + EEPROM_VENDOR + ) + if(${QMK_PLATFORM} STREQUAL "chibios") + if(${QMK_MCU} MATHCES "STM32F.*") + target_compile_definitions(qmk PUBLIC + EEPROM_DRIVER + EEPROM_WEAR_LEVELING + ) + target_sources(qmk PUBLIC + drivers/eeprom/eeprom_driver.c + drivers/eeprom/eeprom_wear_leveling.c + ) + set(WEAR_LEVELING_DRIVER "embedded_flash" FORCE) + endif() + endif() +endif() + +if(NOT ${WEAR_LEVELING_DRIVER} STREQUAL "none") + target_compile_definitions(qmk PUBLIC + WEAR_LEVELING_ENABLE + WEAR_LEVELING_EMBEDDED_FLASH + ) + target_include_directories(qmk PUBLIC + platforms/${QMK_PLATFORM}/drivers/wear_leveling + drivers/wear_leveling + quantum/wear_leveling + ) + target_sources(qmk PUBLIC wear_leveling.c) + + if(NOT ${WEAR_LEVELING_DRIVER} STREQUAL "embedded_flash") + target_compile_definitions(qmk PUBLIC + HAL_USE_EFL + ) + target_sources(qmk PUBLIC wear_leveling_efl.c) + # post config.h platforms/${QMK_PLATFORM}/drivers/wear_leveling/wear_leveling_efl_config.h + endif() +endif() diff --git a/cmake/toolchains/arm-none-eabi.cmake b/cmake/toolchains/arm-none-eabi.cmake index 94c1dd6142d..804c1fdb5dd 100644 --- a/cmake/toolchains/arm-none-eabi.cmake +++ b/cmake/toolchains/arm-none-eabi.cmake @@ -68,23 +68,11 @@ add_compile_options( # $<$:-fno-exceptions> ) -add_compile_definitions( - THUMB_PRESENT - THUMB_NO_INTERWORKING - PROTOCOL_CHIBIOS - MCU_${MCU_FAMILY} - PLATFORM_SUPPORTS_SYNCHRONIZATION - PORT_IGNORE_GCC_VERSION_CHECK=1 -) - add_link_options( -Wl,--gc-sections -nostartfiles -Wl,--no-wchar-size-warning --specs=nano.specs - -mthumb - -mno-thumb-interwork - -mno-unaligned-access ) if(${USE_FPU}) diff --git a/platforms/chibios/CMakeLists.txt b/platforms/chibios/CMakeLists.txt index 622e2a56785..4c0b47402f0 100644 --- a/platforms/chibios/CMakeLists.txt +++ b/platforms/chibios/CMakeLists.txt @@ -1,6 +1,37 @@ -add_link_options( +find_package(chibios REQUIRED) + +target_link_options(qmk PUBLIC -mcpu=${QMK_MCU} + -mthumb + -mno-thumb-interwork + -mno-unaligned-access ) + +target_compile_definitions(qmk PUBLIC + MCU_${MCU_FAMILY} + THUMB_PRESENT + THUMB_NO_INTERWORKING + PLATFORM_SUPPORTS_SYNCHRONIZATION + PORT_IGNORE_GCC_VERSION_CHECK=1 +) + +if(NOT ${USE_FPU}) + target_compile_options(qmk PUBLIC + -mfloat-abi=hard + -mfpu=fpv4-sp-d16 + -fsingle-precision-constant + ) + target_compile_definitions(qmk PUBLIC + CORTEX_USE_FPU=TRUE + ) +else() + target_compile_definitions(qmk PUBLIC + CORTEX_USE_FPU=FALSE + ) +endif() + # add_library(platforms_chibios) target_include_directories(qmk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(qmk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/boards/common/configs) +target_include_directories(qmk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}/configs) # target_link_libraries(platforms_chibios ${QMK_TARGET}) \ No newline at end of file diff --git a/quantum/CMakeLists.txt b/quantum/CMakeLists.txt index 736df9073f6..e5b5d683bfb 100644 --- a/quantum/CMakeLists.txt +++ b/quantum/CMakeLists.txt @@ -35,5 +35,5 @@ target_include_directories(qmk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bootmagic) # target_link_libraries(quantum tmk_core_protocol_${QMK_PROTOCOL}) # if no printf -# target_sources(quantum PRIVATE ${CMAKE_SOURCE_DIR}/lib/printf/src/printf/printf.c) -# target_include_directories(quantum PUBLIC ${CMAKE_SOURCE_DIR}/lib/printf/src/printf) \ No newline at end of file +target_sources(qmk PUBLIC ${CMAKE_SOURCE_DIR}/lib/printf/src/printf/printf.c) +target_include_directories(qmk PUBLIC ${CMAKE_SOURCE_DIR}/lib/printf/src/printf) \ No newline at end of file diff --git a/tmk_core/protocol/chibios/CMakeLists.txt b/tmk_core/protocol/chibios/CMakeLists.txt index 5a4144ccf36..ab80a632d47 100644 --- a/tmk_core/protocol/chibios/CMakeLists.txt +++ b/tmk_core/protocol/chibios/CMakeLists.txt @@ -1,10 +1,10 @@ -add_compile_definitions( +target_compile_definitions(qmk PUBLIC PROTOCOL_CHIBIOS FIXED_CONTROL_ENDPOINT_SIZE=64 FIXED_NUM_CONFIGURATIONS=1 ) -add_library(tmk_core_protocol_chibios +target_sources(qmk PUBLIC usb_main.c chibios.c ../usb_descriptor.c @@ -12,10 +12,7 @@ add_library(tmk_core_protocol_chibios usb_util.c ) -# find_package(lufa REQUIRED) +find_package(chibios REQUIRED) -target_include_directories(tmk_core_protocol_chibios PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_include_directories(tmk_core_protocol_chibios PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lufa_utils) - -# target_link_libraries(tmk_core_protocol_chibios PUBLIC lufa) -target_link_libraries(tmk_core_protocol_chibios PUBLIC quantum) \ No newline at end of file +target_include_directories(qmk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(qmk PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lufa_utils) \ No newline at end of file