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

cs1180&ml5238 小电流测试修改

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

+ 17 - 2
Application/app/sox/measure.c

@@ -9,7 +9,14 @@
 /* measure the temp & current & voltage for battery pack by using
  * ms5238 & cs1180(only used when bms is in small current loading)
 */
-
+/*
+1. СµçÁ÷
+   cs1180(32x):0.305-0.315,      0.277-0.286,      0.255-0.264,      0.220-0.228,      0.184-0.190,      0.137-0.142,      0.062-0.064,      0.011-0.009:0.012
+   ml5289(50x):0.307-0.313:0.330,0.277-0.275:0.290,0.254-0.251:0.271,0.221-0.208:0.253,0.183-0.170:0.196,0.136-0.121:0.155,0.061-0.072:0.096,   0.011-0.033:0.060
+2. ´óµçÁ÷
+   cs1180(32x):1.861-2.082
+   ml5238(50x):1.869-1.865-1.890
+*/
 /* this is the inited gain set to the ms5238, but the really gain is  calibrated
 *  by measure_system_calibrate
 */
@@ -86,8 +93,11 @@ void current_calibrate(void){
 void measure_system_init(void){
 	ml5238_init();
 	adc_init();
-	cs1180_adc_init();
 	current_calibrate();
+	cs1180_adc_init();
+	//AUX_VOL_OPEN(1);
+	//ml5238_enable_discharger_mosfet(1);
+	//ml5238_enable_charger_mosfet(1);
 }
 
 /* get battery pack's current (mA) */
@@ -128,6 +138,11 @@ void adc_select_for_pack_current(int adc){
 	if (adc != GD32_ADC && adc != CS1180_ADC){
 		return;
 	}
+	if (adc == GD32_ADC){
+		select_gain_10x(0);
+	}else {
+		ML5238_IMON_DISABLE();
+	}
 	adc_used = adc;
 }
 

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

@@ -33,7 +33,7 @@ static struct means_task _pack_current_task;
 static u32 pack_current_task_handler(void);
 static void init_pack_current_task(void){
 	_pack_current_task._task.handler = pack_current_task_handler;
-	_pack_current_task.delay = 60;
+	_pack_current_task.delay = 100;
 	_pack_current_task.index = 0;
 	shark_task_add(&_pack_current_task._task);
 }

+ 16 - 18
Application/bsp/cs1180.c

@@ -19,7 +19,7 @@
 #define CS1180_RESET    0xfe
 
 static float _cs1180_gain = 1.0f;
-#define CS1180_INIT_GAIN CS1180_GAIN_64X
+#define CS1180_INIT_GAIN CS1180_GAIN_128X
 
 static int cs1180_send_cmd(uint8_t data);
 static uint8_t cs1180_read_data(uint8_t data);
@@ -50,6 +50,16 @@ static void spi_read_reg(uint8_t reg, uint8_t *data, uint8_t len){
 	}
 }
 
+static uint8_t cs1180_dumy_read(void){
+	uint8_t data[16] = {0x5A, 0x5A, 0x5A};
+	while (IS_CS1180_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)
@@ -121,7 +131,7 @@ __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
 }
 
 void cs1180_adc_set_gain(int gain){
-	/* 输出频率 15hz,参考电压1.25V 输出数据高位在前,
+	/* 输出频率 15hz,参考电压1.235V 输出数据高位在前,
 	 * 输入缓冲器关闭,采样频率 OSC/128,数据格式双极性
 	 */
 	uint8_t data[] = {0x00, 0x01, 0x00};
@@ -132,6 +142,8 @@ void cs1180_adc_set_gain(int gain){
 	cs_delay();
 	cs1180_cs(1);
 	_cs1180_gain = 1 << gain;
+	cs1180_self_calibrate();
+	cs1180_dumy_read();	
 }
 
 static void cs1180_reset(void){
@@ -142,17 +154,6 @@ static void cs1180_reset(void){
 	cs1180_cs(1);
 }
 
-static uint8_t cs1180_dumy_read(void){
-	uint8_t data[16] = {0x5A, 0x5A, 0x5A};
-	while (IS_CS1180_READY());
-	cs1180_cs(0);
-	cs_delay();
-	spi_read_reg(0x0, data, 16);
-	cs_delay();
-	cs1180_cs(1);
-	return data[0];
-}
-
 void cs1180_adc_init(void){
 	CS1180_PWR_ENABLE(0);
 	delay_us(100 * 1000);
@@ -173,11 +174,8 @@ void cs1180_adc_init(void){
 		}
 		count ++;
 	}while(count <= 20);
-	cs1180_self_calibrate();
-	cs1180_dumy_read();
-	delay_us(10 * 1000);
+	delay_us(10*1000);
 	cs1180_sys_offset_calibrate();
-
 	cs1180_dumy_read();
 }
 
@@ -210,7 +208,7 @@ float cs1180_adc_sample(void)
 	}else {
 		a = a&0x7FFFF;
 	}
-	return ((float)a) / _cs1180_gain;	
+	return ((float)a) / _cs1180_gain * 0.9f;/* 0.9是一个多个板子测试后的值,具体什么原因需要再分析*/
 }
 
 

+ 94 - 0
Application/bsp/gd32_adc.c

@@ -5,6 +5,13 @@
  *sampling time + 12.5 ADCCLK cycles 
  *all channel is enabled oversample to reach 16bit accurate
 */
+static int volatile adc_work = ADC_WORK_IDLE;
+//#define DMA_ADC_CH DMA_CH0
+#ifdef DMA_ADC_CH
+#define dma_irq					DMA_Channel0_IRQn
+#define dma_buf_len 66
+static uint16_t dma_buf[dma_buf_len]; 
+#endif
 void adc_init(void){
 	rcu_periph_clock_enable(RCU_GPIOA);
 	rcu_periph_clock_enable(RCU_GPIOB);	
@@ -33,6 +40,92 @@ void adc_init(void){
 
 }
 
+#ifdef DMA_ADC_CH
+void dma_config(void)
+{
+    /* ADC_DMA_channel configuration */
+    dma_parameter_struct dma_data_parameter;
+    
+    /* ADC DMA_channel configuration */
+    dma_deinit(DMA_ADC_CH);
+    
+    /* initialize DMA single data mode */
+    dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA);
+    dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
+    dma_data_parameter.memory_addr  = (uint32_t)dma_buf;
+    dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_DISABLE;
+    dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
+    dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;  
+    dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;
+    dma_data_parameter.number       = 1U;
+    dma_data_parameter.priority     = DMA_PRIORITY_HIGH;
+    dma_init(DMA_ADC_CH, &dma_data_parameter);
+
+    dma_circulation_disable(DMA_ADC_CH);
+
+	dma_interrupt_enable(DMA_ADC_CH, DMA_INT_FTF);
+	
+	nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
+	nvic_irq_enable(dma_irq, 4U, 0U);
+
+}
+
+/* step 1 */
+int adc_start_sample(int chan){
+	//hardware oversample to 16bit
+	adc_oversample_mode_config(ADC_OVERSAMPLING_ALL_CONVERT, ADC_OVERSAMPLING_SHIFT_4B, ADC_OVERSAMPLING_RATIO_MUL256);
+	adc_oversample_mode_enable();
+    /* use max convert time to make sure the adc work fine */
+    adc_regular_channel_config(0, chan, ADC_SAMPLETIME_55POINT5);//239.5 + 12.5 = 242 cycle, 242/(28*1000000)
+	adc_enable();
+	adc_work = ADC_WORK_ING;
+}
+/* step 2 */
+int adc_start_calibration(void){
+	/* ADC calibration and reset calibration */
+	adc_calibration_enable();
+}
+int adc_sample_avg(int chan, int times){
+    /* configure the number of remaining data to be transferred */
+    DMA_CHCNT(DMA_ADC_CH) = (times & DMA_CHANNEL_CNT_MASK);	
+	dma_channel_enable(DMA_ADC_CH);
+	/* ADC DMA function enable */
+    adc_dma_mode_enable();
+    /* ADC software trigger enable */
+    adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
+}
+
+void DMA_Channel0_IRQHandler(void){
+	adc_disable();
+	dma_interrupt_flag_clear(DMA_ADC_CH, DMA_INT_FLAG_FTF);
+	dma_channel_disable(DMA_ADC_CH);
+}
+
+int adc_get_sample(void){
+	if (adc_work != ADC_WORK_FINISH){
+		return 0x7FFFFFFF;
+	}
+	int value = 0;
+	int count = DMA_CHCNT(DMA_ADC_CH) & DMA_CHANNEL_CNT_MASK;	
+	int min = 0xFFFFF;
+	int max = -0xFFFFF;
+	int i;
+	for (i = 0; i < count; i++){
+		int one = dma_buf[i];
+		value += one;
+		if (one > max){
+			max = one;
+		}
+		if (one < min) {
+			min = one;
+		}
+	}
+	adc_work = ADC_WORK_FINISH;
+	return (value - min - max)/(count - 2);
+}
+
+#else
+
 int adc_sample(int chan, int calibration){
 	int value = -0xFFFFFF;
 	int mask = 0xFFFF;
@@ -90,3 +183,4 @@ int adc_sample_avg(int chan, int times){
 	adc_disable();
 	return (value - min - max)/(times-2);
 }
+#endif /* DMA_ADC_CH */

+ 4 - 0
Application/bsp/gd32_adc.h

@@ -10,6 +10,10 @@
 #define ADC_CHAN_TEMPERATURE_3 ADC_CHANNEL_6
 #define ADC_CHAN_TEMPERATURE_4 ADC_CHANNEL_7
 
+#define ADC_WORK_IDLE 0
+#define ADC_WORK_ING 1
+#define ADC_WORK_FINISH 2
+
 int adc_sample(int chan, int calibration);
 int adc_sample_avg(int chan, int times);
 

+ 1 - 1
Application/bsp/ml5238.h

@@ -30,7 +30,7 @@ int ml5238_write(uint8_t regaddr, uint8_t data);
 #define ML5238_SELECT_CELL(i) {ml5238_write(ML5238_VMON, i | VMON_OUT);};
 
 /* IMON output disable */
-#define ML5238_IMON_DISABLE() {ml5238_write(ML5238_IMON, 0x00);};
+#define ML5238_IMON_DISABLE() {ml5238_write(ML5238_IMON, 0x02);};
 /* IMON output 0V, used to cali the GAIN */
 #define ML5238_IMON_OUT_ZERO_10X() {ml5238_write(ML5238_IMON, 0x12);delay_us(1000);};
 #define ML5238_IMON_OUT_ZERO_50X() {ml5238_write(ML5238_IMON, 0x13);delay_us(5000);};

+ 9 - 2
Project/SP700.uvoptx

@@ -135,12 +135,12 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>DLGUARM</Key>
-          <Name>d</Name>
+          <Name></Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
           <Key>JL2CM3</Key>
-          <Name>-U20080643 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO7 -FD20000000 -FC1000 -FN1 -FF0GD32F3x0.FLM -FS08000000 -FL010000 -FP0($$Device:GD32F330C8$Flash\GD32F3x0.FLM)</Name>
+          <Name>-U81480416 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO7 -FD20000000 -FC1000 -FN1 -FF0GD32F3x0.FLM -FS08000000 -FL010000 -FP0($$Device:GD32F330C8$Flash\GD32F3x0.FLM)</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
@@ -161,6 +161,13 @@
           <ItemText>SystemCoreClock,0x0A</ItemText>
         </Ww>
       </WatchWindow1>
+      <WatchWindow2>
+        <Ww>
+          <count>0</count>
+          <WinNumber>2</WinNumber>
+          <ItemText>max_cs1180_adc</ItemText>
+        </Ww>
+      </WatchWindow2>
       <MemoryWindow1>
         <Mm>
           <WinNumber>1</WinNumber>