From 1a4ea70147216f586f1341d4f1981733ed2c203c Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Sat, 03 Jul 2021 21:41:30 +0800
Subject: [PATCH] 采用三个外部中断触发

---
 USR/SRC/Motor.c |  157 ++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 108 insertions(+), 49 deletions(-)

diff --git a/USR/SRC/Motor.c b/USR/SRC/Motor.c
index 416681b..ca17a04 100644
--- a/USR/SRC/Motor.c
+++ b/USR/SRC/Motor.c
@@ -4,10 +4,24 @@
 #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 = 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  ��
@@ -19,8 +33,8 @@
     exti_interrupt_enable(HALL_A_EXTI);
     exti_interrupt_enable(HALL_B_EXTI);
     exti_interrupt_enable(HALL_C_EXTI);
-    StartSpeedTime();    //start speed timer
-    motor_phasechange(); // ִ��һ�λ���
+    StartSpeedTime(); //start speed timer
+    // motor_phasechange(); // ִ��һ�λ���
 
     motor_drive.enable_flag = 1;
 }
@@ -48,19 +62,19 @@
     /* ��ȡ���������� U ��״̬ */
     if (Get_HallSensorA_State())
     {
-        state |= 0x01U << 0;
+        state |= (0x01U << 0);
     }
 
     /* ��ȡ���������� V ��״̬ */
     if (Get_HallSensorB_State())
     {
-        state |= 0x01U << 1;
+        state |= (0x01U << 1);
     }
 
     /* ��ȡ���������� W ��״̬ */
     if (Get_HallSensorC_State())
     {
-        state |= 0x01U << 2;
+        state |= (0x01U << 2);
     }
 
     return state; // ���ش�����״̬
@@ -73,15 +87,16 @@
     float f = 0;
 
     /* �����ٶȣ�
-     ���ÿתһȦ����12�����壬(1.0/(64000000.0/6400)Ϊ�����������ڣ�(1.0/(48000000.0/6400) * 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 / (64000000.0f / SPEED_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);
@@ -191,14 +206,35 @@
   */
 void HAL_HallExti_TriggerCallback(void)
 {
-    /* ��ȡ��������������״̬,��Ϊ��������� */
-    if (exti_interrupt_flag_get(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI)) // �ж��Ƿ��ɴ����жϲ���
+    uint8_t step = 0;
+
+    // /* ��ȡ��������������״̬,��Ϊ��������� */
+    // if (exti_interrupt_flag_get(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI)) // �ж��Ƿ��ɴ����жϲ���
+    // {
+
+    //     // exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI);
+    // }
+    for (step = 0; step < 30; step++)
     {
-        // update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1));//TODO ����ʱ������ü���ʱ��
-        motor_drive.timeout = 0;
-        exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI);
     }
-    motor_phasechange();
+    motor_step = get_hall_state();
+    // update_motor_speed(step, __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1));//TODO ����ʱ������ü���ʱ��
+    // update_motor_speed(step, 300u * GetSpeedTimerOutcnt() + timer_counter_read(TIMER2));
+    SEGGER_RTT_printf(0, "Hall state is:%d!\n", motor_step);
+    motor_drive.timeout = 0;
+    // 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); // ������������¼�����ʱ�Ž�����д��
 }
 
@@ -215,18 +251,21 @@
     /* next step: step 2 configuration .A-C` breakover---------------------------- */
     case 1:
         /*  channel U configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM1);
+        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;
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_DISABLE);
 
         /*  channel V configuration */
         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_DISABLE;
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
 
         /*  channel W configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM1);
+        // 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_ENABLE;
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_ENABLE);
 
         step++;
         break;
@@ -236,16 +275,19 @@
         /*  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_DISABLE;
 
         /*  channel V configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM1);
+        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_ENABLE);
-        timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
+        MOTOR_V_L_DISABLE;
 
         /*  channel W configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM1);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_ENABLE);
+        MOTOR_W_L_ENABLE;
 
         step++;
         break;
@@ -253,18 +295,21 @@
     /* next step: step 4 configuration .B-A` breakover---------------------------- */
     case 3:
         /*  channel U configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM1);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_ENABLE);
+        MOTOR_U_L_ENABLE;
 
         /*  channel V configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_V, TIMER_OC_MODE_PWM1);
+        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_ENABLE);
-        timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
+        MOTOR_W_L_DISABLE;
 
         step++;
         break;
@@ -272,18 +317,21 @@
     /* next step: step 5 configuration .C-A` breakover---------------------------- */
     case 4:
         /*  channel U configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM1);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_U, TIMER_CCXN_ENABLE);
+        MOTOR_U_L_ENABLE;
 
         /*  channel V configuration */
         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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_DISABLE);
+        MOTOR_V_L_DISABLE;
 
         /*  channel W configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM1);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
+        MOTOR_W_L_DISABLE;
 
         step++;
         break;
@@ -292,36 +340,41 @@
     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);
+        // 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_PWM1);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
+        MOTOR_V_L_ENABLE;
 
         /*  channel W configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_W, TIMER_OC_MODE_PWM1);
+        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);
-
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
+        MOTOR_W_L_DISABLE;
         step++;
         break;
 
     /* next step: step 1 configuration .A-B` breakover---------------------------- */
     case 6:
         /*  channel U configuration */
-        timer_channel_output_mode_config(TIMER0, MOTOR_OUT_CH_U, TIMER_OC_MODE_PWM1);
+        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);
+        // 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_PWM1);
+        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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_V, TIMER_CCXN_ENABLE);
+        MOTOR_V_L_ENABLE;
 
         /*  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);
+        // timer_channel_complementary_output_state_config(TIMER0, MOTOR_OUT_CH_W, TIMER_CCXN_DISABLE);
+        MOTOR_W_L_DISABLE;
 
         step = 1;
         break;
@@ -336,11 +389,14 @@
 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_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_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);
+    // 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;
 }
 
 /**
@@ -352,7 +408,10 @@
 {
     /* ���ö�ʱ��ͨ����� PWM ��ռ�ձ� */
     motor_pluse = pulse;
-    //SetPwmDuty();
+    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();

--
Gitblit v1.8.0