thermferm/one-wire.c

changeset 657
38162f374842
parent 656
ca47c742a25d
child 659
bfab45f4d5cd
--- a/thermferm/one-wire.c	Wed Apr 03 12:14:45 2024 +0200
+++ b/thermferm/one-wire.c	Wed Apr 03 16:21:46 2024 +0200
@@ -166,25 +166,29 @@
 		for (device = Config.devices; device; device = device->next) {
 		    if ((strcmp(dev_w1->address, device->address) == 0) && (device->subdevice == i) && (device->direction == DEVDIR_IN_BIN)) {
 			/*
-			 * First make sure that if this device is configured as input
-			 * to drive the output high.
-			 * TODO: This must be made different. Check state and fix only if wrong.
+			 * First make sure that this device is configured as input
+			 * to drive the output high. Fix if programmed as output.
 			 */
 			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
 			    state = (unsigned int)rc;
-			    output = ((state & 0x02) >> 1) + ((state & 0x08) >> 2);         /* Both latch states    */
-			    if (i == 0) {
-				output = (output & 0xfe);
+			    output = ((state & 0x02) >> 1) + ((state & 0x08) >> 2);		/* Both latch states	*/
+			    if ((i == 0) && ((state & 0x02) == 0)) {				/* Fix A side		*/
+				syslog(LOG_NOTICE, "One-wire device %s-%d out %04x -> %04x", dev_w1->address, i, output, output | 0x01);
 				output |= 0x01;
-			    } else if (i == 1) {
-				output = (output & 0xfd);
+				write_w1(device->address, (char *)"output", output);
+				mDelay(10);
+				if ((rc = read_w1(device->address, (char *)"state")) >= 0)	/* Read PIO again	*/
+				    state = (unsigned int)rc;
+			    }
+			    if ((i == 1) && ((state & 0x08) == 0)) {				/* Fix B side		*/
+				syslog(LOG_NOTICE, "One-wire device %s-%d out %04x -> %04x", dev_w1->address, i, output, output | 0x02);
 				output |= 0x02;
-			    } else {
-				output = 0xff;
+				write_w1(device->address, (char *)"output", output);
+				mDelay(10);
+				if ((rc = read_w1(device->address, (char *)"state")) >= 0)      /* Read PIO again       */
+				    state = (unsigned int)rc;
 			    }
-			    write_w1(device->address, (char *)"output", output);
-			}
-			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
+
 			    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			    /*
 			     * Read PIOA or PIOB pin state bits
@@ -198,7 +202,7 @@
 			}
 			mDelay(20);
 		    }
-		}
+		} /* for (device = Config.devices; ... */
 	    }
 	}
     }

mercurial