diff --git a/src/App/main.c b/src/App/main.c index 7dac36e..c9d19d3 100644 --- a/src/App/main.c +++ b/src/App/main.c @@ -14,6 +14,7 @@ hall_handle_t hall_handle; foc_handle_t foc_handle; +const char *data = "hello world"; /* Function ------------------------------------------------------------------*/ /** @@ -25,6 +26,7 @@ int main(void) board_init(); foc_handle.pwm_period = 168 * 1000000 / 16000; + // while (1) // { // int segment = 60; @@ -55,6 +57,7 @@ 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); diff --git a/src/App/main.h b/src/App/main.h index 24c459c..c5134b1 100644 --- a/src/App/main.h +++ b/src/App/main.h @@ -22,6 +22,9 @@ extern "C" /* Functions ----------------------------------------------------------------*/ + extern hall_handle_t hall_handle; + extern foc_handle_t foc_handle; + #ifdef __cplusplus } #endif diff --git a/src/Bsp/board.c b/src/Bsp/board.c index 3c4f4f7..2ae936f 100644 --- a/src/Bsp/board.c +++ b/src/Bsp/board.c @@ -138,12 +138,12 @@ void board_init(void) HAL_Init(); SystemClock_Config(); - SystemCoreClockUpdate(); + // SystemCoreClockUpdate(); MX_GPIO_Init(); MX_USART1_UART_Init(); MX_TIM1_Init(); - // MX_TIM3_Init(); + MX_TIM3_Init(); MX_NVIC_Init(); FOC_Init(); diff --git a/src/Bsp/board.h b/src/Bsp/board.h index 9eac1c2..38f6d0f 100644 --- a/src/Bsp/board.h +++ b/src/Bsp/board.h @@ -17,13 +17,14 @@ extern "C" #endif /* Includes ------------------------------------------------------------------*/ +#include "stm32f407xx.h" -#include "stm32f4xx_hal.h" +#include "stm32f4xx_ll_gpio.h" #include "stm32f4xx_ll_tim.h" +#include "stm32f4xx_hal.h" - -#include "usart.h" #include "gpio.h" +#include "usart.h" #include "timer.h" #include "foc.h" diff --git a/src/Bsp/timer.c b/src/Bsp/timer.c index 5a751f6..8f7637f 100644 --- a/src/Bsp/timer.c +++ b/src/Bsp/timer.c @@ -11,7 +11,7 @@ /* Includes ------------------------------------------------------------------*/ #include "timer.h" #include "board.h" -#include "stm32f4xx_ll_tim.h" +#include "main.h" TIM_HandleTypeDef htim1; TIM_HandleTypeDef htim3; @@ -20,7 +20,6 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base); void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base); - /** * @brief TIM1 Initialization Function * @param None @@ -118,6 +117,10 @@ void MX_TIM3_Init(void) TIM_HallSensor_InitTypeDef sConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; + /** + * 定时器主频48M + * + */ htim3.Instance = TIM3; htim3.Init.Prescaler = 0; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; @@ -342,6 +345,16 @@ void TIM3_IRQHandler(void) if (LL_TIM_IsActiveFlag_CC1(TIM3)) { LL_TIM_ClearFlag_CC1(TIM3); - // HALL_TIMx_CC_IRQHandler(&HALL_M1); + uint8_t new_state = 0; + if (hall_handle.placement_type == HALL_TYPE_DEGREES_120) + { + new_state = (uint8_t)((LL_GPIO_IsInputPinSet(GPIOC, GPIO_PIN_6) << 2) | (LL_GPIO_IsInputPinSet(GPIOC, GPIO_PIN_7) << 1) | LL_GPIO_IsInputPinSet(GPIOC, GPIO_PIN_8)); + } + else + { + new_state = (uint8_t)(((LL_GPIO_IsInputPinSet(GPIOC, GPIO_PIN_7) ^ 1) << 2) | (LL_GPIO_IsInputPinSet(GPIOC, GPIO_PIN_8) << 1) | LL_GPIO_IsInputPinSet(GPIOC, GPIO_PIN_6)); + } + + calc_hall_angle(&hall_handle, new_state); } } diff --git a/src/Bsp/usart.c b/src/Bsp/usart.c index a167be1..c2de5e5 100644 --- a/src/Bsp/usart.c +++ b/src/Bsp/usart.c @@ -52,8 +52,8 @@ 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 */ @@ -62,17 +62,17 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) /* USART1 clock enable */ __HAL_RCC_USART1_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ - GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; + GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USART1 interrupt Init */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); diff --git a/src/Bsp/usart.h b/src/Bsp/usart.h index fce5329..56ee080 100644 --- a/src/Bsp/usart.h +++ b/src/Bsp/usart.h @@ -18,7 +18,7 @@ extern "C" #endif /* Includes ------------------------------------------------------------------*/ -#include "main.h" +#include "stm32f4xx_hal.h" extern UART_HandleTypeDef huart1; diff --git a/src/Foc/hall.c b/src/Foc/hall.c index 88a0080..169bdca 100644 --- a/src/Foc/hall.c +++ b/src/Foc/hall.c @@ -7,6 +7,8 @@ */ void calc_hall_angle(hall_handle_t *handle, uint8_t new_state) { + + switch (new_state) { case HALL_STATE_5: diff --git a/src/Foc/hall.h b/src/Foc/hall.h index efff642..0d6d9d4 100644 --- a/src/Foc/hall.h +++ b/src/Foc/hall.h @@ -25,6 +25,9 @@ #define S16_120_PHASE_SHIFT (int16_t)(65536 / 3) #define S16_60_PHASE_SHIFT (int16_t)(65536 / 6) +#define HALL_TYPE_DEGREES_120 0u +#define HALL_TYPE_DEGREES_60 1u + enum { NEGATIVE = -1, @@ -36,6 +39,8 @@ typedef struct { uint8_t state; // 霍尔状态真值表 + uint8_t placement_type; // 霍尔排列方式 + int16_t phase_shift; // 同步电角度 int8_t direction; // 转子方向 @@ -44,4 +49,6 @@ typedef struct } hall_handle_t; +extern void calc_hall_angle(hall_handle_t *handle, uint8_t new_state); + #endif /* __FOC_HALL_H__ */