Просмотр исходного кода

中断处理放到motor.c中,brake中断统一处理

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 4 лет назад
Родитель
Сommit
686240344f

+ 1 - 1
Applications/app/app.c

@@ -20,7 +20,7 @@ void app_start(void){
 	can_message_init();
 	restore_config();
 	samples_init();
-	PMSM_FOC_CoreInit();
+	mc_init();
 	shark_task_create(_app_low_task, NULL);
 	
 	print_backtrace();

+ 8 - 10
Applications/bsp/mc_irqs.c

@@ -73,18 +73,16 @@ void DebugMon_Handler(void)
 {
 }
 
-extern void mc_phase_current_irq(void);
-extern void mc_brake_irq(void);
+extern void mc_phase_current_handler(void);
 extern void hall_sensor_handler(void);
-extern void foc_normal_task(void);
-extern void foc_brake_handler(bool brake);
-extern void foc_pwm_up_handler(void);
+extern void mc_brake_handler(void);
+extern void mc_pwm_up_handler(void);
 
 
 void ADC0_1_IRQHandler(void)
 {
 	adc_disable_ext_trigger();
-	mc_phase_current_irq();
+	mc_phase_current_handler();
 	//adc_enable_ext_trigger();
     /* clear the ADC flag */
 	adc_clear_irq_flags();	
@@ -93,14 +91,14 @@ void ADC0_1_IRQHandler(void)
 void TIMER0_UP_IRQHandler(void) {
 	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_UP)) {
 		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
-		foc_pwm_up_handler();
+		mc_pwm_up_handler();
 	}
 }
 
 void TIMER0_BRK_IRQHandler(void) {
 	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_BRK)) {
 		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_BRK);
-		foc_brake_handler(true);
+		mc_brake_handler();
 	}
 }
 
@@ -140,7 +138,7 @@ void EXTI4_IRQHandler(void)
 {
 	if(RESET != exti_interrupt_flag_get(EXTI_4)){
 		exti_interrupt_flag_clear(EXTI_4);
-		mc_brake_irq();
+		mc_brake_handler();
 	}	
 }
 
@@ -148,7 +146,7 @@ void EXTI4_IRQHandler(void)
 void EXTI5_9_IRQHandler(void){
 	if(RESET != exti_interrupt_flag_get(EXTI_5)){
 		exti_interrupt_flag_clear(EXTI_5);
-		mc_brake_irq();
+		mc_brake_handler();
 	}
 	if(RESET != exti_interrupt_flag_get(EXTI_6)){
 		exti_interrupt_flag_clear(EXTI_6);

+ 0 - 29
Applications/bsp/pwm.c

@@ -397,32 +397,3 @@ void pwm_update_sample(u32 samp1, u32 samp2, u8 sector) {
 #endif	
 	adc_current_sample_config(sector);
 }
-
-__weak void foc_brake_handler(bool brake) {
-	//dumy function, must implemented in FOC controller
-}
-
-/*do 50 times filter*/
-static void brake_timer_handler(shark_timer_t *t) {
-	int count = 50;
-	int settimes = 0;
-	while(count-- >= 0) {
-		bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
-		bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
-		if (b1 && b2) {
-			settimes++;
-		}		
-	}
-	if (settimes == 0) {
-		foc_brake_handler(true);
-	}else if (settimes == 50) {
-		foc_brake_handler(false);
-	}else {
-		//有干扰,do nothing
-	}
-}
-
-static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
-void mc_brake_irq(void) {
-	shark_timer_post(&_brake_timer, 0);
-}

+ 5 - 25
Applications/foc/core/foc_core.c

@@ -3,8 +3,8 @@
 #include "bsp/adc.h"
 #include "bsp/mc_hall_gpio.h"
 #include "foc/core/foc_core.h"
-#include "foc/core/e_ctrl.h"
 #include "foc/motor/current.h"
+#include "foc/core/e_ctrl.h"
 #include "foc/foc_config.h"
 #include "bsp/timer_count32.h"
 #include "libs/time_measure.h"
@@ -36,7 +36,7 @@ static __INLINE void PMSM_FOC_PWMCurrent_Update(void) {
 }
 
 
-static __INLINE void PMSM_FOC_Controller(void) {
+void PMSM_FOC_Schedule(void) {
 	u8  hall[3];
 	pwm_clear_updata();
 	phase_current_sample(&pmsm_foc.FOC_In->adc_Pha, &pmsm_foc.FOC_In->adc_Phb);
@@ -180,32 +180,12 @@ u8 PMSM_FOC_GetErrCode(void) {
 
 void PMSM_FOC_Brake(bool brake) {
 	pmsm_foc.b_brake_in = brake;
+	if (!pmsm_foc.FOC_In->b_motEna) {
+		return;
+	}
 	if (pmsm_foc.b_brake_in & pmsm_foc.FOC_In->b_cruiseEna) {
 		pmsm_foc.FOC_In->b_cruiseEna = false;
 	}
 	eCtrl_brake_signal(brake);
 }
 
-void foc_brake_handler(bool brake){
-	PMSM_FOC_Brake(brake);
-}
-
-
-void foc_pwm_up_handler(void){
-	phase_current_adc_triger();
-}
-
-measure_time_t g_meas_foc = {.exec_max_time = 15,};
-#define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
-#define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
-
-/*ADC 电流采集中断,调用FOC的核心处理函数*/
-void mc_phase_current_irq(void) {
-	if (phase_current_offset()) {//check if is adc offset checked
-		return;
-	}
-	TIME_MEATURE_START();
-	PMSM_FOC_Controller();
-	TIME_MEATURE_END();
-}
-

+ 1 - 0
Applications/foc/core/foc_core.h

@@ -6,6 +6,7 @@
 #define Default_Spd_Limit 2000
 
 void PMSM_FOC_CoreInit(void);
+void PMSM_FOC_Schedule(void);
 void PMSM_FOC_Start(u8 nCtrlMode);
 void PMSM_FOC_Stop(void);
 void PMSM_FOC_iBusLimit(_s16q5_t ibusLimit);

+ 50 - 9
Applications/foc/motor/motor.c

@@ -4,6 +4,9 @@
 #include "foc/foc_config.h"
 #include "foc/samples.h"
 #include "math/fast_math.h"
+#include "bsp/timer_count32.h"
+#include "libs/time_measure.h"
+#include "os/os_task.h"
 #include "bsp/delay.h"
 #include "bsp/bsp.h"
 #include "bsp/adc.h"
@@ -11,6 +14,12 @@
 
 static bool _motor_started = false;
 static float _motor_throttle = 0;
+static u32 mc_main_task_handler(void *param);
+
+void mc_init(void) {
+	PMSM_FOC_CoreInit();
+	shark_task_create(mc_main_task_handler, NULL);
+}
 
 bool mc_start(u8 mode) {
 	if (_motor_started) {
@@ -102,17 +111,50 @@ static float _get_idq_from_throttle(float throttle) {
 	return (THROTTLE_MIN_IDQ + MAX_iDQ * ration);
 }
 
-static void _brake_io_process(void) {
-	bool brake = gpio_get_brake();
-	for (int i = 0; i < 10; i++) {
-		if (brake != gpio_get_brake()) {
-			return;
-		}
+/*do 50 times filter*/
+static void brake_timer_handler(shark_timer_t *t) {
+	int count = 50;
+	int settimes = 0;
+	while(count-- >= 0) {
+		bool b1 = gpio_input_bit_get(GPIOB, GPIO_PIN_4) == SET;
+		bool b2 = gpio_input_bit_get(GPIOB, GPIO_PIN_5) == SET;
+		if (b1 && b2) {
+			settimes++;
+		}		
+	}
+	if (settimes == 0) {
+		PMSM_FOC_Brake(true);
+	}else if (settimes == 50) {
+		PMSM_FOC_Brake(false);
+	}else {
+		//有干扰,do nothing
+	}
+}
+
+static shark_timer_t _brake_timer = TIMER_INIT(_brake_timer, brake_timer_handler);
+
+void mc_brake_handler(void){
+	shark_timer_post(&_brake_timer, 0);
+}
+void mc_pwm_up_handler(void){
+	phase_current_adc_triger();
+}
+
+measure_time_t g_meas_foc = {.exec_max_time = 15,};
+#define TIME_MEATURE_START() time_measure_start(&g_meas_foc)
+#define TIME_MEATURE_END() time_measure_end(&g_meas_foc)
+
+/*ADC 电流采集中断,调用FOC的核心处理函数*/
+void mc_phase_current_handler(void) {
+	if (phase_current_offset()) {//check if is adc offset checked
+		return;
 	}
-	PMSM_FOC_Brake(brake);
+	TIME_MEATURE_START();
+	PMSM_FOC_Schedule();
+	TIME_MEATURE_END();
 }
 
-u32 mc_main_task(void *param) {
+static u32 mc_main_task_handler(void *param) {
 	if (_motor_started) {
 		if (get_throttle_float() != _motor_throttle) {
 			_motor_throttle = get_throttle_float();
@@ -121,7 +163,6 @@ u32 mc_main_task(void *param) {
 			float torque_idq = _get_idq_from_throttle(_motor_throttle);
 			PMSM_FOC_Set_Current(torque_idq);
 		}
-		_brake_io_process();
 	}
 	return 0;
 }

+ 2 - 0
Applications/foc/motor/motor.h

@@ -2,6 +2,8 @@
 #define _MOTOR_H__
 #include "os/os_types.h"
 #include "foc/core/foc_type.h"
+
+void mc_init(void);
 bool mc_start(u8 mode);
 bool mc_stop(void);
 bool mc_throttle_released(void);