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

debug for deepsleep, 在使用调试器的时候,打开PC13的中断,发现PC13有误报非常多的中断

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

+ 7 - 4
Application/app/sox/iostate.c

@@ -44,6 +44,9 @@ void io_state_init(void){
 	shark_timer_post(&dcdc_pwr_io._timer, io_detect_intv_time);
 
 	small_current_short_irq_enable(1);
+//	charger_detect_irq_enable(1);
+//	hall_1_detect_irq_enable(1);
+//	hall_2_detect_irq_enable(1);
 
 	bms_message_update_insert(_io_state.hall_detect);
 	
@@ -149,21 +152,21 @@ void small_current_short_irq_handler(void){
 		return;
 	}
 	shark_timer_post(&_small_current_irq_timer, 0);
-	io_debug("aux lock irq\n");
+	//io_debug("aux lock irq\n");
 }
 
 
 void charger_detect_irq_handler(void){
 
-	io_debug("charger irq\n");
+	//io_debug("charger irq\n");
 }
 void hall1_detect_irq_handler(void){
 
-	io_debug("hall 1 irq\n");
+	//io_debug("hall 1 irq\n");
 }
 void hall2_detect_irq_handler(void){
 
-	io_debug("hall 2 irq\n");
+	//io_debug("hall 2 irq\n");
 }
 
 #if 0

+ 11 - 7
Application/app/sox/state.c

@@ -12,7 +12,8 @@
 #include "state.h"
 #include "iostate.h"
 
-#define ALLOW_DEEP_SLEEP 0
+#define ALLOW_DEEP_SLEEP 1
+#define SLEEP_IGNORE_UNHEALTH 1
 #define ALLOW_POWER_DOWN 0 //disable power down for debug
 #define ALLOW_5238_BALANCE 1
 
@@ -155,12 +156,14 @@ static void _process_power_down(void){
 #endif
 }
 
-
+static u64 _sleep_time = 0;
 static void _process_deepsleep(s32 health){
-#if (ALLOW_DEEP_SLEEP==1)	
+#if (ALLOW_DEEP_SLEEP==1)
+#if (SLEEP_IGNORE_UNHEALTH==0)
 	if (health != Health_Success){
 		return;
 	}
+#endif
 	if (ml5238_is_charging() || ml5238_is_discharging() || IS_CHARGER_IN()){
 		return;
 	}
@@ -172,16 +175,17 @@ static void _process_deepsleep(s32 health){
 	/*if (ºìÍâ»òÕß485ͨÐÅûÓг¬Ê±){
 		return;
 	}*/
-	if (!AUX_VOL_IS_OPEN()) {
-		AUX_VOL_OPEN(1);		
-		delay_us(5000);//delay 5ms, to detect if short current
-	}
 
 	if (io_state()->aux_lock_detect){
 		return;
 	}
+	if (shark_get_mseconds() < (_sleep_time + 10 * 1000)){
+		return;
+	}
+	
 	nv_save_soc();
 	mcu_enter_deepsleep();
+	_sleep_time = shark_get_mseconds();
 #endif	
 }
 

+ 5 - 1
Application/bsp/bsp.c

@@ -2,7 +2,8 @@
 #include "bsp/gpio.h"
 #include "bsp/uart.h"
 #include "bsp/AT24CXX.h"
-
+#include "bsp/shark_rtc.h"
+#include "bsp/clock.h"
 #if defined CONFIG_BOARD_SP700
 const char iap_board_name[] __attribute__((at(0x08002800))) = "SP700";
 #elif defined CONFIG_BOARD_SP600
@@ -12,11 +13,14 @@ const char iap_fw_version[] __attribute__((at(0x08002A00))) = "1.0";
 const char iap_fw_name[] __attribute__((at(0x08002C00))) = "App";
 extern void system_clock_config(void);
 extern void SystemCoreClockUpdate(void);
+#define ALARM_TEST 1
 //all board's low level init is here
 void bsp_init(void){
 	wdog_start(4);
+	shark_rtc_init();
 	gpio_init();
 	DCDC_VOL_OPEN(1);
+	delay_us(100);
 	system_clock_config(); //after dcdc open, MCU can run on full speed
 	SystemCoreClockUpdate();
 	shark_uart_init(SHARK_UART0);

+ 48 - 35
Application/bsp/gpio.c

@@ -8,9 +8,9 @@ void gpio_init(void){
 
 #if (CONFIG_BOARD_TYPE==SHARK_BOARD_SP700)	
 	//hall 2 detect
-	gpio_mode_input(GPIOC, GPIO_PUPD_NONE, GPIO_PIN_13);
+	gpio_mode_input(GPIOC, GPIO_PUPD_PULLUP, GPIO_PIN_13);
 	//hall 1 detect
-	gpio_mode_input(GPIOC, GPIO_PUPD_NONE, GPIO_PIN_15);
+	gpio_mode_input(GPIOC, GPIO_PUPD_PULLUP, GPIO_PIN_15);
 	
 	//IR uart0 enable
 	gpio_mode_output(GPIOC, GPIO_PUPD_NONE, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_14);
@@ -68,17 +68,35 @@ void gpio_init(void){
 #endif
 }
 
+static int _exti4_15_irq_enable = 0;
+
+static void enable_exti4_15_irq(int enable){
+	__disable_irq();
+	if (enable) {
+		if (_exti4_15_irq_enable++ == 0){
+			nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
+		}
+	}else {
+		if (_exti4_15_irq_enable-- == 1){
+			nvic_irq_disable(EXTI4_15_IRQn);
+		}
+		if (_exti4_15_irq_enable < 0){
+			_exti4_15_irq_enable = 0;
+		}
+	}
+	__enable_irq();
+}
+
 void charger_detect_irq_enable(int enable){
 	if (enable){
 		syscfg_exti_line_config(EXTI_SOURCE_GPIOB, EXTI_SOURCE_PIN10);
 		exti_init(EXTI_10, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
-		nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
-		exti_interrupt_flag_clear(EXTI_10);
-		exti_interrupt_enable(EXTI_10);
+		exti_flag_clear(EXTI_10);
+		enable_exti4_15_irq(1);
 	}else {
-		//nvic_irq_disable(EXTI4_15_IRQn);
+		enable_exti4_15_irq(0);
 		exti_interrupt_disable(EXTI_10);
-		exti_interrupt_flag_clear(EXTI_10);
+		exti_flag_clear(EXTI_10);
 	}
 }
 
@@ -86,27 +104,25 @@ void ml5238_irq_enable(int enable){
 	if (enable){
 		syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN12);
 		exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
-		nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
-		exti_interrupt_flag_clear(EXTI_12);
-		exti_interrupt_enable(EXTI_12);
+		exti_flag_clear(EXTI_12);
+		enable_exti4_15_irq(1);
 	}else {
-		//nvic_irq_disable(EXTI4_15_IRQn);
+		enable_exti4_15_irq(0);
 		exti_interrupt_disable(EXTI_12);
-		exti_interrupt_flag_clear(EXTI_12);
+		exti_flag_clear(EXTI_12);
 	}
 }
 #if (CONFIG_BOARD_TYPE==SHARK_BOARD_SP700)
 void hall_1_detect_irq_enable(int enable){
 	if (enable){
 		syscfg_exti_line_config(EXTI_SOURCE_GPIOC, EXTI_SOURCE_PIN15);
-		exti_init(EXTI_15, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
-		nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
-		exti_interrupt_flag_clear(EXTI_15);
-		exti_interrupt_enable(EXTI_15);
+		exti_flag_clear(EXTI_15);
+		exti_init(EXTI_15, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
+		enable_exti4_15_irq(1);
 	}else {
-		//nvic_irq_disable(EXTI4_15_IRQn);
+		enable_exti4_15_irq(0);
 		exti_interrupt_disable(EXTI_15);
-		exti_interrupt_flag_clear(EXTI_15);
+		exti_flag_clear(EXTI_15);
 	}
 }
 #else
@@ -116,14 +132,13 @@ void hall_1_detect_irq_enable(int enable){
 void hall_2_detect_irq_enable(int enable){
 	if (enable){
 		syscfg_exti_line_config(EXTI_SOURCE_GPIOC, EXTI_SOURCE_PIN13);
-		exti_init(EXTI_13, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
-		nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
-		exti_interrupt_flag_clear(EXTI_13);
-		exti_interrupt_enable(EXTI_13);
+		exti_flag_clear(EXTI_13);
+		exti_init(EXTI_13, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
+		enable_exti4_15_irq(1);
 	}else {
-		//nvic_irq_disable(EXTI4_15_IRQn);
+		enable_exti4_15_irq(0);
 		exti_interrupt_disable(EXTI_13);
-		exti_interrupt_flag_clear(EXTI_13);
+		exti_flag_clear(EXTI_13);
 	}
 }
 
@@ -131,14 +146,13 @@ void hall_2_detect_irq_enable(int enable){
 void small_current_short_irq_enable(int enable){
 	if (enable){
 		syscfg_exti_line_config(EXTI_SOURCE_GPIOB, EXTI_SOURCE_PIN11);
-		exti_init(EXTI_11, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
-		nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
-		exti_interrupt_flag_clear(EXTI_11);
-		exti_interrupt_enable(EXTI_11);
+		exti_flag_clear(EXTI_11);
+		exti_init(EXTI_11, EXTI_INTERRUPT, EXTI_TRIG_FALLING);	
+		enable_exti4_15_irq(1);
 	}else {
-		//nvic_irq_disable(EXTI4_15_IRQn);
+		enable_exti4_15_irq(0);
 		exti_interrupt_disable(EXTI_11);
-		exti_interrupt_flag_clear(EXTI_11);
+		exti_flag_clear(EXTI_11);
 	}
 }
 
@@ -146,13 +160,12 @@ void dcdc_pwr_detect_irq_enable(int enable){
 	if (enable){
 		syscfg_exti_line_config(EXTI_SOURCE_GPIOF, EXTI_SOURCE_PIN7);
 		exti_init(EXTI_7, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
-		nvic_irq_enable(EXTI4_15_IRQn, 4U, 0U);
-		exti_interrupt_flag_clear(EXTI_7);
-		exti_interrupt_enable(EXTI_7);
+		exti_flag_clear(EXTI_7);
+		enable_exti4_15_irq(1);
 	}else {
-		//nvic_irq_disable(EXTI4_15_IRQn);
+		enable_exti4_15_irq(0);
 		exti_interrupt_disable(EXTI_7);
-		exti_interrupt_flag_clear(EXTI_7);
+		exti_flag_clear(EXTI_7);
 	}
 }
 

+ 50 - 23
Application/bsp/mcu_power_sleep.c

@@ -5,73 +5,100 @@
 #include "bsp/spi.h"
 #include "bsp/uart.h"
 #include "bsp/i2c.h"
+#include "bsp/shark_rtc.h"
+#include "app/sox/iostate.h"
 
 extern void system_clock_24m_irc8m(void);
 extern void system_clock_config(void);
+extern void SystemCoreClockUpdate(void);
+
 extern void current_calibrate(void);
+extern void adc_init(void);
+extern void adc_deinit(void);
+static uint32_t _sleep_second_time = 0;
+static void post_deepsleep(void);
+
+#define WDOG_TIME_FOR_SLEEP 15
+#define RTC_ALARM_FOR_SLEEP 10 //rtc alarm time MUST be small than wdog time!!!!
+uint32_t get_system_sleep_time(void){
+	return _sleep_second_time;
+}
 
 static void enable_wakeup_irq(void){
-	charger_detect_irq_enable(1);
-	hall_1_detect_irq_enable(1);
-	hall_2_detect_irq_enable(1);
+	//charger_detect_irq_enable(1);
+	//hall_1_detect_irq_enable(1);
+	//hall_2_detect_irq_enable(1);
 }
 
 static void disable_wakeup_irq(void){
-	charger_detect_irq_enable(0);
-	hall_1_detect_irq_enable(0);
-	hall_2_detect_irq_enable(0);
+	//charger_detect_irq_enable(0);
+	//hall_1_detect_irq_enable(0);
+	//hall_2_detect_irq_enable(0);
 }
 
 
 static void pre_deepsleep(void){
-#if (CONFIG_BOARD_TYPE==SHARK_BOARD_SP700)
-	UART0_IR_EN(0);
-	UART1_IR_EN(0);
-#elif (CONFIG_BOARD_TYPE==SHARK_BOARD_SP600)
-	RS485_PWR_ENABLE(0);
-#endif
 	CS1180_PWR_ENABLE(0);
 	shark_uart_deinit(SHARK_UART0);
 	shark_uart_deinit(SHARK_UART1);
-	//gd32_i2c_deinit();
-	UART0_IR_EN(0);
-	UART0_IR_EN(0);	
+	wdog_reload();
 	ml5238_power_save(1); //call, before spi0_deinit
 	spi0_deinit();
 	spi1_deinit();
 	gd32_i2c_deinit(0);
-	system_clock_24m_irc8m();//before dcdc off, should set clock to 24M, avoid ml5238's supply is not enough
+	adc_deinit();
+	if (AUX_VOL_IS_OPEN()) {
+		AUX_VOL_OPEN(0);//we should close small power, before dcdc close
+		delay_us(1000);
+	}
+	wdog_reload();
+	io_state()->aux_lock_detect = 0;
 	DCDC_VOL_OPEN(0);
-	wdog_start(10);
+	delay_us(1000); // give 1s to wait small current short, when dcdc is closed
+	AUX_VOL_OPEN(1);
+	delay_us(5000); //give 5s to detect if the small current is short
+	if (io_state()->aux_lock_detect){
+		post_deepsleep();
+		return;
+	}
+	
+	wdog_start(WDOG_TIME_FOR_SLEEP);
 }
 
 static void post_deepsleep(void){
 	DCDC_VOL_OPEN(1);
 	SystemInit();
 	system_clock_config();
+	SystemCoreClockUpdate();
 	spi0_init();
 	ml5238_power_save(0);
 	CS1180_PWR_ENABLE(1);
 	cs1180_adc_init();
-#if (CONFIG_BOARD_TYPE==SHARK_BOARD_SP700)
-	UART0_IR_EN(1);
-	UART1_IR_EN(1);
-#elif (CONFIG_BOARD_TYPE==SHARK_BOARD_SP600)
-	RS485_PWR_ENABLE(1);
-#endif
 	shark_uart_init(SHARK_UART0);
 	shark_uart_init(SHARK_UART1);
 
 	gd32_i2c_init(0, 100* 1000);
+	adc_init();
 	wdog_start(4);
 	current_calibrate();
+	wdog_reload();
+	printf("Sleep Exit, Total sleep time = %d s\n", _sleep_second_time);
 }
 
 void mcu_enter_deepsleep(void){
 
 	pre_deepsleep();
 	enable_wakeup_irq();
+	shark_rtc_start_alarm(RTC_ALARM_FOR_SLEEP);
+	u32 start_time = shark_rtc_get_second();
 	pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
+	u32 end_time = shark_rtc_get_second();
+	if (end_time >= start_time) {
+		_sleep_second_time += (end_time - start_time);
+	}else { //rtc second wrap
+		_sleep_second_time += (60 - start_time + end_time);
+	}
+	shark_rtc_stop_alarm();
 	disable_wakeup_irq();
 	post_deepsleep();
 }

+ 1 - 1
Application/bsp/ml5238.c

@@ -214,8 +214,8 @@ void ml5238_power_down(void){
 }
 
 void ml5238_power_save(int save){
-	ml5238_write(ML5238_PSENSE, PSENSE_EPSH|PSENSE_IPSH);
 	if (save) {
+		ml5238_write(ML5238_PSENSE, 0);
 		ML5238_VMON_DISABLE();
 		ML5238_IMON_DISABLE();
 		ml5238_write(ML5238_POWER, POWER_PSV);

+ 3 - 2
Application/libs/shark_task.c

@@ -1,8 +1,9 @@
 #include "libs/shark_task.h"
 #include "bsp/shark_bsp.h"
 
-static u64 shark_mseconds;
+extern uint32_t get_system_sleep_time(void);
 
+static u64 shark_mseconds;
 static u32 shark_timer_task_handler(void);
 
 static shark_timer_t shark_timer_head = {
@@ -26,7 +27,7 @@ u64 shark_get_mseconds(void)
 }
 
 u32 shark_get_seconds(void){
-	return shark_mseconds/1000;
+	return shark_mseconds/1000 + get_system_sleep_time();
 }
 
 static inline void shark_timer_sync(void)