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

优化cs1180

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 5 лет назад
Родитель
Сommit
70ec185651
1 измененных файлов с 16 добавлено и 14 удалено
  1. 16 14
      Application/bsp/cs1180.c

+ 16 - 14
Application/bsp/cs1180.c

@@ -68,7 +68,7 @@ static void cs1180_save_regs(int gain){
 	cs1180_cs(1);
 }
 
-static void cs1180_dymu_read(){
+static void cs1180_dumy_read(){
 	uint8_t data[16];
 	cs1180_cs(0);
 	spi_read_reg(0x0, data, 16);
@@ -128,7 +128,7 @@ __attribute__((unused)) static void cs1180_sys_offset_calibrate(void)
 	delay_us(20);
 	while (IS_CS1180_NOT_READY());
 	while (!IS_CS1180_NOT_READY());
-	delay_us(1000);
+	delay_us(33 * 1000);
 	while (IS_CS1180_NOT_READY()); //drop first data	
 }
 
@@ -189,6 +189,7 @@ int cs1180_adc_set_gain_cali(int gain){
 		if (count ++ > 50) {
 			return 0;
 		}
+		delay_us(20);
 	}while(!_cs1180_check_gain(gain));
 	
 	cs1180_self_gain_calibrate();
@@ -201,11 +202,11 @@ int cs1180_adc_set_gain_cali(int gain){
 		delay_us(20);
 	}while(count++ <= 5);
 	sys_debug("cs1180 cali sys offset try count %d\n", count);
-	if (!cs1180_check_cali_offset()){
+	if (count > 5){
 		return 0;
 	}
 	_cs1180_gain = 1 << gain;
-	delay_us(20);
+	delay_us(50);
 	cs1180_save_regs(gain);
 	return 1;
 }
@@ -230,15 +231,16 @@ void cs1180_adc_init(void){
 		spi1_init();
 		delay_us(10);
 		_cs1180_ready = cs1180_adc_set_gain_cali(CS1180_GAIN_128X);
-		if (!_cs1180_ready) {
-			cs1180_adc_shutdown();
+		if (_cs1180_ready || (count ++ < 5)) {
+			break;
 		}
-	}while(!_cs1180_ready && count++ < 5);
+		cs1180_adc_shutdown();
+	}while(1);
 	sys_debug("cs1180 init retry %d\n", count);
 }
 
-#if 0
-int _cs1180_check_gain(int gain){
+
+int _cs1180_check_regs(int gain){
 	uint8_t data[13] = {0};
 	cs1180_cs(0);
 	cs_delay();	
@@ -251,7 +253,7 @@ int _cs1180_check_gain(int gain){
 	}
 	return 1;
 }
-#endif
+
 
 //return 1: cs1180 is OK, 0: cs1180 can not work
 int cs1180_adc_set_gain_online(int gain){
@@ -268,13 +270,13 @@ int cs1180_adc_set_gain_online(int gain){
 	do {
 		cs1180_reset();
 		delay_us(10);
+		
 		cs1180_cs(0);
-		cs_delay();
 		spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
 		cs1180_cs(1);
 		delay_us(10);
 
-		if (_cs1180_check_gain(gain) && cs1180_check_cali_offset()){
+		if (_cs1180_check_regs(gain)){
 			CS1180_NOW_GAIN = gain;
 			_cs1180_gain = 1 << gain;
 			_cs1180_ready = 1;
@@ -361,9 +363,9 @@ float cs1180_adc_sample(int *valide)
 			cs1180_low_ff_count = 0;
 			cs1180_may_error ++;
 			delay_us(20);
-			cs1180_dymu_read();
+			cs1180_dumy_read();
 			delay_us(20);
-			cs1180_dymu_read();
+			cs1180_dumy_read();
 			if (valide) {
 				*valide = 0;
 			}