瀏覽代碼

系统启动和休眠唤醒后需要先关闭小电,确保cs1180的校准基本没有流量消耗

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 年之前
父節點
當前提交
3be2ec4d76

+ 2 - 0
Application/app/bms_message.c

@@ -7,6 +7,7 @@
 #include "bsp/gpio.h"
 #include "bsp/ml5238.h"
 #include "bsp/fmc_flash.h"
+#include "bsp/cs1180.h"
 #include "app/nv_storage.h"
 #include "libs/logger.h"
 #include "protocol.h"
@@ -201,6 +202,7 @@ void process_bms_message(can_frame_t *frame, int len){
 			if (len < 1) {
 				set_log_all(L_debug);
 				ml5238_reg_log(); //just for debug
+				cs1180_log();
 				result = 1;
 			} else if (len < 2) {
 				set_log_all(frame->data[0]);

+ 0 - 2
Application/app/sox/measure_task.c

@@ -42,8 +42,6 @@ void measure_log(void){
 		//measure_debug("Cell[%d]: %.3fv\n", i, _measure_value.cell_vol[i]/1000.0f);
 	}
 	measure_debug("Gain:%f, Off %f\n", get_ml5238_gain(), get_ml5238_vos());
-
-	cs1180_log();
 }
 /*
  * 测量电芯电压,计算出总电压, 测量总电流,放电和充电

+ 1 - 0
Application/bsp/bsp.c

@@ -62,6 +62,7 @@ void bsp_init(void){
 	shark_uart_init(SHARK_UART0);
 	shark_uart_init(SHARK_UART1);
 	AT24CXX_Init();
+	AUX_VOL_OPEN(0);
 }
 
 void systick_close(void)

+ 23 - 26
Application/bsp/cs1180.c

@@ -61,28 +61,22 @@ static void spi_read_reg(uint8_t reg, uint8_t *data, uint8_t len){
 
 static uint8_t cs1180_dumy_read(int gain){
 	uint8_t *data = _cali_gain_regs + 16 * gain;
-	while (IS_CS1180_NOT_READY());
 	cs1180_cs(0);
-	cs_delay();
 	spi_read_reg(0x0, data, 16);
-	cs_delay();
 	cs1180_cs(1);
 	return data[0];
 }
 
 /* 对芯片的偏移误差和增益误差进行纠正 */
-static void cs1180_self_calibrate(void)
+__attribute__((unused)) static void cs1180_self_calibrate(void)
 {
 	cs1180_cs(0);
-	cs_delay();
 	cs1180_send_cmd(CS1180_CALSELF);
-	cs_delay();
 	cs1180_cs(1);
-	delay_us(50);
 	/* wait calibrate finished */
 	while (IS_CS1180_NOT_READY());
 	while (!IS_CS1180_NOT_READY());
-	delay_us(50 * 1000);
+	delay_us(33 * 1000);
 	while (IS_CS1180_NOT_READY()); //drop first data
 }
 
@@ -92,10 +86,12 @@ __attribute__((unused)) static void cs1180_self_offset_calibrate(void)
 	cs1180_cs(0);
 	cs1180_send_cmd(CS1180_OCALSELF);
 	cs1180_cs(1);
-	delay_us(50);
 	/* wait calibrate finished */
 	while (IS_CS1180_NOT_READY());
 	while (!IS_CS1180_NOT_READY());
+	delay_us(33 * 1000);
+	while (IS_CS1180_NOT_READY()); //drop first data
+
 }
 
 /* 对芯片的增益误差进行纠正 */
@@ -104,26 +100,25 @@ __attribute__((unused)) static void cs1180_self_gain_calibrate(void)
 	cs1180_cs(0);
 	cs1180_send_cmd(CS1180_SLFGCAL);
 	cs1180_cs(1);
-	delay_us(50);
 	/* wait calibrate finished */
 	while (IS_CS1180_NOT_READY());
 	while (!IS_CS1180_NOT_READY());
+	delay_us(33 * 1000);
+	while (IS_CS1180_NOT_READY()); //drop first data
+
 }
 
 /* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
 */
-void cs1180_sys_offset_calibrate(void)
+__attribute__((unused)) static void cs1180_sys_offset_calibrate(void)
 {
 	cs1180_cs(0);
-	cs_delay();
 	cs1180_send_cmd(CS1180_OCALSYS);
-	delay_us(50);
 	cs1180_cs(1);
-	delay_us(50);
 	/* wait calibrate finished */
 	while (IS_CS1180_NOT_READY());
 	while (!IS_CS1180_NOT_READY());
-	delay_us(10 * 1000);
+	delay_us(33 * 1000);
 	while (IS_CS1180_NOT_READY()); //drop first data	
 }
 
@@ -133,10 +128,12 @@ __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
 	cs1180_cs(0);
 	cs1180_send_cmd(CS1180_GCALSYS);
 	cs1180_cs(1);
-	delay_us(50);
 	/* wait calibrate finished */
 	while (IS_CS1180_NOT_READY());
 	while (!IS_CS1180_NOT_READY());
+	delay_us(33 * 1000);
+	while (IS_CS1180_NOT_READY()); //drop first data
+
 }
 
 static void _cs1180_adc_set_gain(int gain){
@@ -146,13 +143,8 @@ static void _cs1180_adc_set_gain(int gain){
 	uint8_t data[] = {0x00, 0x01, 0x00};
 	data[0] = 0xF & gain;
 	cs1180_cs(0);
-	cs_delay();
 	spi_write_reg(0x00, data, 3);
-	cs_delay();
 	cs1180_cs(1);
-	_cs1180_gain = 1 << gain;
-	cs1180_self_calibrate();
-	cs1180_dumy_read(gain);	
 }
 
 int cs1180_adc_set_gain_cali(int gain){
@@ -168,12 +160,14 @@ int cs1180_adc_set_gain_cali(int gain){
 			break;
 		}
 		count ++;
-	}while(count <= 20);
-	if (count >= 20) {
+	}while(count <= 50);
+	if (count >= 50) {
 		if (cs1180_dumy_read(gain) != gain){
 			return -1;
 		}
 	}
+	_cs1180_gain = 1 << gain;
+	cs1180_self_gain_calibrate();
 	delay_us(10*1000);
 	cs1180_sys_offset_calibrate();
 	cs1180_dumy_read(gain);
@@ -192,9 +186,9 @@ static void cs1180_reset(void){
 void cs1180_adc_init(void){
 	_cs1180_ready = 0;
 	CS1180_PWR_ENABLE(0);
-	delay_us(100 * 1000);
+	delay_us(10 * 1000);
 	CS1180_PWR_ENABLE(1);
-	delay_us(200 * 1000);
+	delay_us(20 * 1000);
 	spi1_init();
 	delay_us(10);
 #if 0	
@@ -232,7 +226,7 @@ int cs1180_adc_set_gain_online(int gain){
 
 		cs1180_cs(0);
 		cs_delay();
-		spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 16);
+		spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
 		cs1180_cs(1);
 		delay_us(10);
 
@@ -277,6 +271,9 @@ void cs1180_adc_shutdown(void){
 static int cs1180_may_error = 0;
 void cs1180_log(void){
 	sys_error("cs1180 error %d\n", cs1180_may_error);
+	for (int i = 0; i < 16; i++){
+		sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
+	}
 }
 float cs1180_adc_sample(int *valide)
 {

+ 2 - 0
Application/bsp/mcu_power_sleep.c

@@ -87,6 +87,7 @@ static int pre_deepsleep(void){
 
 static void post_deepsleep(void){
 	DCDC_VOL_OPEN(1);
+	AUX_VOL_OPEN(0);//must close small power, cs1180 cali need small power close
 	SystemInit();
 	system_clock_config();
 	SystemCoreClockUpdate();
@@ -100,6 +101,7 @@ static void post_deepsleep(void){
 	current_calibrate();
 	wdog_reload();
 	systick_open();
+	AUX_VOL_OPEN(1);
 }
 
 void mcu_enter_deepsleep(void){