Read ds2413 moved to one-wire thread. Only reprogram if it is an input and programmed as output.

Wed, 03 Apr 2024 16:21:46 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 03 Apr 2024 16:21:46 +0200
changeset 657
38162f374842
parent 656
ca47c742a25d
child 658
b10d0f6337a3

Read ds2413 moved to one-wire thread. Only reprogram if it is an input and programmed as output.

thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/one-wire.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Wed Apr 03 12:14:45 2024 +0200
+++ b/thermferm/devices.c	Wed Apr 03 16:21:46 2024 +0200
@@ -835,66 +835,6 @@
 			    free(addr);
 			    addr = NULL;
 			} /* if temperature sensor */
-			/*
-			 * DS2413 Dual channel addressable switch
-			 */
-			if (strncmp(device->address, (char *)"3a", 2) == 0) {
-			    addr = xstrcpy((char *)"/sys/bus/w1/devices/");
-			    addr = xstrcat(addr, device->address);
-			    addr = xstrcat(addr, (char *)"/state");
-
-			    if ((access(addr, R_OK)) == 0) {
-				if (device->present != DEVPRESENT_YES) {
-				    syslog(LOG_NOTICE, "DS2413 %s is back", device->address);
-				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-				    device->present = DEVPRESENT_YES;
-				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-				}
-				/*
-				 * First make sure that if this device is configured as input
-				 * to drive the output high.
-				 */
-				if (device->direction == DEVDIR_IN_BIN) {
-				    uint8_t state, 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 (device->subdevice == 0) {
-					    output = (output & 0xfe);
-					    output |= 0x01;
-					} else if (device->subdevice == 1) {
-					    output = (output & 0xfd);
-					    output |= 0x02;
-					} else {
-					    output = 0xff;
-					}
-					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
-				     */
-				    if (device->subdevice == 0)
-					device->value = (rc & 0x01) ? 0 : 1;
-				    else if (device->subdevice == 1)
-					device->value = (rc & 0x04) ? 0 : 1;
-				    device->timestamp = time(NULL);
-				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-				}
-			    } else {
-				if (device->present != DEVPRESENT_NO) {
-				    syslog(LOG_NOTICE, "DS2413 %s is missing", device->address);
-				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-				    device->present = DEVPRESENT_NO;
-				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-				}
-			    }
-			    free(addr);
-			    addr = NULL;
-			}
 
 			break;
 
--- 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