From 6b55dea700fa29b4071fe9857dd6c8e33be2a585 Mon Sep 17 00:00:00 2001 From: Johannes Paehr Date: Fri, 2 Jan 2026 20:44:24 +0100 Subject: [PATCH] working version 1 --- .clang-format | 24 + .clangd | 11 + .mxproject | 30 ++ .settings/bundles-lock.store.json | 224 +++++++++ .settings/bundles.store.json | 28 ++ .settings/ide.store.json | 6 + .vscode/c_cpp_properties.json | 22 +- .vscode/launch.json | 61 --- .vscode/settings.json | 15 + CMakeLists.txt | 16 +- Core/Inc/TLE5012.hpp | 23 +- Core/Inc/main.h | 6 + Core/Inc/stm32g0xx_hal_conf.h | 2 +- Core/Inc/stm32g0xx_it.h | 3 +- Core/Src/TLE5012.cpp | 36 +- Core/Src/main.c | 370 -------------- Core/Src/main.cpp | 803 ++++++++++++++---------------- Core/Src/stm32g0xx_hal_msp.c | 201 ++++++-- Core/Src/stm32g0xx_it.c | 15 + G0Test1.ioc | 92 +++- STM32G030XX_FLASH.ld | 249 +++++++++ cmake/starm-clang.cmake | 65 +++ cmake/stm32cubemx/CMakeLists.txt | 123 +++-- 23 files changed, 1407 insertions(+), 1018 deletions(-) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .mxproject create mode 100644 .settings/bundles-lock.store.json create mode 100644 .settings/bundles.store.json create mode 100644 .settings/ide.store.json delete mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json delete mode 100644 Core/Src/main.c create mode 100644 STM32G030XX_FLASH.ld create mode 100644 cmake/starm-clang.cmake diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..64b7770 --- /dev/null +++ b/.clang-format @@ -0,0 +1,24 @@ +--- +# We'll use defaults from the LLVM style, but with 4 columns indentation. +BasedOnStyle: LLVM +IndentWidth: 4 +--- +Language: Cpp +# Force pointers to the type for C++. +DerivePointerAlignment: false +PointerAlignment: Middle +AlignTrailingComments: true +BreakBeforeBraces: Custom +BraceWrapping: + BeforeElse: true +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +--- +Language: JavaScript +# Use 100 columns for JS. +ColumnLimit: 100 +--- +Language: Proto +# Don't format .proto files. +DisableFormat: true +... diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..50c62c4 --- /dev/null +++ b/.clangd @@ -0,0 +1,11 @@ +CompileFlags: + Add: + - '-ferror-limit=0' + - '-Wno-implicit-int' + CompilationDatabase: build/Debug +Diagnostics: + Suppress: + - unused-includes + - unknown_typename + - unknown_typename_suggest + - typename_requires_specqual diff --git a/.mxproject b/.mxproject new file mode 100644 index 0000000..b3c346a --- /dev/null +++ b/.mxproject @@ -0,0 +1,30 @@ +[PreviousLibFiles] +LibFiles=Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_spi.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_bus.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_system.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_utils.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ramfunc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_gpio.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_pwr.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_cortex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_cortex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_def.h;Drivers/STM32G0xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_exti.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_exti.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_tim.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_usart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_lpuart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart_ex.h;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_dma.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_dmamux.h;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_spi.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_spi_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_rcc_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_rcc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_bus.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_system.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_utils.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_flash_ramfunc.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_gpio_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_gpio.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_dma_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_pwr_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_pwr.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_cortex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_cortex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_def.h;Drivers/STM32G0xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_exti.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_exti.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_tim.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_tim_ex.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_usart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_ll_lpuart.h;Drivers/STM32G0xx_HAL_Driver/Inc/stm32g0xx_hal_uart_ex.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/stm32g030xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/stm32g0xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/system_stm32g0xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Include/system_stm32g0xx.h;Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_armv81mml.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/cmsis_armclang_ltm.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_cm35p.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/mpu_armv8.h; + +[PreviousUsedIarFiles] +SourceFiles=../Core/Src/main.c;../Core/Src/stm32g0xx_it.c;../Core/Src/stm32g0xx_hal_msp.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;../Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;../Core/Src/system_stm32g0xx.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;../Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;../Core/Src/system_stm32g0xx.c;;; +HeaderPath=../Drivers/STM32G0xx_HAL_Driver/Inc;../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32G0xx/Include;../Drivers/CMSIS/Include;../Core/Inc; +CDefines=USE_HAL_DRIVER;STM32G030xx;USE_HAL_DRIVER;USE_HAL_DRIVER; + +[PreviousUsedCMakes] +SourceFiles=Core/Src/main.c;Core/Src/stm32g0xx_it.c;Core/Src/stm32g0xx_hal_msp.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;Core/Src/system_stm32g0xx.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c;Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c;Drivers/CMSIS/Device/ST/STM32G0xx/Source/Templates/system_stm32g0xx.c;Core/Src/system_stm32g0xx.c;;; +HeaderPath=Drivers/STM32G0xx_HAL_Driver/Inc;Drivers/STM32G0xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32G0xx/Include;Drivers/CMSIS/Include;Core/Inc; +CDefines=USE_HAL_DRIVER;STM32G030xx;USE_HAL_DRIVER;USE_HAL_DRIVER; + +[PreviousGenFiles] +AdvancedFolderStructure=true +HeaderFileListSize=3 +HeaderFiles#0=../Core/Inc/stm32g0xx_it.h +HeaderFiles#1=../Core/Inc/stm32g0xx_hal_conf.h +HeaderFiles#2=../Core/Inc/main.h +HeaderFolderListSize=1 +HeaderPath#0=../Core/Inc +HeaderFiles=; +SourceFileListSize=3 +SourceFiles#0=../Core/Src/stm32g0xx_it.c +SourceFiles#1=../Core/Src/stm32g0xx_hal_msp.c +SourceFiles#2=../Core/Src/main.c +SourceFolderListSize=1 +SourcePath#0=../Core/Src +SourceFiles=; + diff --git a/.settings/bundles-lock.store.json b/.settings/bundles-lock.store.json new file mode 100644 index 0000000..85f10f0 --- /dev/null +++ b/.settings/bundles-lock.store.json @@ -0,0 +1,224 @@ +{ + "resolved": [ + { + "name": "cmake", + "version": "4.0.1+st.3", + "platform": "darwin", + "selected_by": [ + { + "name": "cmake", + "version": "4.0.1+st.3" + } + ] + }, + { + "name": "cmake", + "version": "4.0.1+st.3", + "platform": "x86_64-linux", + "selected_by": [ + { + "name": "cmake", + "version": "4.0.1+st.3" + } + ] + }, + { + "name": "cmake", + "version": "4.0.1+st.3", + "platform": "x86_64-windows", + "selected_by": [ + { + "name": "cmake", + "version": "4.0.1+st.3" + } + ] + }, + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2", + "platform": "aarch64-darwin", + "selected_by": [ + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2" + } + ] + }, + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2", + "platform": "x86_64-darwin", + "selected_by": [ + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2" + } + ] + }, + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2", + "platform": "x86_64-linux", + "selected_by": [ + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2" + } + ] + }, + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2", + "platform": "x86_64-windows", + "selected_by": [ + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2" + } + ] + }, + { + "name": "gnu-tools-for-stm32-14_3_1-description", + "version": "1.0.1+st.1", + "platform": "all", + "selected_by": [ + { + "name": "gnu-tools-for-stm32-14_3_1-description", + "version": ">=0.0.1" + } + ] + }, + { + "name": "ninja", + "version": "1.13.1+st.1", + "platform": "darwin", + "selected_by": [ + { + "name": "ninja", + "version": "1.13.1+st.1" + } + ] + }, + { + "name": "ninja", + "version": "1.13.1+st.1", + "platform": "x86_64-linux", + "selected_by": [ + { + "name": "ninja", + "version": "1.13.1+st.1" + } + ] + }, + { + "name": "ninja", + "version": "1.13.1+st.1", + "platform": "x86_64-windows", + "selected_by": [ + { + "name": "ninja", + "version": "1.13.1+st.1" + } + ] + }, + { + "name": "programmer", + "version": "2.21.0", + "platform": "darwin", + "selected_by": [ + { + "name": "programmer", + "version": "2.21.0" + } + ] + }, + { + "name": "programmer", + "version": "2.21.0", + "platform": "x86_64-linux", + "selected_by": [ + { + "name": "programmer", + "version": "2.21.0" + } + ] + }, + { + "name": "programmer", + "version": "2.21.0", + "platform": "x86_64-windows", + "selected_by": [ + { + "name": "programmer", + "version": "2.21.0" + } + ] + }, + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3", + "platform": "darwin", + "selected_by": [ + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3" + } + ] + }, + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3", + "platform": "x86_64-linux", + "selected_by": [ + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3" + } + ] + }, + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3", + "platform": "x86_64-windows", + "selected_by": [ + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3" + } + ] + }, + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2", + "platform": "darwin", + "selected_by": [ + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2" + } + ] + }, + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2", + "platform": "x86_64-linux", + "selected_by": [ + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2" + } + ] + }, + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2", + "platform": "x86_64-windows", + "selected_by": [ + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2" + } + ] + } + ] +} diff --git a/.settings/bundles.store.json b/.settings/bundles.store.json new file mode 100644 index 0000000..f624467 --- /dev/null +++ b/.settings/bundles.store.json @@ -0,0 +1,28 @@ +{ + "bundles": [ + { + "name": "cmake", + "version": "4.0.1+st.3" + }, + { + "name": "ninja", + "version": "1.13.1+st.1" + }, + { + "name": "gnu-tools-for-stm32", + "version": "14.3.1+st.2" + }, + { + "name": "st-arm-clangd", + "version": "19.1.2+st.3" + }, + { + "name": "programmer", + "version": "2.21.0" + }, + { + "name": "stlink-gdbserver", + "version": "7.12.0+st.2" + } + ] +} diff --git a/.settings/ide.store.json b/.settings/ide.store.json new file mode 100644 index 0000000..b6b43ec --- /dev/null +++ b/.settings/ide.store.json @@ -0,0 +1,6 @@ +{ + "device": "STM32G030F6P6", + "core": "Cortex-M0+", + "order": 0, + "toolchain": "GCC" +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index fecd703..233ee1f 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,16 +1,8 @@ -{ - "version": 4, - "configurations": [ - { - /** - * ms-vscode.cmake-tools plugin should be installed. - * - * It provides data for C/C++ plugin, - * such as include paths, browse paths, defines, etc. - */ - "name": "STM32", - "configurationProvider": "ms-vscode.cmake-tools", - "intelliSenseMode": "${default}" - } - ] +{ + "configurations": [ + { + "name": "STM32", + "compileCommands": "${workspaceFolder}/build/Debug/compile_commands.json" + } + ] } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index c539064..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,61 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Build & Debug Microcontroller - ST-Link", - "cwd": "${workspaceFolder}", - "type": "cortex-debug", - "executable": "${command:cmake.launchTargetPath}", - // Let CMake extension decide executable: "${command:cmake.launchTargetPath}" - // Or fixed file path: "${workspaceFolder}/path/to/filename.elf" - "request": "launch", - "servertype": "stlink", - "device": "STM32G030F6Px", //MCU used - "interface": "swd", - "serialNumber": "", //Set ST-Link ID if you use multiple at the same time - "runToEntryPoint": "main", - "svdFile": "${config:STM32VSCodeExtension.cubeCLT.path}/STMicroelectronics_CMSIS_SVD/STM32G030.svd", - "v1": false, //Change it depending on ST Link version - "serverpath": "${config:STM32VSCodeExtension.cubeCLT.path}/STLink-gdb-server/bin/ST-LINK_gdbserver", - "stm32cubeprogrammer":"${config:STM32VSCodeExtension.cubeCLT.path}/STM32CubeProgrammer/bin", - "stlinkPath": "${config:STM32VSCodeExtension.cubeCLT.path}/STLink-gdb-server/bin/ST-LINK_gdbserver", - "armToolchainPath": "${config:STM32VSCodeExtension.cubeCLT.path}/GNU-tools-for-STM32/bin", - "gdbPath":"${config:STM32VSCodeExtension.cubeCLT.path}/GNU-tools-for-STM32/bin/arm-none-eabi-gdb", - "serverArgs": [ - "-m","0", - ], - //"preLaunchTask": "Build + Flash" - /* If you use external loader, add additional arguments */ - //"serverArgs": ["--extload", "path/to/ext/loader.stldr"], - }, - { - "name": "Attach to Microcontroller - ST-Link", - "cwd": "${workspaceFolder}", - "type": "cortex-debug", - "executable": "${command:cmake.launchTargetPath}", - // Let CMake extension decide executable: "${command:cmake.launchTargetPath}" - // Or fixed file path: "${workspaceFolder}/path/to/filename.elf" - "request": "attach", - "servertype": "stlink", - "device": "STM32G030F6Px", //MCU used - "interface": "swd", - "serialNumber": "", //Set ST-Link ID if you use multiple at the same time - "runToEntryPoint": "main", - "svdFile": "${config:STM32VSCodeExtension.cubeCLT.path}/STMicroelectronics_CMSIS_SVD/STM32G030.svd", - "v1": false, //Change it depending on ST Link version - "serverpath": "${config:STM32VSCodeExtension.cubeCLT.path}/STLink-gdb-server/bin/ST-LINK_gdbserver", - "stm32cubeprogrammer":"${config:STM32VSCodeExtension.cubeCLT.path}/STM32CubeProgrammer/bin", - "stlinkPath": "${config:STM32VSCodeExtension.cubeCLT.path}/STLink-gdb-server/bin/ST-LINK_gdbserver", - "armToolchainPath": "${config:STM32VSCodeExtension.cubeCLT.path}/GNU-tools-for-STM32/bin", - "gdbPath":"${config:STM32VSCodeExtension.cubeCLT.path}/GNU-tools-for-STM32/bin/arm-none-eabi-gdb", - "serverArgs": [ - "-m","0", - ], - /* If you use external loader, add additional arguments */ - //"serverArgs": ["--extload", "path/to/ext/loader.stldr"], - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..97b9bec --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,15 @@ +{ + "cmake.cmakePath": "cube-cmake", + "cmake.configureArgs": [ + "-DCMAKE_COMMAND=cube-cmake" + ], + "cmake.preferredGenerators": [ + "Ninja" + ], + "stm32cube-ide-clangd.path": "cube", + "stm32cube-ide-clangd.arguments": [ + "starm-clangd", + "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-gcc", + "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-g++" + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a4f61a7..00aa6ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,14 +15,13 @@ set(CMAKE_C_EXTENSIONS ON) # Define the build type if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE "Debug") + # set(CMAKE_BUILD_TYPE "Debug") + set(CMAKE_BUILD_TYPE "Release") endif() # Set the project name set(CMAKE_PROJECT_NAME G0Test1) -# Include toolchain file -include("cmake/gcc-arm-none-eabi.cmake") # Enable compile command to ease indexing with e.g. clangd @@ -30,7 +29,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) # -DCMAKE_EXPORT_COMPILE_COMMANDS=On # Enable CMake support for ASM and C languages -enable_language(ASM CXX) +enable_language(ASM CXX) set(__cplusplus TRUE) @@ -59,6 +58,15 @@ target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE # Add user defined include paths ) +# get_target_property(APP_SOURCES ${CMAKE_PROJECT_NAME} SOURCES) +# list(REMOVE_ITEM APP_SOURCES +# ${CMAKE_CURRENT_SOURCE_DIR}/Core/Src/main.c +# ) +# set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY SOURCES "${APP_SOURCES}" +# ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/TLE5012.cpp +# ) + + # Add project symbols (macros) target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE # Add user defined symbols diff --git a/Core/Inc/TLE5012.hpp b/Core/Inc/TLE5012.hpp index c443dbc..0ba1af4 100644 --- a/Core/Inc/TLE5012.hpp +++ b/Core/Inc/TLE5012.hpp @@ -2,20 +2,23 @@ #define TLE5012 #include "stm32g030xx.h" #include "stm32g0xx_hal.h" -#define RW 0x8000U -#define AVAL 0x0020U -#define SAFETY 0x0001U +#include + +constexpr uint16_t RW = 0x8000U; +constexpr uint16_t AVAL = 0x0020U; +constexpr uint16_t SAFETY = 0x0001U; class Tle5012 { -public: - Tle5012(GPIO_TypeDef *csPort, uint16_t csPin, SPI_HandleTypeDef *spiHandler); - double getAngel(); + public: + Tle5012(GPIO_TypeDef * csPort, uint16_t csPin, + SPI_HandleTypeDef * spiHandler); + uint16_t getAngel(); -private: - GPIO_TypeDef *csPort; - uint16_t csPin; - SPI_HandleTypeDef *spiHandler; + private: + GPIO_TypeDef * csPort; + uint16_t csPin; + SPI_HandleTypeDef * spiHandler; }; #endif \ No newline at end of file diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 72e8727..579d2ea 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -49,6 +49,8 @@ extern "C" { /* USER CODE END EM */ +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); + /* Exported functions prototypes ---------------------------------------------*/ void Error_Handler(void); @@ -57,6 +59,10 @@ void Error_Handler(void); /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ +#define CS_Sensor_Pin GPIO_PIN_1 +#define CS_Sensor_GPIO_Port GPIOA +#define BRAKE_Pin GPIO_PIN_2 +#define BRAKE_GPIO_Port GPIOA /* USER CODE BEGIN Private defines */ diff --git a/Core/Inc/stm32g0xx_hal_conf.h b/Core/Inc/stm32g0xx_hal_conf.h index 93efd0f..baab2c3 100644 --- a/Core/Inc/stm32g0xx_hal_conf.h +++ b/Core/Inc/stm32g0xx_hal_conf.h @@ -54,7 +54,7 @@ extern "C" { /* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_SMBUS_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED -/* #define HAL_TIM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED /* #define HAL_USART_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */ diff --git a/Core/Inc/stm32g0xx_it.h b/Core/Inc/stm32g0xx_it.h index b3e85d3..f230851 100644 --- a/Core/Inc/stm32g0xx_it.h +++ b/Core/Inc/stm32g0xx_it.h @@ -22,7 +22,7 @@ #define __STM32G0xx_IT_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Private includes ----------------------------------------------------------*/ @@ -52,6 +52,7 @@ void SVC_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void DMA1_Channel1_IRQHandler(void); +void DMA1_Ch4_5_DMAMUX1_OVR_IRQHandler(void); void SPI2_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/TLE5012.cpp b/Core/Src/TLE5012.cpp index bbe3925..c12624e 100644 --- a/Core/Src/TLE5012.cpp +++ b/Core/Src/TLE5012.cpp @@ -4,22 +4,19 @@ #include "stm32g0xx_hal_spi.h" #include - -Tle5012::Tle5012(GPIO_TypeDef * csPort, uint16_t csPin, SPI_HandleTypeDef *spiHandler){ +Tle5012::Tle5012(GPIO_TypeDef * csPort, uint16_t csPin, + SPI_HandleTypeDef * spiHandler) { this->csPort = csPort; this->csPin = csPin; this->spiHandler = spiHandler; - } -double Tle5012::getAngel(){ +uint16_t Tle5012::getAngel() { // enable chip select HAL_GPIO_WritePin(this->csPort, this->csPin, GPIO_PIN_RESET); - - // uint16_t command = 0b1000000000100001; uint16_t command = RW | AVAL | SAFETY; - + uint8_t formatedCommand[6] = {0}; formatedCommand[0] = (uint8_t)(command >> 8); formatedCommand[1] = (uint8_t)command; @@ -30,26 +27,33 @@ double Tle5012::getAngel(){ HAL_SPI_Transmit(this->spiHandler, formatedCommand, 2, 100); HAL_SPI_Receive(this->spiHandler, angle, 4, 0xFFFF); + // HAL_SPI_TransmitReceive(this->spiHandler, formatedCommand, angle, 6, 1); // HAL_SPI_Receive(&hspi1, (uint8_t *)(&safety), 2, 0xFF); + HAL_GPIO_WritePin(this->csPort, this->csPin, GPIO_PIN_SET); int16_t signedAngle = ((angle[0] << 8 | angle[1]) & 0x3FFF); bool bitSet = 0; - if (angle[0] & 0b01000000){ + if (angle[0] & 0b01000000) { signedAngle -= 16384; bitSet = 1; } - else{ + else { bitSet = 0; } - - double angleDeg = 360.0 / 32768.0 * signedAngle; + + // normalize range + uint16_t normalizedValue; + if (signedAngle < 0) { + normalizedValue = -signedAngle; + } + else { + normalizedValue = 16383 + (16383 - signedAngle); + } + + // double angleDeg = 360.0 / 32768.0 * signedAngle; // double angleDeg = 360.0 / 128.0 * ((angle[0] >> 8) & 0x7F); - // disable chip select - HAL_GPIO_WritePin(this->csPort, this->csPin, GPIO_PIN_SET); - - return angleDeg; + return normalizedValue; } - diff --git a/Core/Src/main.c b/Core/Src/main.c deleted file mode 100644 index e9659cb..0000000 --- a/Core/Src/main.c +++ /dev/null @@ -1,370 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2025 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "main.h" - -/* Private includes ----------------------------------------------------------*/ -/* USER CODE BEGIN Includes */ - -/* USER CODE END Includes */ - -/* Private typedef -----------------------------------------------------------*/ -/* USER CODE BEGIN PTD */ - -/* USER CODE END PTD */ - -/* Private define ------------------------------------------------------------*/ -/* USER CODE BEGIN PD */ - -/* USER CODE END PD */ - -/* Private macro -------------------------------------------------------------*/ -/* USER CODE BEGIN PM */ - -/* USER CODE END PM */ - -/* Private variables ---------------------------------------------------------*/ -SPI_HandleTypeDef hspi1; -SPI_HandleTypeDef hspi2; -DMA_HandleTypeDef hdma_spi2_tx; - -UART_HandleTypeDef huart1; - -/* USER CODE BEGIN PV */ - -/* USER CODE END PV */ - -/* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); -static void MX_GPIO_Init(void); -static void MX_DMA_Init(void); -static void MX_USART1_UART_Init(void); -static void MX_SPI1_Init(void); -static void MX_SPI2_Init(void); -/* USER CODE BEGIN PFP */ - -/* USER CODE END PFP */ - -/* Private user code ---------------------------------------------------------*/ -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -/** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ - - /* USER CODE BEGIN 1 */ - - /* USER CODE END 1 */ - - /* MCU Configuration--------------------------------------------------------*/ - - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); - - /* USER CODE BEGIN Init */ - - /* USER CODE END Init */ - - /* Configure the system clock */ - SystemClock_Config(); - - /* USER CODE BEGIN SysInit */ - - /* USER CODE END SysInit */ - - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_USART1_UART_Init(); - MX_SPI1_Init(); - MX_SPI2_Init(); - /* USER CODE BEGIN 2 */ - - /* USER CODE END 2 */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - } - /* USER CODE END 3 */ -} - -/** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - - /** Configure the main internal regulator output voltage - */ - HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); - - /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; - RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; - RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1; - RCC_OscInitStruct.PLL.PLLN = 10; - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { - Error_Handler(); - } - - /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; - - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) - { - Error_Handler(); - } -} - -/** - * @brief SPI1 Initialization Function - * @param None - * @retval None - */ -static void MX_SPI1_Init(void) -{ - - /* USER CODE BEGIN SPI1_Init 0 */ - - /* USER CODE END SPI1_Init 0 */ - - /* USER CODE BEGIN SPI1_Init 1 */ - - /* USER CODE END SPI1_Init 1 */ - /* SPI1 parameter configuration*/ - hspi1.Instance = SPI1; - hspi1.Init.Mode = SPI_MODE_MASTER; - hspi1.Init.Direction = SPI_DIRECTION_2LINES; - hspi1.Init.DataSize = SPI_DATASIZE_8BIT; - hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; - hspi1.Init.NSS = SPI_NSS_SOFT; - hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; - hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi1.Init.TIMode = SPI_TIMODE_DISABLE; - hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi1.Init.CRCPolynomial = 7; - hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; - hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; - if (HAL_SPI_Init(&hspi1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN SPI1_Init 2 */ - - /* USER CODE END SPI1_Init 2 */ - -} - -/** - * @brief SPI2 Initialization Function - * @param None - * @retval None - */ -static void MX_SPI2_Init(void) -{ - - /* USER CODE BEGIN SPI2_Init 0 */ - - /* USER CODE END SPI2_Init 0 */ - - /* USER CODE BEGIN SPI2_Init 1 */ - - /* USER CODE END SPI2_Init 1 */ - /* SPI2 parameter configuration*/ - hspi2.Instance = SPI2; - hspi2.Init.Mode = SPI_MODE_MASTER; - hspi2.Init.Direction = SPI_DIRECTION_2LINES; - hspi2.Init.DataSize = SPI_DATASIZE_8BIT; - hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; - hspi2.Init.NSS = SPI_NSS_SOFT; - hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; - hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi2.Init.TIMode = SPI_TIMODE_DISABLE; - hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi2.Init.CRCPolynomial = 7; - hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; - hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; - if (HAL_SPI_Init(&hspi2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN SPI2_Init 2 */ - - /* USER CODE END SPI2_Init 2 */ - -} - -/** - * @brief USART1 Initialization Function - * @param None - * @retval None - */ -static void MX_USART1_UART_Init(void) -{ - - /* USER CODE BEGIN USART1_Init 0 */ - - /* USER CODE END USART1_Init 0 */ - - /* USER CODE BEGIN USART1_Init 1 */ - - /* USER CODE END USART1_Init 1 */ - huart1.Instance = USART1; - huart1.Init.BaudRate = 9600; - huart1.Init.WordLength = UART_WORDLENGTH_8B; - huart1.Init.StopBits = UART_STOPBITS_1; - huart1.Init.Parity = UART_PARITY_NONE; - huart1.Init.Mode = UART_MODE_TX_RX; - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart1.Init.OverSampling = UART_OVERSAMPLING_16; - huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; - huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; - huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; - if (HAL_UART_Init(&huart1) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN USART1_Init 2 */ - - /* USER CODE END USART1_Init 2 */ - -} - -/** - * Enable DMA controller clock - */ -static void MX_DMA_Init(void) -{ - - /* DMA controller clock enable */ - __HAL_RCC_DMA1_CLK_ENABLE(); - - /* DMA interrupt init */ - /* DMA1_Channel1_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); - -} - -/** - * @brief GPIO Initialization Function - * @param None - * @retval None - */ -static void MX_GPIO_Init(void) -{ - GPIO_InitTypeDef GPIO_InitStruct = {0}; -/* USER CODE BEGIN MX_GPIO_Init_1 */ -/* USER CODE END MX_GPIO_Init_1 */ - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); - - /*Configure GPIO pin : PA7 */ - GPIO_InitStruct.Pin = GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - -/* USER CODE BEGIN MX_GPIO_Init_2 */ -/* USER CODE END MX_GPIO_Init_2 */ -} - -/* USER CODE BEGIN 4 */ - -/* USER CODE END 4 */ - -/** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* USER CODE END Error_Handler_Debug */ -} - -#ifdef USE_FULL_ASSERT -/** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ - /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - /* USER CODE END 6 */ -} -#endif /* USE_FULL_ASSERT */ diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 0463909..9e3667c 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -1,55 +1,44 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2025 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" -#include "stm32g030xx.h" -#include "stm32g0xx_hal.h" -#include "stm32g0xx_hal_gpio.h" - /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "TLE5012.hpp" +#include "stm32g030xx.h" +#include "stm32g0xx_hal.h" +#include "stm32g0xx_hal_gpio.h" +#include "stm32g0xx_hal_tim.h" #include "stm32g0xx_hal_uart.h" +#include #include +constexpr uint8_t NUM_LED = 10; -#define NUM_LED 10 -#define READ_BLOCK_CRC 0x8088 +uint8_t buffer[24 * NUM_LED]; -// TLE5012 sensor -#define READ_SENSOR 0x8000 // READ -#define REG_AVAL (0x0020U) -#define UPD_low 0x0000 -#define SAFE_low 0x0000 //!< \brief switch of safety word generation -#define SAFE_high 0x0001 -#define WRITE_SENSOR 0x5000 - - -uint8_t buffer[24*NUM_LED]; - -struct color{ +struct color { uint8_t red; uint8_t green; uint8_t blue; }; - /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -72,6 +61,9 @@ SPI_HandleTypeDef hspi1; SPI_HandleTypeDef hspi2; DMA_HandleTypeDef hdma_spi2_tx; +TIM_HandleTypeDef htim1; +DMA_HandleTypeDef hdma_tim1_ch4; + UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ @@ -85,16 +77,15 @@ static void MX_DMA_Init(void); static void MX_USART1_UART_Init(void); static void MX_SPI1_Init(void); static void MX_SPI2_Init(void); +static void MX_TIM1_Init(void); /* USER CODE BEGIN PFP */ void setLED(uint8_t led, uint8_t RED, uint8_t GREEN, uint8_t BLUE); void ws2812_spi(struct color * led_data); -void angleToCharArray(double angle, char* charAngle); +void angleToCharArray(double angle, char * charAngle); +uint16_t mapToRange(uint16_t minIn, uint16_t maxIn, uint16_t minOut, + uint16_t maxOut, uint16_t input); // void getAngle(uint8_t * angle); // void setOneSensor(); - - - - /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ @@ -103,405 +94,379 @@ void angleToCharArray(double angle, char* charAngle); /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { - /* USER CODE BEGIN 1 */ + /* USER CODE BEGIN 1 */ - /* USER CODE END 1 */ + /* USER CODE END 1 */ - /* MCU Configuration--------------------------------------------------------*/ + /* MCU + * Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); + /* Reset of all peripherals, Initializes the Flash interface and the + * Systick. */ + HAL_Init(); - /* USER CODE BEGIN Init */ + /* USER CODE BEGIN Init */ - /* USER CODE END Init */ + /* USER CODE END Init */ - /* Configure the system clock */ - SystemClock_Config(); + /* Configure the system clock */ + SystemClock_Config(); - /* USER CODE BEGIN SysInit */ + /* USER CODE BEGIN SysInit */ - /* USER CODE END SysInit */ + /* USER CODE END SysInit */ - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_USART1_UART_Init(); - MX_SPI1_Init(); - MX_SPI2_Init(); - /* USER CODE BEGIN 2 */ -// HAL_DMA_Init(&hdma_spi2_tx); + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_DMA_Init(); + MX_USART1_UART_Init(); + MX_SPI1_Init(); + // MX_SPI2_Init(); + MX_TIM1_Init(); + /* USER CODE BEGIN 2 */ - struct color led_data[NUM_LED]; - for(uint8_t i = 0; i < NUM_LED; i++){ - led_data[i].red = 10; - led_data[i].blue = 10; - led_data[i].green = 10; + // disable brake + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); + + Tle5012 AngleSensor = Tle5012(GPIOA, GPIO_PIN_1, &hspi1); + + uint16_t dutyCycle = 0; + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4); + TIM1->CCR4 = 0; + + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) { + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + + uint16_t angleSensor = AngleSensor.getAngel(); + + dutyCycle = mapToRange(12600, 16100, 0, 1000, angleSensor); + + if (dutyCycle < 100) { + dutyCycle = 0; + // enable brake + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); + } + else { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); + } + + if (dutyCycle > 950) { + dutyCycle = 1000; + } + + TIM1->CCR4 = dutyCycle; + HAL_Delay(5); } - ws2812_spi(led_data); - int i = 0; - uint8_t angle[6]; + /* USER CODE END 3 */ +} - Tle5012 AngleSensor = Tle5012(GPIOA, GPIO_PIN_7, &hspi1); +/** + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - // setOneSensor(); + /** Configure the main internal regulator output voltage + */ + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); - /* USER CODE END 2 */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4); - // HAL_Delay(1000); - // HAL_UART_Transmit(&huart1, buffer, sizeof(buffer), 10000); - - // setLED(0, 50, 1, 20); - - // uint32_t retcode = HAL_SPI_Transmit_DMA(&hspi1, uint8_t *pData, uint16_t Size) - // build read command - - - - - // uint16_t READ_COMMAND = - // HAL_SPI_TransmitReceive_DMA(&hspi1, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) - // uint32_t retcode = HAL_SPI_Transmit_DMA(&hspi2, buffer, 24*NUM_LED); - - - - // uint8_t maxBrightness = 50; - // uint8_t delay = 10; - // for(uint8_t i = 0; i < maxBrightness; i++){ - // led_data[0].blue = i; - // led_data[0].red = 0; - // led_data[0].green = 0; - // ws2812_spi(led_data); - // HAL_Delay(delay); - // } - - // ws2812_spi(led_data); - // for(uint8_t i = 0; i < maxBrightness; i++){ - // led_data[0].blue = 0; - // led_data[0].red = i; - // led_data[0].green = 0; - // ws2812_spi(led_data); - // HAL_Delay(delay); - // } - // for(uint8_t i = 0; i < maxBrightness; i++){ - // led_data[0].blue = 0; - // led_data[0].red = 0; - // led_data[0].green = i; - // ws2812_spi(led_data); - // HAL_Delay(delay); - // } - - // for(uint8_t i = 0; i < maxBrightness; i++){ - // led_data[1].blue = i; - // led_data[1].red = 0; - // led_data[1].green = 0; - // ws2812_spi(led_data); - // HAL_Delay(delay); - // } - // for(uint8_t i = 0; i < maxBrightness; i++){ - // led_data[1].blue = 0; - // led_data[1].red = i; - // led_data[1].green = 0; - // ws2812_spi(led_data); - // HAL_Delay(delay); - // } - // for(uint8_t i = 0; i < maxBrightness; i++){ - // led_data[1].blue = 0; - // led_data[1].red = 0; - // led_data[1].green = i; - // ws2812_spi(led_data); - // HAL_Delay(delay); - // } - - // getAngle(angle); - // uint16_t command = READ_SENSOR | REG_AVAL | UPD_low | SAFE_low; - - // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET); - // HAL_Delay(1); - // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); - - // uint16_t command = 0b1000000000100001; - // // 1000000000100001 - // uint8_t formatedCommand[6] = {0}; - // formatedCommand[0] = (uint8_t)(command >> 8); - // formatedCommand[1] = (uint8_t)command; - - // uint16_t angle[3] = {0}; - // uint16_t safety; - - // // HAL_SPI_TransmitReceive_DMA(&hspi1,formatedCommand, angle, 6); - // HAL_SPI_Transmit(&hspi1, formatedCommand, 2, 100); - - // HAL_SPI_Receive(&hspi1, (uint8_t *)(angle), 4, 0xFFFF); - // // HAL_SPI_Receive(&hspi1, (uint8_t *)(&safety), 2, 0xFF); - - // double angleDeg = 360.0 / 32768.0 * (angle[0] & 0x7FFF); - - // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET); - - // // HAL_SPI_Transmit(&hspi1,formatedCommand,2,1000); - // // HAL_SPI_Receive(&hspi1, angle, 2, 1000); - // HAL_Delay(10); - double angle = AngleSensor.getAngel(); - if(angle < 0){ - angle *= 1; + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; + RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1; + RCC_OscInitStruct.PLL.PLLN = 10; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); } - for(uint8_t i = 0; i < NUM_LED; i++){ - led_data[i].red = (uint8_t)angle; - led_data[i].blue = 180-(uint8_t)angle; - led_data[i].green = (uint8_t)angle*0; + + /** Initializes the CPU, AHB and APB buses clocks + */ + RCC_ClkInitStruct.ClockType = + RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { + Error_Handler(); } - ws2812_spi(led_data); - - // char buf2[] = "Hallo Welt\n"; - // HAL_UART_Transmit(&huart1, (uint8_t*)buf2, sizeof(buf2), 1000); - char charAngle[6] = {0}; - charAngle[5] = '\n'; - angleToCharArray(angle, charAngle); - - HAL_UART_Transmit(&huart1, (uint8_t*)charAngle, sizeof(charAngle), 1000); - HAL_Delay(100); - - } - /* USER CODE END 3 */ } /** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + * @brief SPI1 Initialization Function + * @param None + * @retval None + */ +static void MX_SPI1_Init(void) { - /** Configure the main internal regulator output voltage - */ - HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); + /* USER CODE BEGIN SPI1_Init 0 */ - /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; - RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; - RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1; - RCC_OscInitStruct.PLL.PLLN = 10; - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { - Error_Handler(); - } + /* USER CODE END SPI1_Init 0 */ - /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + /* USER CODE BEGIN SPI1_Init 1 */ - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) - { - Error_Handler(); - } + /* USER CODE END SPI1_Init 1 */ + /* SPI1 parameter configuration*/ + hspi1.Instance = SPI1; + hspi1.Init.Mode = SPI_MODE_MASTER; + hspi1.Init.Direction = SPI_DIRECTION_1LINE; + hspi1.Init.DataSize = SPI_DATASIZE_8BIT; + hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; + hspi1.Init.NSS = SPI_NSS_SOFT; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; + hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi1.Init.TIMode = SPI_TIMODE_DISABLE; + hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi1.Init.CRCPolynomial = 7; + hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; + if (HAL_SPI_Init(&hspi1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN SPI1_Init 2 */ + + /* USER CODE END SPI1_Init 2 */ +} +/** + * @brief SPI2 Initialization Function + * @param None + * @retval None + */ +static void MX_SPI2_Init(void) { + + /* USER CODE BEGIN SPI2_Init 0 */ + + /* USER CODE END SPI2_Init 0 */ + + /* USER CODE BEGIN SPI2_Init 1 */ + + /* USER CODE END SPI2_Init 1 */ + /* SPI2 parameter configuration*/ + hspi2.Instance = SPI2; + hspi2.Init.Mode = SPI_MODE_MASTER; + hspi2.Init.Direction = SPI_DIRECTION_2LINES; + hspi2.Init.DataSize = SPI_DATASIZE_8BIT; + hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi2.Init.NSS = SPI_NSS_SOFT; + hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; + hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi2.Init.TIMode = SPI_TIMODE_DISABLE; + hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi2.Init.CRCPolynomial = 7; + hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; + if (HAL_SPI_Init(&hspi2) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN SPI2_Init 2 */ + + /* USER CODE END SPI2_Init 2 */ } /** - * @brief SPI1 Initialization Function - * @param None - * @retval None - */ -static void MX_SPI1_Init(void) -{ + * @brief TIM1 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM1_Init(void) { - /* USER CODE BEGIN SPI1_Init 0 */ + /* USER CODE BEGIN TIM1_Init 0 */ - /* USER CODE END SPI1_Init 0 */ + /* USER CODE END TIM1_Init 0 */ - /* USER CODE BEGIN SPI1_Init 1 */ + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; - /* USER CODE END SPI1_Init 1 */ - /* SPI1 parameter configuration*/ - hspi1.Instance = SPI1; - hspi1.Init.Mode = SPI_MODE_MASTER; - hspi1.Init.Direction = SPI_DIRECTION_2LINES; - hspi1.Init.DataSize = SPI_DATASIZE_8BIT; - hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; - hspi1.Init.NSS = SPI_NSS_SOFT; - hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; - hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi1.Init.TIMode = SPI_TIMODE_DISABLE; - hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi1.Init.CRCPolynomial = 7; - hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; - hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; - if (HAL_SPI_Init(&hspi1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN SPI1_Init 2 */ + /* USER CODE BEGIN TIM1_Init 1 */ - /* USER CODE END SPI1_Init 2 */ + /* USER CODE END TIM1_Init 1 */ + htim1.Instance = TIM1; + htim1.Init.Prescaler = 20 - 1; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + htim1.Init.Period = 1000 - 1; + htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim1.Init.RepetitionCounter = 0; + htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim1) != HAL_OK) { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) { + Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim1) != HAL_OK) { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != + HAL_OK) { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; + if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != + HAL_OK) { + Error_Handler(); + } + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = 0; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.BreakFilter = 0; + sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT; + sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; + sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; + sBreakDeadTimeConfig.Break2Filter = 0; + sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != + HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN TIM1_Init 2 */ + /* USER CODE END TIM1_Init 2 */ + HAL_TIM_MspPostInit(&htim1); } /** - * @brief SPI2 Initialization Function - * @param None - * @retval None - */ -static void MX_SPI2_Init(void) -{ + * @brief USART1 Initialization Function + * @param None + * @retval None + */ +static void MX_USART1_UART_Init(void) { - /* USER CODE BEGIN SPI2_Init 0 */ + /* USER CODE BEGIN USART1_Init 0 */ - /* USER CODE END SPI2_Init 0 */ + /* USER CODE END USART1_Init 0 */ - /* USER CODE BEGIN SPI2_Init 1 */ + /* USER CODE BEGIN USART1_Init 1 */ - /* USER CODE END SPI2_Init 1 */ - /* SPI2 parameter configuration*/ - hspi2.Instance = SPI2; - hspi2.Init.Mode = SPI_MODE_MASTER; - hspi2.Init.Direction = SPI_DIRECTION_2LINES; - hspi2.Init.DataSize = SPI_DATASIZE_8BIT; - hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; - hspi2.Init.NSS = SPI_NSS_SOFT; - hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; - hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi2.Init.TIMode = SPI_TIMODE_DISABLE; - hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi2.Init.CRCPolynomial = 7; - hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; - hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; - if (HAL_SPI_Init(&hspi2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN SPI2_Init 2 */ - - /* USER CODE END SPI2_Init 2 */ + /* USER CODE END USART1_Init 1 */ + huart1.Instance = USART1; + huart1.Init.BaudRate = 9600; + huart1.Init.WordLength = UART_WORDLENGTH_8B; + huart1.Init.StopBits = UART_STOPBITS_1; + huart1.Init.Parity = UART_PARITY_NONE; + huart1.Init.Mode = UART_MODE_TX_RX; + huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart1.Init.OverSampling = UART_OVERSAMPLING_16; + huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; + huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_UART_Init(&huart1) != HAL_OK) { + Error_Handler(); + } + if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != + HAL_OK) { + Error_Handler(); + } + if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != + HAL_OK) { + Error_Handler(); + } + if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN USART1_Init 2 */ + /* USER CODE END USART1_Init 2 */ } /** - * @brief USART1 Initialization Function - * @param None - * @retval None - */ -static void MX_USART1_UART_Init(void) -{ + * Enable DMA controller clock + */ +static void MX_DMA_Init(void) { - /* USER CODE BEGIN USART1_Init 0 */ - - /* USER CODE END USART1_Init 0 */ - - /* USER CODE BEGIN USART1_Init 1 */ - - /* USER CODE END USART1_Init 1 */ - huart1.Instance = USART1; - huart1.Init.BaudRate = 9600; - huart1.Init.WordLength = UART_WORDLENGTH_8B; - huart1.Init.StopBits = UART_STOPBITS_1; - huart1.Init.Parity = UART_PARITY_NONE; - huart1.Init.Mode = UART_MODE_TX_RX; - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart1.Init.OverSampling = UART_OVERSAMPLING_16; - huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; - huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; - huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; - if (HAL_UART_Init(&huart1) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN USART1_Init 2 */ - - /* USER CODE END USART1_Init 2 */ + /* DMA controller clock enable */ + __HAL_RCC_DMA1_CLK_ENABLE(); + /* DMA interrupt init */ + /* DMA1_Channel1_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); } /** - * Enable DMA controller clock - */ -static void MX_DMA_Init(void) -{ + * @brief GPIO Initialization Function + * @param None + * @retval None + */ +static void MX_GPIO_Init(void) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + /* USER CODE BEGIN MX_GPIO_Init_1 */ - /* DMA controller clock enable */ - __HAL_RCC_DMA1_CLK_ENABLE(); + /* USER CODE END MX_GPIO_Init_1 */ - /* DMA interrupt init */ - /* DMA1_Channel1_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); -} + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOA, CS_Sensor_Pin | BRAKE_Pin, GPIO_PIN_RESET); -/** - * @brief GPIO Initialization Function - * @param None - * @retval None - */ -static void MX_GPIO_Init(void) -{ - GPIO_InitTypeDef GPIO_InitStruct = {0}; -/* USER CODE BEGIN MX_GPIO_Init_1 */ -/* USER CODE END MX_GPIO_Init_1 */ + /*Configure GPIO pins : CS_Sensor_Pin BRAKE_Pin */ + GPIO_InitStruct.Pin = CS_Sensor_Pin | BRAKE_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); + /* USER CODE BEGIN MX_GPIO_Init_2 */ - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); - - /*Configure GPIO pin : PA7 */ - GPIO_InitStruct.Pin = GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - -/* USER CODE BEGIN MX_GPIO_Init_2 */ -/* USER CODE END MX_GPIO_Init_2 */ + /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ +uint16_t mapToRange(uint16_t minIn, uint16_t maxIn, uint16_t minOut, + uint16_t maxOut, uint16_t input) { + float m = (float)(maxOut - minOut) / (maxIn - minIn); + float b = minOut - m * minIn; + + uint16_t valToRet = input * m + b; + if (valToRet > maxOut) { + return 0; + } + return uint16_t(input * m + b); + // minIn should be lower than maxIn +} + // void setLED(uint8_t led, uint8_t RED, uint8_t GREEN, uint8_t BLUE){ // LED_DATA[led][0] = led; // LED_DATA[led][1] = GREEN; @@ -509,7 +474,6 @@ static void MX_GPIO_Init(void) // LED_DATA[led][3] = BLUE; // } - // void setOneSensor(){ // uint8_t command[4] = {0}; @@ -517,37 +481,38 @@ static void MX_GPIO_Init(void) // command[1] = 0; // command[2] = 0; // command[3] = 0; - -// HAL_SPI_Transmit(&hspi1, command, 4, 1000); + +// HAL_SPI_Transmit(&hspi1, command, 4, 1000); // } -void ws2812_spi(struct color *led_data){ +void ws2812_spi(struct color * led_data) { int index = 0; - for(uint8_t x = 0; x < NUM_LED; x++){ - uint32_t color = led_data[x].green << 16 | led_data[x].red << 8 | led_data[x].blue; + for (uint8_t x = 0; x < NUM_LED; x++) { + uint32_t color = + led_data[x].green << 16 | led_data[x].red << 8 | led_data[x].blue; // index = 0; - for(int i = 23; i >= 0; i--){ - if(((color >> i) & 0x01) == 1){ + for (int i = 23; i >= 0; i--) { + if (((color >> i) & 0x01) == 1) { buffer[index++] = 0b110; // 1 } - else{ + else { buffer[index++] = 0b100; // 0 } } } // HAL_SPI_Transmit(&hspi2, sendData, 24*NUM_LED, 1000); - uint32_t retcode = HAL_SPI_Transmit_DMA(&hspi2, buffer, 24*NUM_LED); + uint32_t retcode = HAL_SPI_Transmit_DMA(&hspi2, buffer, 24 * NUM_LED); // uint8_t fuck = 1; } -void angleToCharArray(double angle, char* charAngle){ - - int16_t angleInt = (int16_t)(angle*10); - if (angle < 0){ +void angleToCharArray(double angle, char * charAngle) { + + int16_t angleInt = (int16_t)(angle * 10); + if (angle < 0) { charAngle[0] = '-'; angleInt *= -1; } - else{ + else { charAngle[0] = '+'; } charAngle[4] = angleInt % 10 + 48; @@ -558,38 +523,34 @@ void angleToCharArray(double angle, char* charAngle){ angleInt /= 10; charAngle[1] = angleInt % 10 + 48; } - - /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* USER CODE END Error_Handler_Debug */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) { + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state + */ + __disable_irq(); + while (1) { + } + /* USER CODE END Error_Handler_Debug */ } - -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ - /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - /* USER CODE END 6 */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t * file, uint32_t line) { + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", + file, line) */ + /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/Core/Src/stm32g0xx_hal_msp.c b/Core/Src/stm32g0xx_hal_msp.c index 849c48a..7dc9fd7 100644 --- a/Core/Src/stm32g0xx_hal_msp.c +++ b/Core/Src/stm32g0xx_hal_msp.c @@ -25,6 +25,8 @@ /* USER CODE END Includes */ extern DMA_HandleTypeDef hdma_spi2_tx; +extern DMA_HandleTypeDef hdma_tim1_ch4; + /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN TD */ @@ -58,7 +60,9 @@ extern DMA_HandleTypeDef hdma_spi2_tx; /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ -/** + +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); + /** * Initializes the Global MSP. */ void HAL_MspInit(void) @@ -79,44 +83,43 @@ void HAL_MspInit(void) } /** -* @brief SPI MSP Initialization -* This function configures the hardware resources used in this example -* @param hspi: SPI handle pointer -* @retval None -*/ + * @brief SPI MSP Initialization + * This function configures the hardware resources used in this example + * @param hspi: SPI handle pointer + * @retval None + */ void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(hspi->Instance==SPI1) { - /* USER CODE BEGIN SPI1_MspInit 0 */ + /* USER CODE BEGIN SPI1_MspInit 0 */ - /* USER CODE END SPI1_MspInit 0 */ + /* USER CODE END SPI1_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /**SPI1 GPIO Configuration - PA1 ------> SPI1_SCK - PA2 ------> SPI1_MOSI - PA6 ------> SPI1_MISO + PA5 ------> SPI1_SCK + PA7 ------> SPI1_MOSI */ - GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_6; + GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USER CODE BEGIN SPI1_MspInit 1 */ + /* USER CODE BEGIN SPI1_MspInit 1 */ - /* USER CODE END SPI1_MspInit 1 */ + /* USER CODE END SPI1_MspInit 1 */ } else if(hspi->Instance==SPI2) { - /* USER CODE BEGIN SPI2_MspInit 0 */ + /* USER CODE BEGIN SPI2_MspInit 0 */ - /* USER CODE END SPI2_MspInit 0 */ + /* USER CODE END SPI2_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI2_CLK_ENABLE(); @@ -161,45 +164,44 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) /* SPI2 interrupt Init */ HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(SPI2_IRQn); - /* USER CODE BEGIN SPI2_MspInit 1 */ + /* USER CODE BEGIN SPI2_MspInit 1 */ - /* USER CODE END SPI2_MspInit 1 */ + /* USER CODE END SPI2_MspInit 1 */ } } /** -* @brief SPI MSP De-Initialization -* This function freeze the hardware resources used in this example -* @param hspi: SPI handle pointer -* @retval None -*/ + * @brief SPI MSP De-Initialization + * This function freeze the hardware resources used in this example + * @param hspi: SPI handle pointer + * @retval None + */ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) { if(hspi->Instance==SPI1) { - /* USER CODE BEGIN SPI1_MspDeInit 0 */ + /* USER CODE BEGIN SPI1_MspDeInit 0 */ - /* USER CODE END SPI1_MspDeInit 0 */ + /* USER CODE END SPI1_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_SPI1_CLK_DISABLE(); /**SPI1 GPIO Configuration - PA1 ------> SPI1_SCK - PA2 ------> SPI1_MOSI - PA6 ------> SPI1_MISO + PA5 ------> SPI1_SCK + PA7 ------> SPI1_MOSI */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_6); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_7); - /* USER CODE BEGIN SPI1_MspDeInit 1 */ + /* USER CODE BEGIN SPI1_MspDeInit 1 */ - /* USER CODE END SPI1_MspDeInit 1 */ + /* USER CODE END SPI1_MspDeInit 1 */ } else if(hspi->Instance==SPI2) { - /* USER CODE BEGIN SPI2_MspDeInit 0 */ + /* USER CODE BEGIN SPI2_MspDeInit 0 */ - /* USER CODE END SPI2_MspDeInit 0 */ + /* USER CODE END SPI2_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_SPI2_CLK_DISABLE(); @@ -215,28 +217,121 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) /* SPI2 interrupt DeInit */ HAL_NVIC_DisableIRQ(SPI2_IRQn); - /* USER CODE BEGIN SPI2_MspDeInit 1 */ + /* USER CODE BEGIN SPI2_MspDeInit 1 */ - /* USER CODE END SPI2_MspDeInit 1 */ + /* USER CODE END SPI2_MspDeInit 1 */ } } /** -* @brief UART MSP Initialization -* This function configures the hardware resources used in this example -* @param huart: UART handle pointer -* @retval None -*/ + * @brief TIM_Base MSP Initialization + * This function configures the hardware resources used in this example + * @param htim_base: TIM_Base handle pointer + * @retval None + */ +void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) +{ + if(htim_base->Instance==TIM1) + { + /* USER CODE BEGIN TIM1_MspInit 0 */ + + /* USER CODE END TIM1_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_TIM1_CLK_ENABLE(); + + /* TIM1 DMA Init */ + /* TIM1_CH4 Init */ + hdma_tim1_ch4.Instance = DMA1_Channel4; + hdma_tim1_ch4.Init.Request = DMA_REQUEST_TIM1_CH4; + hdma_tim1_ch4.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_tim1_ch4.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_tim1_ch4.Init.MemInc = DMA_MINC_ENABLE; + hdma_tim1_ch4.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_tim1_ch4.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; + hdma_tim1_ch4.Init.Mode = DMA_NORMAL; + hdma_tim1_ch4.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_tim1_ch4) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_CC4],hdma_tim1_ch4); + + /* USER CODE BEGIN TIM1_MspInit 1 */ + + /* USER CODE END TIM1_MspInit 1 */ + + } + +} + +void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(htim->Instance==TIM1) + { + /* USER CODE BEGIN TIM1_MspPostInit 0 */ + + /* USER CODE END TIM1_MspPostInit 0 */ + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**TIM1 GPIO Configuration + PA11 [PA9] ------> TIM1_CH4 + */ + GPIO_InitStruct.Pin = GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM1_MspPostInit 1 */ + + /* USER CODE END TIM1_MspPostInit 1 */ + } + +} +/** + * @brief TIM_Base MSP De-Initialization + * This function freeze the hardware resources used in this example + * @param htim_base: TIM_Base handle pointer + * @retval None + */ +void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) +{ + if(htim_base->Instance==TIM1) + { + /* USER CODE BEGIN TIM1_MspDeInit 0 */ + + /* USER CODE END TIM1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM1_CLK_DISABLE(); + + /* TIM1 DMA DeInit */ + HAL_DMA_DeInit(htim_base->hdma[TIM_DMA_ID_CC4]); + /* USER CODE BEGIN TIM1_MspDeInit 1 */ + + /* USER CODE END TIM1_MspDeInit 1 */ + } + +} + +/** + * @brief UART MSP Initialization + * This function configures the hardware resources used in this example + * @param huart: UART handle pointer + * @retval None + */ void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; if(huart->Instance==USART1) { - /* USER CODE BEGIN USART1_MspInit 0 */ + /* USER CODE BEGIN USART1_MspInit 0 */ - /* USER CODE END USART1_MspInit 0 */ + /* USER CODE END USART1_MspInit 0 */ /** Initializes the peripherals clocks */ @@ -262,27 +357,27 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) GPIO_InitStruct.Alternate = GPIO_AF0_USART1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USER CODE BEGIN USART1_MspInit 1 */ + /* USER CODE BEGIN USART1_MspInit 1 */ - /* USER CODE END USART1_MspInit 1 */ + /* USER CODE END USART1_MspInit 1 */ } } /** -* @brief UART MSP De-Initialization -* This function freeze the hardware resources used in this example -* @param huart: UART handle pointer -* @retval None -*/ + * @brief UART MSP De-Initialization + * This function freeze the hardware resources used in this example + * @param huart: UART handle pointer + * @retval None + */ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) { if(huart->Instance==USART1) { - /* USER CODE BEGIN USART1_MspDeInit 0 */ + /* USER CODE BEGIN USART1_MspDeInit 0 */ - /* USER CODE END USART1_MspDeInit 0 */ + /* USER CODE END USART1_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART1_CLK_DISABLE(); @@ -292,9 +387,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7|GPIO_PIN_6); - /* USER CODE BEGIN USART1_MspDeInit 1 */ + /* USER CODE BEGIN USART1_MspDeInit 1 */ - /* USER CODE END USART1_MspDeInit 1 */ + /* USER CODE END USART1_MspDeInit 1 */ } } diff --git a/Core/Src/stm32g0xx_it.c b/Core/Src/stm32g0xx_it.c index 02935ca..31caf18 100644 --- a/Core/Src/stm32g0xx_it.c +++ b/Core/Src/stm32g0xx_it.c @@ -57,6 +57,7 @@ /* External variables --------------------------------------------------------*/ extern DMA_HandleTypeDef hdma_spi2_tx; extern SPI_HandleTypeDef hspi2; +extern DMA_HandleTypeDef hdma_tim1_ch4; /* USER CODE BEGIN EV */ /* USER CODE END EV */ @@ -155,6 +156,20 @@ void DMA1_Channel1_IRQHandler(void) /* USER CODE END DMA1_Channel1_IRQn 1 */ } +/** + * @brief This function handles DMA1 channel 4, channel 5 and DMAMUX1 interrupts. + */ +void DMA1_Ch4_5_DMAMUX1_OVR_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Ch4_5_DMAMUX1_OVR_IRQn 0 */ + + /* USER CODE END DMA1_Ch4_5_DMAMUX1_OVR_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_tim1_ch4); + /* USER CODE BEGIN DMA1_Ch4_5_DMAMUX1_OVR_IRQn 1 */ + + /* USER CODE END DMA1_Ch4_5_DMAMUX1_OVR_IRQn 1 */ +} + /** * @brief This function handles SPI2 global interrupt. */ diff --git a/G0Test1.ioc b/G0Test1.ioc index c5b5b7f..f0229b6 100644 --- a/G0Test1.ioc +++ b/G0Test1.ioc @@ -3,7 +3,8 @@ CAD.formats= CAD.pinconfig= CAD.provider= Dma.Request0=SPI2_TX -Dma.RequestsNb=1 +Dma.Request1=TIM1_CH4 +Dma.RequestsNb=2 Dma.SPI2_TX.0.Direction=DMA_MEMORY_TO_PERIPH Dma.SPI2_TX.0.EventEnable=DISABLE Dma.SPI2_TX.0.Instance=DMA1_Channel1 @@ -21,6 +22,23 @@ Dma.SPI2_TX.0.SyncEnable=DISABLE Dma.SPI2_TX.0.SyncPolarity=HAL_DMAMUX_SYNC_NO_EVENT Dma.SPI2_TX.0.SyncRequestNumber=1 Dma.SPI2_TX.0.SyncSignalID=NONE +Dma.TIM1_CH4.1.Direction=DMA_MEMORY_TO_PERIPH +Dma.TIM1_CH4.1.EventEnable=DISABLE +Dma.TIM1_CH4.1.Instance=DMA1_Channel4 +Dma.TIM1_CH4.1.MemDataAlignment=DMA_MDATAALIGN_HALFWORD +Dma.TIM1_CH4.1.MemInc=DMA_MINC_ENABLE +Dma.TIM1_CH4.1.Mode=DMA_NORMAL +Dma.TIM1_CH4.1.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD +Dma.TIM1_CH4.1.PeriphInc=DMA_PINC_DISABLE +Dma.TIM1_CH4.1.Polarity=HAL_DMAMUX_REQ_GEN_RISING +Dma.TIM1_CH4.1.Priority=DMA_PRIORITY_LOW +Dma.TIM1_CH4.1.RequestNumber=1 +Dma.TIM1_CH4.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,SignalID,Polarity,RequestNumber,SyncSignalID,SyncPolarity,SyncEnable,EventEnable,SyncRequestNumber +Dma.TIM1_CH4.1.SignalID=NONE +Dma.TIM1_CH4.1.SyncEnable=DISABLE +Dma.TIM1_CH4.1.SyncPolarity=HAL_DMAMUX_SYNC_NO_EVENT +Dma.TIM1_CH4.1.SyncRequestNumber=1 +Dma.TIM1_CH4.1.SyncSignalID=NONE File.Version=6 GPIO.groupedBy=Group By Peripherals KeepUserPlacement=false @@ -32,30 +50,34 @@ Mcu.IP2=RCC Mcu.IP3=SPI1 Mcu.IP4=SPI2 Mcu.IP5=SYS -Mcu.IP6=USART1 -Mcu.IPNb=7 +Mcu.IP6=TIM1 +Mcu.IP7=USART1 +Mcu.IPNb=8 Mcu.Name=STM32G030F6Px Mcu.Package=TSSOP20 Mcu.Pin0=PB7 Mcu.Pin1=PC14-OSC32_IN (PC14) -Mcu.Pin10=PA13 -Mcu.Pin11=PA14-BOOT0 -Mcu.Pin12=PB6 -Mcu.Pin13=VP_SYS_VS_Systick +Mcu.Pin10=PA11 [PA9] +Mcu.Pin11=PA13 +Mcu.Pin12=PA14-BOOT0 +Mcu.Pin13=PB6 +Mcu.Pin14=VP_SYS_VS_Systick +Mcu.Pin15=VP_TIM1_VS_ClockSourceINT Mcu.Pin2=PC15-OSC32_OUT (PC15) Mcu.Pin3=PA0 Mcu.Pin4=PA1 Mcu.Pin5=PA2 Mcu.Pin6=PA3 Mcu.Pin7=PA4 -Mcu.Pin8=PA6 +Mcu.Pin8=PA5 Mcu.Pin9=PA7 -Mcu.PinsNb=14 +Mcu.PinsNb=16 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32G030F6Px -MxCube.Version=6.13.0 -MxDb.Version=DB.6.0.130 +MxCube.Version=6.16.1 +MxDb.Version=DB.6.0.161 +NVIC.DMA1_Ch4_5_DMAMUX1_OVR_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA1_Channel1_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false @@ -66,23 +88,30 @@ NVIC.SVC_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:true NVIC.SysTick_IRQn=true\:3\:0\:false\:false\:true\:false\:true\:false PA0.Mode=Full_Duplex_Master PA0.Signal=SPI2_SCK -PA1.Mode=Full_Duplex_Master -PA1.Signal=SPI1_SCK +PA1.GPIOParameters=GPIO_Label +PA1.GPIO_Label=CS_Sensor +PA1.Locked=true +PA1.Signal=GPIO_Output +PA11\ [PA9].Signal=S_TIM1_CH4 PA13.Mode=Serial_Wire PA13.Signal=SYS_SWDIO PA14-BOOT0.Mode=Serial_Wire PA14-BOOT0.Signal=SYS_SWCLK PA14-BOOT0__PA15.StandardMode=true -PA2.Mode=Full_Duplex_Master -PA2.Signal=SPI1_MOSI +PA2.GPIOParameters=GPIO_Label +PA2.GPIO_Label=BRAKE +PA2.Locked=true +PA2.Signal=GPIO_Output PA3.Mode=Full_Duplex_Master PA3.Signal=SPI2_MISO PA4.Mode=Full_Duplex_Master PA4.Signal=SPI2_MOSI -PA6.Mode=Full_Duplex_Master -PA6.Signal=SPI1_MISO +PA5.Locked=true +PA5.Mode=Simplex_Bidirectional_Master +PA5.Signal=SPI1_SCK PA7.Locked=true -PA7.Signal=GPIO_Output +PA7.Mode=Simplex_Bidirectional_Master +PA7.Signal=SPI1_MOSI PB0__PB1__PB2__PA8.StandardMode=true PB3__PB4__PB5__PB6.StandardMode=true PB6.Mode=Asynchronous @@ -95,9 +124,17 @@ PC14-OSC32_IN\ (PC14).Signal=RCC_OSC32_IN PC14-OSC32_IN\ (PC14)__PB9.StandardMode=true PC15-OSC32_OUT\ (PC15).Mode=LSE-External-Oscillator PC15-OSC32_OUT\ (PC15).Signal=RCC_OSC32_OUT +PCC.Checker=false +PCC.Line=STM32G0x0 Value line +PCC.MCU=STM32G030F6Px +PCC.PartNumber=STM32G030F6Px +PCC.Series=STM32G0 +PCC.Temperature=25 +PCC.Vdd=3.0 PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false +ProjectManager.CompilerLinker=GCC ProjectManager.CompilerOptimize=6 ProjectManager.ComputerToolchain=false ProjectManager.CoupleFile=false @@ -107,6 +144,7 @@ ProjectManager.DeletePrevious=true ProjectManager.DeviceId=STM32G030F6Px ProjectManager.FirmwarePackage=STM32Cube FW_G0 V1.6.2 ProjectManager.FreePins=false +ProjectManager.FreePinsContext= ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true @@ -126,7 +164,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_SPI2_Init-SPI2-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_SPI1_Init-SPI1-false-HAL-true,6-MX_SPI2_Init-SPI2-false-HAL-true,7-MX_TIM1_Init-TIM1-false-HAL-true RCC.ADCFreq_Value=40000000 RCC.AHBCLKDivider=RCC_SYSCLK_DIV2 RCC.AHBFreq_Value=20000000 @@ -155,8 +193,13 @@ RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK RCC.USART1Freq_Value=20000000 RCC.VCOInputFreq_Value=16000000 RCC.VCOOutputFreq_Value=160000000 -SPI1.Direction=SPI_DIRECTION_2LINES -SPI1.IPParameters=VirtualType,Mode,Direction +SH.S_TIM1_CH4.0=TIM1_CH4,PWM Generation4 CH4 +SH.S_TIM1_CH4.ConfNb=1 +SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4 +SPI1.CLKPhase=SPI_PHASE_2EDGE +SPI1.CalculateBaudRate=5.0 MBits/s +SPI1.Direction=SPI_DIRECTION_1LINE +SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,CLKPhase,BaudRatePrescaler SPI1.Mode=SPI_MODE_MASTER SPI1.VirtualType=VM_MASTER SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_8 @@ -165,9 +208,16 @@ SPI2.Direction=SPI_DIRECTION_2LINES SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler SPI2.Mode=SPI_MODE_MASTER SPI2.VirtualType=VM_MASTER +TIM1.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_DISABLE +TIM1.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 +TIM1.IPParameters=Channel-PWM Generation4 CH4,AutoReloadPreload,Prescaler,Period +TIM1.Period=1000-1 +TIM1.Prescaler=20-1 USART1.BaudRate=9600 USART1.IPParameters=VirtualMode-Asynchronous,BaudRate USART1.VirtualMode-Asynchronous=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick VP_SYS_VS_Systick.Signal=SYS_VS_Systick +VP_TIM1_VS_ClockSourceINT.Mode=Internal +VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT board=custom diff --git a/STM32G030XX_FLASH.ld b/STM32G030XX_FLASH.ld new file mode 100644 index 0000000..83c6ae0 --- /dev/null +++ b/STM32G030XX_FLASH.ld @@ -0,0 +1,249 @@ +/* +****************************************************************************** +** + +** File : LinkerScript.ld +** +** Author : STM32CubeMX +** +** Abstract : Linker script for STM32G030F6Px series +** 32Kbytes FLASH and 8Kbytes RAM +** +** Set heap size, stack size and stack location according +** to application requirements. +** +** Set memory bank area and size if external memory is used. +** +** Target : STMicroelectronics STM32 +** +** Distribution: The file is distributed “as is,” without any warranty +** of any kind. +** +***************************************************************************** +** @attention +** +**

© COPYRIGHT(c) 2025 STMicroelectronics

+** +** Redistribution and use in source and binary forms, with or without modification, +** are permitted provided that the following conditions are met: +** 1. Redistributions of source code must retain the above copyright notice, +** this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright notice, +** this list of conditions and the following disclaimer in the documentation +** and/or other materials provided with the distribution. +** 3. Neither the name of STMicroelectronics nor the names of its contributors +** may be used to endorse or promote products derived from this software +** without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +***************************************************************************** +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +/* Specify the memory areas */ +MEMORY +{ +RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K +FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K +} + +/* Highest address of the user mode stack */ +_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of RAM */ +/* Generate a link error if heap and stack don't fit into RAM */ +_Min_Heap_Size = 0x200; /* required amount of heap */ +_Min_Stack_Size = 0x400; /* required amount of stack */ + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(4); + _etext = .; /* define a global symbols at end of code */ + } >FLASH + + /* Constant data goes into FLASH */ + .rodata : + { + . = ALIGN(4); + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + . = ALIGN(4); + } >FLASH + + .ARM.extab (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); + *(.ARM.extab* .gnu.linkonce.armextab.*) + . = ALIGN(4); + } >FLASH + + .ARM (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + . = ALIGN(4); + } >FLASH + + .preinit_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + . = ALIGN(4); + } >FLASH + + .init_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + . = ALIGN(4); + } >FLASH + + .fini_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + . = ALIGN(4); + } >FLASH + + /* used by the startup to initialize data */ + _sidata = LOADADDR(.data); + + /* Initialized data sections goes into RAM, load LMA copy after code */ + .data : + { + . = ALIGN(4); + _sdata = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + *(.RamFunc) /* .RamFunc sections */ + *(.RamFunc*) /* .RamFunc* sections */ + + . = ALIGN(4); + } >RAM AT> FLASH + + /* Initialized TLS data section */ + .tdata : ALIGN(4) + { + *(.tdata .tdata.* .gnu.linkonce.td.*) + . = ALIGN(4); + _edata = .; /* define a global symbol at data end */ + PROVIDE(__data_end = .); + PROVIDE(__tdata_end = .); + } >RAM AT> FLASH + + PROVIDE( __tdata_start = ADDR(.tdata) ); + PROVIDE( __tdata_size = __tdata_end - __tdata_start ); + + PROVIDE( __data_start = ADDR(.data) ); + PROVIDE( __data_size = __data_end - __data_start ); + + PROVIDE( __tdata_source = LOADADDR(.tdata) ); + PROVIDE( __tdata_source_end = LOADADDR(.tdata) + SIZEOF(.tdata) ); + PROVIDE( __tdata_source_size = __tdata_source_end - __tdata_source ); + + PROVIDE( __data_source = LOADADDR(.data) ); + PROVIDE( __data_source_end = __tdata_source_end ); + PROVIDE( __data_source_size = __data_source_end - __data_source ); + /* Uninitialized data section */ + .tbss (NOLOAD) : ALIGN(4) + { + /* This is used by the startup in order to initialize the .bss secion */ + _sbss = .; /* define a global symbol at bss start */ + __bss_start__ = _sbss; + *(.tbss .tbss.*) + . = ALIGN(4); + PROVIDE( __tbss_end = . ); + } >RAM + + PROVIDE( __tbss_start = ADDR(.tbss) ); + PROVIDE( __tbss_size = __tbss_end - __tbss_start ); + PROVIDE( __tbss_offset = ADDR(.tbss) - ADDR(.tdata) ); + + PROVIDE( __tls_base = __tdata_start ); + PROVIDE( __tls_end = __tbss_end ); + PROVIDE( __tls_size = __tls_end - __tls_base ); + PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) ); + PROVIDE( __tls_size_align = (__tls_size + __tls_align - 1) & ~(__tls_align - 1) ); + PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) ); + PROVIDE( __arm64_tls_tcb_offset = MAX(16, __tls_align) ); + + .bss (NOLOAD) : ALIGN(4) + { + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end */ + __bss_end__ = _ebss; + PROVIDE( __bss_end = .); + } >RAM + PROVIDE( __non_tls_bss_start = ADDR(.bss) ); + + PROVIDE( __bss_start = __tbss_start ); + PROVIDE( __bss_size = __bss_end - __bss_start ); + + /* User_heap_stack section, used to check that there is enough RAM left */ + ._user_heap_stack (NOLOAD) : + { + . = ALIGN(8); + PROVIDE ( end = . ); + PROVIDE ( _end = . ); + . = . + _Min_Heap_Size; + . = . + _Min_Stack_Size; + . = ALIGN(8); + } >RAM + + + + /* Remove information from the standard libraries */ + /DISCARD/ : + { + libc.a:* ( * ) + libm.a:* ( * ) + libgcc.a:* ( * ) + } + +} diff --git a/cmake/starm-clang.cmake b/cmake/starm-clang.cmake new file mode 100644 index 0000000..fd13961 --- /dev/null +++ b/cmake/starm-clang.cmake @@ -0,0 +1,65 @@ +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_PROCESSOR arm) + +set(CMAKE_C_COMPILER_ID Clang) +set(CMAKE_CXX_COMPILER_ID Clang) + +# Some default llvm settings +set(TOOLCHAIN_PREFIX starm-) + +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}clang) +set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}clang++) +set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}clang) +set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy) +set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size) + +set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf") +set(CMAKE_EXECUTABLE_SUFFIX_C ".elf") +set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf") + +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +# STARM_TOOLCHAIN_CONFIG allows you to choose the toolchain configuration. +# Possible values are: +# "STARM_HYBRID" : Hybrid configuration using starm-clang Assemler and Compiler and GNU Linker +# "STARM_NEWLIB" : starm-clang toolchain with NEWLIB C library +# "STARM_PICOLIBC" : starm-clang toolchain with PICOLIBC C library +set(STARM_TOOLCHAIN_CONFIG "STARM_PICOLIBC") + +if(STARM_TOOLCHAIN_CONFIG STREQUAL "STARM_HYBRID") + set(TOOLCHAIN_MULTILIBS "--multi-lib-config=\"$ENV{CLANG_GCC_CMSIS_COMPILER}/multilib.gnu_tools_for_stm32.yaml\" --gcc-toolchain=\"$ENV{GCC_TOOLCHAIN_ROOT}/..\"") +elseif (STARM_TOOLCHAIN_CONFIG STREQUAL "STARM_NEWLIB") + set(TOOLCHAIN_MULTILIBS "--config=newlib.cfg") +endif() + +# MCU specific flags +set(TARGET_FLAGS "-mcpu=cortex-m0plus ${TOOLCHAIN_MULTILIBS}") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_FLAGS}") +set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -MP") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fdata-sections -ffunction-sections") + +set(CMAKE_C_FLAGS_DEBUG "-Og -g3") +set(CMAKE_C_FLAGS_RELEASE "-Oz -g0") +set(CMAKE_CXX_FLAGS_DEBUG "-Og -g3") +set(CMAKE_CXX_FLAGS_RELEASE "-Oz -g0") + +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fno-rtti -fno-exceptions -fno-threadsafe-statics") + +set(CMAKE_EXE_LINKER_FLAGS "${TARGET_FLAGS}") + +if (STARM_TOOLCHAIN_CONFIG STREQUAL "STARM_HYBRID") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --gcc-specs=nano.specs") + set(TOOLCHAIN_LINK_LIBRARIES "m") +elseif(STARM_TOOLCHAIN_CONFIG STREQUAL "STARM_NEWLIB") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lcrt0-nosys") +elseif(STARM_TOOLCHAIN_CONFIG STREQUAL "STARM_PICOLIBC") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lcrt0-hosted -z norelro") + +endif() + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T \"${CMAKE_SOURCE_DIR}/STM32G030XX_FLASH.ld\"") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${CMAKE_PROJECT_NAME}.map -Wl,--gc-sections") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z noexecstack") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--print-memory-usage ") diff --git a/cmake/stm32cubemx/CMakeLists.txt b/cmake/stm32cubemx/CMakeLists.txt index e2496d8..04b8f5f 100644 --- a/cmake/stm32cubemx/CMakeLists.txt +++ b/cmake/stm32cubemx/CMakeLists.txt @@ -1,62 +1,95 @@ cmake_minimum_required(VERSION 3.22) - -project(stm32cubemx) -add_library(stm32cubemx INTERFACE) - # Enable CMake support for ASM and C languages enable_language(C ASM) - -target_compile_definitions(stm32cubemx INTERFACE - USE_HAL_DRIVER - STM32G030xx +# STM32CubeMX generated symbols (macros) +set(MX_Defines_Syms + USE_HAL_DRIVER + STM32G030xx $<$:DEBUG> ) -target_include_directories(stm32cubemx INTERFACE - ../../Core/Inc - ../../Drivers/STM32G0xx_HAL_Driver/Inc - ../../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy - ../../Drivers/CMSIS/Device/ST/STM32G0xx/Include - ../../Drivers/CMSIS/Include +# STM32CubeMX generated include paths +set(MX_Include_Dirs + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Inc + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Inc + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS/Device/ST/STM32G0xx/Include + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS/Include ) -target_sources(stm32cubemx INTERFACE - ../../Core/Src/main.cpp - ../../Core/Src/stm32g0xx_it.c - ../../Core/Src/stm32g0xx_hal_msp.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c - ../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c - ../../Core/Src/system_stm32g0xx.c - ../../Core/Src/sysmem.c - ../../Core/Src/TLE5012.cpp - ../../Core/Src/syscalls.c - ../../startup_stm32g030xx.s +# STM32CubeMX generated application sources +set(MX_Application_Src + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/main.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/TLE5012.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32g0xx_it.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32g0xx_hal_msp.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/sysmem.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/syscalls.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../startup_stm32g030xx.s ) -target_link_directories(stm32cubemx INTERFACE +# STM32 HAL/LL Drivers +set(STM32_Drivers_Src + ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/system_stm32g0xx.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_spi_ex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_rcc_ex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_ll_rcc.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_flash_ex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_gpio.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_dma_ex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_pwr_ex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_cortex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_exti.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_tim_ex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_uart_ex.c ) -target_link_libraries(stm32cubemx INTERFACE +# Drivers Midllewares + + + +# Link directories setup +set(MX_LINK_DIRS + ) +# Project static libraries +set(MX_LINK_LIBS + STM32_Drivers + ${TOOLCHAIN_LINK_LIBRARIES} + +) +# Interface library for includes and symbols +add_library(stm32cubemx INTERFACE) +target_include_directories(stm32cubemx INTERFACE ${MX_Include_Dirs}) +target_compile_definitions(stm32cubemx INTERFACE ${MX_Defines_Syms}) + +# Create STM32_Drivers static library +add_library(STM32_Drivers OBJECT) +target_sources(STM32_Drivers PRIVATE ${STM32_Drivers_Src}) +target_link_libraries(STM32_Drivers PUBLIC stm32cubemx) + + +# Add STM32CubeMX generated application sources to the project +target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${MX_Application_Src}) + +# Link directories setup +target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${MX_LINK_DIRS}) + +# Add libraries to the project +target_link_libraries(${CMAKE_PROJECT_NAME} ${MX_LINK_LIBS}) + +# Add the map file to the list of files to be removed with 'clean' target +set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map) # Validate that STM32CubeMX code is compatible with C standard -if(CMAKE_C_STANDARD LESS 11) +if((CMAKE_C_STANDARD EQUAL 90) OR (CMAKE_C_STANDARD EQUAL 99)) message(ERROR "Generated code requires C11 or higher") endif() - -