Răsfoiți Sursa

ds18b20: re-arm poll timer even with no sensor present

gettemp() returned early when _1WireInit() found no presence pulse
(no sensor, or a bus short), before reaching the
atomic_set_uint(&System.timers.owire, ...) call at the end that
re-arms the 4s poll interval. With no sensor ever connected, this
timer never gets set at all, so gettemp() (and its ~1ms delay plus a
~960us interrupts-disabled 1-Wire init pulse) runs on every single
main loop pass instead of once per 4 seconds.

Bad CRC and the power-on-reset scratchpad default (85.0C, still in
place if a conversion never completed) were already handled
correctly - both fall through to the normal error-counting and
timer-rearming path since they only skip the point where
temp_ok_out is set to 1, not the whole function.
k4be 1 săptămână în urmă
părinte
comite
05050f4e7e
1 a modificat fișierele cu 27 adăugiri și 21 ștergeri
  1. 27 21
      soft/ds18b20.c

+ 27 - 21
soft/ds18b20.c

@@ -28,24 +28,29 @@ void gettemp(void){
 
 	_delay_ms(1);
 
+	/* No presence pulse (bus short, or no sensor at all): skip straight to
+	 * the error accounting below instead of returning here - an early
+	 * return would skip re-arming System.timers.owire at the end of this
+	 * function, so with no sensor ever connected, this whole function would
+	 * run on every single main loop pass (never gated by the timer again)
+	 * instead of the intended once-per-4s poll. */
+	if(_1WireInit() == 1){
+		_1WireWriteByte(0xcc);
+		_1WireWriteByte(0xbe);
 
-	if(_1WireInit() != 1){
-		return;
-	}
-
-	_1WireWriteByte(0xcc);
-	_1WireWriteByte(0xbe);
-
-	for(i=0;i<9;i++){
-		tmp = _1WireReadByte();
-		t.owbuffer[i] = tmp;
-		crc = _crc_ibutton_update(crc, tmp);
-	}
-	if(!crc){
-		if(t.owbuffer[0] != 0x50 || t.owbuffer[1] != 0x05 || t.owbuffer[5] != 0xff || t.owbuffer[7] != 0x10){
-			temp = (float)t.t*0.0625;
-			System.temperature = temp;
-			temp_ok_out = 1;
+		for(i=0;i<9;i++){
+			tmp = _1WireReadByte();
+			t.owbuffer[i] = tmp;
+			crc = _crc_ibutton_update(crc, tmp);
+		}
+		if(!crc){
+			/* Reject the power-on-reset scratchpad default (85.0C) still in
+			 * place if a conversion never actually completed. */
+			if(t.owbuffer[0] != 0x50 || t.owbuffer[1] != 0x05 || t.owbuffer[5] != 0xff || t.owbuffer[7] != 0x10){
+				temp = (float)t.t*0.0625;
+				System.temperature = temp;
+				temp_ok_out = 1;
+			}
 		}
 	}
 
@@ -59,9 +64,10 @@ void gettemp(void){
 		error_cnt = 0;
 		System.temperature_ok = 1;
 	}
-	
-	_1WireInit();
-	_1WireWriteByte(0xcc);
-	_1WireWriteBytePR(0x44);
+
+	if(_1WireInit() == 1){
+		_1WireWriteByte(0xcc);
+		_1WireWriteBytePR(0x44);
+	}
 	atomic_set_uint(&System.timers.owire, ms(4000));
 }