diff --git a/project/cmake/CMakeLists.txt b/project/cmake/CMakeLists.txt index 63abb7d..c23b165 100644 --- a/project/cmake/CMakeLists.txt +++ b/project/cmake/CMakeLists.txt @@ -6,8 +6,6 @@ SET(CMAKE_CXX_COMPILER_WORKS TURE) project(stm32_template) - - ENABLE_LANGUAGE(ASM C) add_definitions( @@ -15,22 +13,11 @@ add_definitions( -DUSE_HAL_DRIVER ) -set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections") - -set(CMAKE_ASM_FLAGS "-c ${MCU_FLAGS} -x assembler-with-cpp -Wa,-mimplicit-it=thumb -gdwarf-2") - -set(CMAKE_C_FLAGS "${MCU_FLAGS} -Dgcc -w -Wno-unknown-pragmas -O0 -gdwarf-2 -g") -set(CMAKE_C_FLAGS_DEBUG "-O0 -gdwarf-2 -g") -set(CMAKE_C_FLAGS_RELEASE "-O3") - -set(LINKER_SCRIPT STM32F407ZGTX_FLASH.ld) -set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} -Wl,--gc-sections,-Map=${PROJECT_NAME}.map,--cref,-u,Reset_Handler -T${LINKER_SCRIPT}") -#set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs -specs=nosys.specs -nostartfiles -T${LINKER_SCRIPT} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections") - set(SRC_PATH ../../src) include_directories(${SRC_PATH}/App) include_directories(${SRC_PATH}/Bsp) +include_directories(${SRC_PATH}/Foc) include_directories(${SRC_PATH}/Common) include_directories(${SRC_PATH}/CMSIS/Include) include_directories(${SRC_PATH}/CMSIS/Device/ST/STM32F4xx/Include) @@ -100,9 +87,16 @@ set(Bsp_SRC ${SRC_PATH}/Bsp/board.c ${SRC_PATH}/Bsp/gpio.c ${SRC_PATH}/Bsp/usart.c +${SRC_PATH}/Bsp/timer.c ${SRC_PATH}/Bsp/stm32f4xx_it.c ) +set(Foc_SRC +${SRC_PATH}/Foc/math.c +${SRC_PATH}/Foc/hall.c +${SRC_PATH}/Foc/svpwm.c +) + set(Common_SRC ) @@ -114,8 +108,8 @@ ${SRC_PATH}/App/main.c add_executable(${PROJECT_NAME}.elf ${CMSIS_SRC} ${STM32F4xx_HAL_Driver_SRC} - ${RTThread_SRC} - ${FinSH_SRC} + #${RTThread_SRC} + #${FinSH_SRC} ${Common_SRC} ${Bsp_SRC} ${App_SRC} diff --git a/project/cmake/toolchain-arm-none-eabi.cmake b/project/cmake/toolchain-arm-none-eabi.cmake index 0ede6dc..d663b57 100644 --- a/project/cmake/toolchain-arm-none-eabi.cmake +++ b/project/cmake/toolchain-arm-none-eabi.cmake @@ -28,17 +28,17 @@ elseif(UNIX) endif() -# set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections") -# -# set(CMAKE_ASM_FLAGS "-c ${MCU_FLAGS} -x assembler-with-cpp -Wa,-mimplicit-it=thumb -gdwarf-2") -# -# set(CMAKE_C_FLAGS "${MCU_FLAGS} -Dgcc -w -Wno-unknown-pragmas -O0 -gdwarf-2 -g") -# set(CMAKE_C_FLAGS_DEBUG "-O0 -gdwarf-2 -g") -# set(CMAKE_C_FLAGS_RELEASE "-O3") -# -# set(LINKER_SCRIPT STM32F407ZGTX_FLASH.ld) -# set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} -Wl,--gc-sections,-Map=${PROJECT_NAME}.map,--cref,-u,Reset_Handler -T${LINKER_SCRIPT}") -# set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs -specs=nosys.specs -nostartfiles -T${LINKER_SCRIPT} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections") +set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections") + +set(CMAKE_ASM_FLAGS "-c ${MCU_FLAGS} -x assembler-with-cpp -Wa,-mimplicit-it=thumb -gdwarf-2") + +set(CMAKE_C_FLAGS "${MCU_FLAGS} -Dgcc -w -Wno-unknown-pragmas -O0 -gdwarf-2 -g") +set(CMAKE_C_FLAGS_DEBUG "-O0 -gdwarf-2 -g") +set(CMAKE_C_FLAGS_RELEASE "-O3") + +set(LINKER_SCRIPT STM32F407ZGTX_FLASH.ld) +set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} -Wl,--gc-sections,-Map=${PROJECT_NAME}.map,--cref,-u,Reset_Handler -T${LINKER_SCRIPT}") +#set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs -specs=nosys.specs -nostartfiles -T${LINKER_SCRIPT} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections") # find additional toolchain executables diff --git a/src/App/main.c b/src/App/main.c index 200c1d8..2a48689 100644 --- a/src/App/main.c +++ b/src/App/main.c @@ -35,6 +35,11 @@ int main(void) // // svmpw() while (1) { + // hall_handle.control_elec_angle++; + // foc_handle.Valphabeta = math_rev_park(foc_handle.Vqd, hall_handle.control_elec_angle); + // svmpw(foc_handle); + // 测试PWM输出 + led_ctrl(1); HAL_Delay(500); led_ctrl(0); diff --git a/src/Bsp/board.c b/src/Bsp/board.c index 567f7fd..8884564 100644 --- a/src/Bsp/board.c +++ b/src/Bsp/board.c @@ -61,6 +61,32 @@ void SystemClock_Config(void) } } +/** + * @brief NVIC Configuration. + * @retval None + */ +static void MX_NVIC_Init(void) +{ + /* ADC_IRQn interrupt configuration */ + // HAL_NVIC_SetPriority(ADC_IRQn, 2, 0); + // HAL_NVIC_EnableIRQ(ADC_IRQn); + /* TIM1_UP_TIM10_IRQn interrupt configuration */ + // HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0); + // HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); + /* TIM1_BRK_TIM9_IRQn interrupt configuration */ + // HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 4, 1); + // HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn); + /* TIM3_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0); + HAL_NVIC_EnableIRQ(TIM3_IRQn); + /* USART1_IRQn interrupt configuration */ + // HAL_NVIC_SetPriority(USART1_IRQn, 3, 1); + // HAL_NVIC_EnableIRQ(USART1_IRQn); + /* EXTI2_IRQn interrupt configuration */ + // HAL_NVIC_SetPriority(EXTI2_IRQn, 3, 0); + // HAL_NVIC_EnableIRQ(EXTI2_IRQn); +} + /** * This function will initial your board. */ @@ -74,6 +100,8 @@ void board_init(void) SystemCoreClockUpdate(); MX_GPIO_Init(); MX_USART1_UART_Init(); + // MX_TIM1_Init(); + MX_TIM3_Init(); } /** diff --git a/src/Bsp/board.h b/src/Bsp/board.h index 7906109..cf6390b 100644 --- a/src/Bsp/board.h +++ b/src/Bsp/board.h @@ -23,6 +23,7 @@ extern "C" #include "usart.h" #include "gpio.h" +#include "timer.h" #include "foc.h" diff --git a/src/Foc/foc.h b/src/Foc/foc.h index 8847ae7..414c4ad 100644 --- a/src/Foc/foc.h +++ b/src/Foc/foc.h @@ -8,6 +8,9 @@ * @brief */ +#ifndef __FOC_H__ +#define __FOC_H__ + #include "stm32f407xx.h" #include "type.h" @@ -18,12 +21,15 @@ typedef struct { uint8_t sector; // 扇区 - + qd_t Vqd; + alphabeta_t Valphabeta; uint16_t pwmA; uint16_t pwmB; uint16_t pwmC; uint16_t pwm_period; -} foc_handle_t; \ No newline at end of file +} foc_handle_t; + +#endif /* __FOC_H__ */ diff --git a/src/Foc/hall.h b/src/Foc/hall.h index b9613a8..efff642 100644 --- a/src/Foc/hall.h +++ b/src/Foc/hall.h @@ -7,6 +7,10 @@ * * @brief */ + +#ifndef __FOC_HALL_H__ +#define __FOC_HALL_H__ + #include "stm32f407xx.h" #define HALL_STATE_0 (uint8_t)0 @@ -39,3 +43,5 @@ typedef struct int16_t control_elec_angle; // 控制转子电角度 } hall_handle_t; + +#endif /* __FOC_HALL_H__ */ diff --git a/src/Foc/math.h b/src/Foc/math.h index 0a494d5..c9a505e 100644 --- a/src/Foc/math.h +++ b/src/Foc/math.h @@ -1,3 +1,16 @@ +/** + * @file math.h + * @author jimingqing + * @date 2023-08-29 + * @version 0.0.1 + * @copyright XiazhiTech Copyright (c) 2023 + * + * @brief + */ + +#ifndef __FOC_MATH_H__ +#define __FOC_MATH_H__ + #include "type.h" #define SQRT_2 1.4142 @@ -51,3 +64,5 @@ alphabeta_t math_rev_park(qd_t Input, int16_t Theta); * @retval int32_t Square root of Input (0 if Input<0) */ int32_t math_sqrt(int32_t wInput); + +#endif /* __FOC_MATH_H__ */ diff --git a/src/Foc/svpwm.c b/src/Foc/svpwm.c index f39cb75..2287d66 100644 --- a/src/Foc/svpwm.c +++ b/src/Foc/svpwm.c @@ -1,12 +1,12 @@ #include "foc.h" -void svpwm(foc_handle_t *handle, alphabeta_t v) +void svpwm(foc_handle_t *handle) { int32_t wX, wY, wZ, wUAlpha, wUBeta, wTimePhA, wTimePhB, wTimePhC; - // 后续还要根据该变量算三相占空比,所以这里乘以了周期T - wUAlpha = v.alpha * (int32_t)(1.732051f * handle->pwm_period) * 2; - wUBeta = -(v.beta * (int32_t)(handle->pwm_period)) * 2; + // 后续还要根据该变量算三相占空比,所以这里乘了系数2T + wUAlpha = handle->Valphabeta.alpha * (int32_t)(1.732051f * handle->pwm_period) * 2; + wUBeta = -(handle->Valphabeta.beta * (int32_t)(handle->pwm_period)) * 2; wX = wUBeta; wY = (wUBeta + wUAlpha) / 2; diff --git a/src/Foc/svpwm.h b/src/Foc/svpwm.h index b2548b3..33168b7 100644 --- a/src/Foc/svpwm.h +++ b/src/Foc/svpwm.h @@ -8,6 +8,9 @@ * @brief */ +#ifndef __FOC_SVPWM_H__ +#define __FOC_SVPWM_H__ + #include "stm32f407xx.h" #define SECTOR_1 0u @@ -17,3 +20,4 @@ #define SECTOR_5 4u #define SECTOR_6 5u +#endif /* __FOC_SVPWM_H__ */ \ No newline at end of file diff --git a/src/Foc/type.h b/src/Foc/type.h index e3057d3..ee6de3a 100644 --- a/src/Foc/type.h +++ b/src/Foc/type.h @@ -1,5 +1,20 @@ +/** + * @file type.h + * @author jimingqing + * @date 2023-08-29 + * @version 0.0.1 + * @copyright XiazhiTech Copyright (c) 2023 + * + * @brief + */ + +#ifndef __FOC_TYPE_H__ +#define __FOC_TYPE_H__ + #include "stm32f407xx.h" + + /** * @brief Two components q, d type definition */ @@ -23,4 +38,6 @@ typedef struct { int16_t alpha; int16_t beta; -} alphabeta_t; \ No newline at end of file +} alphabeta_t; + +#endif /* __FOC_TYPE_H__ */ \ No newline at end of file