tao_z
2021-07-10 4a89e24804b91902def506a9e898293fa36ccb59
USR/SRC/Motor.c
@@ -1,3 +1,27 @@
#include "gpio.h"
#include "motor.h"
#include "gd32e23x_exti.h"
#include "gd32e23x_timer.h"
#include "bldc_ctrl.h"
#include "pwm.h"
#include "string.h"
#include "SEGGER_RTT_Conf.h"
#include "SEGGER_RTT.h"
static volatile motor_rotate_t motor_drive = {0};
static uint32_t motor_pluse = 1500;
static volatile uint8_t motor_step = 0;
static void motor_phasechange(void);
static void update_speed_dir(uint8_t dir_in);
extern void Motor_Init(void)
{
    motor_drive.timeout = 0;
    motor_drive.speed = 0;
    motor_drive.enable_flag = 0;
    memset(motor_drive.speed_group, 0, SPEED_FILTER_NUM);
    hall_enable();
};
/**
  * @brief  Ê¹ÄÜ»ô¶û´«¸ÐÆ÷
  * @param  ÎÞ
@@ -6,14 +30,11 @@
void hall_enable(void)
{
    /* Ê¹ÄÜ»ô¶û´«¸ÐÆ÷½Ó¿Ú */
    __HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_TRIGGER);
    __HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_UPDATE);
    HAL_TIMEx_HallSensor_Start(&htimx_hall);
    LED1_OFF;
    HAL_TIM_TriggerCallback(&htimx_hall); // Ö´ÐÐÒ»´Î»»Ïà
    exti_interrupt_enable(HALL_A_EXTI);
    exti_interrupt_enable(HALL_B_EXTI);
    exti_interrupt_enable(HALL_C_EXTI);
    StartSpeedTime();    //start speed timer
    motor_phasechange(); // Ö´ÐÐÒ»´Î»»Ïà
    motor_drive.enable_flag = 1;
}
@@ -26,9 +47,11 @@
void hall_disable(void)
{
    /* ½ûÓûô¶û´«¸ÐÆ÷½Ó¿Ú */
    __HAL_TIM_DISABLE_IT(&htimx_hall, TIM_IT_TRIGGER);
    __HAL_TIM_DISABLE_IT(&htimx_hall, TIM_IT_UPDATE);
    HAL_TIMEx_HallSensor_Stop(&htimx_hall);
    exti_interrupt_disable(HALL_A_EXTI);
    exti_interrupt_disable(HALL_B_EXTI);
    exti_interrupt_disable(HALL_C_EXTI);
    // HAL_TIMEx_HallSensor_Stop(&htimx_hall);
    StopSpeedTime(); //stop speed timer
    motor_drive.enable_flag = 0;
    motor_drive.speed = 0;
}
@@ -36,32 +59,27 @@
uint8_t get_hall_state(void)
{
    uint8_t state = 0;
#if 1
    /* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ U µÄ״̬ */
    if (HAL_GPIO_ReadPin(HALL_INPUTU_GPIO_PORT, HALL_INPUTU_PIN) != GPIO_PIN_RESET)
    if (Get_HallSensorA_State())
    {
        state |= 0x01U << 0;
        state |= (0x01U << 0);
    }
    /* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ V µÄ״̬ */
    if (HAL_GPIO_ReadPin(HALL_INPUTV_GPIO_PORT, HALL_INPUTV_PIN) != GPIO_PIN_RESET)
    if (Get_HallSensorB_State())
    {
        state |= 0x01U << 1;
        state |= (0x01U << 1);
    }
    /* ¶ÁÈ¡»ô¶û´«¸ÐÆ÷ W µÄ״̬ */
    if (HAL_GPIO_ReadPin(HALL_INPUTW_GPIO_PORT, HALL_INPUTW_PIN) != GPIO_PIN_RESET)
    if (Get_HallSensorC_State())
    {
        state |= 0x01U << 2;
        state |= (0x01U << 2);
    }
#else
    state = (GPIOH->IDR >> 10) & 7; // ¶Á 3 ¸ö»ô¶û´«¸ÐÆ÷µÄ״̬
#endif
    return state; // ·µ»Ø´«¸ÐÆ÷״̬
}
static uint8_t count = 0;
static void update_motor_speed(uint8_t dir_in, uint32_t time)
{
    int speed_temp = 0;
@@ -69,15 +87,16 @@
    float f = 0;
    /* ¼ÆËãËÙ¶È£º
     µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(72000000.0/128.0)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(72000000.0/128.0) * time)Ϊʱ¼ä³¤¡£
     µç»úÿתһȦ¹²ÓÃ12¸öÂö³å£¬(1.0/(48000000.0/6400)Ϊ¼ÆÊýÆ÷µÄÖÜÆÚ£¬(1.0/(48000000.0/6400) * time)Ϊʱ¼ä³¤¡£
  */
    if (time == 0)
        motor_drive.speed_group[count++] = 0;
    else
    {
        f = (1.0f / (72000000.0f / HALL_PRESCALER_COUNT) * time);
        f = (1.0f / 12.0f) / (f / 60.0f);
        // f = (1.0f / (48000000.0f / SPEED_PRESCALER_COUNT) * time);
        // f = (1.0f / 12.0f) / (f / 60.0f);
        f = time;
        motor_drive.speed_group[count++] = f;
    }
    update_speed_dir(dir_in);
@@ -182,164 +201,231 @@
/**
  * @brief  »ô¶û´«¸ÐÆ÷´¥·¢»Øµ÷º¯Êý
  * @param  htim:¶¨Ê±Æ÷¾ä±ú
  * @param  void
  * @retval ÎÞ
  */
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
void HAL_HallExti_TriggerCallback(void)
{
    /* »ñÈ¡»ô¶û´«¸ÐÆ÷Òý½Å״̬,×÷Ϊ»»ÏàµÄÒÀ¾Ý */
    uint8_t step = 0;
    step = get_hall_state();
    if (htim == &htimx_hall) // ÅжÏÊÇ·ñÓÉ´¥·¢ÖжϲúÉú
    motor_step = get_hall_state();
    // /* »ñÈ¡»ô¶û´«¸ÐÆ÷Òý½Å״̬,×÷Ϊ»»ÏàµÄÒÀ¾Ý */
    if (exti_interrupt_flag_get(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI)) // ÅжÏÊÇ·ñÓÉ´¥·¢ÖжϲúÉú
    {
        update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1));
        // update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1)); //TODO Ö÷¶¨Ê±Æ÷´¦»ñµÃ¼ÆÊýʱ¼ä
        update_motor_speed(motor_step, 300u * GetSpeedTimerOutcnt() + timer_counter_read(TIMER2));
        SEGGER_RTT_printf(0, "Hall state is:%d!\n", motor_step);
        motor_drive.timeout = 0;
        //     // exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI);
    }
    // if (RESET != exti_interrupt_flag_get(EXTI_4))
    // {
    //     SEGGER_RTT_printf(0, "HALL_A_EXTI  triggle!\n");
    // }
    // else if (RESET != exti_interrupt_flag_get(EXTI_5))
    // {
    //     SEGGER_RTT_printf(0, "HALL_C_EXTI  triggle!\n");
    // }
    // else if (RESET != exti_interrupt_flag_get(EXTI_15))
    // {
    //     SEGGER_RTT_printf(0, "HALL_B_EXTI  triggle!\n");
    // }
    motor_phasechange();
    // HAL_TIM_GenerateEvent(&htimx_bldcm, TIM_EVENTSOURCE_COM); // Èí¼þ²úÉú»»Ïàʼþ£¬´Ëʱ²Å½«ÅäÖÃдÈë
}
static void motor_phasechange(void)
{
    uint8_t step = 0;
    step = get_hall_state();
    if (get_bldcm_direction() == MOTOR_FWD)
    {
        switch (step)
        {
        case 1:                                                                            /* U+ W- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        step = 7u - step;
    }
    switch (step)
    {
    /* next step: step 3 configuration .W-U` breakover---------------------------- */
    case 1:
        /*  channel U configuration */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        MOTOR_U_L_ENABLE;
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse);             // Í¨µÀ 1 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        /*  channel V configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        MOTOR_V_L_DISABLE;
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        case 2:                                                                            /* V+ U- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        /*  channel W configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE);
        MOTOR_W_L_DISABLE;
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_ENABLE);
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        break;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse);             // Í¨µÀ 2 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
    /* next step: step 6 configuration .u-v` breakover---------------------------- */
    case 2:
        /*  channel U configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_ENABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        MOTOR_U_L_DISABLE;
            break;
        /*  channel V configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        MOTOR_V_L_ENABLE;
        case 3:                                                                            /* V+ W- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        /*  channel W configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_ENABLE);
        MOTOR_W_L_DISABLE;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        break;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse);             // Í¨µÀ 2 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
    /* next step: step 2 configuration .W-V` breakover---------------------------- */
    case 3:
        /*  channel U configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_ENABLE);
        MOTOR_U_L_DISABLE;
        case 4:                                                                            /* W+ V- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        /*  channel V configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        MOTOR_V_L_ENABLE;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        /*  channel W configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_ENABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_DISABLE;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse);             // Í¨µÀ 3 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        break;
        case 5:                                                                            /* U+  V -*/
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
    /* next step: step 5 configuration .V-W` breakover---------------------------- */
    case 4:
        /*  channel U configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_ENABLE);
        MOTOR_U_L_DISABLE;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        /*  channel V configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
        MOTOR_V_L_DISABLE;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse);             // Í¨µÀ 1 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        /*  channel W configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_ENABLE;
        case 6:                                                                            /* W+ U- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
        break;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
    /* next step: step 1 configuration .V-U` breakover---------------------------- */
    case 5:
        /*  channel U configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        MOTOR_U_L_ENABLE;
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse);             // Í¨µÀ 3 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        }
        /*  channel V configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        MOTOR_V_L_DISABLE;
        /*  channel W configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_DISABLE;
        break;
    /* next step: step 4 configuration .U-W` breakover---------------------------- */
    case 6:
        /*  channel U configuration */
        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCX_ENABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
        MOTOR_U_L_DISABLE;
        /*  channel V configuration */
        // timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM0);
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
        MOTOR_V_L_DISABLE;
        /*  channel W configuration */
        timer_channel_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCX_DISABLE);
        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
        MOTOR_W_L_ENABLE;
        break;
    }
}
/**
  * @brief  Í£Ö¹pwmÊä³ö
  * @param  ÎÞ
  * @retval ÎÞ
  */
extern void stop_pwm_output(void)
{
    timer_channel_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCX_DISABLE);
    // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_0, TIMER_CCXN_DISABLE);
    timer_channel_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCX_DISABLE);
    // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_1, TIMER_CCXN_DISABLE);
    timer_channel_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCX_DISABLE);
    // timer_channel_complementary_output_state_config(TIMER0, TIMER_CH_2, TIMER_CCXN_DISABLE);
    MOTOR_W_L_DISABLE;
    MOTOR_V_L_DISABLE;
    MOTOR_U_L_DISABLE;
}
/**
  * @brief  ÉèÖÃpwmÊä³öµÄÕ¼¿Õ±È
  * @param  pulse:ÒªÉèÖõÄÕ¼¿Õ±È
  * @retval ÎÞ
  */
void set_pwm_pulse(uint16_t pulse)
{
    /* ÉèÖö¨Ê±Æ÷ͨµÀÊä³ö PWM µÄÕ¼¿Õ±È */
    motor_pluse = pulse;
    SetPwmDuty(TIMER_CH_0, motor_pluse);
    SetPwmDuty(TIMER_CH_1, motor_pluse);
    SetPwmDuty(TIMER_CH_2, motor_pluse);
    if (motor_drive.enable_flag)
    {
        motor_phasechange();
    }
}
extern void BLDC_SpeedAndPID(void)
{
    if (motor_drive.timeout++ > 100) // ÓÐÒ»´ÎÔÚ²úÉú¸üÐÂÖжÏǰ»ô¶û´«¸ÐÆ÷ûÓв¶»ñµ½Öµ
    {
        // printf("¶Âת³¬Ê±\r\n");
        motor_drive.timeout = 0;
        /* ¶Âת³¬Ê±Í£Ö¹ PWM Êä³ö */
        hall_disable();    // ½ûÓûô¶û´«¸ÐÆ÷½Ó¿Ú
        stop_pwm_output(); // Í£Ö¹ PWM Êä³ö
        set_bldcm_disable();
        motor_drive.speed = 0;
    }
    else
    {
        switch (step)
        {
        case 1:                                                                            /* W+ U- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse);             // Í¨µÀ 3 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        case 2:                                                                            /* U+  V -*/
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse);             // Í¨µÀ 1 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        case 3:                                                                            /* W+ V- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, bldcm_pulse);             // Í¨µÀ 3 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        case 4:                                                                            /* V+ W- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse);             // Í¨µÀ 2 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        case 5:                                                                            /* V+ U- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, 0);                         // Í¨µÀ 1 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, bldcm_pulse);             // Í¨µÀ 2 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        case 6:                                                                            /* U+ W- */
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_2, 0);                         // Í¨µÀ 2 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM2_GPIO_PORT, MOTOR_OCNPWM2_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_3, 0);                         // Í¨µÀ 3 ÅäÖÃΪ 0
            HAL_GPIO_WritePin(MOTOR_OCNPWM1_GPIO_PORT, MOTOR_OCNPWM1_PIN, GPIO_PIN_RESET); // ¹Ø±ÕÏÂÇűÛ
            __HAL_TIM_SET_COMPARE(&htimx_bldcm, TIM_CHANNEL_1, bldcm_pulse);             // Í¨µÀ 1 ÅäÖõÄÕ¼¿Õ±È
            HAL_GPIO_WritePin(MOTOR_OCNPWM3_GPIO_PORT, MOTOR_OCNPWM3_PIN, GPIO_PIN_SET); // ¿ªÆôÏÂÇűÛ
            break;
        }
        bldcm_pid_control();
    }
    HAL_TIM_GenerateEvent(&htimx_bldcm, TIM_EVENTSOURCE_COM); // Èí¼þ²úÉú»»Ïàʼþ£¬´Ëʱ²Å½«ÅäÖÃдÈë
}