]> the.earth.li Git - temper-clone.git/commitdiff
Add w1_read_bit() to improve waiting for result
authorJonathan McDowell <noodles@earth.li>
Thu, 10 May 2018 07:03:19 +0000 (08:03 +0100)
committerJonathan McDowell <noodles@earth.li>
Thu, 10 May 2018 07:03:19 +0000 (08:03 +0100)
Rather than using a full w1_read_byte() to wait for the temperature
sensor to indicate a result is ready introduce a w1_read_bit() and
use that instead. This lets us service USB requests more often while
waiting for the result.

main.c
w1.c
w1.h

diff --git a/main.c b/main.c
index 61064a084cbcc45ddf358d7437676c26a2db0155..a75a4fcd53bd0caccf8ddbe36bdc6be45fbde988 100644 (file)
--- a/main.c
+++ b/main.c
@@ -426,7 +426,7 @@ int main(void)
                        w1_write(0x44);         /* Convert T */
                        temp_state = 4;
                } else if (temp_state == 4) {
-                       if (w1_read_byte() == 0xFF)
+                       if (w1_read_bit())
                                temp_state = 5;
                } else if (temp_state == 5) {
                        if (w1_reset()) {
diff --git a/w1.c b/w1.c
index 8217602e0dcaa277ace9d3614ba47c54d420e56a..add1b4895c82d0f6484d213408629581bc686d69 100644 (file)
--- a/w1.c
+++ b/w1.c
@@ -65,26 +65,35 @@ void w1_write(uint8_t val)
        }
 }
 
+bool w1_read_bit()
+{
+       bool val;
+
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+       {
+               /* Pull low for 6µs */
+               DDRB |= 1 << W1_PIN;
+               _delay_us(6);
+               /* Release for 9µs */
+               DDRB &= ~(1 << W1_PIN);
+               _delay_us(9);
+
+               /* Read the line state */
+               val = ((PINB >> W1_PIN) & 1);
+       }
+       _delay_us(55);
+
+       return val;
+}
+
 uint8_t w1_read_byte()
 {
        uint8_t i, val;
 
        val = 0;
        for (i = 0; i < 8; i++) {
-               ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
-               {
-                       /* Pull low for 6µs */
-                       DDRB |= 1 << W1_PIN;
-                       _delay_us(6);
-                       /* Release for 9µs */
-                       DDRB &= ~(1 << W1_PIN);
-                       _delay_us(9);
-
-                       /* Read the line state */
-                       val |= ((PINB >> W1_PIN) & 1) << i;
-               }
-
-               _delay_us(55);
+               if (w1_read_bit())
+                       val |= (1 << i);
        }
 
        return val;
diff --git a/w1.h b/w1.h
index 54c9f5b7df814d2e558f50fb14daa76d4c16150d..bef02a948dc00d644e6de83ea753765ac28b3f5f 100644 (file)
--- a/w1.h
+++ b/w1.h
@@ -5,6 +5,7 @@
 
 uint8_t w1_crc(uint8_t *buf, uint8_t len);
 void w1_write(uint8_t val);
+bool w1_read_bit();
 uint8_t w1_read_byte();
 void w1_read(uint8_t *buf, uint8_t len);
 bool w1_reset(void);