Vladimir N. Shilov 1 gadu atpakaļ
vecāks
revīzija
c821dde8c3
2 mainītis faili ar 66 papildinājumiem un 56 dzēšanām
  1. 51 53
      lib/onewire/testhal_onewire.c
  2. 15 3
      main.c

+ 51 - 53
lib/onewire/testhal_onewire.c

@@ -150,67 +150,65 @@ void onewireTest(void) {
   for (i=0; i<3; i++)
     temperature[i] = -666;
 
-  while (true) {
-    if (true == onewireReset(&OWD1)){
-
-      memset(rombuf, 0x55, sizeof(rombuf));
-      search_led_on();
-      devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
-      search_led_off();
-      osalDbgCheck(devices_on_bus <= 3);
-      osalDbgCheck(devices_on_bus  > 0);
-
-      if (1 == devices_on_bus){
-        /* test read rom command */
-        presence = onewireReset(&OWD1);
-        osalDbgCheck(true == presence);
-        testbuf[0] = ONEWIRE_CMD_READ_ROM;
-        onewireWrite(&OWD1, testbuf, 1, 0);
-        onewireRead(&OWD1, testbuf, 8);
-        osalDbgCheck(testbuf[7] == onewireCRC(testbuf, 7));
-        osalDbgCheck(0 == memcmp(rombuf, testbuf, 8));
-      }
-
-      /* start temperature measurement on all connected devices at once */
+  if (true == onewireReset(&OWD1)){
+
+    memset(rombuf, 0x55, sizeof(rombuf));
+    search_led_on();
+    devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
+    search_led_off();
+    osalDbgCheck(devices_on_bus <= 3);
+    osalDbgCheck(devices_on_bus  > 0);
+
+    if (1 == devices_on_bus){
+      /* test read rom command */
       presence = onewireReset(&OWD1);
       osalDbgCheck(true == presence);
-      testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
-      testbuf[1] = ONEWIRE_CMD_CONVERT_TEMP;
+      testbuf[0] = ONEWIRE_CMD_READ_ROM;
+      onewireWrite(&OWD1, testbuf, 1, 0);
+      onewireRead(&OWD1, testbuf, 8);
+      osalDbgCheck(testbuf[7] == onewireCRC(testbuf, 7));
+      osalDbgCheck(0 == memcmp(rombuf, testbuf, 8));
+    }
+
+    /* start temperature measurement on all connected devices at once */
+    presence = onewireReset(&OWD1);
+    osalDbgCheck(true == presence);
+    testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
+    testbuf[1] = ONEWIRE_CMD_CONVERT_TEMP;
 
 #if ONEWIRE_USE_STRONG_PULLUP
-      onewireWrite(&OWD1, testbuf, 2, TIME_MS2I(750));
+    onewireWrite(&OWD1, testbuf, 2, TIME_MS2I(750));
 #else
-      onewireWrite(&OWD1, testbuf, 2, 0);
-      /* poll bus waiting ready signal from all connected devices */
-      testbuf[0] = 0;
-      while (testbuf[0] == 0){
-        osalThreadSleepMilliseconds(50);
-        onewireRead(&OWD1, testbuf, 1);
-      }
+    onewireWrite(&OWD1, testbuf, 2, 0);
+    /* poll bus waiting ready signal from all connected devices */
+    testbuf[0] = 0;
+    while (testbuf[0] == 0){
+      osalThreadSleepMilliseconds(50);
+      onewireRead(&OWD1, testbuf, 1);
+    }
 #endif
 
-      for (i=0; i<devices_on_bus; i++) {
-        /* read temperature device by device from their scratchpads */
-        presence = onewireReset(&OWD1);
-        osalDbgCheck(true == presence);
-
-        testbuf[0] = ONEWIRE_CMD_MATCH_ROM;
-        memcpy(&testbuf[1], &rombuf[i*8], 8);
-        testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD;
-        onewireWrite(&OWD1, testbuf, 10, 0);
-
-        onewireRead(&OWD1, testbuf, 9);
-        osalDbgCheck(testbuf[8] == onewireCRC(testbuf, 8));
-        memcpy(&tmp, &testbuf, 2);
-        temperature[i] = ((int32_t)tmp * 625) / 10;
-      }
-    }
-    else {
-      devices_on_bus = 0;
+    for (i=0; i<devices_on_bus; i++) {
+      /* read temperature device by device from their scratchpads */
+      presence = onewireReset(&OWD1);
+      osalDbgCheck(true == presence);
+
+      testbuf[0] = ONEWIRE_CMD_MATCH_ROM;
+      memcpy(&testbuf[1], &rombuf[i*8], 8);
+      testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD;
+      onewireWrite(&OWD1, testbuf, 10, 0);
+
+      onewireRead(&OWD1, testbuf, 9);
+      osalDbgCheck(testbuf[8] == onewireCRC(testbuf, 8));
+      memcpy(&tmp, &testbuf, 2);
+      temperature[i] = ((int32_t)tmp * 625) / 10;
     }
-    osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
   }
-
+  else {
+    devices_on_bus = 0;
+  }
+  osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
+  
   onewireStop(&OWD1);
 }
 
@@ -222,6 +220,6 @@ int32_t onewireGetTemperature(const size_t num) {
   if (num > devices_on_bus || num < devices_on_bus) {
     return -999;
   } else {
-    return temperature[num];
+    return temperature[num-1];
   }
 }

+ 15 - 3
main.c

@@ -70,7 +70,7 @@ int main(void) {
   /*
    * Creates the blinker thread.
    */
-  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
+  //chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
 
   /*
    * Buttons
@@ -89,18 +89,30 @@ int main(void) {
   /*
    * Normal main() thread activity.
    */
+  ST7735_WriteString(0, 20, "onewireTest...", LiberM_7x10, Blue, Black);
   onewireTest();
+  int32_t t;
   int n = onewireGetDevicesNum();
+  chsnprintf(buf, 22, "Found %d device[s]", n);
+  ST7735_WriteString(0, 80, buf, LiberM_7x10, Cyan, Black);
   if (n != 0) {
-    int32_t t = onewireGetTemperature(1);
+    t = onewireGetTemperature(1);
     chsnprintf(buf, 22, "    T = %d °C     ", t);
     ST7735_WriteString(0, 20, buf, LiberM_7x10, Yellow, Black);
   } else {
     ST7735_WriteString(0, 20, "DS18B20 not found.", LiberM_7x10, Red, Black);
   }
 
+  int a, b;
   while (true) {
-    chThdSleepMilliseconds(500);
+    chThdSleepMilliseconds(1000);
+    if (n != 0) {
+      onewireTest();
+      t = onewireGetTemperature(1);
+      a = t / 1000; b = t % 1000;
+      chsnprintf(buf, 22, "T = %d.%03u oC  ", a, b);
+      ST7735_WriteString(0, 40, buf, LiberM_7x10, Yellow, Black);
+    }
   }
 }