Sfoglia il codice sorgente

工厂测试模式进入退出,3相电流采集,udet功能

Signed-off-by: kevin <huhui@sharkgulf.com>
kevin 2 anni fa
parent
commit
fe6465237c
3 ha cambiato i file con 27 aggiunte e 64 eliminazioni
  1. 24 20
      Applications/app/factory.c
  2. 2 43
      Applications/bsp/gd32/pwm.c
  3. 1 1
      Applications/bsp/gd32/pwm.h

+ 24 - 20
Applications/app/factory.c

@@ -12,8 +12,11 @@
 static u8 factory_mode = 0;
 
 static void stop_pwm_adc(void);
-
+static bool phase_adc_start = false;
 static bool start_pwm_adc(void) {
+	if (phase_adc_start) {
+		return true;
+	}
 	pwm_turn_on_low_side();
 	delay_ms(10);
 	phase_current_offset_calibrate();
@@ -26,10 +29,15 @@ static bool start_pwm_adc(void) {
 		stop_pwm_adc();
 		return false;
 	}
+	phase_adc_start = true;
 	return true;
 }
 
 static void stop_pwm_adc(void) {
+	if (!phase_adc_start) {
+		return;
+	}
+	phase_adc_start = false;
 	u32 mask = cpu_enter_critical();
 	adc_stop_convert();
 	pwm_stop();
@@ -57,7 +65,7 @@ void can_process_factory_message(can_message_t *can_message){
 			u8 item = decode_u8(can_message->data);
 			if (item == 1) { //3相驱动测试
 				u8 duty = decode_u8((u8 *)can_message->data + 1);
-				pwm_3phase_sides(true, 0);
+				pwm_3phase_test();
 				u16 duty_time = (u16)((float)duty * FOC_PWM_Half_Period / 100.0f);
 				pwm_update_duty(duty_time, duty_time, duty_time);
 			}else if (item == 2) {//获取所有电压的采集值
@@ -70,28 +78,24 @@ void can_process_factory_message(can_message_t *can_message){
 				int count = 20;
 				float uvw[3] = {0, 0, 0};
 				s16 uvw_total[3] = {0, 0, 0};
-				if (can_message->len < 2) {
-					response[2] = 2; //长度错误
-					break;
-				}
 				u8 detect = decode_u8((u8 *)can_message->data + 1);
-				gpio_phase_u_detect(detect?true:false);
+				gpio_phase_u_detect(true);
 				while(count-- > 0) {
 					delay_us(100);
 					get_uvw_phases_raw(uvw);
-					uvw_total[0] += (s16)(uvw[0] * 100);
-					uvw_total[1] += (s16)(uvw[1] * 100);
-					uvw_total[2] += (s16)(uvw[2] * 100);
+					uvw_total[0] += S16Q5(uvw[0]);
+					uvw_total[1] += S16Q5(uvw[1]);
+					uvw_total[2] += S16Q5(uvw[2]);
 				}
-				encode_u16(response + 3, uvw_total[0]/20);
-				encode_u16(response + 5, uvw_total[1]/20);
-				encode_u16(response + 7, uvw_total[2]/20);
+				encode_s16(response + 3, uvw_total[0]/20);
+				encode_s16(response + 5, uvw_total[1]/20);
+				encode_s16(response + 7, uvw_total[2]/20);
 				gpio_phase_u_detect(false);
 				rsplen += 6;
 			}else if (item == 5) { //phase current test
 				u8 start = decode_u8((u8 *)can_message->data + 1);
 				if (start == 1) {
-					pwm_3phase_sides(true, 0); //use pwm output, disable timer break in
+					pwm_3phase_test(); //use pwm output, disable timer break in
 					if (!start_pwm_adc()) {
 						response[2] = 1;
 						break;
@@ -100,12 +104,12 @@ void can_process_factory_message(can_message_t *can_message){
 					stop_pwm_adc();
 					pwm_3phase_init();
 				}else {
-					s16 ui = (s16)mot_contrl()->foc.in.curr_abc[0];
-					s16 vi = (s16)mot_contrl()->foc.in.curr_abc[1];
-					s16 wi = (s16)mot_contrl()->foc.in.curr_abc[2];
-					encode_s16(response + 3, ui);
-					encode_s16(response + 5, ui);
-					encode_s16(response + 7, ui);
+					s16 ia = S16Q5(foc()->in.curr_abc[0]);
+					s16 ib = S16Q5(foc()->in.curr_abc[1]);
+					s16 ic = S16Q5(foc()->in.curr_abc[2]);
+					encode_s16(response + 3, ia);
+					encode_s16(response + 5, ib);
+					encode_s16(response + 7, ic);
 					rsplen += 6;
 				}
 			}

+ 2 - 43
Applications/bsp/gd32/pwm.c

@@ -44,53 +44,12 @@ void pwm_3phase_init(void){
 	phase_start = false;
 }
 
-void pwm_3phase_sides(bool pwm, u8 mask) {
-	if (pwm && !phase_start) {
+void pwm_3phase_test(void) {
+	if (!phase_start) {
 		timer_deinit(MOS_PWM_TIMER);
 		_pwm_gpio_config();
 		_init_pwm_timer(false);
 		phase_start = true;
-		return;
-	}
-	timer_deinit(MOS_PWM_TIMER);
-	rcu_periph_clock_enable(_rcu_clk(MOS_PWM_TIMER));
-    gpio_init(PWM_U_P_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_U_P_PIN);
-    gpio_init(PWM_V_P_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_V_P_PIN);
-    gpio_init(PWM_W_P_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_W_P_PIN);
-
-    gpio_init(PWM_U_N_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_U_N_PIN);
-    gpio_init(PWM_V_N_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_V_N_PIN);
-    gpio_init(PWM_W_N_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_W_N_PIN);
-
-	sys_debug("pwm_3phase_sides\n");
-	gpio_bit_write(PWM_U_P_GROUP, PWM_U_P_PIN, RESET);
-	gpio_bit_write(PWM_V_P_GROUP, PWM_V_P_PIN, RESET);
-	gpio_bit_write(PWM_W_P_GROUP, PWM_W_P_PIN, RESET);
-	gpio_bit_write(PWM_U_N_GROUP, PWM_U_N_PIN, RESET);
-	gpio_bit_write(PWM_V_N_GROUP, PWM_V_N_PIN, RESET);
-	gpio_bit_write(PWM_W_N_GROUP, PWM_W_N_PIN, RESET);
-	delay_us(1);
-	
-	if (mask & 0x01) {
-		gpio_bit_write(PWM_U_P_GROUP, PWM_U_P_PIN, SET);
-		gpio_bit_write(PWM_U_N_GROUP, PWM_U_N_PIN, RESET);
-	}else {
-		gpio_bit_write(PWM_U_P_GROUP, PWM_U_P_PIN, RESET);
-		gpio_bit_write(PWM_U_N_GROUP, PWM_U_N_PIN, SET);
-	}
-	if (mask & 0x02) {
-		gpio_bit_write(PWM_V_P_GROUP, PWM_V_P_PIN, SET);
-		gpio_bit_write(PWM_V_N_GROUP, PWM_V_N_PIN, RESET);
-	}else {
-		gpio_bit_write(PWM_V_P_GROUP, PWM_V_P_PIN, RESET);
-		gpio_bit_write(PWM_V_N_GROUP, PWM_V_N_PIN, SET);
-	}
-	if (mask & 0x04) {
-		gpio_bit_write(PWM_W_P_GROUP, PWM_W_P_PIN, SET);
-		gpio_bit_write(PWM_W_N_GROUP, PWM_W_N_PIN, RESET);
-	}else {
-		gpio_bit_write(PWM_W_P_GROUP, PWM_W_P_PIN, RESET);
-		gpio_bit_write(PWM_W_N_GROUP, PWM_W_N_PIN, SET);
 	}
 }
 

+ 1 - 1
Applications/bsp/gd32/pwm.h

@@ -90,7 +90,7 @@
 #define PWM_Direction_Down() true
 #endif
 void pwm_3phase_init(void);
-void pwm_3phase_sides(bool pwm, u8 mask);
+void pwm_3phase_test(void);
 void pwm_start(void);
 void pwm_stop(void);
 void pwm_turn_on_low_side(void);