Parcourir la source

部分工厂测试实现

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui il y a 3 ans
Parent
commit
994c222641

+ 41 - 9
Applications/app/factory.c

@@ -1,6 +1,8 @@
 #include "factory.h"
 #include "bsp/bsp_driver.h"
 #include "prot/can_message.h"
+#include "prot/can_foc_msg.h"
+#include "foc/samples.h"
 #include "libs/utils.h"
 #include "libs/logger.h"
 #include "os/os_task.h"
@@ -8,7 +10,7 @@
 static u8 factory_mode = 0;
 
 void can_process_factory_message(can_message_t *can_message){
-	uint8_t response[8];
+	uint8_t response[32];
 	uint8_t rsplen;
 	encoder_can_key(response, can_message->key);
 	response[2] = 0;
@@ -19,16 +21,46 @@ void can_process_factory_message(can_message_t *can_message){
 			break;
 		case BUILD_CMD_KEY(0xE1):
 		{
+			if (factory_mode == 0) {
+				response[2] = 1;
+				break;
+			}
 			u8 item = decode_u8(can_message->data);
-			u8 mode = decode_u8((u8 *)can_message->data +1);
-			if (item == 1) {
-				if (mode == 0) {
-					pwm_3phase_sides(false, false);
-				}else if (mode == 1) {
-					pwm_3phase_sides(false, true);
-				}else if (mode == 2) {
-					pwm_3phase_sides(true, false);
+			if (item == 1) { //3相驱动测试
+				u8 mask = decode_u8((u8 *)can_message->data + 1);
+				pwm_3phase_sides(mask);
+			}else if (item == 2) {//获取所有电压的采集值
+				can_response_vols(can_message->src, can_message->key);
+				return;
+			}else if (item == 3) { //读取gpio状态
+				encode_u16(response + 3, GPIOA_VALUE);
+				encode_u16(response + 5, GPIOB_VALUE);
+				encode_u16(response + 7, GPIOC_VALUE);
+				encode_u16(response + 9, GPIOD_VALUE);
+				encode_u16(response + 11, GPIOE_VALUE);
+				rsplen += 10;
+			}else if (item == 4) { // u phase detect
+				int count = 20;
+				s16 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);
+				while(count-- > 0) {
+					delay_us(100);
+					sample_uvw_phases_raw(uvw);
+					uvw_total[0] += uvw[0];
+					uvw_total[1] += uvw[1];
+					uvw_total[2] += 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);
+				gpio_phase_u_detect(false);
+				rsplen += 6;
 			}
 			break;
 		}

+ 6 - 0
Applications/bsp/gd32/gpio.h

@@ -13,6 +13,12 @@ typedef struct {
 	int init_value; //-1 input, 0 L, 1 H
 }gpio_pin_config_t;
 
+#define GPIOA_VALUE ((u16)GPIO_ISTAT(GPIOA))
+#define GPIOB_VALUE ((u16)GPIO_ISTAT(GPIOB))
+#define GPIOC_VALUE ((u16)GPIO_ISTAT(GPIOC))
+#define GPIOD_VALUE ((u16)GPIO_ISTAT(GPIOD))
+#define GPIOE_VALUE ((u16)GPIO_ISTAT(GPIOE))
+
 void gpio_pin_init(void);
 bool gpio_get_brake(void) ;
 void gpio_ir2136_enable(bool enable);

+ 22 - 24
Applications/bsp/gd32/pwm.c

@@ -42,10 +42,7 @@ void pwm_3phase_init(void){
     _init_pwm_timer(true);
 }
 
-void pwm_3phase_sides(bool hon, bool lon) {
-	if (hon && lon) {
-		return;
-	}
+void pwm_3phase_sides(u8 mask) {
 	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);
@@ -57,34 +54,35 @@ void pwm_3phase_sides(bool hon, bool lon) {
     gpio_init(PWM_W_N_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_W_N_PIN);
 
 	sys_debug("pwm_3phase_sides\n");
-	/* 开上桥或者下桥之前先关闭下桥或者上桥 */
-	if (hon) {
-		_pwm_gpio_config();
-		_init_pwm_timer(false);
-		delay_us(10);
-		pwm_start();
-		pwm_update_duty(FOC_PWM_Half_Period-200, FOC_PWM_Half_Period-200, FOC_PWM_Half_Period-200);
-	}else if (lon) {
+	if (mask & 0x01) {
+		gpio_bit_write(PWM_U_P_GROUP, PWM_U_P_PIN, SET);
+	}else {
 		gpio_bit_write(PWM_U_P_GROUP, PWM_U_P_PIN, RESET);
+	}
+	if (mask & 0x02) {
+		gpio_bit_write(PWM_V_P_GROUP, PWM_V_P_PIN, SET);
+	}else {
 		gpio_bit_write(PWM_V_P_GROUP, PWM_V_P_PIN, RESET);
+	}
+	if (mask & 0x04) {
+		gpio_bit_write(PWM_W_P_GROUP, PWM_W_P_PIN, SET);
+	}else {
 		gpio_bit_write(PWM_W_P_GROUP, PWM_W_P_PIN, RESET);
-
-		delay_us(10);
+	}
+	if (mask & 0x10) {
 		gpio_bit_write(PWM_U_N_GROUP, PWM_U_N_PIN, SET);
-		gpio_bit_write(PWM_V_N_GROUP, PWM_V_N_PIN, SET);
-		gpio_bit_write(PWM_W_N_GROUP, PWM_W_N_PIN, SET);
 	}else {
-#if 0		
-		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);
+	}
+	if (mask & 0x20) {
+		gpio_bit_write(PWM_V_N_GROUP, PWM_V_N_PIN, SET);
+	}else {
 		gpio_bit_write(PWM_V_N_GROUP, PWM_V_N_PIN, RESET);
+	}
+	if (mask & 0x40) {
+		gpio_bit_write(PWM_W_N_GROUP, PWM_W_N_PIN, SET);
+	}else {
 		gpio_bit_write(PWM_W_N_GROUP, PWM_W_N_PIN, RESET);
-#else
-		pwm_3phase_init();
-#endif
 	}
 }
 

+ 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 hon, bool lon);
+void pwm_3phase_sides(u8 mask);
 void pwm_start(void);
 void pwm_stop(void);
 void pwm_turn_on_low_side(void);

+ 8 - 0
Applications/foc/samples.c

@@ -230,6 +230,14 @@ void sample_throttle(void){
 
 }
 
+void sample_uvw_phases_raw(s16 *uvw_raw) {
+	u16 uvw[3];
+	adc_get_uvw_phaseV(uvw);
+	uvw_raw[0] = (s16)((float)uvw[0] * UVW_VOL_CEOF * 100.0f);
+	uvw_raw[1] = (s16)((float)uvw[1] * UVW_VOL_CEOF * 100.0f);
+	uvw_raw[2] = (s16)((float)uvw[2] * UVW_VOL_CEOF * 100.0f);
+}
+
 void sample_uvw_phase(void) {
 #ifdef U_VOL_ADC_CHAN
 	u16 uvw[3];

+ 1 - 0
Applications/foc/samples.h

@@ -24,6 +24,7 @@ float get_adc_vref(void);
 float get_thro2_5v_float(void);
 float get_thro_5v_float(void);
 float get_throttle2_float(void);
+void sample_uvw_phases_raw(s16 *uvw_raw);
 
 #endif /* _SAMPLES_H__ */