From b150690b6ebe42a4ffd50278d761b8994121eb94 Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Mon, 14 Jun 2021 22:18:45 +0800
Subject: [PATCH] 添加霍尔外部中断 motot模块初次提交尚未调试

---
 USR/SRC/Motor.c       |   43 +++++---
 USR/INC/bldc_ctrl.h   |   46 +++++++++
 USR/INC/motor.h       |   31 ++++++
 USR/SRC/bldc_ctrl.c   |  145 +++++++++++++++++++++++++++++
 USR/SRC/gd32e23x_it.c |   11 ++
 USR/INC/gd32e23x_it.h |    2 
 USR/SRC/pwm.c         |    6 
 7 files changed, 263 insertions(+), 21 deletions(-)

diff --git a/USR/INC/bldc_ctrl.h b/USR/INC/bldc_ctrl.h
new file mode 100644
index 0000000..3353ddb
--- /dev/null
+++ b/USR/INC/bldc_ctrl.h
@@ -0,0 +1,46 @@
+#ifndef BLDC_CONTROL_H
+#define BLDC_CONTROL_H
+#include "gpio.h"
+//���Ŷ���
+/*******************************************************/
+// ����������� SD ��
+#define SHUTDOWN_PIN GPIO_PIN_15
+#define SHUTDOWN_GPIO_PORT GPIOD
+// #define SHUTDOWN_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
+/*******************************************************/
+
+/* ��� SD or EN ʹ�ܽ� */
+#define BLDCM_ENABLE_SD()     \
+    do                        \
+    {                         \
+        Set_SDH2136_Enable(); \
+    } while (0) // �ߵ�ƽ��-�ߵ�ƽʹ��
+#define BLDCM_DISABLE_SD()     \
+    do                         \
+    {                          \
+        Set_SDH2136_Disable(); \
+    } while (0) // �͵�ƽ�ض�-�͵�ƽ����
+
+/* ����������ö�� */
+typedef enum
+{
+    MOTOR_FWD = 0,
+    MOTOR_REV,
+} motor_dir_t;
+
+typedef struct
+{
+    motor_dir_t direction; // �������
+    uint16_t dutyfactor;   // PWM ���ռ�ձ�
+    uint8_t is_enable;     // ʹ�ܵ��
+    uint32_t lock_timeout; // �����ת��ʱ
+} bldcm_data_t;
+
+void bldcm_init(void);
+void set_bldcm_speed(uint16_t v);
+void set_bldcm_direction(motor_dir_t dir);
+motor_dir_t get_bldcm_direction(void);
+void set_bldcm_enable(void);
+void set_bldcm_disable(void);
+void bldcm_pid_control(void);
+#endif
\ No newline at end of file
diff --git a/USR/INC/gd32e23x_it.h b/USR/INC/gd32e23x_it.h
index f995d4f..2aa91ff 100644
--- a/USR/INC/gd32e23x_it.h
+++ b/USR/INC/gd32e23x_it.h
@@ -58,5 +58,7 @@
 void PendSV_Handler(void);
 /* this function handles SysTick exception */
 void SysTick_Handler(void);
+/* this function handles XTI4-15  exception */
+void EXTI4_15_IRQHandler(void);
 
 #endif /* GD32E23X_IT_H */
diff --git a/USR/INC/motor.h b/USR/INC/motor.h
new file mode 100644
index 0000000..0ca8444
--- /dev/null
+++ b/USR/INC/motor.h
@@ -0,0 +1,31 @@
+#ifndef MOTOR_H
+#define MOTOR_H
+#include ""
+/* �������תʵ�ֽṹ�� */
+
+#define SPEED_FILTER_NUM 30 // �ٶ��˲�����
+
+typedef struct
+{
+    unsigned long timeout;     // ��ʱ�����¼���
+    float speed;               // ����ٶ� rps��ת/���ӣ�
+    unsigned long enable_flag; // ���ʹ�ܱ�־
+    signed int speed_group[SPEED_FILTER_NUM];
+} motor_rotate_t;
+
+/* �ۼ� TIM_Period�������һ�����»����ж�		
+	����ʱ����0������65535����Ϊ65535�Σ�Ϊһ���������� */
+#define HALL_PERIOD_COUNT (0xFFFF)
+
+/* ͨ�ÿ��ƶ�ʱ��ʱ��ԴTIMxCLK = HCLK = 72MHz
+	 �趨��ʱ��Ƶ��Ϊ = TIMxCLK / (PWM_PRESCALER_COUNT) / PWM_PERIOD_COUNT = 9.987Hz
+   ���� T = 100ms */
+#define HALL_PRESCALER_COUNT (110)
+
+#define HALL_A_EXTI (EXTI_4)
+#define HALL_B_EXTI (EXTI_5)
+#define HALL_C_EXTI (EXTI_15)
+
+extern void HAL_HallExti_TriggerCallback(void);
+
+#endif
\ No newline at end of file
diff --git a/USR/SRC/Motor.c b/USR/SRC/Motor.c
index 841048d..64b2163 100644
--- a/USR/SRC/Motor.c
+++ b/USR/SRC/Motor.c
@@ -1,3 +1,7 @@
+#include "gpio.h"
+#include "motor.h"
+#include "gd32e23x_exti.h"
+static motor_rotate_t motor_drive = {0};
 /**
   * @brief  ʹ�ܻ���������
   * @param  ��
@@ -6,14 +10,15 @@
 void hall_enable(void)
 {
     /* ʹ�ܻ����������ӿ� */
-    __HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_TRIGGER);
-    __HAL_TIM_ENABLE_IT(&htimx_hall, TIM_IT_UPDATE);
+    exti_interrupt_enable(HALL_A_EXTI);
+    exti_interrupt_enable(HALL_B_EXTI);
+    exti_interrupt_enable(HALL_C_EXTI);
 
-    HAL_TIMEx_HallSensor_Start(&htimx_hall);
+    // HAL_TIMEx_HallSensor_Start(&htimx_hall);
 
-    LED1_OFF;
+    // LED1_OFF;
 
-    HAL_TIM_TriggerCallback(&htimx_hall); // ִ��һ�λ���
+    // HAL_TIM_TriggerCallback(&htimx_hall); // ִ��һ�λ���
 
     motor_drive.enable_flag = 1;
 }
@@ -26,9 +31,10 @@
 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);
     motor_drive.enable_flag = 0;
     motor_drive.speed = 0;
 }
@@ -39,19 +45,19 @@
 
 #if 1
     /* ��ȡ���������� U ��״̬ */
-    if (HAL_GPIO_ReadPin(HALL_INPUTU_GPIO_PORT, HALL_INPUTU_PIN) != GPIO_PIN_RESET)
+    if (Get_HallSensorA_State())
     {
         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;
     }
 
     /* ��ȡ���������� W ��״̬ */
-    if (HAL_GPIO_ReadPin(HALL_INPUTW_GPIO_PORT, HALL_INPUTW_PIN) != GPIO_PIN_RESET)
+    if (Get_HallSensorC_State())
     {
         state |= 0x01U << 2;
     }
@@ -61,7 +67,7 @@
 
     return state; // ���ش�����״̬
 }
-
+static uint8_t count = 0;
 static void update_motor_speed(uint8_t dir_in, uint32_t time)
 {
     int speed_temp = 0;
@@ -69,14 +75,14 @@
     float f = 0;
 
     /* �����ٶȣ�
-     ���ÿתһȦ����12�����壬(1.0/(72000000.0/128.0)Ϊ�����������ڣ�(1.0/(72000000.0/128.0) * time)Ϊʱ�䳤��
+     ���ÿתһȦ����12�����壬(1.0/(48000000.0/128.0)Ϊ�����������ڣ�(1.0/(48000000.0/128.0) * time)Ϊʱ�䳤��
   */
 
     if (time == 0)
         motor_drive.speed_group[count++] = 0;
     else
     {
-        f = (1.0f / (72000000.0f / HALL_PRESCALER_COUNT) * time);
+        f = (1.0f / (48000000.0f / HALL_PRESCALER_COUNT) * time);
         f = (1.0f / 12.0f) / (f / 60.0f);
         motor_drive.speed_group[count++] = f;
     }
@@ -182,19 +188,20 @@
 
 /**
   * @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) // �ж��Ƿ��ɴ����жϲ���
+    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 ����ʱ������ü���ʱ��
         motor_drive.timeout = 0;
+        exti_interrupt_flag_clear(HALL_A_EXTI | HALL_B_EXTI | HALL_C_EXTI);
     }
 
     if (get_bldcm_direction() == MOTOR_FWD)
diff --git a/USR/SRC/bldc_ctrl.c b/USR/SRC/bldc_ctrl.c
new file mode 100644
index 0000000..04f1b7d
--- /dev/null
+++ b/USR/SRC/bldc_ctrl.c
@@ -0,0 +1,145 @@
+
+#include <math.h>
+#include <stdlib.h>
+
+/* ˽�б��� */
+static bldcm_data_t bldcm_data;
+
+/* �ֲ����� */
+static void sd_gpio_config(void);
+
+/**
+  * @brief  �����ʼ��
+  * @param  ��
+  * @retval ��
+  */
+void bldcm_init(void)
+{
+    PWM_TIMx_Configuration(); // ������ƶ�ʱ�������ų�ʼ��
+    hall_tim_config();        // ������������ʼ��
+    sd_gpio_config();         // sd ���ų�ʼ��
+}
+
+/**
+  * @brief  ��� SD �������ų�ʼ��
+  * @param  ��
+  * @retval ��
+  */
+static void sd_gpio_config(void)
+{
+    GPIO_InitTypeDef GPIO_InitStruct;
+
+    /* ��ʱ��ͨ���������Ŷ˿�ʱ��ʹ�� */
+    SHUTDOWN_GPIO_CLK_ENABLE();
+
+    /* ����IO��ʼ�� */
+    /*�����������*/
+    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+    /*������������ */
+    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
+    /*ѡ��Ҫ���Ƶ�GPIO����*/
+    GPIO_InitStruct.Pin = SHUTDOWN_PIN;
+
+    /*���ÿ⺯����ʹ���������õ�GPIO_InitStructure��ʼ��GPIO*/
+    HAL_GPIO_Init(SHUTDOWN_GPIO_PORT, &GPIO_InitStruct);
+
+    BLDCM_ENABLE_SD(); // Ĭ�Ͽ���
+    HAL_Delay(1);
+}
+
+/**
+  * @brief  ���õ���ٶ�
+  * @param  v: �ٶȣ�ռ�ձȣ�
+  * @retval ��
+  */
+void set_bldcm_speed(uint16_t v)
+{
+    bldcm_data.dutyfactor = v;
+
+    set_pwm_pulse(v); // �����ٶ�
+}
+
+/**
+  * @brief  ���õ������
+  * @param  ��
+  * @retval ��
+  */
+void set_bldcm_direction(motor_dir_t dir)
+{
+    bldcm_data.direction = dir;
+}
+
+/**
+  * @brief  ��ȡ�����ǰ����
+  * @param  ��
+  * @retval ��
+  */
+motor_dir_t get_bldcm_direction(void)
+{
+    return bldcm_data.direction;
+}
+
+/**
+  * @brief  ʹ�ܵ��
+  * @param  ��
+  * @retval ��
+  */
+void set_bldcm_enable(void)
+{
+    bldcm_data.is_enable = 1;
+    hall_enable();
+}
+
+/**
+  * @brief  ���õ��
+  * @param  ��
+  * @retval ��
+  */
+void set_bldcm_disable(void)
+{
+    /* ���û����������ӿ� */
+    hall_disable();
+
+    /* ֹͣ PWM ��� */
+    stop_pwm_output();
+
+    /* �ر� MOS �� */
+    bldcm_data.is_enable = 0;
+}
+
+/**
+  * @brief  ���λ��ʽ PID ����ʵ��(��ʱ����)
+  * @param  ��
+  * @retval ��
+  */
+void bldcm_pid_control(void)
+{
+    int32_t speed_actual = get_motor_speed(); // �����ת�ĵ�ǰ�ٶ�
+
+    if (bldcm_data.is_enable)
+    {
+        float cont_val = 0; // ��ǰ����ֵ
+
+        cont_val = PID_realize(speed_actual);
+
+        if (cont_val < 0)
+        {
+            cont_val = -cont_val;
+            bldcm_data.direction = MOTOR_REV;
+        }
+        else
+        {
+            bldcm_data.direction = MOTOR_FWD;
+        }
+
+        cont_val = (cont_val > PWM_PERIOD_COUNT) ? PWM_PERIOD_COUNT : cont_val; // ���޴���
+
+        set_bldcm_speed(cont_val);
+
+#ifdef PID_ASSISTANT_EN
+        set_computer_value(SEND_FACT_CMD, CURVES_CH1, &speed_actual, 1); // ��ͨ�� 1 ����ʵ��ֵ
+#else
+        printf("ʵ��ֵ��%d, Ŀ��ֵ��%.0f������ֵ: %.0f\n", speed_actual, get_pid_target(), cont_val);
+#endif
+    }
+}
diff --git a/USR/SRC/gd32e23x_it.c b/USR/SRC/gd32e23x_it.c
index 197b2d2..441dd97 100644
--- a/USR/SRC/gd32e23x_it.c
+++ b/USR/SRC/gd32e23x_it.c
@@ -35,6 +35,7 @@
 */
 
 #include "gd32e23x_it.h"
+#include "motor.h"
 
 /*!
     \brief      this function handles NMI exception
@@ -142,3 +143,13 @@
 {
     delay_decrement();
 }
+/*!
+    \brief      this function handles EXTI4-15 exception
+    \param[in]  none
+    \param[out] none
+    \retval     none
+*/
+void EXTI4_15_IRQHandler(void)
+{
+    HAL_HallExti_TriggerCallback();
+}
diff --git a/USR/SRC/pwm.c b/USR/SRC/pwm.c
index 096a83d..3c4c5e5 100644
--- a/USR/SRC/pwm.c
+++ b/USR/SRC/pwm.c
@@ -129,12 +129,12 @@
 
 void TimerInit(void)
 {
-    rcu_periph_clock_enable(RCU_TIMER2);
+    // rcu_periph_clock_enable(RCU_TIMER2);
     rcu_periph_clock_enable(RCU_TIMER0);
     Timer0Init();
-    Timer2Init();
+    // Timer2Init();
     timer_enable(TIMER0);
-    timer_enable(TIMER2);
+    // timer_enable(TIMER2);
 }
 
 void SetPwmDuty(uint16_t ch, uint32_t duty)

--
Gitblit v1.8.0