From b600aa63575adb721f61e131c8369545db393090 Mon Sep 17 00:00:00 2001 From: MQjehovah <1421706030@qq.com> Date: Fri, 1 Sep 2023 18:30:05 +0800 Subject: [PATCH] fix --- build.cmd | 2 +- project/cmake/CMakeLists.txt | 22 +++++------- project/cmake/toolchain-arm-none-eabi.cmake | 5 +-- src/App/main.c | 28 +++++++++++---- src/Bsp/board.c | 40 +++------------------ src/Bsp/stm32f4xx_hal_conf.h | 2 +- src/Bsp/usart.c | 9 ++--- src/Foc/hall.c | 2 -- 8 files changed, 42 insertions(+), 68 deletions(-) diff --git a/build.cmd b/build.cmd index d2975b6..7197282 100644 --- a/build.cmd +++ b/build.cmd @@ -1,3 +1,3 @@ cd project/cmake cmake -DCMAKE_TOOLCHAIN_FILE="toolchain-arm-none-eabi.cmake" -G "MinGW Makefiles" .\CMakeLists.txt -mingw32-make \ No newline at end of file +mingw32-make -j16 \ No newline at end of file diff --git a/project/cmake/CMakeLists.txt b/project/cmake/CMakeLists.txt index 45ca422..005a0e6 100644 --- a/project/cmake/CMakeLists.txt +++ b/project/cmake/CMakeLists.txt @@ -132,18 +132,12 @@ add_executable(${PROJECT_NAME}.elf # ${PROJECT_NAME} # ) -# set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf) -# set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) -# set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) +set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf) +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} -Obinary ${ELF_FILE} ${BIN_FILE} -# COMMAND ${CMAKE_OBJCOPY} -Oihex ${ELF_FILE} ${HEX_FILE} -# COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex" - -# COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex" -# COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin" - -# COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex -# COMMENT "Invoking: Cross ARM GNU Print Size" -# ) +add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -Obinary ${ELF_FILE} ${BIN_FILE} + COMMAND ${CMAKE_OBJCOPY} -Oihex ${ELF_FILE} ${HEX_FILE} + COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex +) diff --git a/project/cmake/toolchain-arm-none-eabi.cmake b/project/cmake/toolchain-arm-none-eabi.cmake index d663b57..67e0a9d 100644 --- a/project/cmake/toolchain-arm-none-eabi.cmake +++ b/project/cmake/toolchain-arm-none-eabi.cmake @@ -12,8 +12,9 @@ if (WIN32) set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}gcc.exe) set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}gcc.exe) set(CMAKE_AR ${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}ar.exe) - set(CMAKE_OBJCOPY arm-none-eabi-objcopy) - set(CMAKE_OBJDUMP arm-none-eabi-objdump) + set(CMAKE_OBJCOPY ${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}objcopy.exe) + set(CMAKE_OBJDUMP ${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}objdump.exe) + set(CMAKE_SIZE ${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}size.exe) elseif(UNIX) diff --git a/src/App/main.c b/src/App/main.c index c9d19d3..66af4d8 100644 --- a/src/App/main.c +++ b/src/App/main.c @@ -9,12 +9,14 @@ */ /* Includes ------------------------------------------------------------------*/ +#include + #include "main.h" hall_handle_t hall_handle; foc_handle_t foc_handle; -const char *data = "hello world"; +const char *data = "hello world\n"; /* Function ------------------------------------------------------------------*/ /** @@ -24,7 +26,6 @@ const char *data = "hello world"; int main(void) { board_init(); - foc_handle.pwm_period = 168 * 1000000 / 16000; // while (1) @@ -57,12 +58,11 @@ int main(void) // // svmpw() while (1) { - HAL_UART_Transmit(&huart1, data, 1, 0xFFFF); - // hall_handle.control_elec_angle++; - // foc_handle.Valphabeta = math_rev_park(foc_handle.Vqd, hall_handle.control_elec_angle); - // svmpw(foc_handle); // 测试PWM输出 // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 42 * 1000000 / 16000); + + // printf("direction is %d", hall_handle.direction); + led_ctrl(1); HAL_Delay(500); led_ctrl(0); @@ -70,4 +70,20 @@ int main(void) } } +/// 重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数 +int fputc(int ch, FILE *f) +{ + /* 发送一个字节数据到串口DEBUG_USART */ + HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); + return (ch); +} + +/// 重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数 +int fgetc(FILE *f) +{ + int ch; + HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xFFFF); + return (ch); +} + /* END OF FILE ---------------------------------------------------------------*/ diff --git a/src/Bsp/board.c b/src/Bsp/board.c index 2ae936f..5e02c03 100644 --- a/src/Bsp/board.c +++ b/src/Bsp/board.c @@ -42,7 +42,7 @@ void SystemClock_Config(void) RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLQ = 4; + RCC_OscInitStruct.PLL.PLLQ = 7; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); @@ -65,7 +65,7 @@ void SystemClock_Config(void) * @brief NVIC Configuration. * @retval None */ -static void MX_NVIC_Init(void) +void MX_NVIC_Init(void) { /* ADC_IRQn interrupt configuration */ // HAL_NVIC_SetPriority(ADC_IRQn, 2, 0); @@ -91,41 +91,10 @@ void FOC_Init() { HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // 开启三个PWM通道输出 HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); // 开启三个PWM通道输出 + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); // 开启三个PWM通道输出 + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3); - - // LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000); - // LL_TIM_OC_SetCompareCH2(TIM1, 42 * 1000000 / 16000); - // LL_TIM_OC_SetCompareCH3(TIM1, 42 * 1000000 / 16000); - // LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000); - // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2400); - // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2400); - - // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); - // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); - - /* wait for a new PWM period */ - // LL_TIM_ClearFlag_UPDATE(TIM1); - // // while (LL_TIM_IsActiveFlag_UPDATE(TIM1) == 0) - // // { - // // } - // /* Clear Update Flag */ - // LL_TIM_ClearFlag_UPDATE(TIM1); - - /* Main PWM Output Enable */ - // TIM1->BDTR |= LL_TIM_OSSI_ENABLE; - // LL_TIM_EnableAllOutputs(TIM1); - - // /* Clear Update Flag */ - // LL_TIM_ClearFlag_UPDATE(TIM1); - // /* Enable Update IRQ */ - // LL_TIM_EnableIT_UPDATE(TIM1); - - // LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000); - // LL_TIM_OC_SetCompareCH2(TIM1, 42 * 1000000 / 16000); - // LL_TIM_OC_SetCompareCH3(TIM1, 42 * 1000000 / 16000); } /** @@ -138,7 +107,6 @@ void board_init(void) HAL_Init(); SystemClock_Config(); - // SystemCoreClockUpdate(); MX_GPIO_Init(); MX_USART1_UART_Init(); diff --git a/src/Bsp/stm32f4xx_hal_conf.h b/src/Bsp/stm32f4xx_hal_conf.h index 45cb5d4..3d6df74 100644 --- a/src/Bsp/stm32f4xx_hal_conf.h +++ b/src/Bsp/stm32f4xx_hal_conf.h @@ -95,7 +95,7 @@ * (when HSE is used as system clock source, directly or through the PLL). */ #if !defined (HSE_VALUE) - #define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */ + #define HSE_VALUE 25000000U /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ #if !defined (HSE_STARTUP_TIMEOUT) diff --git a/src/Bsp/usart.c b/src/Bsp/usart.c index c2de5e5..2f0e157 100644 --- a/src/Bsp/usart.c +++ b/src/Bsp/usart.c @@ -53,7 +53,7 @@ void MX_USART1_UART_Init(void) void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - + if (uartHandle->Instance == USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ @@ -69,14 +69,11 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) */ GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLUP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USART1 interrupt Init */ - HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_MspInit 1 */ diff --git a/src/Foc/hall.c b/src/Foc/hall.c index 169bdca..88a0080 100644 --- a/src/Foc/hall.c +++ b/src/Foc/hall.c @@ -7,8 +7,6 @@ */ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) { - - switch (new_state) { case HALL_STATE_5: