From 6e407c8c88389b14a3a57207b21a6195ba9ccf04 Mon Sep 17 00:00:00 2001 From: lrc <571244399@qq.com> Date: Tue, 22 Jul 2025 22:23:55 +0800 Subject: [PATCH] init 3.0 --- .cproject | 171 + .gitignore | 1 + .idea/.gitignore | 8 + .idea/ESHL.iml | 2 + .idea/editor.xml | 348 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/runConfigurations/ESHL_elf.xml | 7 + .idea/vcs.xml | 6 + .mxproject | 39 + .project | 32 + CMakeLists.txt | 75 + CMakeLists_template.txt | 72 + Core/Inc/adc.h | 52 + Core/Inc/comp.h | 52 + Core/Inc/dma.h | 52 + Core/Inc/gpio.h | 49 + Core/Inc/main.h | 75 + Core/Inc/rtc.h | 52 + Core/Inc/stm32f0xx_hal_conf.h | 322 + Core/Inc/stm32f0xx_it.h | 67 + Core/Inc/tim.h | 63 + Core/Inc/usart.h | 52 + Core/Src/adc.c | 173 + Core/Src/comp.c | 120 + Core/Src/dma.c | 61 + Core/Src/gpio.c | 65 + Core/Src/main.c | 409 + Core/Src/rtc.c | 94 + Core/Src/stm32f0xx_hal_msp.c | 82 + Core/Src/stm32f0xx_it.c | 223 + Core/Src/syscalls.c | 176 + Core/Src/sysmem.c | 79 + Core/Src/system_stm32f0xx.c | 249 + Core/Src/tim.c | 438 + Core/Src/usart.c | 160 + Core/Startup/startup_stm32f051k8ux.s | 285 + Driver/ESHL_driver.c | 1115 +++ Driver/ESHL_driver.h | 170 + Driver/ESHL_protocol.c | 296 + Driver/ESHL_protocol.h | 97 + Driver/Internal_Flash.c | 188 + Driver/Internal_Flash.h | 26 + Driver/communication_management.c | 244 + Driver/communication_management.h | 25 + Driver/crc.c | 178 + Driver/crc.h | 23 + Driver/ws2812b.c | 130 + Driver/ws2812b.h | 42 + .../Device/ST/STM32F0xx/Include/stm32f051x8.h | 6776 +++++++++++++++ .../Device/ST/STM32F0xx/Include/stm32f0xx.h | 269 + .../ST/STM32F0xx/Include/system_stm32f0xx.h | 103 + Drivers/CMSIS/Device/ST/STM32F0xx/LICENSE.txt | 6 + Drivers/CMSIS/Include/cmsis_armcc.h | 865 ++ Drivers/CMSIS/Include/cmsis_armclang.h | 1869 ++++ Drivers/CMSIS/Include/cmsis_compiler.h | 266 + Drivers/CMSIS/Include/cmsis_gcc.h | 2085 +++++ Drivers/CMSIS/Include/cmsis_iccarm.h | 935 ++ Drivers/CMSIS/Include/cmsis_version.h | 39 + Drivers/CMSIS/Include/core_armv8mbl.h | 1918 +++++ Drivers/CMSIS/Include/core_armv8mml.h | 2927 +++++++ Drivers/CMSIS/Include/core_cm0.h | 949 ++ Drivers/CMSIS/Include/core_cm0plus.h | 1083 +++ Drivers/CMSIS/Include/core_cm1.h | 976 +++ Drivers/CMSIS/Include/core_cm23.h | 1993 +++++ Drivers/CMSIS/Include/core_cm3.h | 1941 +++++ Drivers/CMSIS/Include/core_cm33.h | 3002 +++++++ Drivers/CMSIS/Include/core_cm4.h | 2129 +++++ Drivers/CMSIS/Include/core_cm7.h | 2671 ++++++ Drivers/CMSIS/Include/core_sc000.h | 1022 +++ Drivers/CMSIS/Include/core_sc300.h | 1915 +++++ Drivers/CMSIS/Include/mpu_armv7.h | 270 + Drivers/CMSIS/Include/mpu_armv8.h | 333 + Drivers/CMSIS/Include/tz_context.h | 70 + Drivers/CMSIS/LICENSE.txt | 201 + .../Inc/Legacy/stm32_hal_legacy.h | 4360 ++++++++++ .../STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal.h | 584 ++ .../Inc/stm32f0xx_hal_adc.h | 1017 +++ .../Inc/stm32f0xx_hal_adc_ex.h | 296 + .../Inc/stm32f0xx_hal_comp.h | 699 ++ .../Inc/stm32f0xx_hal_cortex.h | 131 + .../Inc/stm32f0xx_hal_def.h | 179 + .../Inc/stm32f0xx_hal_dma.h | 561 ++ .../Inc/stm32f0xx_hal_dma_ex.h | 809 ++ .../Inc/stm32f0xx_hal_exti.h | 373 + .../Inc/stm32f0xx_hal_flash.h | 350 + .../Inc/stm32f0xx_hal_flash_ex.h | 445 + .../Inc/stm32f0xx_hal_gpio.h | 320 + .../Inc/stm32f0xx_hal_gpio_ex.h | 797 ++ .../Inc/stm32f0xx_hal_i2c.h | 838 ++ .../Inc/stm32f0xx_hal_i2c_ex.h | 190 + .../Inc/stm32f0xx_hal_pwr.h | 185 + .../Inc/stm32f0xx_hal_pwr_ex.h | 455 + .../Inc/stm32f0xx_hal_rcc.h | 1683 ++++ .../Inc/stm32f0xx_hal_rcc_ex.h | 2082 +++++ .../Inc/stm32f0xx_hal_rtc.h | 920 ++ .../Inc/stm32f0xx_hal_rtc_ex.h | 1028 +++ .../Inc/stm32f0xx_hal_tim.h | 2157 +++++ .../Inc/stm32f0xx_hal_tim_ex.h | 267 + .../Inc/stm32f0xx_hal_uart.h | 1672 ++++ .../Inc/stm32f0xx_hal_uart_ex.h | 593 ++ .../Inc/stm32f0xx_ll_adc.h | 3408 ++++++++ .../Inc/stm32f0xx_ll_bus.h | 842 ++ .../Inc/stm32f0xx_ll_comp.h | 829 ++ .../Inc/stm32f0xx_ll_cortex.h | 318 + .../Inc/stm32f0xx_ll_crs.h | 780 ++ .../Inc/stm32f0xx_ll_dma.h | 2234 +++++ .../Inc/stm32f0xx_ll_exti.h | 1014 +++ .../Inc/stm32f0xx_ll_gpio.h | 938 ++ .../Inc/stm32f0xx_ll_pwr.h | 549 ++ .../Inc/stm32f0xx_ll_rcc.h | 2258 +++++ .../Inc/stm32f0xx_ll_rtc.h | 3109 +++++++ .../Inc/stm32f0xx_ll_system.h | 1851 ++++ .../Inc/stm32f0xx_ll_tim.h | 4016 +++++++++ .../Inc/stm32f0xx_ll_usart.h | 3849 +++++++++ .../Inc/stm32f0xx_ll_utils.h | 272 + Drivers/STM32F0xx_HAL_Driver/LICENSE.txt | 6 + .../STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c | 515 ++ .../Src/stm32f0xx_hal_adc.c | 2490 ++++++ .../Src/stm32f0xx_hal_adc_ex.c | 189 + .../Src/stm32f0xx_hal_comp.c | 983 +++ .../Src/stm32f0xx_hal_cortex.c | 342 + .../Src/stm32f0xx_hal_dma.c | 899 ++ .../Src/stm32f0xx_hal_exti.c | 547 ++ .../Src/stm32f0xx_hal_flash.c | 691 ++ .../Src/stm32f0xx_hal_flash_ex.c | 981 +++ .../Src/stm32f0xx_hal_gpio.c | 539 ++ .../Src/stm32f0xx_hal_i2c.c | 7548 ++++++++++++++++ .../Src/stm32f0xx_hal_i2c_ex.c | 362 + .../Src/stm32f0xx_hal_pwr.c | 450 + .../Src/stm32f0xx_hal_pwr_ex.c | 271 + .../Src/stm32f0xx_hal_rcc.c | 1365 +++ .../Src/stm32f0xx_hal_rcc_ex.c | 961 +++ .../Src/stm32f0xx_hal_rtc.c | 1779 ++++ .../Src/stm32f0xx_hal_rtc_ex.c | 1740 ++++ .../Src/stm32f0xx_hal_tim.c | 7641 +++++++++++++++++ .../Src/stm32f0xx_hal_tim_ex.c | 2372 +++++ .../Src/stm32f0xx_hal_uart.c | 4180 +++++++++ .../Src/stm32f0xx_hal_uart_ex.c | 839 ++ ESHL.ioc | 245 + STM32F051K8UX_FLASH.ld | 187 + daplink.cfg | 11 + 142 files changed, 130764 insertions(+) create mode 100644 .cproject create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/ESHL.iml create mode 100644 .idea/editor.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations/ESHL_elf.xml create mode 100644 .idea/vcs.xml create mode 100644 .mxproject create mode 100644 .project create mode 100644 CMakeLists.txt create mode 100644 CMakeLists_template.txt create mode 100644 Core/Inc/adc.h create mode 100644 Core/Inc/comp.h create mode 100644 Core/Inc/dma.h create mode 100644 Core/Inc/gpio.h create mode 100644 Core/Inc/main.h create mode 100644 Core/Inc/rtc.h create mode 100644 Core/Inc/stm32f0xx_hal_conf.h create mode 100644 Core/Inc/stm32f0xx_it.h create mode 100644 Core/Inc/tim.h create mode 100644 Core/Inc/usart.h create mode 100644 Core/Src/adc.c create mode 100644 Core/Src/comp.c create mode 100644 Core/Src/dma.c create mode 100644 Core/Src/gpio.c create mode 100644 Core/Src/main.c create mode 100644 Core/Src/rtc.c create mode 100644 Core/Src/stm32f0xx_hal_msp.c create mode 100644 Core/Src/stm32f0xx_it.c create mode 100644 Core/Src/syscalls.c create mode 100644 Core/Src/sysmem.c create mode 100644 Core/Src/system_stm32f0xx.c create mode 100644 Core/Src/tim.c create mode 100644 Core/Src/usart.c create mode 100644 Core/Startup/startup_stm32f051k8ux.s create mode 100644 Driver/ESHL_driver.c create mode 100644 Driver/ESHL_driver.h create mode 100644 Driver/ESHL_protocol.c create mode 100644 Driver/ESHL_protocol.h create mode 100644 Driver/Internal_Flash.c create mode 100644 Driver/Internal_Flash.h create mode 100644 Driver/communication_management.c create mode 100644 Driver/communication_management.h create mode 100644 Driver/crc.c create mode 100644 Driver/crc.h create mode 100644 Driver/ws2812b.c create mode 100644 Driver/ws2812b.h create mode 100644 Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f051x8.h create mode 100644 Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f0xx.h create mode 100644 Drivers/CMSIS/Device/ST/STM32F0xx/Include/system_stm32f0xx.h create mode 100644 Drivers/CMSIS/Device/ST/STM32F0xx/LICENSE.txt create mode 100644 Drivers/CMSIS/Include/cmsis_armcc.h create mode 100644 Drivers/CMSIS/Include/cmsis_armclang.h create mode 100644 Drivers/CMSIS/Include/cmsis_compiler.h create mode 100644 Drivers/CMSIS/Include/cmsis_gcc.h create mode 100644 Drivers/CMSIS/Include/cmsis_iccarm.h create mode 100644 Drivers/CMSIS/Include/cmsis_version.h create mode 100644 Drivers/CMSIS/Include/core_armv8mbl.h create mode 100644 Drivers/CMSIS/Include/core_armv8mml.h create mode 100644 Drivers/CMSIS/Include/core_cm0.h create mode 100644 Drivers/CMSIS/Include/core_cm0plus.h create mode 100644 Drivers/CMSIS/Include/core_cm1.h create mode 100644 Drivers/CMSIS/Include/core_cm23.h create mode 100644 Drivers/CMSIS/Include/core_cm3.h create mode 100644 Drivers/CMSIS/Include/core_cm33.h create mode 100644 Drivers/CMSIS/Include/core_cm4.h create mode 100644 Drivers/CMSIS/Include/core_cm7.h create mode 100644 Drivers/CMSIS/Include/core_sc000.h create mode 100644 Drivers/CMSIS/Include/core_sc300.h create mode 100644 Drivers/CMSIS/Include/mpu_armv7.h create mode 100644 Drivers/CMSIS/Include/mpu_armv8.h create mode 100644 Drivers/CMSIS/Include/tz_context.h create mode 100644 Drivers/CMSIS/LICENSE.txt create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_adc.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_adc_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_comp.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_cortex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_def.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_dma.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_dma_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_exti.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_flash.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_flash_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_gpio.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_gpio_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_i2c.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_i2c_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_pwr.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_pwr_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_rcc.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_rcc_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_rtc.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_rtc_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_tim.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_tim_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_uart.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_uart_ex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_adc.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_bus.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_comp.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_cortex.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_crs.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_dma.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_exti.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_gpio.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_pwr.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_rcc.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_rtc.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_system.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_tim.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_usart.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_ll_utils.h create mode 100644 Drivers/STM32F0xx_HAL_Driver/LICENSE.txt create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_adc.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_adc_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_comp.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_cortex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_dma.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_exti.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_flash_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_gpio.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2c.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_i2c_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_pwr_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rtc.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rtc_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_tim_ex.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_uart.c create mode 100644 Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_uart_ex.c create mode 100644 ESHL.ioc create mode 100644 STM32F051K8UX_FLASH.ld create mode 100644 daplink.cfg diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..407a947 --- /dev/null +++ b/.cproject @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb575c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +!*.bin \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/ESHL.iml b/.idea/ESHL.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/ESHL.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..5b0c0e7 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,348 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..718ba6f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/ESHL_elf.xml b/.idea/runConfigurations/ESHL_elf.xml new file mode 100644 index 0000000..88a71a2 --- /dev/null +++ b/.idea/runConfigurations/ESHL_elf.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.mxproject b/.mxproject new file mode 100644 index 0000000..9b0aa81 --- /dev/null +++ b/.mxproject @@ -0,0 +1,39 @@ +[PreviousLibFiles] +LibFiles=Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_adc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_adc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_adc_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rcc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_rcc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rcc_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_bus.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_crs.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_system.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_utils.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_def.h;Drivers\STM32F0xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_i2c.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_i2c_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_gpio.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_gpio_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_gpio.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_dma_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_dma.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_dma.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_cortex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_cortex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_pwr.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_pwr_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_pwr.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_flash.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_flash_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_exti.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_exti.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_comp.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_comp.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rtc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_rtc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rtc_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_tim.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_tim.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_tim_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_uart.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_usart.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_uart_ex.h;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_adc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_adc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rcc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rcc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_i2c.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_i2c_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_gpio.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_dma.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_cortex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_pwr.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_pwr_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_flash.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_flash_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_exti.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_comp.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rtc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rtc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_tim.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_tim_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_uart.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_uart_ex.c;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_adc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_adc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_adc_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rcc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_rcc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rcc_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_bus.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_crs.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_system.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_utils.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_def.h;Drivers\STM32F0xx_HAL_Driver\Inc\Legacy\stm32_hal_legacy.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_i2c.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_i2c_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_gpio.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_gpio_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_gpio.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_dma_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_dma.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_dma.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_cortex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_cortex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_pwr.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_pwr_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_pwr.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_flash.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_flash_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_exti.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_exti.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_comp.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_comp.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rtc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_rtc.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_rtc_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_tim.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_tim.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_tim_ex.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_uart.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_ll_usart.h;Drivers\STM32F0xx_HAL_Driver\Inc\stm32f0xx_hal_uart_ex.h;Drivers\CMSIS\Device\ST\STM32F0xx\Include\stm32f051x8.h;Drivers\CMSIS\Device\ST\STM32F0xx\Include\stm32f0xx.h;Drivers\CMSIS\Device\ST\STM32F0xx\Include\system_stm32f0xx.h;Drivers\CMSIS\Device\ST\STM32F0xx\Include\system_stm32f0xx.h;Drivers\CMSIS\Device\ST\STM32F0xx\Source\Templates\system_stm32f0xx.c;Drivers\CMSIS\Include\cmsis_armcc.h;Drivers\CMSIS\Include\cmsis_armclang.h;Drivers\CMSIS\Include\cmsis_compiler.h;Drivers\CMSIS\Include\cmsis_gcc.h;Drivers\CMSIS\Include\cmsis_iccarm.h;Drivers\CMSIS\Include\cmsis_version.h;Drivers\CMSIS\Include\core_armv8mbl.h;Drivers\CMSIS\Include\core_armv8mml.h;Drivers\CMSIS\Include\core_cm0.h;Drivers\CMSIS\Include\core_cm0plus.h;Drivers\CMSIS\Include\core_cm1.h;Drivers\CMSIS\Include\core_cm23.h;Drivers\CMSIS\Include\core_cm3.h;Drivers\CMSIS\Include\core_cm33.h;Drivers\CMSIS\Include\core_cm4.h;Drivers\CMSIS\Include\core_cm7.h;Drivers\CMSIS\Include\core_sc000.h;Drivers\CMSIS\Include\core_sc300.h;Drivers\CMSIS\Include\mpu_armv7.h;Drivers\CMSIS\Include\mpu_armv8.h;Drivers\CMSIS\Include\tz_context.h; + +[PreviousUsedCubeIDEFiles] +SourceFiles=Core\Src\main.c;Core\Src\gpio.c;Core\Src\adc.c;Core\Src\comp.c;Core\Src\dma.c;Core\Src\rtc.c;Core\Src\tim.c;Core\Src\usart.c;Core\Src\stm32f0xx_it.c;Core\Src\stm32f0xx_hal_msp.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_adc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_adc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rcc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rcc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_i2c.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_i2c_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_gpio.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_dma.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_cortex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_pwr.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_pwr_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_flash.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_flash_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_exti.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_comp.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rtc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rtc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_tim.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_tim_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_uart.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_uart_ex.c;Drivers\CMSIS\Device\ST\STM32F0xx\Source\Templates\system_stm32f0xx.c;Core\Src\system_stm32f0xx.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_adc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_adc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rcc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rcc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_i2c.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_i2c_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_gpio.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_dma.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_cortex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_pwr.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_pwr_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_flash.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_flash_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_exti.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_comp.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rtc.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_rtc_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_tim.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_tim_ex.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_uart.c;Drivers\STM32F0xx_HAL_Driver\Src\stm32f0xx_hal_uart_ex.c;Drivers\CMSIS\Device\ST\STM32F0xx\Source\Templates\system_stm32f0xx.c;Core\Src\system_stm32f0xx.c;;; +HeaderPath=Drivers\STM32F0xx_HAL_Driver\Inc;Drivers\STM32F0xx_HAL_Driver\Inc\Legacy;Drivers\CMSIS\Device\ST\STM32F0xx\Include;Drivers\CMSIS\Include;Core\Inc; +CDefines=USE_HAL_DRIVER;STM32F051x8;USE_HAL_DRIVER;USE_HAL_DRIVER; + +[PreviousGenFiles] +AdvancedFolderStructure=true +HeaderFileListSize=10 +HeaderFiles#0=..\Core\Inc\gpio.h +HeaderFiles#1=..\Core\Inc\adc.h +HeaderFiles#2=..\Core\Inc\comp.h +HeaderFiles#3=..\Core\Inc\dma.h +HeaderFiles#4=..\Core\Inc\rtc.h +HeaderFiles#5=..\Core\Inc\tim.h +HeaderFiles#6=..\Core\Inc\usart.h +HeaderFiles#7=..\Core\Inc\stm32f0xx_it.h +HeaderFiles#8=..\Core\Inc\stm32f0xx_hal_conf.h +HeaderFiles#9=..\Core\Inc\main.h +HeaderFolderListSize=1 +HeaderPath#0=..\Core\Inc +HeaderFiles=; +SourceFileListSize=10 +SourceFiles#0=..\Core\Src\gpio.c +SourceFiles#1=..\Core\Src\adc.c +SourceFiles#2=..\Core\Src\comp.c +SourceFiles#3=..\Core\Src\dma.c +SourceFiles#4=..\Core\Src\rtc.c +SourceFiles#5=..\Core\Src\tim.c +SourceFiles#6=..\Core\Src\usart.c +SourceFiles#7=..\Core\Src\stm32f0xx_it.c +SourceFiles#8=..\Core\Src\stm32f0xx_hal_msp.c +SourceFiles#9=..\Core\Src\main.c +SourceFolderListSize=1 +SourcePath#0=..\Core\Src +SourceFiles=; + diff --git a/.project b/.project new file mode 100644 index 0000000..92aae04 --- /dev/null +++ b/.project @@ -0,0 +1,32 @@ + + + ESHL + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + com.st.stm32cube.ide.mcu.MCUProjectNature + org.eclipse.cdt.core.cnature + com.st.stm32cube.ide.mcu.MCUCubeIdeServicesRevAev2ProjectNature + com.st.stm32cube.ide.mcu.MCUCubeProjectNature + com.st.stm32cube.ide.mcu.MCUAdvancedStructureProjectNature + com.st.stm32cube.ide.mcu.MCUSingleCpuProjectNature + com.st.stm32cube.ide.mcu.MCURootProjectNature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..79928e0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,75 @@ +#此文件从模板自动生成! 请勿更改! +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_VERSION 1) +cmake_minimum_required(VERSION 3.28.1) + +# specify cross-compilers and tools +set(CMAKE_C_COMPILER arm-none-eabi-gcc) +set(CMAKE_CXX_COMPILER arm-none-eabi-g++) +set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) +set(CMAKE_AR arm-none-eabi-ar) +set(CMAKE_OBJCOPY arm-none-eabi-objcopy) +set(CMAKE_OBJDUMP arm-none-eabi-objdump) +set(SIZE arm-none-eabi-size) +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +# project settings +project(ESHL C CXX ASM) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_C_STANDARD 11) + +#Uncomment for hardware floating point +#add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) +#add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) +#add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) + +#Uncomment for software floating point +#add_compile_options(-mfloat-abi=soft) + +add_compile_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork) +add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) + +# uncomment to mitigate c++17 absolute addresses warnings +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register") + +# Enable assembler files preprocessing +add_compile_options($<$:-x$assembler-with-cpp>) + +if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") + message(STATUS "Maximum optimization for speed") + add_compile_options(-Ofast) +elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") + message(STATUS "Maximum optimization for speed, debug info included") + add_compile_options(-Ofast -g) +elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") + message(STATUS "Maximum optimization for size") + add_compile_options(-Os) +else () + message(STATUS "Minimal optimization, debug info included") + add_compile_options(-Og -g) +endif () + +include_directories(Core/Inc Drivers/STM32F0xx_HAL_Driver/Inc Drivers/STM32F0xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F0xx/Include Drivers/CMSIS/Include + Driver/ +) + +add_definitions(-DDEBUG -DUSE_HAL_DRIVER -DSTM32F051x8) + +file(GLOB_RECURSE SOURCES "Core/*.*" "Drivers/*.*" "Driver/*.*") + +set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F051K8UX_FLASH.ld) + +add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) +add_link_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork) +add_link_options(-T ${LINKER_SCRIPT}) + +add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) + +set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) +set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) + +add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -Oihex $ ${HEX_FILE} + COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE} + COMMENT "Building ${HEX_FILE} +Building ${BIN_FILE}") diff --git a/CMakeLists_template.txt b/CMakeLists_template.txt new file mode 100644 index 0000000..3b7fc44 --- /dev/null +++ b/CMakeLists_template.txt @@ -0,0 +1,72 @@ +#${templateWarning} +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_VERSION 1) +${cmakeRequiredVersion} +# specify cross-compilers and tools +set(CMAKE_C_COMPILER arm-none-eabi-gcc) +set(CMAKE_CXX_COMPILER arm-none-eabi-g++) +set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) +set(CMAKE_AR arm-none-eabi-ar) +set(CMAKE_OBJCOPY arm-none-eabi-objcopy) +set(CMAKE_OBJDUMP arm-none-eabi-objdump) +set(SIZE arm-none-eabi-size) +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +# project settings +project(${projectName} C CXX ASM) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_C_STANDARD 11) + +#Uncomment for hardware floating point +#add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) +#add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) +#add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) + +#Uncomment for software floating point +#add_compile_options(-mfloat-abi=soft) + +add_compile_options(-mcpu=${mcpu} -mthumb -mthumb-interwork) +add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) + +# uncomment to mitigate c++17 absolute addresses warnings +#set(CMAKE_CXX_FLAGS "$${CMAKE_CXX_FLAGS} -Wno-register") + +# Enable assembler files preprocessing +add_compile_options($<$:-x$assembler-with-cpp>) + +if ("$${CMAKE_BUILD_TYPE}" STREQUAL "Release") + message(STATUS "Maximum optimization for speed") + add_compile_options(-Ofast) +elseif ("$${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") + message(STATUS "Maximum optimization for speed, debug info included") + add_compile_options(-Ofast -g) +elseif ("$${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") + message(STATUS "Maximum optimization for size") + add_compile_options(-Os) +else () + message(STATUS "Minimal optimization, debug info included") + add_compile_options(-Og -g) +endif () + +include_directories(${includes}) + +add_definitions(${defines}) + +file(GLOB_RECURSE SOURCES ${sources}) + +set(LINKER_SCRIPT $${CMAKE_SOURCE_DIR}/${linkerScript}) + +add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=$${PROJECT_BINARY_DIR}/$${PROJECT_NAME}.map) +add_link_options(-mcpu=${mcpu} -mthumb -mthumb-interwork) +add_link_options(-T $${LINKER_SCRIPT}) + +add_executable($${PROJECT_NAME}.elf $${SOURCES} $${LINKER_SCRIPT}) + +set(HEX_FILE $${PROJECT_BINARY_DIR}/$${PROJECT_NAME}.hex) +set(BIN_FILE $${PROJECT_BINARY_DIR}/$${PROJECT_NAME}.bin) + +add_custom_command(TARGET $${PROJECT_NAME}.elf POST_BUILD + COMMAND $${CMAKE_OBJCOPY} -Oihex $ $${HEX_FILE} + COMMAND $${CMAKE_OBJCOPY} -Obinary $ $${BIN_FILE} + COMMENT "Building $${HEX_FILE} +Building $${BIN_FILE}") diff --git a/Core/Inc/adc.h b/Core/Inc/adc.h new file mode 100644 index 0000000..a6a5dba --- /dev/null +++ b/Core/Inc/adc.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file adc.h + * @brief This file contains all the function prototypes for + * the adc.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __ADC_H__ +#define __ADC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern ADC_HandleTypeDef hadc; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_ADC_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __ADC_H__ */ + diff --git a/Core/Inc/comp.h b/Core/Inc/comp.h new file mode 100644 index 0000000..1d58fb3 --- /dev/null +++ b/Core/Inc/comp.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file comp.h + * @brief This file contains all the function prototypes for + * the comp.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __COMP_H__ +#define __COMP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern COMP_HandleTypeDef hcomp2; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_COMP2_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __COMP_H__ */ + diff --git a/Core/Inc/dma.h b/Core/Inc/dma.h new file mode 100644 index 0000000..493d98e --- /dev/null +++ b/Core/Inc/dma.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file dma.h + * @brief This file contains all the function prototypes for + * the dma.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __DMA_H__ +#define __DMA_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* DMA memory to memory transfer handles -------------------------------------*/ + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_DMA_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __DMA_H__ */ + diff --git a/Core/Inc/gpio.h b/Core/Inc/gpio.h new file mode 100644 index 0000000..9ac4857 --- /dev/null +++ b/Core/Inc/gpio.h @@ -0,0 +1,49 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.h + * @brief This file contains all the function prototypes for + * the gpio.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __GPIO_H__ +#define __GPIO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_GPIO_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ GPIO_H__ */ + diff --git a/Core/Inc/main.h b/Core/Inc/main.h new file mode 100644 index 0000000..28afa0d --- /dev/null +++ b/Core/Inc/main.h @@ -0,0 +1,75 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @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 */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __MAIN_H +#define __MAIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f0xx_hal.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void Error_Handler(void); + +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +/* Private defines -----------------------------------------------------------*/ +#define BLDC_CD_Pin GPIO_PIN_5 +#define BLDC_CD_GPIO_Port GPIOB +#define BLDC_BD_Pin GPIO_PIN_6 +#define BLDC_BD_GPIO_Port GPIOB +#define BLDC_AD_Pin GPIO_PIN_7 +#define BLDC_AD_GPIO_Port GPIOB + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +#ifdef __cplusplus +} +#endif + +#endif /* __MAIN_H */ diff --git a/Core/Inc/rtc.h b/Core/Inc/rtc.h new file mode 100644 index 0000000..2b87ae5 --- /dev/null +++ b/Core/Inc/rtc.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file rtc.h + * @brief This file contains all the function prototypes for + * the rtc.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __RTC_H__ +#define __RTC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern RTC_HandleTypeDef hrtc; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_RTC_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __RTC_H__ */ + diff --git a/Core/Inc/stm32f0xx_hal_conf.h b/Core/Inc/stm32f0xx_hal_conf.h new file mode 100644 index 0000000..59d40b7 --- /dev/null +++ b/Core/Inc/stm32f0xx_hal_conf.h @@ -0,0 +1,322 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f0xx_hal_conf.h + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2016 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 */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F0xx_HAL_CONF_H +#define __STM32F0xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ +#define HAL_MODULE_ENABLED + #define HAL_ADC_MODULE_ENABLED +/*#define HAL_CRYP_MODULE_ENABLED */ +/*#define HAL_CAN_MODULE_ENABLED */ +/*#define HAL_CEC_MODULE_ENABLED */ +#define HAL_COMP_MODULE_ENABLED +/*#define HAL_CRC_MODULE_ENABLED */ +/*#define HAL_CRYP_MODULE_ENABLED */ +/*#define HAL_TSC_MODULE_ENABLED */ +/*#define HAL_DAC_MODULE_ENABLED */ +/*#define HAL_I2S_MODULE_ENABLED */ +/*#define HAL_IWDG_MODULE_ENABLED */ +/*#define HAL_LCD_MODULE_ENABLED */ +/*#define HAL_LPTIM_MODULE_ENABLED */ +/*#define HAL_RNG_MODULE_ENABLED */ +#define HAL_RTC_MODULE_ENABLED +/*#define HAL_SPI_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/*#define HAL_USART_MODULE_ENABLED */ +/*#define HAL_IRDA_MODULE_ENABLED */ +/*#define HAL_SMARTCARD_MODULE_ENABLED */ +/*#define HAL_SMBUS_MODULE_ENABLED */ +/*#define HAL_WWDG_MODULE_ENABLED */ +/*#define HAL_PCD_MODULE_ENABLED */ +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_GPIO_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED + +/* ########################## HSE/HSI Values adaptation ##################### */ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +/** + * @brief In the following line adjust the External High Speed oscillator (HSE) Startup + * Timeout value + */ +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup + * Timeout value + */ +#if !defined (HSI_STARTUP_TIMEOUT) + #define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ +#endif /* HSI_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator for ADC (HSI14) value. + */ +#if !defined (HSI14_VALUE) +#define HSI14_VALUE ((uint32_t)14000000) /*!< Value of the Internal High Speed oscillator for ADC in Hz. + The real value may vary depending on the variations + in voltage and temperature. */ +#endif /* HSI14_VALUE */ + +/** + * @brief Internal High Speed oscillator for USB (HSI48) value. + */ +#if !defined (HSI48_VALUE) + #define HSI48_VALUE ((uint32_t)48000000) /*!< Value of the Internal High Speed oscillator for USB in Hz. + The real value may vary depending on the variations + in voltage and temperature. */ +#endif /* HSI48_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSI) value. + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +/** + * @brief Time out for LSE start up value in ms. + */ +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)3) /*!< tick interrupt priority (lowest by default) */ + /* Warning: Must be set to higher priority for HAL_Delay() */ + /* and HAL_GetTick() usage under interrupt context */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 0 +#define DATA_CACHE_ENABLE 0 +#define USE_SPI_CRC 0U + +#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */ +#define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */ +#define USE_HAL_COMP_REGISTER_CALLBACKS 0U /* COMP register callback disabled */ +#define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */ +#define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */ +#define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */ +#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U /* SMBUS register callback disabled */ +#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */ +#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */ +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */ +#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */ +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */ +#define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */ +#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */ +#define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */ +#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */ +#define USE_HAL_TSC_REGISTER_CALLBACKS 0U /* TSC register callback disabled */ +#define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */ + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1U */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED + #include "stm32f0xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED + #include "stm32f0xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_EXTI_MODULE_ENABLED + #include "stm32f0xx_hal_exti.h" +#endif /* HAL_EXTI_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED + #include "stm32f0xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED + #include "stm32f0xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED + #include "stm32f0xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED + #include "stm32f0xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CEC_MODULE_ENABLED + #include "stm32f0xx_hal_cec.h" +#endif /* HAL_CEC_MODULE_ENABLED */ + +#ifdef HAL_COMP_MODULE_ENABLED + #include "stm32f0xx_hal_comp.h" +#endif /* HAL_COMP_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED + #include "stm32f0xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED + #include "stm32f0xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED + #include "stm32f0xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED + #include "stm32f0xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED + #include "stm32f0xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED + #include "stm32f0xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED + #include "stm32f0xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED + #include "stm32f0xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED + #include "stm32f0xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED + #include "stm32f0xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED + #include "stm32f0xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_SMBUS_MODULE_ENABLED + #include "stm32f0xx_hal_smbus.h" +#endif /* HAL_SMBUS_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED + #include "stm32f0xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED + #include "stm32f0xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_TSC_MODULE_ENABLED + #include "stm32f0xx_hal_tsc.h" +#endif /* HAL_TSC_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED + #include "stm32f0xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED + #include "stm32f0xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED + #include "stm32f0xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ + #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ + void assert_failed(uint8_t* file, uint32_t line); +#else + #define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F0xx_HAL_CONF_H */ + diff --git a/Core/Inc/stm32f0xx_it.h b/Core/Inc/stm32f0xx_it.h new file mode 100644 index 0000000..b0320be --- /dev/null +++ b/Core/Inc/stm32f0xx_it.h @@ -0,0 +1,67 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f0xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @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 */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F0xx_IT_H +#define __STM32F0xx_IT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void NMI_Handler(void); +void HardFault_Handler(void); +void SVC_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); +void DMA1_Channel1_IRQHandler(void); +void DMA1_Channel2_3_IRQHandler(void); +void DMA1_Channel4_5_IRQHandler(void); +void ADC1_COMP_IRQHandler(void); +void USART1_IRQHandler(void); +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F0xx_IT_H */ diff --git a/Core/Inc/tim.h b/Core/Inc/tim.h new file mode 100644 index 0000000..971b286 --- /dev/null +++ b/Core/Inc/tim.h @@ -0,0 +1,63 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file tim.h + * @brief This file contains all the function prototypes for + * the tim.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __TIM_H__ +#define __TIM_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern TIM_HandleTypeDef htim2; + +extern TIM_HandleTypeDef htim3; + +extern TIM_HandleTypeDef htim6; + +extern TIM_HandleTypeDef htim16; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_TIM2_Init(void); +void MX_TIM3_Init(void); +void MX_TIM6_Init(void); +void MX_TIM16_Init(void); + +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIM_H__ */ + diff --git a/Core/Inc/usart.h b/Core/Inc/usart.h new file mode 100644 index 0000000..10c6193 --- /dev/null +++ b/Core/Inc/usart.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file usart.h + * @brief This file contains all the function prototypes for + * the usart.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USART_H__ +#define __USART_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern UART_HandleTypeDef huart1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_USART1_UART_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USART_H__ */ + diff --git a/Core/Src/adc.c b/Core/Src/adc.c new file mode 100644 index 0000000..2a7d2d9 --- /dev/null +++ b/Core/Src/adc.c @@ -0,0 +1,173 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file adc.c + * @brief This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @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 "adc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +ADC_HandleTypeDef hadc; +DMA_HandleTypeDef hdma_adc; + +/* ADC init function */ +void MX_ADC_Init(void) +{ + + /* USER CODE BEGIN ADC_Init 0 */ + + /* USER CODE END ADC_Init 0 */ + + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC_Init 1 */ + + /* USER CODE END ADC_Init 1 */ + + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + */ + hadc.Instance = ADC1; + hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc.Init.Resolution = ADC_RESOLUTION_12B; + hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; + hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc.Init.LowPowerAutoWait = DISABLE; + hadc.Init.LowPowerAutoPowerOff = DISABLE; + hadc.Init.ContinuousConvMode = ENABLE; + hadc.Init.DiscontinuousConvMode = DISABLE; + hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc.Init.DMAContinuousRequests = DISABLE; + hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED; + if (HAL_ADC_Init(&hadc) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel to be converted. + */ + sConfig.Channel = ADC_CHANNEL_0; + sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel to be converted. + */ + sConfig.Channel = ADC_CHANNEL_1; + if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC_Init 2 */ + + /* USER CODE END ADC_Init 2 */ + +} + +void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspInit 0 */ + + /* USER CODE END ADC1_MspInit 0 */ + /* ADC1 clock enable */ + __HAL_RCC_ADC1_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**ADC GPIO Configuration + PA0 ------> ADC_IN0 + PA1 ------> ADC_IN1 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* ADC1 DMA Init */ + /* ADC Init */ + hdma_adc.Instance = DMA1_Channel1; + hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_adc.Init.MemInc = DMA_MINC_ENABLE; + hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; + hdma_adc.Init.Mode = DMA_NORMAL; + hdma_adc.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_adc) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc); + + /* ADC1 interrupt Init */ + HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn); + /* USER CODE BEGIN ADC1_MspInit 1 */ + + /* USER CODE END ADC1_MspInit 1 */ + } +} + +void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) +{ + + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ + + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_ADC1_CLK_DISABLE(); + + /**ADC GPIO Configuration + PA0 ------> ADC_IN0 + PA1 ------> ADC_IN1 + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1); + + /* ADC1 DMA DeInit */ + HAL_DMA_DeInit(adcHandle->DMA_Handle); + + /* ADC1 interrupt Deinit */ + /* USER CODE BEGIN ADC1:ADC1_COMP_IRQn disable */ + /** + * Uncomment the line below to disable the "ADC1_COMP_IRQn" interrupt + * Be aware, disabling shared interrupt may affect other IPs + */ + /* HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn); */ + /* USER CODE END ADC1:ADC1_COMP_IRQn disable */ + + /* USER CODE BEGIN ADC1_MspDeInit 1 */ + + /* USER CODE END ADC1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/comp.c b/Core/Src/comp.c new file mode 100644 index 0000000..1259841 --- /dev/null +++ b/Core/Src/comp.c @@ -0,0 +1,120 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file comp.c + * @brief This file provides code for the configuration + * of the COMP instances. + ****************************************************************************** + * @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 "comp.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +COMP_HandleTypeDef hcomp2; + +/* COMP2 init function */ +void MX_COMP2_Init(void) +{ + + /* USER CODE BEGIN COMP2_Init 0 */ + + /* USER CODE END COMP2_Init 0 */ + + /* USER CODE BEGIN COMP2_Init 1 */ + + /* USER CODE END COMP2_Init 1 */ + hcomp2.Instance = COMP2; + hcomp2.Init.InvertingInput = COMP_INVERTINGINPUT_IO1; + hcomp2.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_IO1; + hcomp2.Init.Output = COMP_OUTPUT_NONE; + hcomp2.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED; + hcomp2.Init.Hysteresis = COMP_HYSTERESIS_NONE; + hcomp2.Init.Mode = COMP_MODE_HIGHSPEED; + hcomp2.Init.WindowMode = COMP_WINDOWMODE_DISABLE; + hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING; + if (HAL_COMP_Init(&hcomp2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN COMP2_Init 2 */ + + /* USER CODE END COMP2_Init 2 */ + +} + +void HAL_COMP_MspInit(COMP_HandleTypeDef* compHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(compHandle->Instance==COMP2) + { + /* USER CODE BEGIN COMP2_MspInit 0 */ + + /* USER CODE END COMP2_MspInit 0 */ + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**COMP2 GPIO Configuration + PA2 ------> COMP2_INM + PA3 ------> COMP2_INP + */ + GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* COMP2 interrupt Init */ + HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn); + /* USER CODE BEGIN COMP2_MspInit 1 */ + + /* USER CODE END COMP2_MspInit 1 */ + } +} + +void HAL_COMP_MspDeInit(COMP_HandleTypeDef* compHandle) +{ + + if(compHandle->Instance==COMP2) + { + /* USER CODE BEGIN COMP2_MspDeInit 0 */ + + /* USER CODE END COMP2_MspDeInit 0 */ + + /**COMP2 GPIO Configuration + PA2 ------> COMP2_INM + PA3 ------> COMP2_INP + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); + + /* COMP2 interrupt Deinit */ + /* USER CODE BEGIN COMP2:ADC1_COMP_IRQn disable */ + /** + * Uncomment the line below to disable the "ADC1_COMP_IRQn" interrupt + * Be aware, disabling shared interrupt may affect other IPs + */ + /* HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn); */ + /* USER CODE END COMP2:ADC1_COMP_IRQn disable */ + + /* USER CODE BEGIN COMP2_MspDeInit 1 */ + + /* USER CODE END COMP2_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/dma.c b/Core/Src/dma.c new file mode 100644 index 0000000..c78ad58 --- /dev/null +++ b/Core/Src/dma.c @@ -0,0 +1,61 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file dma.c + * @brief This file provides code for the configuration + * of all the requested memory to memory DMA transfers. + ****************************************************************************** + * @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 "dma.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure DMA */ +/*----------------------------------------------------------------------------*/ + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** + * Enable DMA controller clock + */ +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); + /* DMA1_Channel2_3_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn); + /* DMA1_Channel4_5_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel4_5_IRQn, 1, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel4_5_IRQn); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ + diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c new file mode 100644 index 0000000..a95fb8c --- /dev/null +++ b/Core/Src/gpio.c @@ -0,0 +1,65 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.c + * @brief This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @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 "gpio.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure GPIO */ +/*----------------------------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** Configure pins as + * Analog + * Input + * Output + * EVENT_OUT + * EXTI +*/ +void MX_GPIO_Init(void) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, BLDC_CD_Pin|BLDC_BD_Pin|BLDC_AD_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pins : BLDC_CD_Pin BLDC_BD_Pin BLDC_AD_Pin */ + GPIO_InitStruct.Pin = BLDC_CD_Pin|BLDC_BD_Pin|BLDC_AD_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ diff --git a/Core/Src/main.c b/Core/Src/main.c new file mode 100644 index 0000000..2671fd1 --- /dev/null +++ b/Core/Src/main.c @@ -0,0 +1,409 @@ +/* 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" +#include "adc.h" +#include "comp.h" +#include "dma.h" +#include "rtc.h" +#include "tim.h" +#include "usart.h" +#include "gpio.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +#include +#include "ESHL_driver.h" +#include "ws2812b.h" +#include "communication_management.h" +#include "ESHL_protocol.h" + +/* 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 ---------------------------------------------------------*/ + +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +extern uint16_t ESHL_RunPWMBuff; //电调运行pwm缓存 +extern ESHL_PROTOCOL_PACK_ANALYSIS_T recv_str; + +/* 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_RTC_Init(); + MX_COMP2_Init(); + MX_TIM2_Init(); + MX_TIM3_Init(); + MX_TIM6_Init(); + MX_TIM16_Init(); + MX_ADC_Init(); + MX_USART1_UART_Init(); + /* USER CODE BEGIN 2 */ + + uint8_t ESHL_OpenLoopRestartNum = 0;//电调重开环启动计数 + + WS2812B_OBJ_T ESHL_StateLed; //创建电调ws2812b指示灯对象 + WS2812_Init(&ESHL_StateLed,&htim16,TIM_CHANNEL_1,1);//初始化 + WS2812_Set(&ESHL_StateLed,0,15,15,0); //默认为黄色 + + //电调指示灯 内容 + //-------------------------------------------------- + //绿色常亮 表示电调准备就绪,正常运行 + //-------------------------------------------------- + //黄色常亮 表示正在初始化 + //-------------------------------------------------- + //红色闪烁 表示MOS异常,闪烁次数代表错误代码 + //-------------------------------------------------- + //橙色闪烁 表示电流异常 + //-------------------------------------------------- + //蓝色闪烁 表示主机离线,数据接收超时 + //-------------------------------------------------- + //白色闪烁 表示电调开环启动超过最大次数,开环启动失败 + //-------------------------------------------------- + //紫色常亮 表示电调正在设置地址 + //-------------------------------------------------- + //紫色闪烁 表示地址设置完毕,闪烁次数代表电调地址 + //-------------------------------------------------- + //浅蓝闪烁 表示电池电压异常 + //-------------------------------------------------- + + ESHL_ESC_Init(); + + uint8_t mos_error_id = MOS_SelfTest();//MOS自检,并获取错误代码 + if (mos_error_id != 0) { + while (1) { + for (uint8_t i = 0;i < mos_error_id;i++) { + WS2812_SetAll(&ESHL_StateLed,15,0,0);//红灯闪烁,闪烁次数代表错误码 + HAL_Delay(200); + WS2812_SetAll(&ESHL_StateLed,0,0,0); + HAL_Delay(200); + ESHL_CommunicationSendCode(ESHL_PROTOCOL_CMD_ERROR,0xE0); + } + HAL_Delay(800); + } + } + + //----------开机音乐---------- + ESHL_Beep(ESHL_BEEP_SHORT);//E + HAL_Delay(100); + + ESHL_Beep(ESHL_BEEP_SHORT);//S + ESHL_Beep(ESHL_BEEP_SHORT); + ESHL_Beep(ESHL_BEEP_SHORT); + HAL_Delay(100); + + ESHL_Beep(ESHL_BEEP_LONG);//C + ESHL_Beep(ESHL_BEEP_SHORT); + ESHL_Beep(ESHL_BEEP_LONG); + ESHL_Beep(ESHL_BEEP_SHORT); + HAL_Delay(100); + + // ESHL_Beep(ESHL_BEEP_SHORT);//E + // HAL_Delay(100); + // + // ESHL_Beep(ESHL_BEEP_SHORT);//S + // ESHL_Beep(ESHL_BEEP_SHORT); + // ESHL_Beep(ESHL_BEEP_SHORT); + // HAL_Delay(100); + // + // ESHL_Beep(ESHL_BEEP_SHORT);//H + // ESHL_Beep(ESHL_BEEP_SHORT); + // ESHL_Beep(ESHL_BEEP_SHORT); + // ESHL_Beep(ESHL_BEEP_SHORT); + // HAL_Delay(100); + // + // ESHL_Beep(ESHL_BEEP_SHORT);//L + // ESHL_Beep(ESHL_BEEP_LONG); + // ESHL_Beep(ESHL_BEEP_SHORT); + // ESHL_Beep(ESHL_BEEP_SHORT); + // HAL_Delay(100); + + //----------end of 开机音乐---------- + + if (ESHL_GetState() == ESHL_STATE_OFF) {//初始化无错误 + ESHL_SetState(EShl_STATE_READY);//将电调状态设置为准备就绪 + } + + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) + { + ESHL_RuningCurrentVBATChack();//电流和电池电压检测 + switch (ESHL_GetState()) { + + case ESHL_STATE_OFF://电调关闭 + ESHL_CloseMOSComp();//关闭所有MOS管并且关闭比较器 + WS2812_SetAll(&ESHL_StateLed,0,0,0); + break; + + case EShl_STATE_READY://电调准备就绪 + WS2812_SetAll(&ESHL_StateLed,0,15,0);//状态指示灯设为绿色 + ESHL_CommunicationStart();//开始通信 + break; + + case ESHL_STATE_START://启动电机 + WS2812_SetAll(&ESHL_StateLed,0,15,0);//状态指示灯设为绿色 + ESHL_Start(ESHL_GetDirection()); + break; + + case ESHL_STATE_OPEN_LOOP_START_FAIL://电调开环启动失败 + if (ESHL_OpenLoopRestartNum > ESHL_OPEN_LOOP_RESTART_MAX_NUM - 2) { + ESHL_SetState(ESHL_STATE_OPEN_LOOP_START_ERROR);//设置电调状态为无法开环启动 + } + else{ + ESHL_OpenLoopRestartNum++; + ESHL_Start(ESHL_GetDirection()); + } + break; + + case ESHL_STATE_OPEN_LOOP_START_ERROR://电调开环启动重试超过最大次数,无法开环启动 + ESHL_CloseMOSComp();//关闭所有MOS管并且关闭比较器 + WS2812_SetAll(&ESHL_StateLed,15,15,15); + HAL_Delay(200); + WS2812_SetAll(&ESHL_StateLed,0,0,0); + HAL_Delay(200); + ESHL_CommunicationSendCode(ESHL_PROTOCOL_CMD_ERROR,0XE2);//发送电调错误码 + break; + + case ESHL_STATE_RUN_CLOCKWISE://顺时针方向运动中 + ESHL_OpenLoopRestartNum = 0;//开环重启动计数清零 + ESHL_RuningChack(); //堵转检测 + ESHL_SET_PWM(ESHL_RunPWMBuff);//更新运行PWM值 + WS2812_SetAll(&ESHL_StateLed,0,15,0);//状态指示灯设为绿色 + break; + + case ESHL_STATE_RUN_COUNTER_CLOCKWISE://逆时针方向运动中 + ESHL_OpenLoopRestartNum = 0;//开环重启动计数清零 + ESHL_RuningChack(); //堵转检测 + ESHL_SET_PWM(ESHL_RunPWMBuff);//更新运行PWM值 + WS2812_SetAll(&ESHL_StateLed,0,15,0);//状态指示灯设为绿色 + break; + + case ESHL_STATE_MOTOR_RUNING_STOP://电机运行时停转 + ESHL_CommunicationSendCode(ESHL_PROTOCOL_CMD_ERROR,0XE3);//发送电调错误码 + ESHL_CommunicationStart();//开启接收 + ESHL_Start(ESHL_GetDirection());//开环启动 + break; + + case ESHL_STATE_CURRENT_ERROR://电流异常 + ESHL_CloseMOSComp();//关闭所有MOS管并且关闭比较器 + WS2812_SetAll(&ESHL_StateLed,15,5,0); + HAL_Delay(200); + WS2812_SetAll(&ESHL_StateLed,0,0,0); + HAL_Delay(200); + ESHL_CommunicationSendCode(ESHL_PROTOCOL_CMD_ERROR,0XE1);//发送电调错误码 + break; + + case ESHL_STATE_SET_HOST_TIMEOUT://主机离线 + ESHL_CloseMOSComp();//关闭所有MOS管并且关闭比较器 + WS2812_SetAll(&ESHL_StateLed,0,0,15); + HAL_Delay(200); + WS2812_SetAll(&ESHL_StateLed,0,0,0); + HAL_Delay(200); + break; + + case ESHL_STATE_SET_ADDR://电调设置地址中 + ESHL_SetAddr(recv_str.addr_dat); + WS2812_SetAll(&ESHL_StateLed,15,0,15); + break; + + case ESHL_STATE_SET_ADDR_OK://电调地址设置成功 + while (1) { + for (uint8_t i = 0;i < (uint8_t)(ESHL_GetAddr() & 0xff);i++) { + WS2812_SetAll(&ESHL_StateLed,15,0,15); + HAL_Delay(200); + WS2812_SetAll(&ESHL_StateLed,0,0,0); + HAL_Delay(200); + ESHL_CommunicationAddressSend();//广播电调地址 + } + HAL_Delay(800); + } + break; + + case ESHL_STATE_BATTERY_VOLTAGE_ERROR://电池电压异常 + ESHL_CloseMOSComp();//关闭所有MOS管并且关闭比较器 + WS2812_SetAll(&ESHL_StateLed,0,15,15); + HAL_Delay(200); + WS2812_SetAll(&ESHL_StateLed,0,0,0); + HAL_Delay(200); + ESHL_CommunicationSendCode(ESHL_PROTOCOL_CMD_ERROR,0XE4);//发送电调错误码 + break; + + case ESHL_STATE_BRAKE:///刹车中 + ESHL_Break(); + break; + + case ESHL_STATE_BRAKE_OK://刹车完毕 + ESHL_CommunicationSendCode(ESHL_PROTOCOL_CMD_BREAK,0xA3); + ESHL_SetState(EShl_STATE_READY); + break; + + default: + break; + } + ESHL_CommunicationDataProcessing();//通信数据处理 + /* 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}; + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14 + |RCC_OSCILLATORTYPE_LSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSI14State = RCC_HSI14_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.HSI14CalibrationValue = 16; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; + RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1; + 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_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) + { + Error_Handler(); + } + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_RTC; + PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1; + PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) + { + Error_Handler(); + } +} + +/* 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/rtc.c b/Core/Src/rtc.c new file mode 100644 index 0000000..9b7171f --- /dev/null +++ b/Core/Src/rtc.c @@ -0,0 +1,94 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file rtc.c + * @brief This file provides code for the configuration + * of the RTC instances. + ****************************************************************************** + * @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 "rtc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +RTC_HandleTypeDef hrtc; + +/* RTC init function */ +void MX_RTC_Init(void) +{ + + /* USER CODE BEGIN RTC_Init 0 */ + + /* USER CODE END RTC_Init 0 */ + + /* USER CODE BEGIN RTC_Init 1 */ + + /* USER CODE END RTC_Init 1 */ + + /** Initialize RTC Only + */ + hrtc.Instance = RTC; + hrtc.Init.HourFormat = RTC_HOURFORMAT_24; + hrtc.Init.AsynchPrediv = 127; + hrtc.Init.SynchPrediv = 255; + hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; + hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; + hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; + if (HAL_RTC_Init(&hrtc) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN RTC_Init 2 */ + + /* USER CODE END RTC_Init 2 */ + +} + +void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) +{ + + if(rtcHandle->Instance==RTC) + { + /* USER CODE BEGIN RTC_MspInit 0 */ + + /* USER CODE END RTC_MspInit 0 */ + /* RTC clock enable */ + __HAL_RCC_RTC_ENABLE(); + /* USER CODE BEGIN RTC_MspInit 1 */ + + /* USER CODE END RTC_MspInit 1 */ + } +} + +void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) +{ + + if(rtcHandle->Instance==RTC) + { + /* USER CODE BEGIN RTC_MspDeInit 0 */ + + /* USER CODE END RTC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_RTC_DISABLE(); + /* USER CODE BEGIN RTC_MspDeInit 1 */ + + /* USER CODE END RTC_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/stm32f0xx_hal_msp.c b/Core/Src/stm32f0xx_hal_msp.c new file mode 100644 index 0000000..085205d --- /dev/null +++ b/Core/Src/stm32f0xx_hal_msp.c @@ -0,0 +1,82 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f0xx_hal_msp.c + * @brief This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @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" +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN Define */ + +/* USER CODE END Define */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN Macro */ + +/* USER CODE END Macro */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* External functions --------------------------------------------------------*/ +/* USER CODE BEGIN ExternalFunctions */ + +/* USER CODE END ExternalFunctions */ + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) +{ + + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + + /* System interrupt init*/ + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/stm32f0xx_it.c b/Core/Src/stm32f0xx_it.c new file mode 100644 index 0000000..c9db0b9 --- /dev/null +++ b/Core/Src/stm32f0xx_it.c @@ -0,0 +1,223 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f0xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @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 "stm32f0xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +extern DMA_HandleTypeDef hdma_adc; +extern ADC_HandleTypeDef hadc; +extern COMP_HandleTypeDef hcomp2; +extern DMA_HandleTypeDef hdma_tim16_ch1_up; +extern DMA_HandleTypeDef hdma_usart1_rx; +extern DMA_HandleTypeDef hdma_usart1_tx; +extern UART_HandleTypeDef huart1; +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M0 Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + while (1) + { + } + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVC_IRQn 0 */ + + /* USER CODE END SVC_IRQn 0 */ + /* USER CODE BEGIN SVC_IRQn 1 */ + + /* USER CODE END SVC_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + HAL_IncTick(); + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32F0xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32f0xx.s). */ +/******************************************************************************/ + +/** + * @brief This function handles DMA1 channel 1 interrupt. + */ +void DMA1_Channel1_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel1_IRQn 0 */ + + /* USER CODE END DMA1_Channel1_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_adc); + /* USER CODE BEGIN DMA1_Channel1_IRQn 1 */ + + /* USER CODE END DMA1_Channel1_IRQn 1 */ +} + +/** + * @brief This function handles DMA1 channel 2 and 3 interrupts. + */ +void DMA1_Channel2_3_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */ + + /* USER CODE END DMA1_Channel2_3_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_usart1_tx); + HAL_DMA_IRQHandler(&hdma_tim16_ch1_up); + /* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */ + + /* USER CODE END DMA1_Channel2_3_IRQn 1 */ +} + +/** + * @brief This function handles DMA1 channel 4 and 5 interrupts. + */ +void DMA1_Channel4_5_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel4_5_IRQn 0 */ + + /* USER CODE END DMA1_Channel4_5_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_usart1_rx); + /* USER CODE BEGIN DMA1_Channel4_5_IRQn 1 */ + + /* USER CODE END DMA1_Channel4_5_IRQn 1 */ +} + +/** + * @brief This function handles ADC and COMP interrupts (COMP interrupts through EXTI lines 21 and 22). + */ +void ADC1_COMP_IRQHandler(void) +{ + /* USER CODE BEGIN ADC1_COMP_IRQn 0 */ + + /* USER CODE END ADC1_COMP_IRQn 0 */ + HAL_ADC_IRQHandler(&hadc); + HAL_COMP_IRQHandler(&hcomp2); + /* USER CODE BEGIN ADC1_COMP_IRQn 1 */ + + /* USER CODE END ADC1_COMP_IRQn 1 */ +} + +/** + * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25. + */ +void USART1_IRQHandler(void) +{ + /* USER CODE BEGIN USART1_IRQn 0 */ + + /* USER CODE END USART1_IRQn 0 */ + HAL_UART_IRQHandler(&huart1); + /* USER CODE BEGIN USART1_IRQn 1 */ + + /* USER CODE END USART1_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/syscalls.c b/Core/Src/syscalls.c new file mode 100644 index 0000000..8884b5a --- /dev/null +++ b/Core/Src/syscalls.c @@ -0,0 +1,176 @@ +/** + ****************************************************************************** + * @file syscalls.c + * @author Auto-generated by STM32CubeIDE + * @brief STM32CubeIDE Minimal System calls file + * + * For more information about which c-functions + * need which of these lowlevel functions + * please consult the Newlib libc-manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2020-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. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include +#include +#include +#include +#include +#include +#include + + +/* Variables */ +extern int __io_putchar(int ch) __attribute__((weak)); +extern int __io_getchar(void) __attribute__((weak)); + + +char *__env[1] = { 0 }; +char **environ = __env; + + +/* Functions */ +void initialise_monitor_handles() +{ +} + +int _getpid(void) +{ + return 1; +} + +int _kill(int pid, int sig) +{ + (void)pid; + (void)sig; + errno = EINVAL; + return -1; +} + +void _exit (int status) +{ + _kill(status, -1); + while (1) {} /* Make sure we hang here */ +} + +__attribute__((weak)) int _read(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + *ptr++ = __io_getchar(); + } + + return len; +} + +__attribute__((weak)) int _write(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + __io_putchar(*ptr++); + } + return len; +} + +int _close(int file) +{ + (void)file; + return -1; +} + + +int _fstat(int file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _isatty(int file) +{ + (void)file; + return 1; +} + +int _lseek(int file, int ptr, int dir) +{ + (void)file; + (void)ptr; + (void)dir; + return 0; +} + +int _open(char *path, int flags, ...) +{ + (void)path; + (void)flags; + /* Pretend like we always fail */ + return -1; +} + +int _wait(int *status) +{ + (void)status; + errno = ECHILD; + return -1; +} + +int _unlink(char *name) +{ + (void)name; + errno = ENOENT; + return -1; +} + +int _times(struct tms *buf) +{ + (void)buf; + return -1; +} + +int _stat(char *file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _link(char *old, char *new) +{ + (void)old; + (void)new; + errno = EMLINK; + return -1; +} + +int _fork(void) +{ + errno = EAGAIN; + return -1; +} + +int _execve(char *name, char **argv, char **env) +{ + (void)name; + (void)argv; + (void)env; + errno = ENOMEM; + return -1; +} diff --git a/Core/Src/sysmem.c b/Core/Src/sysmem.c new file mode 100644 index 0000000..5d9f7e6 --- /dev/null +++ b/Core/Src/sysmem.c @@ -0,0 +1,79 @@ +/** + ****************************************************************************** + * @file sysmem.c + * @author Generated by STM32CubeIDE + * @brief STM32CubeIDE System Memory calls file + * + * For more information about which C functions + * need which of these lowlevel functions + * please consult the newlib libc manual + ****************************************************************************** + * @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. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include + +/** + * Pointer to the current high watermark of the heap usage + */ +static uint8_t *__sbrk_heap_end = NULL; + +/** + * @brief _sbrk() allocates memory to the newlib heap and is used by malloc + * and others from the C library + * + * @verbatim + * ############################################################################ + * # .data # .bss # newlib heap # MSP stack # + * # # # # Reserved by _Min_Stack_Size # + * ############################################################################ + * ^-- RAM start ^-- _end _estack, RAM end --^ + * @endverbatim + * + * This implementation starts allocating at the '_end' linker symbol + * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack + * The implementation considers '_estack' linker symbol to be RAM end + * NOTE: If the MSP stack, at any point during execution, grows larger than the + * reserved size, please increase the '_Min_Stack_Size'. + * + * @param incr Memory size + * @return Pointer to allocated memory + */ +void *_sbrk(ptrdiff_t incr) +{ + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; + + /* Initialize heap end at first call */ + if (NULL == __sbrk_heap_end) + { + __sbrk_heap_end = &_end; + } + + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) + { + errno = ENOMEM; + return (void *)-1; + } + + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; + + return (void *)prev_heap_end; +} diff --git a/Core/Src/system_stm32f0xx.c b/Core/Src/system_stm32f0xx.c new file mode 100644 index 0000000..b634424 --- /dev/null +++ b/Core/Src/system_stm32f0xx.c @@ -0,0 +1,249 @@ +/** + ****************************************************************************** + * @file system_stm32f0xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M0 Device Peripheral Access Layer System Source File. + * + * 1. This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32f0xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * + ****************************************************************************** + * @attention + * + * Copyright (c) 2016 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. + * + ****************************************************************************** + */ +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32f0xx_system + * @{ + */ + +/** @addtogroup STM32F0xx_System_Private_Includes + * @{ + */ + +#include "stm32f0xx.h" + +/** + * @} + */ + +/** @addtogroup STM32F0xx_System_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F0xx_System_Private_Defines + * @{ + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz. + This value can be provided and adapted by the user application. */ +#endif /* HSE_VALUE */ + +#if !defined (HSI_VALUE) + #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz. + This value can be provided and adapted by the user application. */ +#endif /* HSI_VALUE */ + +#if !defined (HSI48_VALUE) +#define HSI48_VALUE ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz. + This value can be provided and adapted by the user application. */ +#endif /* HSI48_VALUE */ +/** + * @} + */ + +/** @addtogroup STM32F0xx_System_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F0xx_System_Private_Variables + * @{ + */ + /* This variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. + */ +uint32_t SystemCoreClock = 8000000; + +const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; +const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4}; + +/** + * @} + */ + +/** @addtogroup STM32F0xx_System_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F0xx_System_Private_Functions + * @{ + */ + +/** + * @brief Setup the microcontroller system + * @param None + * @retval None + */ +void SystemInit(void) +{ + /* NOTE :SystemInit(): This function is called at startup just after reset and + before branch to main program. This call is made inside + the "startup_stm32f0xx.s" file. + User can setups the default system clock (System clock source, PLL Multiplier + and Divider factors, AHB/APBx prescalers and Flash settings). + */ +} + +/** + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * - If SYSCLK source is HSI48, SystemCoreClock will contain the HSI48_VALUE(***) + * + * (*) HSI_VALUE is a constant defined in stm32f0xx_hal_conf.h file (default value + * 8 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (**) HSE_VALUE is a constant defined in stm32f0xx_hal_conf.h file (its value + * depends on the application requirements), user has to ensure that HSE_VALUE + * is same as the real frequency of the crystal used. Otherwise, this function + * may have wrong result. + * + * (***) HSI48_VALUE is a constant defined in stm32f0xx_hal_conf.h file (default value + * 48 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ +void SystemCoreClockUpdate (void) +{ + uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0; + + /* Get SYSCLK source -------------------------------------------------------*/ + tmp = RCC->CFGR & RCC_CFGR_SWS; + + switch (tmp) + { + case RCC_CFGR_SWS_HSI: /* HSI used as system clock */ + SystemCoreClock = HSI_VALUE; + break; + case RCC_CFGR_SWS_HSE: /* HSE used as system clock */ + SystemCoreClock = HSE_VALUE; + break; + case RCC_CFGR_SWS_PLL: /* PLL used as system clock */ + /* Get PLL clock source and multiplication factor ----------------------*/ + pllmull = RCC->CFGR & RCC_CFGR_PLLMUL; + pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; + pllmull = ( pllmull >> 18) + 2; + predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1; + + if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV) + { + /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */ + SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull; + } +#if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx) + else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV) + { + /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */ + SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull; + } +#endif /* STM32F042x6 || STM32F048xx || STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */ + else + { +#if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) \ + || defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) \ + || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC) + /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */ + SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull; +#else + /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */ + SystemCoreClock = (HSI_VALUE >> 1) * pllmull; +#endif /* STM32F042x6 || STM32F048xx || STM32F070x6 || + STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB || + STM32F091xC || STM32F098xx || STM32F030xC */ + } + break; + default: /* HSI used as system clock */ + SystemCoreClock = HSI_VALUE; + break; + } + /* Compute HCLK clock frequency ----------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + diff --git a/Core/Src/tim.c b/Core/Src/tim.c new file mode 100644 index 0000000..4b44a1f --- /dev/null +++ b/Core/Src/tim.c @@ -0,0 +1,438 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file tim.c + * @brief This file provides code for the configuration + * of the TIM instances. + ****************************************************************************** + * @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 "tim.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +TIM_HandleTypeDef htim2; +TIM_HandleTypeDef htim3; +TIM_HandleTypeDef htim6; +TIM_HandleTypeDef htim16; +DMA_HandleTypeDef hdma_tim16_ch1_up; + +/* TIM2 init function */ +void MX_TIM2_Init(void) +{ + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + + /* USER CODE BEGIN TIM2_Init 1 */ + + /* USER CODE END TIM2_Init 1 */ + htim2.Instance = TIM2; + htim2.Init.Prescaler = 2; + htim2.Init.CounterMode = TIM_COUNTERMODE_UP; + htim2.Init.Period = 991; + htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if (HAL_TIM_Base_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + Error_Handler(); + } + if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM2_Init 2 */ + + /* USER CODE END TIM2_Init 2 */ + HAL_TIM_MspPostInit(&htim2); + +} +/* TIM3 init function */ +void MX_TIM3_Init(void) +{ + + /* USER CODE BEGIN TIM3_Init 0 */ + + /* USER CODE END TIM3_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + + /* USER CODE BEGIN TIM3_Init 1 */ + + /* USER CODE END TIM3_Init 1 */ + htim3.Instance = TIM3; + htim3.Init.Prescaler = 2; + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 991; + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM3_Init 2 */ + + /* USER CODE END TIM3_Init 2 */ + HAL_TIM_MspPostInit(&htim3); + +} +/* TIM6 init function */ +void MX_TIM6_Init(void) +{ + + /* USER CODE BEGIN TIM6_Init 0 */ + + /* USER CODE END TIM6_Init 0 */ + + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + /* USER CODE BEGIN TIM6_Init 1 */ + + /* USER CODE END TIM6_Init 1 */ + htim6.Instance = TIM6; + htim6.Init.Prescaler = 47; + htim6.Init.CounterMode = TIM_COUNTERMODE_UP; + htim6.Init.Period = 65535; + htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if (HAL_TIM_Base_Init(&htim6) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM6_Init 2 */ + + /* USER CODE END TIM6_Init 2 */ + +} +/* TIM16 init function */ +void MX_TIM16_Init(void) +{ + + /* USER CODE BEGIN TIM16_Init 0 */ + + /* USER CODE END TIM16_Init 0 */ + + TIM_OC_InitTypeDef sConfigOC = {0}; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; + + /* USER CODE BEGIN TIM16_Init 1 */ + + /* USER CODE END TIM16_Init 1 */ + htim16.Instance = TIM16; + htim16.Init.Prescaler = 0; + htim16.Init.CounterMode = TIM_COUNTERMODE_UP; + htim16.Init.Period = 60; + htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim16.Init.RepetitionCounter = 0; + htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim16) != HAL_OK) + { + Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim16) != HAL_OK) + { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; + if (HAL_TIM_PWM_ConfigChannel(&htim16, &sConfigOC, TIM_CHANNEL_1) != 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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + if (HAL_TIMEx_ConfigBreakDeadTime(&htim16, &sBreakDeadTimeConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM16_Init 2 */ + + /* USER CODE END TIM16_Init 2 */ + HAL_TIM_MspPostInit(&htim16); + +} + +void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) +{ + + if(tim_baseHandle->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspInit 0 */ + + /* USER CODE END TIM2_MspInit 0 */ + /* TIM2 clock enable */ + __HAL_RCC_TIM2_CLK_ENABLE(); + /* USER CODE BEGIN TIM2_MspInit 1 */ + + /* USER CODE END TIM2_MspInit 1 */ + } + else if(tim_baseHandle->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspInit 0 */ + + /* USER CODE END TIM3_MspInit 0 */ + /* TIM3 clock enable */ + __HAL_RCC_TIM3_CLK_ENABLE(); + /* USER CODE BEGIN TIM3_MspInit 1 */ + + /* USER CODE END TIM3_MspInit 1 */ + } + else if(tim_baseHandle->Instance==TIM6) + { + /* USER CODE BEGIN TIM6_MspInit 0 */ + + /* USER CODE END TIM6_MspInit 0 */ + /* TIM6 clock enable */ + __HAL_RCC_TIM6_CLK_ENABLE(); + /* USER CODE BEGIN TIM6_MspInit 1 */ + + /* USER CODE END TIM6_MspInit 1 */ + } + else if(tim_baseHandle->Instance==TIM16) + { + /* USER CODE BEGIN TIM16_MspInit 0 */ + + /* USER CODE END TIM16_MspInit 0 */ + /* TIM16 clock enable */ + __HAL_RCC_TIM16_CLK_ENABLE(); + + /* TIM16 DMA Init */ + /* TIM16_CH1_UP Init */ + hdma_tim16_ch1_up.Instance = DMA1_Channel3; + hdma_tim16_ch1_up.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_tim16_ch1_up.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_tim16_ch1_up.Init.MemInc = DMA_MINC_ENABLE; + hdma_tim16_ch1_up.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_tim16_ch1_up.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; + hdma_tim16_ch1_up.Init.Mode = DMA_CIRCULAR; + hdma_tim16_ch1_up.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_tim16_ch1_up) != HAL_OK) + { + Error_Handler(); + } + + /* Several peripheral DMA handle pointers point to the same DMA handle. + Be aware that there is only one channel to perform all the requested DMAs. */ + __HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_CC1],hdma_tim16_ch1_up); + __HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_UPDATE],hdma_tim16_ch1_up); + + /* USER CODE BEGIN TIM16_MspInit 1 */ + + /* USER CODE END TIM16_MspInit 1 */ + } +} +void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(timHandle->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspPostInit 0 */ + + /* USER CODE END TIM2_MspPostInit 0 */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**TIM2 GPIO Configuration + PA15 ------> TIM2_CH1 + PB3 ------> TIM2_CH2 + */ + GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM2_MspPostInit 1 */ + + /* USER CODE END TIM2_MspPostInit 1 */ + } + else if(timHandle->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspPostInit 0 */ + + /* USER CODE END TIM3_MspPostInit 0 */ + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**TIM3 GPIO Configuration + PB4 ------> TIM3_CH1 + */ + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF1_TIM3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM3_MspPostInit 1 */ + + /* USER CODE END TIM3_MspPostInit 1 */ + } + else if(timHandle->Instance==TIM16) + { + /* USER CODE BEGIN TIM16_MspPostInit 0 */ + + /* USER CODE END TIM16_MspPostInit 0 */ + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**TIM16 GPIO Configuration + PB8 ------> TIM16_CH1 + */ + GPIO_InitStruct.Pin = GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM16; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM16_MspPostInit 1 */ + + /* USER CODE END TIM16_MspPostInit 1 */ + } + +} + +void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) +{ + + if(tim_baseHandle->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspDeInit 0 */ + + /* USER CODE END TIM2_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM2_CLK_DISABLE(); + /* USER CODE BEGIN TIM2_MspDeInit 1 */ + + /* USER CODE END TIM2_MspDeInit 1 */ + } + else if(tim_baseHandle->Instance==TIM3) + { + /* USER CODE BEGIN TIM3_MspDeInit 0 */ + + /* USER CODE END TIM3_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM3_CLK_DISABLE(); + /* USER CODE BEGIN TIM3_MspDeInit 1 */ + + /* USER CODE END TIM3_MspDeInit 1 */ + } + else if(tim_baseHandle->Instance==TIM6) + { + /* USER CODE BEGIN TIM6_MspDeInit 0 */ + + /* USER CODE END TIM6_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM6_CLK_DISABLE(); + /* USER CODE BEGIN TIM6_MspDeInit 1 */ + + /* USER CODE END TIM6_MspDeInit 1 */ + } + else if(tim_baseHandle->Instance==TIM16) + { + /* USER CODE BEGIN TIM16_MspDeInit 0 */ + + /* USER CODE END TIM16_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM16_CLK_DISABLE(); + + /* TIM16 DMA DeInit */ + HAL_DMA_DeInit(tim_baseHandle->hdma[TIM_DMA_ID_CC1]); + HAL_DMA_DeInit(tim_baseHandle->hdma[TIM_DMA_ID_UPDATE]); + /* USER CODE BEGIN TIM16_MspDeInit 1 */ + + /* USER CODE END TIM16_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/usart.c b/Core/Src/usart.c new file mode 100644 index 0000000..ed3d29d --- /dev/null +++ b/Core/Src/usart.c @@ -0,0 +1,160 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file usart.c + * @brief This file provides code for the configuration + * of the USART instances. + ****************************************************************************** + * @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 "usart.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +UART_HandleTypeDef huart1; +DMA_HandleTypeDef hdma_usart1_rx; +DMA_HandleTypeDef hdma_usart1_tx; + +/* USART1 init function */ + +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 = 115200; + 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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + if (HAL_HalfDuplex_Init(&huart1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN USART1_Init 2 */ + + /* USER CODE END USART1_Init 2 */ + +} + +void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(uartHandle->Instance==USART1) + { + /* USER CODE BEGIN USART1_MspInit 0 */ + + /* USER CODE END USART1_MspInit 0 */ + /* USART1 clock enable */ + __HAL_RCC_USART1_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**USART1 GPIO Configuration + PA9 ------> USART1_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF1_USART1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USART1 DMA Init */ + /* USART1_RX Init */ + hdma_usart1_rx.Instance = DMA1_Channel5; + hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE; + hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_usart1_rx.Init.Mode = DMA_NORMAL; + hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_DMA_REMAP_CHANNEL_ENABLE(DMA_REMAP_USART1_RX_DMA_CH5); + + __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx); + + /* USART1_TX Init */ + hdma_usart1_tx.Instance = DMA1_Channel2; + hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE; + hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_usart1_tx.Init.Mode = DMA_NORMAL; + hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx); + + /* USART1 interrupt Init */ + HAL_NVIC_SetPriority(USART1_IRQn, 1, 0); + HAL_NVIC_EnableIRQ(USART1_IRQn); + /* USER CODE BEGIN USART1_MspInit 1 */ + + /* USER CODE END USART1_MspInit 1 */ + } +} + +void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) +{ + + if(uartHandle->Instance==USART1) + { + /* USER CODE BEGIN USART1_MspDeInit 0 */ + + /* USER CODE END USART1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_USART1_CLK_DISABLE(); + + /**USART1 GPIO Configuration + PA9 ------> USART1_TX + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9); + + /* USART1 DMA DeInit */ + HAL_DMA_DeInit(uartHandle->hdmarx); + HAL_DMA_DeInit(uartHandle->hdmatx); + + /* USART1 interrupt Deinit */ + HAL_NVIC_DisableIRQ(USART1_IRQn); + /* USER CODE BEGIN USART1_MspDeInit 1 */ + + /* USER CODE END USART1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Startup/startup_stm32f051k8ux.s b/Core/Startup/startup_stm32f051k8ux.s new file mode 100644 index 0000000..ec04c00 --- /dev/null +++ b/Core/Startup/startup_stm32f051k8ux.s @@ -0,0 +1,285 @@ +/** + ****************************************************************************** + * @file startup_stm32f051x8.s + * @author MCD Application Team + * @brief STM32F051x4/STM32F051x6/STM32F051x8 devices vector table for GCC toolchain. + * This module performs: + * - Set the initial SP + * - Set the initial PC == Reset_Handler, + * - Set the vector table entries with the exceptions ISR address + * - Branches to main in the C library (which eventually + * calls main()). + * After Reset the Cortex-M0 processor is in Thread mode, + * priority is Privileged, and the Stack is set to Main. + ****************************************************************************** + * @attention + * + * Copyright (c) 2016 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. + * + ****************************************************************************** + */ + + .syntax unified + .cpu cortex-m0 + .fpu softvfp + .thumb + +.global g_pfnVectors +.global Default_Handler + +/* start address for the initialization values of the .data section. +defined in linker script */ +.word _sidata +/* start address for the .data section. defined in linker script */ +.word _sdata +/* end address for the .data section. defined in linker script */ +.word _edata +/* start address for the .bss section. defined in linker script */ +.word _sbss +/* end address for the .bss section. defined in linker script */ +.word _ebss + + .section .text.Reset_Handler + .weak Reset_Handler + .type Reset_Handler, %function +Reset_Handler: + ldr r0, =_estack + mov sp, r0 /* set stack pointer */ + + /* Call the clock system initialization function.*/ + bl SystemInit + +/* Copy the data segment initializers from flash to SRAM */ + ldr r0, =_sdata + ldr r1, =_edata + ldr r2, =_sidata + movs r3, #0 + b LoopCopyDataInit + +CopyDataInit: + ldr r4, [r2, r3] + str r4, [r0, r3] + adds r3, r3, #4 + +LoopCopyDataInit: + adds r4, r0, r3 + cmp r4, r1 + bcc CopyDataInit + +/* Zero fill the bss segment. */ + ldr r2, =_sbss + ldr r4, =_ebss + movs r3, #0 + b LoopFillZerobss + +FillZerobss: + str r3, [r2] + adds r2, r2, #4 + +LoopFillZerobss: + cmp r2, r4 + bcc FillZerobss + +/* Call static constructors */ + bl __libc_init_array +/* Call the application's entry point.*/ + bl main + +LoopForever: + b LoopForever + + +.size Reset_Handler, .-Reset_Handler + +/** + * @brief This is the code that gets called when the processor receives an + * unexpected interrupt. This simply enters an infinite loop, preserving + * the system state for examination by a debugger. + * + * @param None + * @retval : None +*/ + .section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop + .size Default_Handler, .-Default_Handler +/****************************************************************************** +* +* The minimal vector table for a Cortex M0. Note that the proper constructs +* must be placed on this to ensure that it ends up at physical address +* 0x0000.0000. +* +******************************************************************************/ + .section .isr_vector,"a",%progbits + .type g_pfnVectors, %object + .size g_pfnVectors, .-g_pfnVectors + + +g_pfnVectors: + .word _estack + .word Reset_Handler + .word NMI_Handler + .word HardFault_Handler + .word 0 + .word 0 + .word 0 + .word 0 + .word 0 + .word 0 + .word 0 + .word SVC_Handler + .word 0 + .word 0 + .word PendSV_Handler + .word SysTick_Handler + .word WWDG_IRQHandler /* Window WatchDog */ + .word PVD_IRQHandler /* PVD through EXTI Line detect */ + .word RTC_IRQHandler /* RTC through the EXTI line */ + .word FLASH_IRQHandler /* FLASH */ + .word RCC_CRS_IRQHandler /* RCC and CRS */ + .word EXTI0_1_IRQHandler /* EXTI Line 0 and 1 */ + .word EXTI2_3_IRQHandler /* EXTI Line 2 and 3 */ + .word EXTI4_15_IRQHandler /* EXTI Line 4 to 15 */ + .word TSC_IRQHandler /* TSC */ + .word DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */ + .word DMA1_Channel2_3_IRQHandler /* DMA1 Channel 2 and Channel 3 */ + .word DMA1_Channel4_5_IRQHandler /* DMA1 Channel 4 and Channel 5 */ + .word ADC1_COMP_IRQHandler /* ADC1, COMP1 and COMP2 */ + .word TIM1_BRK_UP_TRG_COM_IRQHandler /* TIM1 Break, Update, Trigger and Commutation */ + .word TIM1_CC_IRQHandler /* TIM1 Capture Compare */ + .word TIM2_IRQHandler /* TIM2 */ + .word TIM3_IRQHandler /* TIM3 */ + .word TIM6_DAC_IRQHandler /* TIM6 and DAC */ + .word 0 /* Reserved */ + .word TIM14_IRQHandler /* TIM14 */ + .word TIM15_IRQHandler /* TIM15 */ + .word TIM16_IRQHandler /* TIM16 */ + .word TIM17_IRQHandler /* TIM17 */ + .word I2C1_IRQHandler /* I2C1 */ + .word I2C2_IRQHandler /* I2C2 */ + .word SPI1_IRQHandler /* SPI1 */ + .word SPI2_IRQHandler /* SPI2 */ + .word USART1_IRQHandler /* USART1 */ + .word USART2_IRQHandler /* USART2 */ + .word 0 /* Reserved */ + .word CEC_CAN_IRQHandler /* CEC and CAN */ + .word 0 /* Reserved */ + +/******************************************************************************* +* +* Provide weak aliases for each Exception handler to the Default_Handler. +* As they are weak aliases, any function with the same name will override +* this definition. +* +*******************************************************************************/ + + .weak NMI_Handler + .thumb_set NMI_Handler,Default_Handler + + .weak HardFault_Handler + .thumb_set HardFault_Handler,Default_Handler + + .weak SVC_Handler + .thumb_set SVC_Handler,Default_Handler + + .weak PendSV_Handler + .thumb_set PendSV_Handler,Default_Handler + + .weak SysTick_Handler + .thumb_set SysTick_Handler,Default_Handler + + .weak WWDG_IRQHandler + .thumb_set WWDG_IRQHandler,Default_Handler + + .weak PVD_IRQHandler + .thumb_set PVD_IRQHandler,Default_Handler + + .weak RTC_IRQHandler + .thumb_set RTC_IRQHandler,Default_Handler + + .weak FLASH_IRQHandler + .thumb_set FLASH_IRQHandler,Default_Handler + + .weak RCC_CRS_IRQHandler + .thumb_set RCC_CRS_IRQHandler,Default_Handler + + .weak EXTI0_1_IRQHandler + .thumb_set EXTI0_1_IRQHandler,Default_Handler + + .weak EXTI2_3_IRQHandler + .thumb_set EXTI2_3_IRQHandler,Default_Handler + + .weak EXTI4_15_IRQHandler + .thumb_set EXTI4_15_IRQHandler,Default_Handler + + .weak TSC_IRQHandler + .thumb_set TSC_IRQHandler,Default_Handler + + .weak DMA1_Channel1_IRQHandler + .thumb_set DMA1_Channel1_IRQHandler,Default_Handler + + .weak DMA1_Channel2_3_IRQHandler + .thumb_set DMA1_Channel2_3_IRQHandler,Default_Handler + + .weak DMA1_Channel4_5_IRQHandler + .thumb_set DMA1_Channel4_5_IRQHandler,Default_Handler + + .weak ADC1_COMP_IRQHandler + .thumb_set ADC1_COMP_IRQHandler,Default_Handler + + .weak TIM1_BRK_UP_TRG_COM_IRQHandler + .thumb_set TIM1_BRK_UP_TRG_COM_IRQHandler,Default_Handler + + .weak TIM1_CC_IRQHandler + .thumb_set TIM1_CC_IRQHandler,Default_Handler + + .weak TIM2_IRQHandler + .thumb_set TIM2_IRQHandler,Default_Handler + + .weak TIM3_IRQHandler + .thumb_set TIM3_IRQHandler,Default_Handler + + .weak TIM6_DAC_IRQHandler + .thumb_set TIM6_DAC_IRQHandler,Default_Handler + + .weak TIM14_IRQHandler + .thumb_set TIM14_IRQHandler,Default_Handler + + .weak TIM15_IRQHandler + .thumb_set TIM15_IRQHandler,Default_Handler + + .weak TIM16_IRQHandler + .thumb_set TIM16_IRQHandler,Default_Handler + + .weak TIM17_IRQHandler + .thumb_set TIM17_IRQHandler,Default_Handler + + .weak I2C1_IRQHandler + .thumb_set I2C1_IRQHandler,Default_Handler + + .weak I2C2_IRQHandler + .thumb_set I2C2_IRQHandler,Default_Handler + + .weak SPI1_IRQHandler + .thumb_set SPI1_IRQHandler,Default_Handler + + .weak SPI2_IRQHandler + .thumb_set SPI2_IRQHandler,Default_Handler + + .weak USART1_IRQHandler + .thumb_set USART1_IRQHandler,Default_Handler + + .weak USART2_IRQHandler + .thumb_set USART2_IRQHandler,Default_Handler + + .weak CEC_CAN_IRQHandler + .thumb_set CEC_CAN_IRQHandler,Default_Handler + + + diff --git a/Driver/ESHL_driver.c b/Driver/ESHL_driver.c new file mode 100644 index 0000000..0ba4203 --- /dev/null +++ b/Driver/ESHL_driver.c @@ -0,0 +1,1115 @@ +// +// Created by E_LJF on 25-6-7. +// + +#include "ESHL_driver.h" +#include "tim.h" +#include "comp.h" +#include "adc.h" +#include "Internal_Flash.h" + + +#define ESHL_MAX_CURRENT_ADC_MAX 2191 //电调设计最大电流ADC值 + +uint16_t ESHL_Data[2] = {0}; //电调需要离线保存的数据,[0]为电调地址(默认EC00,可更改),[1]为电调运行电流限制ADC值,默认1158 +static uint8_t ESHL_step = 0; //六步换向步骤 +static uint16_t ESHL_run_pwm = ESHL_RUN_MIN_PWM;//电调运行pwm +ESHL_DIRECTION_ENUM_T ESHL_direction; //电调运行方向 +ESHL_STATE_ENUM_T ESHL_state; //电调状态 +ESHL_BAT_ENUM_T ESHL_BatType; //电调使用的电池类型 + +uint16_t adc_val_buff[5] = {0}; //ADC值缓存 +uint16_t ESHL_MOS_Current_ADC_Value = 0; //测得的MOS电流 +uint16_t ESHL_LeakageCurrent_ADC_Value = 0; //电调总漏电流 + + + +//us延时,需要传入一个指向我们所配置的定时器的指针变量(句柄) +void delay_us(TIM_HandleTypeDef *usTIMER,uint16_t us) +{ + __HAL_TIM_SET_COUNTER(usTIMER, 0); // 把TIMER的counter设为0 + while (__HAL_TIM_GET_COUNTER(usTIMER) < us); //读取定时器的counter值 +} +//由于定时器的频率是1MHz,所以其counter每次加1的时候,就表示过了1us +//每次调用时都把定时器的counter先置为0 + + +//关闭所有MOS管 +static inline void MOS_CloseAll() { + ESHL_AD_DISABLE();//关闭A相MOS管 + ESHL_AU_DISABLE(); + + ESHL_BU_DISABLE();//关闭B相MOS管 + ESHL_BD_DISABLE(); + + ESHL_CU_DISABLE();//关闭C相MOS管 + ESHL_CD_DISABLE(); + + delay_us(&ESHL_US_TIM,20); +} + + +static void ESHL_U_D_Ctrl(uint16_t pwm) +{ + switch (ESHL_step) { +//--------------正转------------- + case 0://BC MOS + ESHL_AU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_BD_DISABLE(); + + ESHL_BU_ENABLE(pwm); + ESHL_CD_ENABLE(); + break; + + case 1://BA MOS + ESHL_AU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_BD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_BU_ENABLE(pwm); + ESHL_AD_ENABLE(); + break; + + case 2://CA MOS + ESHL_AU_DISABLE(); + ESHL_BU_DISABLE(); + ESHL_BD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_CU_ENABLE(pwm); + ESHL_AD_ENABLE(); + break; + + case 3://CB MOS + ESHL_AU_DISABLE(); + ESHL_BU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_CU_ENABLE(pwm); + ESHL_BD_ENABLE(); + break; + + case 4://AB MOS + ESHL_BU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_AU_ENABLE(pwm); + ESHL_BD_ENABLE(); + break; + + case 5://AC MOS + ESHL_BU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_BD_DISABLE(); + + ESHL_AU_ENABLE(pwm); + ESHL_CD_ENABLE(); + break; + + +//--------------反转------------- + case 6://AC MOS + ESHL_BU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_BD_DISABLE(); + + ESHL_AU_ENABLE(pwm); + ESHL_CD_ENABLE(); + break; + + case 7://AB MOS + ESHL_BU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_AU_ENABLE(pwm); + ESHL_BD_ENABLE(); + break; + + case 8://CB MOS + ESHL_AU_DISABLE(); + ESHL_BU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_CU_ENABLE(pwm); + ESHL_BD_ENABLE(); + break; + + case 9://CA MOS + ESHL_AU_DISABLE(); + ESHL_BU_DISABLE(); + ESHL_BD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_CU_ENABLE(pwm); + ESHL_AD_ENABLE(); + break; + + case 10://BA MOS + ESHL_AU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_BD_DISABLE(); + ESHL_CD_DISABLE(); + + ESHL_BU_ENABLE(pwm); + ESHL_AD_ENABLE(); + break; + + case 11://BC MOS + ESHL_AU_DISABLE(); + ESHL_CU_DISABLE(); + ESHL_AD_DISABLE(); + ESHL_BD_DISABLE(); + + ESHL_BU_ENABLE(pwm); + ESHL_CD_ENABLE(); + break; + + default: + break; + } +} + + +//电调初始化 +void ESHL_ESC_Init() +{ + + uint16_t read_buff[2] = {0}; + float Vbat = 0; + + ESHL_AD_DISABLE();//关闭A相MOS管 + ESHL_AU_DISABLE(); + + ESHL_BU_DISABLE();//关闭B相MOS管 + ESHL_BD_DISABLE(); + + ESHL_CU_DISABLE();//关闭C相MOS管 + ESHL_CD_DISABLE(); + + ESHL_state = ESHL_STATE_OFF; + ESHL_direction = ESHL_CLOCKWISE; + + HAL_TIM_PWM_Start(&ESHL_A_TIM,ESHL_A_TIM_CH);//开启MOS上管定时器 + HAL_TIM_PWM_Start(&ESHL_B_TIM,ESHL_B_TIM_CH); + HAL_TIM_PWM_Start(&ESHL_C_TIM,ESHL_C_TIM_CH); + + HAL_TIM_Base_Start(&ESHL_US_TIM);//开启us延时定时器 + + HAL_ADCEx_Calibration_Start(&ESHL_Current_ADC);//ADC校准 + + for (uint8_t i = 0; i < 10; i++) { + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2);//获取电压ADC值 + Vbat += (float)adc_val_buff[1]; + } + + Vbat = (Vbat/10) * 3.3f / 4096 * 10;//ADC值转为电压 + Vbat += 2.18f; //加是因为ADC不准,有偏差 + + if (Vbat > 22.2f && Vbat < 25.2f) { + ESHL_BatType = ESHL_BAT_6S; + }else if (Vbat > 18.5f && Vbat < 21.0f) { + ESHL_BatType = ESHL_BAT_5S; + }else if (Vbat > 14.8f && Vbat < 16.8f) { + ESHL_BatType = ESHL_BAT_4S; + }else if (Vbat > 11.1f && Vbat < 12.6f) { + ESHL_BatType = ESHL_BAT_3S; + }else if (Vbat > 7.4f && Vbat < 8.4f) { + ESHL_BatType = ESHL_BAT_2S; + }else if (Vbat > 3.7f && Vbat < 4.2f) { + ESHL_BatType = ESHL_BAT_1S; + }else { + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR; + } + + + InternalFLASH_ReadMore16(ESHL_ADDR_FLASH_ADD,read_buff,2); + if ((read_buff[0] >> 8) != 0xEC) {//如果没有存储地址 + ESHL_Data[0] = ESHL_DEFALT_ADDR;//默认地址 + ESHL_Data[1] = ESHL_RUN_Current_Limit_ADC_DEFALT; //默认限制电流ADC值 + InternalFlashWriteMoreUint_16(ESHL_ADDR_FLASH_ADD,ESHL_Data,2);//写入内部Flash + } + else {//已经储存地址 + ESHL_Data[0] = read_buff[0];//复制电调地址 + ESHL_Data[1] = read_buff[1];//复制限制电流ADC值 + } + +} + + +//电调开环启动 +void ESHL_Start(ESHL_DIRECTION_ENUM_T direction) +{ + static uint16_t time = 100; + uint8_t BMEF_num = 0;//开环启动时累计过零事件数量 + ESHL_direction = direction; + ESHL_run_pwm = ESHL_START_PWM; + + HAL_COMP_Stop_IT(&ESHL_COMP);//关闭比较器中断 + HAL_COMP_Start(&ESHL_COMP); //打开比较器 + MOS_CloseAll(); + + ESHL_state = ESHL_STATE_OPEN_LOOP_START;//电调状态更新为开环启动状态 + ESHL_step = (ESHL_direction == ESHL_CLOCKWISE) ? 0 : 6; + + ESHL_U_D_Ctrl(19);//转子定位 + for (uint8_t i = 0; i < 200; i++){ + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + if (adc_val_buff[0] >= ESHL_RotoCurrent_ADC_MAX) { + MOS_CloseAll(); + HAL_COMP_Stop(&ESHL_COMP); + ESHL_run_pwm = 0; + ESHL_state = ESHL_STATE_CURRENT_ERROR;//电调状态更新为电流异常 + return ; + } + HAL_Delay(1); + } + + while (1) + { + for (uint16_t i = 0; i < time; ++i) { + delay_us(&ESHL_US_TIM,80); + } + + if (time < 25) + { + time = 100; + + MOS_CloseAll(); + if ((BMEF_num >= 30) && (BMEF_num <= 35))//开环启动成功 + { + HAL_COMP_Stop(&ESHL_COMP); + HAL_COMP_Start_IT(&ESHL_COMP); + ESHL_state = (ESHL_direction == ESHL_CLOCKWISE) ? ESHL_STATE_RUN_CLOCKWISE : ESHL_STATE_RUN_COUNTER_CLOCKWISE;//电调状态更新为对应方向运动状态 + } + else { //开环启动失败 + HAL_COMP_Stop(&ESHL_COMP); + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + ESHL_state = ESHL_STATE_OPEN_LOOP_START_FAIL;//电调状态更新为开环启动失败 + } + break; + } + + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2);//检测开环过渡期电流值 + if (adc_val_buff[0] >= ESHL_OPEN_LOOP_Transition_Period_ADC_MAX) { + HAL_COMP_Stop(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + ESHL_state = ESHL_STATE_CURRENT_ERROR;//电调状态更新为电流异常 + return ; + } + + time -= time/85+1; + ESHL_step++; + + if(ESHL_direction == ESHL_COUNTER_CLOCKWISE) + { + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + }else + { + ESHL_step %= 6; + } + ESHL_U_D_Ctrl(ESHL_run_pwm); + + if (SENSE_H) { + BMEF_num ++; + } + + } +} + + +void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) +{ + uint8_t sense = 0; + __disable_irq(); + do + { + if(SENSE_H) sense = 1; else sense = 0; + switch(ESHL_step) + { +//--------------正转------------- + case 0: + if(sense) + { + ESHL_step++; + ESHL_step %= 6; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x05 << 4U);//下一步检测C相电动势,切换比较器输入端为PA5 + CLEAR_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2);//下降沿触发 + SET_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 1: + if(!sense) + { + + ESHL_step++; + ESHL_step %= 6; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x04 << 4U);//下一步检测B相电动势,切换比较器输入端为PA4 + SET_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2); //上升沿触发 + CLEAR_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 2: + if(sense) + { + + ESHL_step++; + ESHL_step %= 6; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x06 << 4U);//下一步检测A相电动势,切换比较器输入端为PA2 + CLEAR_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2);//下降沿触发 + SET_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 3: + if(!sense) + { + + + ESHL_step++; + ESHL_step %= 6; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x05 << 4U);//下一步检测C相电动势,切换比较器输入端为PA5 + SET_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2); //上升沿触发 + CLEAR_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 4: + if(sense) + { + + + ESHL_step++; + ESHL_step %= 6; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x04 << 4U);//下一步检测B相电动势,切换比较器输入端为PA4 + CLEAR_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2);//下降沿触发 + SET_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 5: + if(!sense) + { + + ESHL_step++; + ESHL_step %= 6; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x06 << 4U);//下一步检测A相电动势,切换比较器输入端为PA2 + SET_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2); //上升沿触发 + CLEAR_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + +//--------------反转------------- + + case 6: + if(sense) + { + + + ESHL_step++; + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x05 << 4U);//下一步检测C相电动势,切换比较器输入端为PA5 + CLEAR_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2);//下降沿触发 + SET_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 7: + if(!sense) + { + + ESHL_step++; + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x06 << 4U);//下一步检测A相电动势,切换比较器输入端为PA2 + SET_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2); //上升沿触发 + CLEAR_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 8: + if(sense) + { + + + ESHL_step++; + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x04 << 4U);//下一步检测B相电动势,切换比较器输入端为PA4 + CLEAR_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2);//下降沿触发 + SET_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 9: + if(!sense) + { + + ESHL_step++; + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x05 << 4U);//下一步检测C相电动势,切换比较器输入端为PA5 + SET_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2); //上升沿触发 + CLEAR_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 10: + if(sense) + { + + + ESHL_step++; + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x06 << 4U);//下一步检测A相电动势,切换比较器输入端为PA2 + CLEAR_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2);//下降沿触发 + SET_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + case 11: + if(!sense) + { + + + ESHL_step++; + ESHL_step = (ESHL_step > 11) ? 6 : ESHL_step; + ESHL_U_D_Ctrl(ESHL_run_pwm); + + ESHL_COMP.Instance->CSR = (ESHL_COMP.Instance->CSR & ~(0x7 << 4U)) | (0x04 << 4U);//下一步检测B相电动势,切换比较器输入端为PA4 + SET_BIT(EXTI->RTSR, COMP_EXTI_LINE_COMP2); //上升沿触发 + CLEAR_BIT(EXTI->FTSR, COMP_EXTI_LINE_COMP2); + } + else + { + ESHL_U_D_Ctrl(ESHL_run_pwm); + } + break; + + default: + MOS_CloseAll(); + break; + } +} while((SENSE_L && sense) || (SENSE_H && !sense)); + + __enable_irq(); +} + + + + +//ESHL_Beep(ESHL_BEEP_SHORT);//E +// +// ESHL_Beep(ESHL_BEEP_SHORT);//S +// ESHL_Beep(ESHL_BEEP_SHORT); +// ESHL_Beep(ESHL_BEEP_SHORT); +// +// ESHL_Beep(ESHL_BEEP_SHORT);//H +// ESHL_Beep(ESHL_BEEP_SHORT); +// ESHL_Beep(ESHL_BEEP_SHORT); +// ESHL_Beep(ESHL_BEEP_SHORT); +// +// ESHL_Beep(ESHL_BEEP_SHORT);//L +// ESHL_Beep(ESHL_BEEP_LONG); +// ESHL_Beep(ESHL_BEEP_SHORT); +// ESHL_Beep(ESHL_BEEP_SHORT); + + + + +//电机发声函数 +void ESHL_Beep(ESHL_BEEP_ENUM_T beep) +{ + if (ESHL_state == ESHL_STATE_OFF) { + MOS_CloseAll(); + + ESHL_step = 0; + + switch (beep) { + case ESHL_BEEP_SHORT: + for (uint16_t i = 0; i < 250; i++) { + ESHL_step = 0; + ESHL_U_D_Ctrl(100); + delay_us(&ESHL_US_TIM, 20); + ESHL_step = 1; + ESHL_U_D_Ctrl(100); + delay_us(&ESHL_US_TIM, 180); + } + break; + + case ESHL_BEEP_LONG: + for (uint16_t i = 0; i < 500; i++) { + ESHL_step = 0; + ESHL_U_D_Ctrl(100); + delay_us(&ESHL_US_TIM, 50); + ESHL_step = 1; + ESHL_U_D_Ctrl(100); + delay_us(&ESHL_US_TIM, 180); + } + break; + + // case ESHL_BEEP_DOU: + // for(uint16_t i = 0;i < 50;i++) + // { + // ESHL_step = 0; + // ESHL_U_D_Ctrl(100); + // delay_us(&ESHL_US_TIM,100); + // ESHL_step = 1; + // ESHL_U_D_Ctrl(100); + // delay_us(&ESHL_US_TIM,500); + // } + // break; + + default: + break; + } + + ESHL_U_D_Ctrl(0); + + MOS_CloseAll(); + HAL_Delay(20); + } +} + + +//设置电调运行PWM值 +void ESHL_SET_PWM(uint16_t pwm) { + if (ESHL_state == ESHL_STATE_RUN_CLOCKWISE || ESHL_state == ESHL_STATE_RUN_COUNTER_CLOCKWISE) { + pwm = (pwm <= ESHL_RUN_MAX_PWM) ? pwm : ESHL_RUN_MAX_PWM;//限制最大值 + pwm = (pwm >= ESHL_RUN_MIN_PWM) ? pwm : ESHL_RUN_MIN_PWM;//限制最小值 + + if (pwm > ESHL_run_pwm) { + ESHL_run_pwm += ESHL_RUN_PWM_STEP; + ESHL_run_pwm = (ESHL_run_pwm <= 991) ? ESHL_run_pwm : 991; + } + else if (pwm < ESHL_run_pwm) { + if (ESHL_run_pwm <= ESHL_RUN_PWM_STEP) { + ESHL_run_pwm = 0; + } + else { + ESHL_run_pwm -= ESHL_RUN_PWM_STEP; + } + } + else if (ESHL_run_pwm == pwm){ + ESHL_run_pwm = pwm; + } + } +} + + + +//检测MOS管是否短路 +//返回问题MOS管编号,0为无异常,10为未知错误 +// 12为漏电流过大 +//MOS管编号如下: +// 1(A+) 3(B+) 5(C+) +// 2(A-) 4(B-) 6(C-) +uint8_t MOS_SelfTest() { + + uint8_t error_mos_id = 0; //异常MOS id + uint8_t step = 0; //电流检测步骤 + uint8_t flag = 1; //电流检测标志 + + HAL_COMP_Stop_IT(&ESHL_COMP);//关闭比较器 + MOS_CloseAll();//关闭所有MOS管 + + + while (flag){ + switch (step) { + + case 0://测总漏电流 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测总漏电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_LeakageCurrent_ADC_Value = (adc_val_buff[0] > ESHL_LeakageCurrent_ADC_Value) ? adc_val_buff[0] : ESHL_LeakageCurrent_ADC_Value; + } + if (ESHL_LeakageCurrent_ADC_Value >= 6) {//大于200ma触发 + ESHL_state = ESHL_STATE_CURRENT_ERROR; + error_mos_id = 12; + flag = 0; + }else { + step = 1; + } + break; + + + case 1://测A相上管电流 + ESHL_AD_ENABLE();//打开A相下管 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测A相上管电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_MOS_Current_ADC_Value = (adc_val_buff[0] > ESHL_MOS_Current_ADC_Value) ? adc_val_buff[0] : ESHL_MOS_Current_ADC_Value; + } + if (ESHL_MOS_Current_ADC_Value > ESHL_MOS_Current_ADC_MAX) { + MOS_CloseAll();//关闭所有MOS管 + error_mos_id = 1; + ESHL_state = ESHL_STATE_MOS_ERROR; + flag = 0; + } + else{ + MOS_CloseAll(); //关闭所有MOS管 + ESHL_MOS_Current_ADC_Value = 0; + step = 2; + } + break; + + + case 2://测A相下管电流 + ESHL_AU_ENABLE(ESHL_MOS_TestPWM);//打开A相上管 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测A相下管电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_MOS_Current_ADC_Value = (adc_val_buff[0] > ESHL_MOS_Current_ADC_Value) ? adc_val_buff[0] : ESHL_MOS_Current_ADC_Value; + } + if (ESHL_MOS_Current_ADC_Value > ESHL_MOS_Current_ADC_MAX) { + MOS_CloseAll();//关闭所有MOS管 + error_mos_id = 2; + ESHL_state = ESHL_STATE_MOS_ERROR; + flag = 0; + } + else{ + MOS_CloseAll(); //关闭所有MOS管 + ESHL_MOS_Current_ADC_Value = 0; + step = 3; + } + break; + + + case 3://测B相上管电流 + ESHL_BD_ENABLE();//打开B相下管 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测B相上管电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_MOS_Current_ADC_Value = (adc_val_buff[0] > ESHL_MOS_Current_ADC_Value) ? adc_val_buff[0] : ESHL_MOS_Current_ADC_Value; + } + if (ESHL_MOS_Current_ADC_Value > ESHL_MOS_Current_ADC_MAX) { + MOS_CloseAll();//关闭所有MOS管 + error_mos_id = 3; + ESHL_state = ESHL_STATE_MOS_ERROR; + flag = 0; + } + else{ + MOS_CloseAll(); //关闭所有MOS管 + ESHL_MOS_Current_ADC_Value = 0; + step = 4; + } + break; + + + case 4: + ESHL_BU_ENABLE(ESHL_MOS_TestPWM);//打开B相上管 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测B相下管电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_MOS_Current_ADC_Value = (adc_val_buff[0] > ESHL_MOS_Current_ADC_Value) ? adc_val_buff[0] : ESHL_MOS_Current_ADC_Value; + } + if (ESHL_MOS_Current_ADC_Value > ESHL_MOS_Current_ADC_MAX) { + MOS_CloseAll();//关闭所有MOS管 + error_mos_id = 4; + ESHL_state = ESHL_STATE_MOS_ERROR; + flag = 0; + } + else{ + MOS_CloseAll(); //关闭所有MOS管 + ESHL_MOS_Current_ADC_Value = 0; + step = 5; + } + break; + + + case 5: + ESHL_CD_ENABLE();//打开C相下管 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测C相上管电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_MOS_Current_ADC_Value = (adc_val_buff[0] > ESHL_MOS_Current_ADC_Value) ? adc_val_buff[0] : ESHL_MOS_Current_ADC_Value; + } + if (ESHL_MOS_Current_ADC_Value > ESHL_MOS_Current_ADC_MAX) { + MOS_CloseAll();//关闭所有MOS管 + error_mos_id = 5; + ESHL_state = ESHL_STATE_MOS_ERROR; + flag = 0; + } + else{ + MOS_CloseAll(); //关闭所有MOS管 + ESHL_MOS_Current_ADC_Value = 0; + step = 6; + } + break; + + + + case 6: + ESHL_CU_ENABLE(ESHL_MOS_TestPWM);//打开C相上管 + for (uint8_t i = 0; i < ESHL_MOS_Current_Test_num; i++) { //测C相下管电流 + HAL_ADC_Start_DMA(&ESHL_Current_ADC,adc_val_buff,2); + ESHL_MOS_Current_ADC_Value = (adc_val_buff[0] > ESHL_MOS_Current_ADC_Value) ? adc_val_buff[0] : ESHL_MOS_Current_ADC_Value; + } + if (ESHL_MOS_Current_ADC_Value > ESHL_MOS_Current_ADC_MAX) { + MOS_CloseAll();//关闭所有MOS管 + error_mos_id = 6; + ESHL_state = ESHL_STATE_MOS_ERROR; + flag = 0; + } + else{ + MOS_CloseAll();//关闭所有MOS管 + ESHL_MOS_Current_ADC_Value = 0; + // step = 6; + flag = 0; + } + break; + + + default: + MOS_CloseAll(); + error_mos_id = 10; + break; + } + } + return error_mos_id; +} + + + +//电机运行过程中检测电机是否停转 +void ESHL_RuningChack() { + static uint32_t last_tick = 0; + static uint8_t last_step = 0; + if ((ESHL_state == ESHL_STATE_RUN_CLOCKWISE) || (ESHL_state == ESHL_STATE_RUN_COUNTER_CLOCKWISE) /*|| (ESHL_state == ESHL_STATE_MOTOR_RUNING_STOP)*/) { + if (ESHL_step != last_step) { + last_step = ESHL_step; + last_tick = HAL_GetTick(); + } + else if (HAL_GetTick() - last_tick >= ESHL_MOTOR_TIMEOUT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + last_step = 0; + ESHL_state = ESHL_STATE_MOTOR_RUNING_STOP;//电调状态更新为电机运行时停转 + } + } +} + + +//电机运行时定时检测电流和电池电压 +void ESHL_RuningCurrentVBATChack() { + + static uint32_t last_tick = 0; + +#if ESHL_VBAT_CHACK_EN + float Vbat = 0; +#endif + + + if (HAL_GetTick() - last_tick >= ESHL_RUNING_CURRENT_VBAT_CHACK_TIMOUT) { + HAL_ADC_Start_DMA(&ESHL_Current_ADC, adc_val_buff, 2);//检测电调电流 + last_tick = HAL_GetTick(); + } + + if (adc_val_buff[0] >= ESHL_Data[1]) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_CURRENT_ERROR;//电调状态更新为电流异常 + } + +#if ESHL_VBAT_CHACK_EN + Vbat = (float)adc_val_buff[1] * 3.3f / 4096 * 10 + 2.18f;//ADC值转为电压,加是因为ADC不准,有偏差 + Vbat += 1.1f; //加1.1是因为ADC不准,有偏差 + + switch (ESHL_BatType) { + + case ESHL_BAT_1S: + if ((Vbat - 3.7f) <= ESHL_VBAT_LIMIT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR;//电调状态更新为电池电压异常 + } + break; + + case ESHL_BAT_2S: + if ((Vbat - 7.4f) <= ESHL_VBAT_LIMIT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR;//电调状态更新为电池电压异常 + } + break; + + case ESHL_BAT_3S: + if ((Vbat - 11.1f) <= ESHL_VBAT_LIMIT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR;//电调状态更新为电池电压异常 + } + break; + + case ESHL_BAT_4S: + if ((Vbat - 14.8f) <= ESHL_VBAT_LIMIT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR;//电调状态更新为电池电压异常 + } + break; + + case ESHL_BAT_5S: + if ((Vbat - 18.5f) <= ESHL_VBAT_LIMIT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR;//电调状态更新为电池电压异常 + } + break; + + case ESHL_BAT_6S: + if ((Vbat - 22.2f) <= ESHL_VBAT_LIMIT) { + HAL_COMP_Stop_IT(&ESHL_COMP); + MOS_CloseAll(); + ESHL_run_pwm = 0; + last_tick = 0; + ESHL_state = ESHL_STATE_BATTERY_VOLTAGE_ERROR;//电调状态更新为电池电压异常 + } + break; + + default: + break; + } +#endif +} + + +//更改电调运行限制电流 +void ESHL_ChangeRuningCurrentLimit(uint16_t current) { + ESHL_Data[1] = (current >= ESHL_MAX_CURRENT_ADC_MAX) ? ESHL_MAX_CURRENT_ADC_MAX : current; + InternalFlashWriteMoreUint_16(ESHL_ADDR_FLASH_ADD,ESHL_Data,2);//限制电流ADC值写入内部Flash +} + + +//外部电调状态设置接口 +void ESHL_SetState(ESHL_STATE_ENUM_T state) { + ESHL_state = state; +} + + +//获取电调当前状态 +ESHL_STATE_ENUM_T ESHL_GetState() { + return ESHL_state; +} + + +//电调刹车 +void ESHL_Break() { + + static uint32_t BMEF_NUM = 0;//过零事件计数 + static uint32_t BMEF_NUM_Last = 0;//上一次过零事件计数 + static uint16_t Same_BMEF_NUM = 0;//过零事件计数相同的次数 + + if (ESHL_state == ESHL_STATE_BRAKE) { + ESHL_run_pwm = 0; + MOS_CloseAll(); + + HAL_COMP_Start(&ESHL_COMP); + + if (SENSE_H) { + BMEF_NUM ++; + } + + if ((BMEF_NUM - BMEF_NUM_Last) != 0) {//电机还没停转 + BMEF_NUM_Last = BMEF_NUM; + }else if (BMEF_NUM - BMEF_NUM_Last == 0) { + Same_BMEF_NUM ++; + } + + if (Same_BMEF_NUM >= ESHL_BREAK_OK_SAME_BMEF_NUM) {//判定为电机停转 + MOS_CloseAll(); + HAL_COMP_Stop(&ESHL_COMP); + ESHL_state = ESHL_STATE_BRAKE_OK;//更新状态为刹车成功 + BMEF_NUM = 0; + BMEF_NUM_Last = 0; + Same_BMEF_NUM = 0; + return;//退出函数 + } + +#if ESHL_BREAK_MOD == 1//启用三相短路刹车 + ESHL_AU_ENABLE(ESHL_BREAK_PWM); + ESHL_BU_ENABLE(ESHL_BREAK_PWM); + ESHL_CU_ENABLE(ESHL_BREAK_PWM); + + for (uint8_t i = 0;i < 25; i++) { + HAL_ADC_Start_DMA(&ESHL_Current_ADC, adc_val_buff, 2); //检测电调电流 + if (adc_val_buff[0] > ESHL_Data[1]) {//过流保护 + MOS_CloseAll(); + break; + } + } +#endif + } +} + + +//获取电调地址 +uint16_t ESHL_GetAddr() { + return ESHL_Data[0]; +} + + +//设置电调地址 +//addr为接收到的地址,不是要设置的地址 +/*主机用广播地址发送更改地址命令,更改地址时,所有已连接的电调地址都需要重新设置 +地址设置方法: 快速旋转已经连接上电调的电机,此时电调将自动将自身地址设为EC00 +同时电调状态指示灯会闪烁,闪烁次数代表电调地址 +此时旋转第二个电调的电机,此时电调将自动将自身地址设为EC01,以此类推,直到所有电调地址设置完毕 +电调设置地址后需要重新上电才能使用*/ +void ESHL_SetAddr(uint16_t addr) { + + static uint16_t max_addr = 0;//当前最大地址 + static uint8_t BMEF_Num = 0;//过零事件计数 + + HAL_COMP_Start(&ESHL_COMP); + + if (SENSE_H) { + BMEF_Num ++; + } + + if (((addr >> 8) != 0xEC) && (addr != 0x0000)) {//输入处理 + BMEF_Num = 0; + return; + } + if ((addr & 0xff) > (max_addr & 0xff)) { + max_addr = addr;//更新地址最大值 + } + + if (BMEF_Num >= 12) { + if (((max_addr & 0xff) + 1) >= 0xFF) {//达到最大地址 + ESHL_Data[0] = 0xECFF;//最大地址 + } + else if (addr == 0x0000) { + ESHL_Data[0] = 0xEC01;//第一个设置地址的电调 + } + else { + ESHL_Data[0] = (max_addr + 1); + } + InternalFlashWriteMoreUint_16(ESHL_ADDR_FLASH_ADD,ESHL_Data,2);//电调地址写入内部Flash + ESHL_state = ESHL_STATE_SET_ADDR_OK;//更新电调状态 + HAL_COMP_Stop(&ESHL_COMP); + max_addr = 0; + BMEF_Num = 0; + } +} + + +//更改电调运行方向 +void ESHL_SetDirection(ESHL_DIRECTION_ENUM_T direction) { + if ((ESHL_state != ESHL_STATE_RUN_CLOCKWISE) && (ESHL_state != ESHL_STATE_RUN_COUNTER_CLOCKWISE)) {//电调非运行状态才能更改方向 + ESHL_direction = direction; + } +} + + +//获取电调当前运行方向外部接口 +ESHL_DIRECTION_ENUM_T ESHL_GetDirection() { + return ESHL_direction; +} + + +//关闭电调,可被通信唤醒 +// void ESHL_OFF() { +// ESHL_run_pwm = 0; +// HAL_COMP_Stop_IT(&ESHL_COMP); +// HAL_COMP_Stop(&ESHL_COMP); +// MOS_CloseAll(); +// ESHL_state = ESHL_STATE_OFF; +// } + + +//关闭所有MOS管并且关闭比较器 +void ESHL_CloseMOSComp() { + ESHL_run_pwm = 0; + HAL_COMP_Stop_IT(&ESHL_COMP); + HAL_COMP_Stop(&ESHL_COMP); + MOS_CloseAll(); +} + + diff --git a/Driver/ESHL_driver.h b/Driver/ESHL_driver.h new file mode 100644 index 0000000..c242eb3 --- /dev/null +++ b/Driver/ESHL_driver.h @@ -0,0 +1,170 @@ +// +// Created by E_LJF on 25-6-7. +// + +#ifndef ESHL_DRIVER_H +#define ESHL_DRIVER_H + +#include "main.h" + +#define ESHL_COMP hcomp2 //电调使用的比较器 + +#define ESHL_Current_ADC hadc //MOS电流计使用的ADC + +#define ESHL_US_TIM htim6 //电调us定时器 + +#define ESHL_A_TIM htim3 //电调A相定时器 +#define ESHL_A_TIM_CH TIM_CHANNEL_1 //电调A相定时器通道 + +#define ESHL_B_TIM htim2 //电调B相定时器 +#define ESHL_B_TIM_CH TIM_CHANNEL_2 //电调B相定时器通道 + +#define ESHL_C_TIM htim2 //电调C相定时器 +#define ESHL_C_TIM_CH TIM_CHANNEL_1 //电调A相定时器通道 + +#define SENSE_L (!(hcomp2.Instance->CSR & 0x4000)) +#define SENSE_H ((hcomp2.Instance->CSR & 0x4000)) //比较器输出值,通过寄存器获得 + +//电调A相上管配置 +#define ESHL_AU_ENABLE(pwm) __HAL_TIM_SetCompare(&ESHL_A_TIM,ESHL_A_TIM_CH,pwm) +#define ESHL_AU_DISABLE() __HAL_TIM_SetCompare(&ESHL_A_TIM,ESHL_A_TIM_CH,0) +//电调A相下管配置 +#define ESHL_AD_DISABLE() HAL_GPIO_WritePin(BLDC_AD_GPIO_Port,BLDC_AD_Pin,GPIO_PIN_RESET) +#define ESHL_AD_ENABLE() HAL_GPIO_WritePin(BLDC_AD_GPIO_Port,BLDC_AD_Pin,GPIO_PIN_SET) + +//电调B相上管配置 +#define ESHL_BU_ENABLE(pwm) __HAL_TIM_SetCompare(&ESHL_B_TIM,ESHL_B_TIM_CH,pwm) +#define ESHL_BU_DISABLE() __HAL_TIM_SetCompare(&ESHL_B_TIM,ESHL_B_TIM_CH,0) +//电调B相下管配置 +#define ESHL_BD_DISABLE() HAL_GPIO_WritePin(BLDC_BD_GPIO_Port,BLDC_BD_Pin,GPIO_PIN_RESET) +#define ESHL_BD_ENABLE() HAL_GPIO_WritePin(BLDC_BD_GPIO_Port,BLDC_BD_Pin,GPIO_PIN_SET) + +//电调C相上管配置 +#define ESHL_CU_ENABLE(pwm) __HAL_TIM_SetCompare(&ESHL_C_TIM,ESHL_C_TIM_CH,pwm) +#define ESHL_CU_DISABLE() __HAL_TIM_SetCompare(&ESHL_C_TIM,ESHL_C_TIM_CH,0) +//电调C相下管配置 +#define ESHL_CD_DISABLE() HAL_GPIO_WritePin(BLDC_CD_GPIO_Port,BLDC_CD_Pin,GPIO_PIN_RESET) +#define ESHL_CD_ENABLE() HAL_GPIO_WritePin(BLDC_CD_GPIO_Port,BLDC_CD_Pin,GPIO_PIN_SET) + + +#define ESHL_DEFALT_ADDR 0xEC00 //电调默认地址 +#define ESHL_ADDR_FLASH_ADD 0x0800F800 //用于存放电调地址的STM32内部Flash地址 + +#define ESHL_START_PWM 130 //电调启动PWM值 +#define ESHL_RUN_PWM_STEP 10 //电调pwm步进值 +#define ESHL_RUN_MAX_PWM 991 //电调运行最大PWM值 +#define ESHL_RUN_MIN_PWM 80 //电调运行最低PWM值 + +#define ESHL_BREAK_MOD 0 //电调刹车方式选择,0为滑动刹车,1为三相短路刹车 +#define ESHL_BREAK_PWM 100 //电调短路刹车时用的PWM值,默认100 +#define ESHL_BREAK_OK_SAME_BMEF_NUM 500 //电机停转阈值,刹车时过零事件相同的次数超过此值视为电机停转 + +#define ESHL_VBAT_CHACK_EN 1 //是否开启电调运行电压检测标志0为关闭,1为开启,(注:目前的策略是电池电压过低直接停转,请慎重考虑是否开启) +#define ESHL_VBAT_LIMIT 0.1f //电调电池电压限制阈值(V float),当前电池电压与电池额定电压差值小于此值时,视为电池电压过低 + + +//注:ADC值计算公式为 ADC = 目标电流(A)*0.0005(采样电阻阻值Ω)*50(电流计放大倍数)*4096(12位ADC精度)/3.3(单片机参考电压) +// 即 ADC = 目标电流(A)*31.30 +#define ESHL_MOS_TestPWM 20 //MOS管测试时使用的PWM值 +#define ESHL_MOS_Current_Test_num 50 //MOS电流单路测试次数 + + +#define ESHL_MOS_Current_ADC_MAX 8 //MOS电流阈值,超过此值视为电流不正常 +#define ESHL_RotoCurrent_ADC_MAX 45 //电调转子定位电流阈值,超过此值视为电流不正常 +#define ESHL_OPEN_LOOP_Transition_Period_ADC_MAX 125 //电调开环过渡期电流阈值,超过此值视为电流不正常 +#define ESHL_RUN_Current_Limit_ADC_DEFALT 1158 //电调运行电流限制ADC值,默认1158 + + +#define ESHL_MOTOR_TIMEOUT 250 //电机换向超时时间(ms),超过此值视为换向失败 +#define ESHL_RUNING_CURRENT_VBAT_CHACK_TIMOUT 30 //电调运行时电流和电池电压检测时间间隔(ms) + +#define ESHL_OPEN_LOOP_RESTART_MAX_NUM 5 //电调开环启动最大重试次数,超过此值视为无法开环启动 + // ↑↑↑(注:必须大于或等于2,乱填数据后果自负)↑↑↑ + + + + + + + + + + + +typedef enum ESHL_BAT_ENUM { + ESHL_BAT_1S = 1, //1S锂电池 + ESHL_BAT_2S, //2S锂电池 + ESHL_BAT_3S, //3S锂电池 + ESHL_BAT_4S, //4S锂电池 + ESHL_BAT_5S, //5S锂电池 + ESHL_BAT_6S, //6S锂电池 +}ESHL_BAT_ENUM_T;//电调电池枚举 + + + + + +typedef enum ESHL_BEEP_ENUM +{ + ESHL_BEEP_SHORT, + ESHL_BEEP_LONG, +// ESHL_BEEP_DOU, +// ESHL_BEEP_RAI, +// ESHL_BEEP_MI, +// ESHL_BEEP_FA, +// ESHL_BEEP_SOU, +// ESHL_BEEP_LA, +// ESHL_BEEP_SI, +// ESHL_BEEP_DOUi, +}ESHL_BEEP_ENUM_T;//电调声音枚举 + + +typedef enum ESHL_STATE_ENUM { + ESHL_STATE_OFF = 0, //电调关闭 + EShl_STATE_READY, //电调初始化完成,具备启动条件 + ESHL_STATE_START, //启动电机 + ESHL_STATE_OPEN_LOOP_START, //电调开环启动中 + ESHL_STATE_OPEN_LOOP_START_FAIL, //电调开环启动失败 + ESHL_STATE_OPEN_LOOP_START_ERROR, //电调开环启动重试超过最大次数,无法开环启动 + ESHL_STATE_RUN_CLOCKWISE, //顺时针方向运动中 + ESHL_STATE_RUN_COUNTER_CLOCKWISE, //逆时针方向运动中 + ESHL_STATE_MOTOR_RUNING_STOP, //电机运行时停转 + ESHL_STATE_BRAKE, //刹车中 + ESHL_STATE_BRAKE_OK, //刹车完毕 + ESHL_STATE_MOS_ERROR, //MOS异常 + ESHL_STATE_CURRENT_ERROR, //电流异常 + ESHL_STATE_BATTERY_VOLTAGE_ERROR, //电池电压异常 + ESHL_STATE_SET_ADDR, //电调设置地址中 + ESHL_STATE_SET_ADDR_OK, //电调地址设置成功 + ESHL_STATE_SET_HOST_TIMEOUT, //主机离线 +}ESHL_STATE_ENUM_T;//电调状态枚举 + +typedef enum ESHL_DIRECTION_ENUM { + ESHL_CLOCKWISE = 0, //电调顺时针运动标记 + ESHL_COUNTER_CLOCKWISE = 1, //电调逆时针运动标记 +}ESHL_DIRECTION_ENUM_T; + + + + + +void delay_us(TIM_HandleTypeDef *usTIMER,uint16_t us); +void ESHL_ESC_Init(); +void ESHL_Start(ESHL_DIRECTION_ENUM_T direction); +void ESHL_Beep(ESHL_BEEP_ENUM_T beep); +void ESHL_SET_PWM(uint16_t pwm); +uint8_t MOS_SelfTest(); +void ESHL_RuningChack(); +void ESHL_RuningCurrentVBATChack(); +void ESHL_ChangeRuningCurrentLimit(uint16_t current); +void ESHL_SetState(ESHL_STATE_ENUM_T state); +ESHL_STATE_ENUM_T ESHL_GetState(); +void ESHL_Break(); +uint16_t ESHL_GetAddr(); +void ESHL_SetAddr(uint16_t addr); +void ESHL_SetDirection(ESHL_DIRECTION_ENUM_T direction); +ESHL_DIRECTION_ENUM_T ESHL_GetDirection(); +// void ESHL_OFF(); +void ESHL_CloseMOSComp(); + +#endif //ESHL_DRIVER_H diff --git a/Driver/ESHL_protocol.c b/Driver/ESHL_protocol.c new file mode 100644 index 0000000..e75f6b6 --- /dev/null +++ b/Driver/ESHL_protocol.c @@ -0,0 +1,296 @@ +// +// Created by E_LJF on 25-7-17. +// + + + +/*电调在通信过程中始终作为从机 + +构成: +帧头[0]+电调地址[1]+数据包长[3]+命令码[4]+数据[5]+整包CRC16校验码 + +帧头: +主机→电调: 0XEC +电调→主机: 0XCE + +地址: +默认 EC00(可更改) +广播地址 AAAA + +命令码: +0XC1:油门命令,表示数据包传输油门百分比,float类型,4字节 +0XC2:关闭电调,电调数据0XD0表示电调成功关闭,主机不发送数据,填0,uint8_t,类型,1字节 +0XC3:启动电调,数据0XA1表示顺时针启动,0XA2表示逆时针启动,uint8_t类型,1字节 +0XC4:电调刹车,电调数据0XA3表示刹车完毕,主机不发送数据,填0,uint8_t,类型,1字节 +0XC5:更改电调运行电流限制ADC值,uint16_t类型 +0XC6:电调异常,表示数据包传输电调异常代码,uint8_t类型,1字节 +0XC7:更改电调地址,主机用广播地址发送此命令,更改地址时,所有已连接的电调地址都需要重新设置 + 地址设置方法: 快速旋转已经连接上电调的电机,此时电调将自动将自身地址设为EC00 + 同时电调状态指示灯会闪烁,闪烁次数代表电调地址 + 此时旋转第二个电调的电机,此时电调将自动将自身地址设为EC01,以此类推,直到所有电调地址设置完毕 + 电调设置地址后需要重新上电才能使用 + 设置地址主机不发送数据,电调广播自身地址表示该地址已被占用 + +电调异常代码: +0XE0:MOS异常 +0XE1:电流异常 +0XE2:电调开环启动达到最大重启次数,开环启动失败 +0XE3:电机运行过程中意外停转 +0XE4:电池电压异常*//*电调在通信过程中始终作为从机 + +构成: +帧头[0]+电调地址[1]+数据包长[3]+命令码[4]+数据[5]+整包CRC16校验码 + +帧头: +主机→电调: 0XEC +电调→主机: 0XCE + +地址: +默认 EC00(可更改) +广播地址 AAAA + +命令码: +0XC1:油门命令,表示数据包传输油门百分比,float类型,4字节 +0XC2:关闭电调,电调数据0XD0表示电调成功关闭,主机不发送数据,填0,uint8_t,类型,1字节 +0XC3:启动电调,数据0XA1表示顺时针启动,0XA2表示逆时针启动,uint8_t类型,1字节 +0XC4:电调刹车,电调数据0XA3表示刹车完毕,主机不发送数据,填0,uint8_t,类型,1字节 +0XC5:更改电调运行电流限制ADC值,uint16_t类型 +0XC6:电调异常,表示数据包传输电调异常代码,uint8_t类型,1字节 +0XC7:更改电调地址,主机用广播地址发送此命令,更改地址时,所有已连接的电调地址都需要重新设置 + 地址设置方法: 快速旋转已经连接上电调的电机,此时电调将自动将自身地址设为EC00 + 同时电调状态指示灯会闪烁,闪烁次数代表电调地址 + 此时旋转第二个电调的电机,此时电调将自动将自身地址设为EC01,以此类推,直到所有电调地址设置完毕 + 电调设置地址后需要重新上电才能使用 + 设置地址主机不发送数据,电调广播自身地址表示该地址已被占用 + +电调异常代码: +0XE0:MOS异常 +0XE1:电流异常 +0XE2:电调开环启动达到最大重启次数,开环启动失败 +0XE3:电机运行过程中意外停转 +0XE4:电池电压异常*/ + +#include "ESHL_protocol.h" +#include "crc.h" + + +//将1个float类型浮点数转为uint8数组 +void float_to_uint8(const float f,uint8_t* u8) { + uint8_t *p = (uint8_t*)&f; + u8[0] = p[3]; + u8[1] = p[2]; + u8[2] = p[1]; + u8[3] = p[0]; +} + + +//将uint8数组转为float类型浮点数 +void uint8_to_float(const uint8_t* u8,float* f) { + uint8_t *p = (uint8_t*)f; + p[3] = u8[0]; + p[2] = u8[1]; + p[1] = u8[2]; + p[0] = u8[3]; +} + + + +//将数据打包成ESHL协议格式 +// pack 为数据包数组 +// dat 为要打包的数据数组,地址放在第一位,地址后面接命令码,命令码后面接数据,程序会根据命令码打包 +// dat_direction 为数据发送方向 +void ESHL_ProtocolPackMake(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* dat_str,ESHL_PROTOCOL_ENUM dat_direction) { + + if ((pack == NULL) || (dat_str == NULL)) {return;}//输入处理 + + uint8_t dat_len = 0;//数据包长 + + if (dat_direction == ESHL_DATA_HOST_TO_ESC) {//帧头 + pack[0] = ESHL_PROTOCOL_HEAD_HOST_TO_ESC; + } + else if (dat_direction == ESHL_DATA_ESC_TO_HOST) { + pack[0] = ESHL_PROTOCOL_HEAD_ESC_TO_HOST; + } + + dat_len += 1; + + pack[1] = (dat_str->addr >> 8) & 0xFF;//地址高8位 + pack[2] = dat_str->addr & 0xFF;//地址低8位 + + dat_len += 2; + + //pack[3] = dat_len + dat_len += 1; + + pack[4] = dat_str->cmd;//命令码 + dat_len += 1; + + switch (dat_str->cmd) { + + case ESHL_PROTOCOL_CMD_THR://油门百分比 + float_to_uint8(dat_str->throttle,&pack[dat_len]); + dat_len += 4; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + case ESHL_PROTOCOL_CMD_ESC_OFF://关闭电调 + pack[dat_len] = dat_str->data;//电调是否成功关闭标志 + dat_len += 1; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + case ESHL_PROTOCOL_CMD_ESC_ON://启动电调 + pack[dat_len] = dat_str->data;//启动方向 + dat_len += 1; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + case ESHL_PROTOCOL_CMD_BREAK://电调刹车 + pack[dat_len] = dat_str->data;//刹车标志 + dat_len += 1; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + case ESHL_PROTOCOL_CMD_CURRENT_LIMIT://更改电调运行电流限制 + pack[dat_len] = (dat_str->current_limit >> 8) & 0xFF; + pack[dat_len+1] = dat_str->current_limit & 0xFF; + dat_len += 2; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + case ESHL_PROTOCOL_CMD_ERROR://电调异常 + pack[dat_len] = dat_str->data;//电调错误码 + dat_len += 1; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + case ESHL_PROTOCOL_CMD_CHANGE_ADDR://更改电调地址 + pack[dat_len] =(dat_str->addr_dat >> 8) & 0xFF; + pack[dat_len+1] = dat_str->addr_dat & 0xFF; + dat_len += 2; + dat_len += 2; + pack[3] = dat_len; + append_crc16_check_sum(pack,dat_len);//CRC16校验码 + break; + + default: + break; + } +} + + +//提取数据包地址和帧头和包长 +//返回CRC校验结果0或1,帧头不匹配返回0 +uint8_t ESHL_ProtocolAnalysisAddr(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* analysis_str) { + if ((pack == NULL) || (analysis_str == NULL)) {return 0;} + + if (pack[0] != ESHL_PROTOCOL_HEAD_ESC_TO_HOST && pack[0] != ESHL_PROTOCOL_HEAD_HOST_TO_ESC) { + return 0; + } + + if(verify_crc16_check_sum(pack,pack[3])) {//CRC16校验 + analysis_str->head = pack[0]; + analysis_str->addr = (pack[1] << 8) | pack[2]; + analysis_str->len = pack[3]; + return 1; + } + return 0; +} + + +//提取完帧头,地址,和包长后,接着提取数据,运行此函数前应先运行ESHL_ProtocolAnalysisAddr +void ESHL_ProtocolAnalysisData(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* analysis_str) { + + analysis_str->cmd = pack[4];//获取命令码 + + switch (analysis_str->cmd) { + + case ESHL_PROTOCOL_CMD_THR://油门命令 + uint8_to_float(&pack[5],&analysis_str->throttle);//获取油门百分比 + analysis_str->data = 0; + analysis_str->addr_dat = 0; + analysis_str->current_limit = 0; + break; + + case ESHL_PROTOCOL_CMD_ESC_OFF://关闭电调 + analysis_str->data = pack[5]; + analysis_str->addr_dat = 0; + analysis_str->current_limit = 0; + analysis_str->throttle = 0; + break; + + case ESHL_PROTOCOL_CMD_ESC_ON://启动电调 + analysis_str->data = pack[5]; + analysis_str->addr_dat = 0; + analysis_str->current_limit = 0; + analysis_str->throttle = 0; + break; + + case ESHL_PROTOCOL_CMD_BREAK://电调刹车 + analysis_str->data = pack[5]; + analysis_str->addr_dat = 0; + analysis_str->current_limit = 0; + analysis_str->throttle = 0; + break; + + case ESHL_PROTOCOL_CMD_CURRENT_LIMIT://更改电调运行电流限制ADC值 + analysis_str->current_limit = (pack[5] << 8) | pack[6]; + analysis_str->data = 0; + analysis_str->addr_dat = 0; + analysis_str->throttle = 0; + break; + + case ESHL_PROTOCOL_CMD_ERROR://电调异常 + analysis_str->data = pack[5]; + analysis_str->addr_dat = 0; + analysis_str->current_limit = 0; + analysis_str->throttle = 0; + break; + + case ESHL_PROTOCOL_CMD_CHANGE_ADDR://更改电调地址 + analysis_str->addr_dat = (pack[5] << 8) | pack[6]; + analysis_str->data = 0; + analysis_str->current_limit = 0; + analysis_str->throttle = 0; + break; + + default: + break; + } +} + + +//解析ESHL协议数据包,全流程一步到位 +void ESHL_ProtocolPackAnalysis(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* analysis_str) { + if ((pack == NULL) || (analysis_str == NULL)) {return;} + + if(ESHL_ProtocolAnalysisAddr(pack,analysis_str)) {//提取帧头和地址 + + ESHL_ProtocolAnalysisData(pack,analysis_str);//提取数据 + } + else { + analysis_str->head = 0; + analysis_str->addr = 0; + analysis_str->current_limit = 0; + analysis_str->throttle = 0; + analysis_str->addr_dat = 0; + analysis_str->cmd = 0; + analysis_str->data = 0; + analysis_str->len = 0; + } + +} + + diff --git a/Driver/ESHL_protocol.h b/Driver/ESHL_protocol.h new file mode 100644 index 0000000..5f354e8 --- /dev/null +++ b/Driver/ESHL_protocol.h @@ -0,0 +1,97 @@ +// +// Created by E_LJF on 25-7-17. +// + + +/*电调在通信过程中始终作为从机 + +构成: +帧头[0]+电调地址[1]+数据包长[3]+命令码[4]+数据[5]+整包CRC16校验码 + +帧头: +主机→电调: 0XEC +电调→主机: 0XCE + +地址: +默认 EC00(可更改) +广播地址 AAAA + +命令码: +0XC1:油门命令,表示数据包传输油门百分比,float类型,4字节 +0XC2:关闭电调,电调数据0XD0表示电调成功关闭,主机不发送数据,填0,uint8_t,类型,1字节 +0XC3:启动电调,数据0XA1表示顺时针启动,0XA2表示逆时针启动,uint8_t类型,1字节 +0XC4:电调刹车,电调数据0XA3表示刹车完毕,主机不发送数据,填0,uint8_t,类型,1字节 +0XC5:更改电调运行电流限制ADC值,uint16_t类型 +0XC6:电调异常,表示数据包传输电调异常代码,uint8_t类型,1字节 +0XC7:更改电调地址,主机用广播地址发送此命令,更改地址时,所有已连接的电调地址都需要重新设置 + 地址设置方法: 快速旋转已经连接上电调的电机,此时电调将自动将自身地址设为EC00 + 同时电调状态指示灯会闪烁,闪烁次数代表电调地址 + 此时旋转第二个电调的电机,此时电调将自动将自身地址设为EC01,以此类推,直到所有电调地址设置完毕 + 电调设置地址后需要重新上电才能使用 + 设置地址主机不发送数据,电调广播自身地址表示该地址已被占用 + +电调异常代码: +0XE0:MOS异常 +0XE1:电流异常 +0XE2:电调开环启动达到最大重启次数,开环启动失败 +0XE3:电机运行过程中意外停转 +0XE4:电池电压异常*/ + + +#ifndef ESHL_PROTOCOL_H +#define ESHL_PROTOCOL_H + +#include "main.h" + +#define ESHL_PROTOCOL_HEAD_ESC_TO_HOST 0xCE //数据从电调发往主机帧头 +#define ESHL_PROTOCOL_HEAD_HOST_TO_ESC 0xEC //数据从电调发往主机帧头 + + +#define ESHL_PROTOCOL_BROADCAST_ADDR 0xAAAA //ESHL协仪广播地址 + + +#define ESHL_PROTOCOL_CMD_THR 0xC1 //油门命令,表示数据包传输油门百分比,float类型,4字节 +#define ESHL_PROTOCOL_CMD_ESC_OFF 0xC2 //关闭电调,电调数据0XD0表示电调成功关闭,主机不发送数据,填0,uint8_t,类型,1字节 +#define ESHL_PROTOCOL_CMD_ESC_ON 0xC3 //启动电调,数据0XA1表示顺时针启动,0XA2表示逆时针启动,uint8_t类型,1字节 +#define ESHL_PROTOCOL_CMD_BREAK 0xC4 //电调刹车,电调数据0XA3表示刹车完毕,主机不发送数据,填0,uint8_t,类型,1字节 +#define ESHL_PROTOCOL_CMD_CURRENT_LIMIT 0xC5 //更改电调运行电流限制ADC值,uint16_t类型 +#define ESHL_PROTOCOL_CMD_ERROR 0xC6 //电调异常,表示数据包传输电调异常代码,uint8_t类型,1字节 +#define ESHL_PROTOCOL_CMD_CHANGE_ADDR 0xC7 //更改电调地址,主机用广播地址发送此命令,更改地址时,所有已连接的电调地址都需要重新设置 + + +#define ESHL_PROTOCOL_ERROR_CODE_MOS 0xE0 //MOS异常 +#define ESHL_PROTOCOL_ERROR_CODE_CURRENT 0xE1 //电流异常 +#define ESHL_PROTOCOL_ERROR_CODE_START_FAIL 0xE2 //电调开环启动达到最大重启次数,开环启动失败 +#define ESHL_PROTOCOL_ERROR_CODE_RUNING_STOP 0xE3 //电机运行过程中意外停转 + + + + +typedef enum{ + ESHL_DATA_ESC_TO_HOST,//数据从电调发往主机 + ESHL_DATA_HOST_TO_ESC,//数据从主机发往电调 +}ESHL_PROTOCOL_ENUM;//ESHL协议枚举 + + +typedef struct ESHL_PROTOCOL_PACK_ANALYSIS_T +{ + uint8_t head;//数据包帧头 + uint16_t addr;//数据包地址 + uint8_t len;//数据包大小 + + uint8_t cmd;//数据包命令码 + uint8_t data;//发送或接收的数据 + uint16_t addr_dat;//发送或接收地址 + uint16_t current_limit;//发送或接收电调运行电流限制 + float throttle;//发送或接收油门百分比 +}ESHL_PROTOCOL_PACK_ANALYSIS_T; + + +void float_to_uint8(float f,uint8_t* u8); +void uint8_to_float(const uint8_t* u8,float* f); +void ESHL_ProtocolPackMake(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* dat_str,ESHL_PROTOCOL_ENUM dat_direction); +uint8_t ESHL_ProtocolAnalysisAddr(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* analysis_str); +void ESHL_ProtocolAnalysisData(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* analysis_str); +void ESHL_ProtocolPackAnalysis(uint8_t* pack,ESHL_PROTOCOL_PACK_ANALYSIS_T* analysis_str); + +#endif //ESHL_PROTOCOL_H diff --git a/Driver/Internal_Flash.c b/Driver/Internal_Flash.c new file mode 100644 index 0000000..692c775 --- /dev/null +++ b/Driver/Internal_Flash.c @@ -0,0 +1,188 @@ +// +// Created by E_LJF on 25-7-17. +// + +#include "Internal_Flash.h" +#include "stm32f0xx_hal_flash.h" + + +//按uint64大小写stm32内部Flash +void InternalFlashWriteUint_64(uint32_t Address,uint64_t dat) { + + if (Address == 0) { + return; + } + + HAL_FLASH_Unlock(); + + FLASH_EraseInitTypeDef EraseInit = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .PageAddress = Address, + .NbPages = 1 + }; + + uint32_t page_error = 0; + + HAL_FLASHEx_Erase(&EraseInit,&page_error); + HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,Address,dat); + + HAL_FLASH_Lock(); +} + + +//按uint32大小写stm32内部Flash +void InternalFlashWriteUint_32(uint32_t Address,uint32_t dat) { + + if (Address == 0) { + return; + } + + HAL_FLASH_Unlock(); + + FLASH_EraseInitTypeDef EraseInit = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .PageAddress = Address, + .NbPages = 1 + }; + + uint32_t page_error = 0; + + HAL_FLASHEx_Erase(&EraseInit,&page_error); + HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address,dat); + + HAL_FLASH_Lock(); +} + + +//按uint16大小写stm32内部Flash +void InternalFlashWriteUint_16(uint32_t Address,uint16_t dat) { + + if (Address == 0) { + return; + } + + HAL_FLASH_Unlock(); + + FLASH_EraseInitTypeDef EraseInit = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .PageAddress = Address, + .NbPages = 1 + }; + + uint32_t page_error = 0; + + HAL_FLASHEx_Erase(&EraseInit,&page_error); + HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address,dat); + + HAL_FLASH_Lock(); +} + + +//按uint16大小往stm32内部Flash写多个值 +void InternalFlashWriteMoreUint_16(uint32_t Address,uint16_t* dat,uint16_t dat_len) { + if (Address == 0) {return;} + HAL_FLASH_Unlock(); + FLASH_EraseInitTypeDef EraseInit = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .PageAddress = Address, + .NbPages = 1 + }; + uint32_t page_error = 0; + HAL_FLASHEx_Erase(&EraseInit,&page_error); + for (uint16_t index = 0; index < dat_len; index++) + { + HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,(Address + (2U*index)),dat[index]); + } + HAL_FLASH_Lock(); +} + + +//按uint32大小往stm32内部Flash写多个值 +void InternalFlashWriteMoreUint_32(uint32_t Address,uint32_t* dat,uint16_t dat_len) { + if (Address == 0) {return;} + HAL_FLASH_Unlock(); + FLASH_EraseInitTypeDef EraseInit = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .PageAddress = Address, + .NbPages = 1 + }; + uint32_t page_error = 0; + HAL_FLASHEx_Erase(&EraseInit,&page_error); + for (uint16_t index = 0; index < dat_len; index++) + { + HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,(Address + (4U*index)),dat[index]); + } + HAL_FLASH_Lock(); +} + + +//按uint64大小往stm32内部Flash写多个值 +void InternalFlashWriteMoreUint_64(uint32_t Address,uint64_t* dat,uint16_t dat_len) { + if (Address == 0) {return;} + HAL_FLASH_Unlock(); + FLASH_EraseInitTypeDef EraseInit = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .PageAddress = Address, + .NbPages = 1 + }; + uint32_t page_error = 0; + HAL_FLASHEx_Erase(&EraseInit,&page_error); + for (uint16_t index = 0; index < dat_len; index++) + { + HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,(Address + (8U*index)),dat[index]); + } + HAL_FLASH_Lock(); +} + + +//读取指定地址的半字(16位数据) +//也是按照半字读出,即每次读2个字节数据返回 +uint16_t InternalFLASH_Read16(uint32_t address) +{ + if (address == 0) {return 0;} + return *(__IO uint16_t*)address; +} + + +//读取指定地址的字(32位数据) +//也是按照字读出,即每次读4个字节数据返回 +uint32_t InternalFLASH_Read32(uint32_t address) { + if (address == 0) {return 0;} + return *(__IO uint32_t*)address; +} + + +//读取指定地址的两字(64位数据) +//也是按照两字读出,即每次读8个字节数据返回 +uint64_t InternalFLASH_Read64(uint32_t address) { + if (address == 0) {return 0;} + return *(__IO uint64_t*)address; +} + + +//读取指定地址多个uint16数据 +void InternalFLASH_ReadMore16(uint32_t address,uint16_t* read_dat,uint16_t dat_len) { + if (address == 0) {return ;} + for (uint16_t index = 0; index < dat_len; index++) { + read_dat[index] = *(__IO uint16_t*)(address+index*2); + } +} + + +//读取指定地址多个uint32数据 +void InternalFLASH_ReadMore32(uint32_t address,uint32_t* read_dat,uint16_t dat_len) { + if (address == 0) {return ;} + for (uint16_t index = 0; index < dat_len; index++) { + read_dat[index] = *(__IO uint32_t*)(address+index*4); + } +} + + +//读取指定地址多个uint64数据 +void InternalFLASH_ReadMore64(uint32_t address,uint64_t* read_dat,uint16_t dat_len) { + if (address == 0) {return ;} + for (uint16_t index = 0; index < dat_len; index++) { + read_dat[index] = *(__IO uint64_t*)(address+index*8); + } +} + diff --git a/Driver/Internal_Flash.h b/Driver/Internal_Flash.h new file mode 100644 index 0000000..b6e78fc --- /dev/null +++ b/Driver/Internal_Flash.h @@ -0,0 +1,26 @@ +// +// Created by E_LJF on 25-7-17. +// + +#ifndef INTERNAL_FLASH_H +#define INTERNAL_FLASH_H + +#include "main.h" + +void InternalFlashWriteUint_64(uint32_t Address,uint64_t dat); +void InternalFlashWriteUint_32(uint32_t Address,uint32_t dat); +void InternalFlashWriteUint_16(uint32_t Address,uint16_t dat); + +void InternalFlashWriteMoreUint_16(uint32_t Address,uint16_t* dat,uint16_t dat_len); +void InternalFlashWriteMoreUint_32(uint32_t Address,uint32_t* dat,uint16_t dat_len); +void InternalFlashWriteMoreUint_64(uint32_t Address,uint64_t* dat,uint16_t dat_len); + +uint16_t InternalFLASH_Read16(uint32_t address); +uint32_t InternalFLASH_Read32(uint32_t address); +uint64_t InternalFLASH_Read64(uint32_t address); + +void InternalFLASH_ReadMore16(uint32_t address,uint16_t* read_dat,uint16_t dat_len); +void InternalFLASH_ReadMore32(uint32_t address,uint32_t* read_dat,uint16_t dat_len); +void InternalFLASH_ReadMore64(uint32_t address,uint64_t* read_dat,uint16_t dat_len); + +#endif //INTERNAL_FLASH_H diff --git a/Driver/communication_management.c b/Driver/communication_management.c new file mode 100644 index 0000000..79ea507 --- /dev/null +++ b/Driver/communication_management.c @@ -0,0 +1,244 @@ +// +// Created by E_LJF on 25-7-18. +// + +#include "communication_management.h" + +#include "ESHL_driver.h" +#include "ESHL_protocol.h" +#include "usart.h" + +ESHL_PROTOCOL_PACK_ANALYSIS_T send_str =//电调数据发送结构体 +{ + .head = 0, + .addr = 0, + .cmd = 0, + .data = 0, + .addr_dat = 0, + .current_limit = 0, + .throttle = 0 +}; + + + +ESHL_PROTOCOL_PACK_ANALYSIS_T recv_str =//电调数据接收结构体 +{ + .head = 0, + .addr = 0, + .cmd = 0, + .data = 0, + .addr_dat = 0, + .current_limit = 0, + .throttle = 0 +}; + +uint8_t rx_buff[ESHL_RX_PACK_LEN] = ""; //电调数据接收缓存 +uint8_t ESHL_RXPack[ESHL_RX_PACK_LEN] = ""; //电调数据接收包 +uint8_t ESHL_TXPack[ESHL_TX_PACK_LEN] = ""; //电调数据发送包 + +uint16_t ESHL_RunPWMBuff = 0; //电调运行pwm缓存 + +uint8_t data_updated_flag = 0; //串口数据是否已更新标志,1为已更新 +uint8_t uart_updated_flag = 0;//串口是否收到数据标志,1为收到数据 + + +void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { + + uart_updated_flag = 1; + + for (uint16_t i = 0; i < Size; i++) { + ESHL_RXPack[i] = rx_buff[i]; //复制数据 + } + + if (ESHL_RX_PACK_LEN > Size) {//数据包数组还有空位 + for (uint16_t i = Size; i < ESHL_RX_PACK_LEN; i++) { + ESHL_RXPack[i] = 0;//空位填0,防止读到上一次数据 + } + } + + HAL_UARTEx_ReceiveToIdle_DMA(&ESHL_UART, (uint8_t *)rx_buff, ESHL_RX_PACK_LEN);//开启串口通信 + __HAL_DMA_DISABLE_IT(ESHL_UART.hdmarx,DMA_IT_HT);//关闭DMA半满中断 +} + + +void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { + uart_updated_flag = 0; + HAL_UARTEx_ReceiveToIdle_DMA(&ESHL_UART, (uint8_t *)rx_buff, ESHL_RX_PACK_LEN);//开启串口通信 + __HAL_DMA_DISABLE_IT(ESHL_UART.hdmarx,DMA_IT_HT);//关闭DMA半满中断 +} + + +//开始通信 +void ESHL_CommunicationStart() { + uart_updated_flag = 0;//清除标志 + HAL_HalfDuplex_EnableReceiver(&ESHL_UART); + HAL_UARTEx_ReceiveToIdle_DMA(&ESHL_UART, (uint8_t *)rx_buff, ESHL_RX_PACK_LEN);//开启串口通信 + __HAL_DMA_DISABLE_IT(huart1.hdmarx,DMA_IT_HT);//关闭DMA半满中断 +} + + +//停止通信 +void ESHL_CommunicationStop() { + uart_updated_flag = 0;//清除标志 + HAL_UART_Abort(&ESHL_UART); + HAL_DMA_Abort_IT(ESHL_UART.hdmarx); +} + + +//命令处理及响应 +static void ESHL_CMDProcessing() { + + ESHL_STATE_ENUM_T state = ESHL_GetState(); + + switch (recv_str.cmd) { + + case ESHL_PROTOCOL_CMD_THR://油门命令 + + if (state == ESHL_STATE_RUN_CLOCKWISE || state == ESHL_STATE_RUN_COUNTER_CLOCKWISE) {//运行状态中修改 + ESHL_RunPWMBuff = (uint16_t) (recv_str.throttle * ESHL_RUN_MAX_PWM); //将油门百分比转为油门PWM值 + }/*else if (state == ESHL_STATE_SET_HOST_TIMEOUT) {//退出超时 + ESHL_SetState(EShl_STATE_READY); + }*/ + break; + + case ESHL_PROTOCOL_CMD_ESC_OFF: //关闭电调 + ESHL_SetState(ESHL_STATE_OFF);//更新电调状态 + break; + + case ESHL_PROTOCOL_CMD_ESC_ON: //启动电调,数据0XA1表示顺时针启动,0XA2表示逆时针启动 + if ((state == ESHL_STATE_OFF) || (state == EShl_STATE_READY) || (state == ESHL_STATE_SET_HOST_TIMEOUT)) { + if (recv_str.data == 0XA1) { + ESHL_RunPWMBuff = ESHL_RUN_MIN_PWM; + ESHL_SetDirection(ESHL_CLOCKWISE);//方向设为顺时针 + ESHL_SetState(ESHL_STATE_START); + } + else if (recv_str.data == 0XA2) { + ESHL_RunPWMBuff = ESHL_RUN_MIN_PWM; + ESHL_SetDirection(ESHL_COUNTER_CLOCKWISE);//方向设为逆时针 + ESHL_SetState(ESHL_STATE_START); + }/*else if (state == ESHL_STATE_SET_HOST_TIMEOUT) {//退出超时 + ESHL_SetState(EShl_STATE_READY); + }*/ + } + break; + + case ESHL_PROTOCOL_CMD_BREAK://电调刹车 + if (state == ESHL_STATE_RUN_CLOCKWISE || state == ESHL_STATE_RUN_COUNTER_CLOCKWISE) {//运行状态生效 + ESHL_CloseMOSComp(); + ESHL_SetState(ESHL_STATE_BRAKE);//更新电调状态 + }/*else if (state == ESHL_STATE_SET_HOST_TIMEOUT) {//退出超时 + ESHL_SetState(EShl_STATE_READY); + }*/ + break; + + case ESHL_PROTOCOL_CMD_CURRENT_LIMIT://更改电调运行电流限制ADC值 + ESHL_ChangeRuningCurrentLimit(recv_str.current_limit); + // if (state == ESHL_STATE_SET_HOST_TIMEOUT) {//退出超时 + // ESHL_SetState(EShl_STATE_READY); + // } + break; + + case ESHL_PROTOCOL_CMD_CHANGE_ADDR: //更改电调地址 + if ((state == ESHL_STATE_OFF) || (state == EShl_STATE_READY) || (state == ESHL_STATE_SET_HOST_TIMEOUT)) { + ESHL_CloseMOSComp(); + ESHL_SetState(ESHL_STATE_SET_ADDR);//更新电调状态 + } + break; + + default: + break; + } +} + + +//主机离线检测,返回主机是否离线,1离线,0在线 +static uint8_t HostTimeOutCheck(const ESHL_STATE_ENUM_T state) { + + if (state == ESHL_STATE_OFF || //电调关闭 + state == ESHL_STATE_OPEN_LOOP_START_ERROR || //电调开环启动重试超过最大次数,无法开环启动 + state == ESHL_STATE_CURRENT_ERROR || //电流异常 + state == ESHL_STATE_SET_ADDR || //电调设置地址中 + state == ESHL_STATE_SET_ADDR_OK || //电调地址设置成功 + state == ESHL_STATE_BATTERY_VOLTAGE_ERROR //电池电压异常 + ){return 0;} //以上状态不进行主机离线检测 + + static uint32_t last_tick = 0;//上一次数据更新时的系统时间 + + if (uart_updated_flag) {//串口有动静 + uart_updated_flag = 0;//清除标志 + last_tick = HAL_GetTick();//更新时间 + } + + if (HAL_GetTick() - last_tick >= ESHL_HOST_TIMEOUT) {//超时 + ESHL_SetState(ESHL_STATE_SET_HOST_TIMEOUT);//更新电调状态为主机离线 + last_tick = 0; + return 1; + } + return 0; +} + + +//通信数据处理 +void ESHL_CommunicationDataProcessing() { + + ESHL_STATE_ENUM_T state = ESHL_GetState(); + + if (HostTimeOutCheck(state)) {//主机离线检测 + return;//主机离线 + } + if (state == ESHL_STATE_SET_HOST_TIMEOUT) {//退出超时 + ESHL_SetState(EShl_STATE_READY); + } + + for (uint16_t i = 0; i < (ESHL_RX_PACK_LEN - ESHL_DATA_PACK_MAX_LEN);i++) {//遍历接收缓存 + if(ESHL_ProtocolAnalysisAddr(&ESHL_RXPack[i],&recv_str)) {//提取帧头,地址,数据包长 + if ((recv_str.addr == ESHL_GetAddr()) || (recv_str.addr == ESHL_PROTOCOL_BROADCAST_ADDR)) {//地址匹配 + ESHL_ProtocolAnalysisData(ESHL_RXPack,&recv_str);//提取数据 + ESHL_CMDProcessing();//命令处理及响应 + i += recv_str.len;//跳过已经读取数据的部分 + } + } + } +} + +//广播电调地址 +void ESHL_CommunicationAddressSend() { + uint8_t packet[10] = ""; + ESHL_PROTOCOL_PACK_ANALYSIS_T send_str; + + send_str.cmd = ESHL_PROTOCOL_CMD_CHANGE_ADDR; + send_str.addr = ESHL_PROTOCOL_BROADCAST_ADDR; + send_str.data = 0; + send_str.addr_dat = ESHL_GetAddr(); + + ESHL_CommunicationStop();//停止通信 + HAL_HalfDuplex_EnableTransmitter(&ESHL_UART);//启用发送 + ESHL_ProtocolPackMake(packet,&send_str,ESHL_DATA_ESC_TO_HOST);//数据打包 + HAL_UART_Transmit_DMA(&ESHL_UART,(uint8_t*)packet,10);//发送数据 + HAL_Delay(1); + ESHL_CommunicationStop();//停止通信 +} + + +//向主机发送一字节数据 +//cmd为要发送的命令 +//code为要发送的1字节数据 +void ESHL_CommunicationSendCode(uint8_t cmd,uint8_t code) { + + uint8_t packet[10] = ""; + ESHL_PROTOCOL_PACK_ANALYSIS_T send_str; + + send_str.cmd = cmd; + send_str.addr = ESHL_GetAddr(); + send_str.data = code; + send_str.addr_dat = 0; + + ESHL_CommunicationStop();//停止通信 + HAL_HalfDuplex_EnableTransmitter(&ESHL_UART);//启用发送 + ESHL_ProtocolPackMake(packet,&send_str,ESHL_DATA_ESC_TO_HOST);//数据打包 + HAL_UART_Transmit_DMA(&ESHL_UART,(uint8_t*)packet,10);//发送数据 + HAL_Delay(10); + ESHL_CommunicationStop();//停止通信 +} + + diff --git a/Driver/communication_management.h b/Driver/communication_management.h new file mode 100644 index 0000000..72667fd --- /dev/null +++ b/Driver/communication_management.h @@ -0,0 +1,25 @@ +// +// Created by E_LJF on 25-7-18. +// + +#ifndef COMMUNICATION_MANAGEMENT_H +#define COMMUNICATION_MANAGEMENT_H + +#include "main.h" + +#define ESHL_UART huart1 //电调通信使用的串口 + +#define ESHL_RX_PACK_LEN 50 //电调接收缓存大小 +#define ESHL_TX_PACK_LEN 20 //电调发送缓存大小 +#define ESHL_DATA_PACK_MAX_LEN 11 //电调数据包最大长度 + +#define ESHL_HOST_TIMEOUT 1500 //主机离线时间(ms)阈值,超过此值视为主机离线 + + +void ESHL_CommunicationStart(); +void ESHL_CommunicationStop(); +void ESHL_CommunicationDataProcessing(); +void ESHL_CommunicationAddressSend(); +void ESHL_CommunicationSendCode(uint8_t cmd,uint8_t code); + +#endif //COMMUNICATION_MANAGEMENT_H diff --git a/Driver/crc.c b/Driver/crc.c new file mode 100644 index 0000000..5420b9e --- /dev/null +++ b/Driver/crc.c @@ -0,0 +1,178 @@ +/******************************************************************************* + * Robofuture RM Team + * File name: crc.c + * Author: Zhb Version: 1.0 Date: 2021/4/6 + * Description: crc校验 + * Function List: + * 1. get_crc8_check_sum 计算crc8校验值 + * 2. verify_crc8_check_sum 检查crc8校验码 + * 3. append_crc8_check_sum 在数据末添加crc8校验 + * 4. get_crc16_check_sum 计算crc16校验值 + * 5. verify_crc16_check_sum 检查crc16校验码 + * 6. append_crc16_check_sum 在数据末添加crc16校验 + * History: + *