Andrew Boyson / Mbed 2 deprecated iot

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Wed May 11 16:42:35 2016 +0000
Parent:
4:e076884ef8bd
Child:
6:be97d38e0b01
Commit message:
Added CRC to 1-wire

Changed in this revision

1-wire.cpp Show annotated file Show diff for this revision Revisions of this file
1-wire.h Show annotated file Show diff for this revision Revisions of this file
ds18b20.cpp Show annotated file Show diff for this revision Revisions of this file
ds18b20.h Show annotated file Show diff for this revision Revisions of this file
server.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/1-wire.cpp	Tue May 03 09:23:26 2016 +0000
+++ b/1-wire.cpp	Wed May 11 16:42:35 2016 +0000
@@ -10,69 +10,69 @@
 static int busvalue;
 
 //Delays
-#define           ATTACH_DELAY    7
-#define          INITIAL_DELAY  100
-#define    RESET_BUS_LOW_DELAY  480
-#define    READ_PRESENCE_DELAY   70
-#define    RESET_RELEASE_DELAY  410
-#define  WRITE_0_BUS_LOW_DELAY   60
-#define  WRITE_0_RELEASE_DELAY   10
-#define  WRITE_1_BUS_LOW_DELAY    6
-#define  WRITE_1_RELEASE_DELAY   64
-#define READ_BIT_BUS_LOW_DELAY    6
-#define         READ_BIT_DELAY    9
-#define READ_BIT_RELEASE_DELAY   55
-#define         BUS_TIMEOUT_MS 1000
+#define           ATTACH_US    7
+#define          INITIAL_US  100
+#define    RESET_BUS_LOW_US  480
+#define    READ_PRESENCE_US   70
+#define    RESET_RELEASE_US  410
+#define  WRITE_0_BUS_LOW_US   60
+#define  WRITE_0_RELEASE_US   10
+#define  WRITE_1_BUS_LOW_US    6
+#define  WRITE_1_RELEASE_US   64
+#define READ_BIT_BUS_LOW_US    6
+#define         READ_BIT_US    9
+#define READ_BIT_RELEASE_US   55
+#define      BUS_TIMEOUT_MS 1000
 
 static int busbusy = false;   //Set whenever an operation starts; reset by finishbus interrupt;
 static Timer busbusytimer;
 
-static void         buslow (void) {        pin = 0;   pin.output();                  }
 static Timeout startbuslow;
-static void         busfree(void) {                   pin.input ();                  }
 static Timeout startbusfree;
-static void         bushigh(void) {        pin = 1;   pin.output();                  }
 static Timeout startbushigh;
-static void         busread(void) {   busvalue = pin;                                }
 static Timeout startbusread;
-static void         busidle(void) {                   pin.input (); busbusy = false; }
 static Timeout startbusidle;
+static void         buslow (void) { pin.output(); pin = 0;                         }
+static void         busfree(void) { pin.input ();                                  }
+static void         bushigh(void) { pin.output(); pin = 1;                         }
+static void         busread(void) {               busvalue = pin;                  }
+static void         busidle(void) { pin.input ();                 busbusy = false; }
 
 static void reset()
 {
     busbusy = true;
-    int delay =    INITIAL_DELAY - ATTACH_DELAY; startbuslow.attach_us(&buslow,   delay);
-    delay += RESET_BUS_LOW_DELAY - ATTACH_DELAY; startbusfree.attach_us(&busfree, delay);
-    delay += READ_PRESENCE_DELAY - ATTACH_DELAY; startbusread.attach_us(&busread, delay);
-    delay += RESET_RELEASE_DELAY - ATTACH_DELAY; startbusidle.attach_us(&busidle, delay);
+    int start =    INITIAL_US - ATTACH_US; startbuslow .attach_us(&buslow,  start);
+    start += RESET_BUS_LOW_US - ATTACH_US; startbusfree.attach_us(&busfree, start);
+    start += READ_PRESENCE_US - ATTACH_US; startbusread.attach_us(&busread, start);
+    start += RESET_RELEASE_US - ATTACH_US; startbusidle.attach_us(&busidle, start);
 }
 static void writebit(int bit, int msToPullUp)
 {
     busbusy = true;
-    int delay = INITIAL_DELAY - ATTACH_DELAY;
-    startbuslow.attach_us(&buslow, delay);
-    delay += bit ? WRITE_1_BUS_LOW_DELAY : WRITE_0_BUS_LOW_DELAY;
-    delay -= ATTACH_DELAY;
+    int start = INITIAL_US - ATTACH_US;
+    startbuslow.attach_us(&buslow, start);
+    start += bit ? WRITE_1_BUS_LOW_US : WRITE_0_BUS_LOW_US;
+    start -= ATTACH_US;
     if (msToPullUp)
     {
-        startbushigh.attach_us(&bushigh, delay);
-        delay += msToPullUp * 1000 - ATTACH_DELAY;
+        startbushigh.attach_us(&bushigh, start);
+        start += msToPullUp * 1000 - ATTACH_US;
     }
     else
     {
-        startbusfree.attach_us(&busfree, delay);
-        delay += bit ? WRITE_1_RELEASE_DELAY : WRITE_0_RELEASE_DELAY;
-        delay -= ATTACH_DELAY;
+        startbusfree.attach_us(&busfree, start);
+        start += bit ? WRITE_1_RELEASE_US : WRITE_0_RELEASE_US;
+        start -= ATTACH_US;
     }
-    startbusidle.attach_us(&busidle,  delay);
+    startbusidle.attach_us(&busidle,  start);
 }
 static void initiatebitread()
 {
     busbusy = true;
-    int delay =       INITIAL_DELAY - ATTACH_DELAY; startbuslow.attach_us(&buslow,   delay);
-    delay += READ_BIT_BUS_LOW_DELAY - ATTACH_DELAY; startbusfree.attach_us(&busfree, delay);
-    delay +=         READ_BIT_DELAY - ATTACH_DELAY; startbusread.attach_us(&busread, delay);
-    delay += READ_BIT_RELEASE_DELAY - ATTACH_DELAY; startbusidle.attach_us(&busidle, delay);
+    int start =       INITIAL_US - ATTACH_US; startbuslow .attach_us(&buslow,  start);
+    start += READ_BIT_BUS_LOW_US - ATTACH_US; startbusfree.attach_us(&busfree, start);
+    start +=         READ_BIT_US - ATTACH_US; startbusread.attach_us(&busread, start);
+    start += READ_BIT_RELEASE_US - ATTACH_US; startbusidle.attach_us(&busidle, start);
 }
 
 //Exchange zone
@@ -90,6 +90,23 @@
 static char*  precv = NULL;
 static int pullupms = 0;
 
+static char crc;
+static void resetCrc()
+{
+    crc = 0;
+}
+static void addBitToCrc(int bit)
+{
+    //Logical Exclusive Or of the 9th output of the shift register with the input
+    int feedback = !(crc & 0x80) != !bit;
+    
+    //Move the shift register one place to the left leaving a zero in the lsb
+    crc <<= 1;
+    
+    //Exclusive Or the shift register with polynomial X5 + X4 + 1
+    if (feedback) crc ^= 0x31;
+}
+
 static void resetBitPosition(int* pByteIndex, char* pBitMask)
 {
     *pBitMask = 1;
@@ -148,6 +165,10 @@
     xchgstate = XCHG_RESET;
     reset();
 }
+char OneWireCrc()
+{
+    return crc;
+}
 int OneWireMain()
 {
     static int byteindex;
@@ -198,6 +219,7 @@
                 resetBitPosition(&byteindex, &bitmask);
                 if (moreBitsToRead(byteindex))
                 {
+                    resetCrc();
                     initiatebitread();
                     xchgstate = XCHG_READ;
                     
@@ -209,6 +231,7 @@
             }
             break;
         case XCHG_READ:
+            addBitToCrc(busvalue);
             setBitAtPosition(byteindex, bitmask, busvalue);
             incrementBitPosition(&byteindex, &bitmask);
             if (moreBitsToRead(byteindex))
--- a/1-wire.h	Tue May 03 09:23:26 2016 +0000
+++ b/1-wire.h	Wed May 11 16:42:35 2016 +0000
@@ -1,4 +1,5 @@
 extern int  OneWireInit();
 extern int  OneWireBusy();
 extern void OneWireExchange(int lenBytesToSend, int lenBytesToRecv, char *pBytesToSend, char *pBytesToRecv, int msToPullUp);
+extern char OneWireCrc();
 extern int  OneWireMain();
--- a/ds18b20.cpp	Tue May 03 09:23:26 2016 +0000
+++ b/ds18b20.cpp	Wed May 11 16:42:35 2016 +0000
@@ -12,6 +12,8 @@
 static int sendlen = 0;
 static int recvlen = 0;
 
+uint16_t DS18B20Value;
+
 void DS18B20ReadRom()
 {
     sendlen = 1;
@@ -20,11 +22,12 @@
     OneWireExchange(sendlen, recvlen, send, recv, 0);
 }
 void DS18B20ReadScratchpad()
-{
+{   
     sendlen = 2;
     send[0] = 0xCC;
     send[1] = 0xBE;
     recvlen = 9;
+    for (int i = 0; i < recvlen; i++) recv[i] = 0;
     OneWireExchange(sendlen, recvlen, send, recv, 0);
 }
 void DS18B20ConvertT()
@@ -33,17 +36,52 @@
     send[0] = 0xCC;
     send[1] = 0x44;
     recvlen = 0;
+    for (int i = 0; i < recvlen; i++) recv[i] = 0;
     OneWireExchange(sendlen, recvlen, send, recv, 750);
 }
-int DS18B20Init()
+#define IDLE                0
+#define CONVERT_T           1
+#define READ_SCRATCH_PAD    2
+#define EXTRACT_TEMPERATURE 3
+static int state = IDLE;
+static int handlestate()
 {
-    ticker.attach(&DS18B20ReadScratchpad, 10.0);
+    if (OneWireBusy()) return 0;
+    
+    switch (state)
+    {
+        case IDLE:
+            break;
+        case CONVERT_T:
+            DS18B20ConvertT();
+            state = READ_SCRATCH_PAD;
+            break;
+        case READ_SCRATCH_PAD:
+            DS18B20ReadScratchpad();
+            state = EXTRACT_TEMPERATURE;
+            break;
+        case EXTRACT_TEMPERATURE:
+            if (OneWireCrc())
+            {
+                DS18B20Value = 0xFFFF;
+            }
+            else
+            {
+                DS18B20Value = recv[1];
+                DS18B20Value <<= 8;
+                DS18B20Value |= recv[0];
+            }
+            state = IDLE;
+            break;
+        default:
+            LogF("Unknown DS18B20 state %d\r\n", state);
+            return -1;
+    }
     return 0;
 }
-int DS18B20Main()
+static void logcomms()
 {
     static int wasbusy = false;
-
     if (!OneWireBusy() && wasbusy)
     {
         LogF("1-wire | send:");
@@ -52,7 +90,24 @@
         for (int i = 0; i < recvlen; i++) LogF(" %02x", recv[i]);
         LogF("\r\n");
     }
-
     wasbusy = OneWireBusy();
+}
+static void timed()
+{
+    state = CONVERT_T;
+}
+int DS18B20Init()
+{
+    ticker.attach(&timed, 10.0);
+    return 0;
+}
+int DS18B20Main()
+{
+    logcomms();
+    
+    int r = handlestate();
+    
+    if (r) return -1;
+    
     return 0;
 }
\ No newline at end of file
--- a/ds18b20.h	Tue May 03 09:23:26 2016 +0000
+++ b/ds18b20.h	Wed May 11 16:42:35 2016 +0000
@@ -1,2 +1,4 @@
-extern int DS18B20Init();
-extern int DS18B20Main();
\ No newline at end of file
+extern uint16_t DS18B20Value;
+
+extern  int     DS18B20Init();
+extern  int     DS18B20Main();
\ No newline at end of file
--- a/server.cpp	Tue May 03 09:23:26 2016 +0000
+++ b/server.cpp	Wed May 11 16:42:35 2016 +0000
@@ -1,9 +1,10 @@
-#include  "esp.h"
-#include   "at.h"
-#include   "io.h"
-#include  "log.h"
-#include "time.h"
-#include "wifi.h"
+#include     "esp.h"
+#include      "at.h"
+#include      "io.h"
+#include     "log.h"
+#include    "time.h"
+#include    "wifi.h"
+#include "ds18b20.h"
 #define RECV_BUFFER_SIZE 1024
 #define SEND_BUFFER_SIZE  256
 #define SERVER_PORT 80
@@ -33,6 +34,26 @@
     }
     return 0;
 }
+static int addChunkF(int prevlen, char *fmt, ...)
+{
+    //Set up variable arguments
+    va_list argptr;
+    va_start(argptr, fmt);
+        
+    //Fill the buffer
+    int room = SEND_BUFFER_SIZE - prevlen;
+    int sent = vsnprintf(sendbuffer + prevlen, room, fmt, argptr);
+    if (sent > room) sent = room;
+    if (sent < 0) sent = 0;
+    
+    //Finish with variable arguments
+    va_end(argptr);
+    
+    //Return length
+    int len = prevlen + sent;
+    return len < SEND_BUFFER_SIZE ? len : SEND_BUFFER_SIZE;
+    
+}
 
 static int addChunk(int prevlen, char *text)
 {
@@ -125,9 +146,9 @@
             lineToSendToId[id] += 1;
             break;
         case 2:
-            len = fillChunk("Version: ");
-            len = addChunk(len, AtEspVersion);
-            len = addChunk(len, "\r\n");
+            len = 0;
+            len = addChunkF(len, "Version: %s<br/>\r\n", AtEspVersion);
+            len = addChunkF(len, "Temperature: %1.1f\r\n", DS18B20Value / 16.0);
             lineToSendToId[id] += 1;
             break;
         case 3: