Browse Source

短路保护调试OK

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 years ago
parent
commit
b599039541

+ 3 - 1
Application/app/sox/health.c

@@ -69,6 +69,7 @@ bms_health_t *bms_health(){
 
 
 static void clear_short_current_handler(shark_timer_t *timer){
 static void clear_short_current_handler(shark_timer_t *timer){
 	_health.load_current_short = 0; //负载移除,clear load current short
 	_health.load_current_short = 0; //负载移除,clear load current short
+	health_warning("clear load current short\n");
 }
 }
 
 
 static void load_detect_handler(shark_timer_t *timer){
 static void load_detect_handler(shark_timer_t *timer){
@@ -79,8 +80,9 @@ static void load_detect_handler(shark_timer_t *timer){
 	}
 	}
 	if (_load_detect_timer.count >= _load_detect_timer.max_count) {
 	if (_load_detect_timer.count >= _load_detect_timer.max_count) {
 		ml5238_enable_load_detect(0);
 		ml5238_enable_load_detect(0);
+		_load_detect_timer.count = 0;
 		shark_timer_post(&_clear_short_current_timer, 60 * 1000); //负载移除1分钟后,清除current short flags, can open discharger again
 		shark_timer_post(&_clear_short_current_timer, 60 * 1000); //负载移除1分钟后,清除current short flags, can open discharger again
-		health_debug("load disconnect\n");
+		health_warning("load disconnect\n");
 	}else {
 	}else {
 		shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
 		shark_timer_post(&_load_detect_timer._timer, _load_detect_timer.interval);
 	}
 	}

+ 0 - 1
Application/app/sox/iostate.c

@@ -166,7 +166,6 @@ void charger_detect_irq_handler(void){
 	mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_CHARGER);
 	mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_CHARGER);
 	_io_state.charger_detect_irq = 1;
 	_io_state.charger_detect_irq = 1;
 	shark_timer_post(&_charger_detect_irq_timer, 2000);//延时2s给充电判断充足的时间
 	shark_timer_post(&_charger_detect_irq_timer, 2000);//延时2s给充电判断充足的时间
-	io_debug("charger detect\n");
 }
 }
 
 
 void hall1_detect_irq_handler(void){
 void hall1_detect_irq_handler(void){

+ 10 - 1
Application/app/sox/state.c

@@ -119,7 +119,16 @@ static void _debug_timer_handler(shark_timer_t *t){
 static void discharger_open(int open){
 static void discharger_open(int open){
 	/* 打开大电前,先打开短路保护*/
 	/* 打开大电前,先打开短路保护*/
 	if (open) {
 	if (open) {
-		ml5238_short_current_detect(SHORT_CURRENT_MODE_100A_200A);//SP600:100A, SP700:200A
+		int mode = SHORT_CURRENT_MODE_100A_200A;
+		int try_count = 3;
+		/* 确保短路保护设置成功后才能开大电 */
+		do {
+			ml5238_short_current_detect(mode);//SP600:100A, SP700:200A
+		}while(!ml5238_is_short_current_enabled(mode) && (try_count-- >= 0));
+		if ((try_count < 0) && !ml5238_is_short_current_enabled(mode)){
+			state_error("set short current error\n");
+			return;
+		}
 	}else {
 	}else {
 		ml5238_short_current_detect(SHORT_CURRENT_MODE_DISABLE);
 		ml5238_short_current_detect(SHORT_CURRENT_MODE_DISABLE);
 	}
 	}

+ 27 - 10
Application/bsp/ml5238.c

@@ -14,6 +14,7 @@ static shark_timer_t irq_task = {.handler = irq_hander_in_timer};
 void ml5238_init(void){
 void ml5238_init(void){
 	spi0_init();
 	spi0_init();
 	ml5238_softreset();
 	ml5238_softreset();
+	ml5238_irq_enable(1);
 }
 }
 
 
 void ml5238_register_notify_handler(ml5238_notify_hander handler){
 void ml5238_register_notify_handler(ml5238_notify_hander handler){
@@ -27,7 +28,6 @@ int ml5238_charger_is_disconnect(int small_current_on){
 	uint8_t fet = 0;
 	uint8_t fet = 0;
 	ml5238_read(ML5238_FET, &fet);	
 	ml5238_read(ML5238_FET, &fet);	
 	ml5238_read(ML5238_PSENSE, &value);
 	ml5238_read(ML5238_PSENSE, &value);
-	sys_error("fet 0x%x, PSENSE 0x%x\n", fet, value);
 	if ((fet & FET_DF) || small_current_on){
 	if ((fet & FET_DF) || small_current_on){
 		return (value & PSENSE_PSL);
 		return (value & PSENSE_PSL);
 	}
 	}
@@ -177,26 +177,41 @@ int ml5238_short_current_detect(int mode){
 	if (mode >= SHORT_CURRENT_MODE_50A_100A){
 	if (mode >= SHORT_CURRENT_MODE_50A_100A){
 		if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
 		if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
 			if (ml5238_write(ML5238_SETSC, mode) == 0){
 			if (ml5238_write(ML5238_SETSC, mode) == 0){
-				if (rsense & (RSENSE_ESC | RSENSE_ISC)){
-					return 0; //already enabled short current detect
-				}
 				rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
 				rsense |= (RSENSE_ESC | RSENSE_ISC);//enable short current detect && irq
 				rsense &= ~RSENSE_RSC;
 				rsense &= ~RSENSE_RSC;
-				return ml5238_write(ML5238_SETSC, rsense);
+				return ml5238_write(ML5238_RSENSE, rsense);
 			}
 			}
 		}
 		}
 	}else {
 	}else {
 		if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
 		if (ml5238_read(ML5238_RSENSE, &rsense) == 0){
-			if ((rsense & RSENSE_ESC) == 0){
-				return 0; //already disabled 
-			}
 			rsense &= ~(RSENSE_ESC|RSENSE_ISC|RSENSE_RSC);
 			rsense &= ~(RSENSE_ESC|RSENSE_ISC|RSENSE_RSC);
-			return ml5238_write(ML5238_SETSC, rsense);
+			return ml5238_write(ML5238_RSENSE, rsense);
 		}
 		}
 	}
 	}
 	return -1;
 	return -1;
 }
 }
 
 
+int ml5238_is_short_current_enabled(int mode){
+	uint8_t value = 0;
+	if (ml5238_read(ML5238_SETSC, &value) < 0){
+		return 0;
+	}
+	if (value != mode) {
+		return 0;
+	}
+	value = 0;
+	if (ml5238_read(ML5238_RSENSE, &value) < 0){
+		return 0;
+	}
+	if ((value & (RSENSE_ESC | RSENSE_ISC)) != (RSENSE_ESC | RSENSE_ISC)){
+		return 0;
+	}
+	if (value & RSENSE_RSC){
+		return 0;
+	}
+	return 1;
+}
+
 void ml5238_softreset(void)
 void ml5238_softreset(void)
 {
 {
 	unsigned char i;
 	unsigned char i;
@@ -236,18 +251,20 @@ static void irq_hander_in_timer(shark_timer_t *timer){
 	uint8_t status = 0;
 	uint8_t status = 0;
 	ml5238_read(ML5238_STATUS, &status);
 	ml5238_read(ML5238_STATUS, &status);
 	if (status & STATUS_RPSL){//chargering over current
 	if (status & STATUS_RPSL){//chargering over current
+		sys_error("charger over current\n");
 		ml5238_enable_charger_detect(0, 0);
 		ml5238_enable_charger_detect(0, 0);
 		ml5238_enable_charger_detect(1, 0);
 		ml5238_enable_charger_detect(1, 0);
 		call_handler(ML5238_Event_Charger_Over_Current);
 		call_handler(ML5238_Event_Charger_Over_Current);
 	}
 	}
 	if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
 	if (status & STATUS_RSC) { //short current detect, close charger/discharger mosfet
+		sys_error("short current\n");
 		if (_charger_mosfet_is_open()) {
 		if (_charger_mosfet_is_open()) {
 			ml5238_enable_charger_mosfet(0);
 			ml5238_enable_charger_mosfet(0);
 		}
 		}
 		if (_discharger_mosfet_is_open()) {
 		if (_discharger_mosfet_is_open()) {
 			ml5238_enable_discharger_mosfet(0);
 			ml5238_enable_discharger_mosfet(0);
 		}
 		}
-		ml5238_enable_irq(0, ICS_IRQ); //disable short current detect
+		ml5238_short_current_detect(SHORT_CURRENT_MODE_DISABLE);
 		call_handler(ML5238_Event_Short_Current);
 		call_handler(ML5238_Event_Short_Current);
 	}
 	}
 	if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger
 	if (status & STATUS_RRS) {//load disconnect, if short detect, we must wait load disconnected, and then can open discharger

+ 1 - 0
Application/bsp/ml5238.h

@@ -15,6 +15,7 @@ int ml5238_charger_is_disconnect(int small_current_on);
 int ml5238_enable_charger_detect(int small_current_on, int enable);
 int ml5238_enable_charger_detect(int small_current_on, int enable);
 int ml5238_is_charging(void);
 int ml5238_is_charging(void);
 int ml5238_is_discharging(void);
 int ml5238_is_discharging(void);
+int ml5238_is_short_current_enabled(int mode);
 
 
 
 
 typedef void (*ml5238_notify_hander)(int event);
 typedef void (*ml5238_notify_hander)(int event);