orgua / OneWireHub

OneWire slave device emulator
GNU General Public License v3.0
343 stars 86 forks source link

Arduino NANO, CRC not ok, and no DS18x temperatures transmitted. #57

Closed git-Space closed 6 years ago

git-Space commented 6 years ago

Hello,

I am currently testing the Arduino Nano as a slave in the following setup, two Nanos connected to each other:

Here is what the master sees when polling (COM port output slightly edited for your viewing pleasure):

ms ;   ROM                  ;Chip;Address;    CRC                   ;  C  ;  F
 14;10  4 55 44 33 22 11 27 ;DS18S20;1;FF FF FF FF FF FF FF FF FF C9;-0.50;31.10;
 50;10  5 55 44 33 22 11 10 ;DS18S20;1;FF FF FF FF FF FF FF FF FF C9;-0.50;31.10;
 84;28  0 55 44 33 22 11 1E ;DS18B20;1;FF FF FF FF FF FF FF FF FF C9;-0.06;31.89;
120;28  2 55 44 33 22 11 70 ;DS18B20;1;FF FF FF FF FF FF FF FF FF C9;-0.06;31.89;
156;28  1 55 44 33 22 11 29 ;DS18B20;1;FF FF FF FF FF FF FF FF FF C9;-0.06;31.89;
191;28  3 55 44 33 22 11 47 ;DS18B20;1;FF FF FF FF FF FF FF FF FF C9;-0.06;31.89;
227;28 8B  D 43 98  4  0 BE ;DS18B20;1;89  1 64  5 7F  0  0 FF  C  C;24.56;76.21;
262;22  6 55 44 33 22 11 27 ;DS1822 ;1;FF FF FF FF FF FF FF FF FF C9;-0.06;31.89;
297;22  7 55 44 33 22 11 10 ;DS1822 ;1;FF FF FF FF FF FF FF FF FF C9;-0.06;31.89; 

Obviously, there seems to be an issue with the emulated sensors, the hardware one (at 227ms) is fine!

What I did so far:

The calibration was run, and usually spits out the value 12:

OneWire-Hub calibration by observing the OW-Bus 12 instructions per loop 13 instructions per loop 12 instructions per loop 12 instructions per loop

This value has been updated in the AVR section of the \libraries\OneWireHub\src\platform.h file (I found out that this section is used when the compiler runs):

#if defined(__AVR__) /* arduino (all with atmega, atiny) */

#define PIN_TO_BASEREG(pin)             (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))
using io_reg_t = uint8_t; // define special datatype for register-access
constexpr uint8_t VALUE_IPL {12}; // instructions per loop, compare 0 takes 11, compare 1 takes 13 cycles

Since the different addresses are transmitted without any issue, my guess is that the bus is working fine. Maybe there is something with the variables (int8, int16, float and so on); here is the code on the slave side, where I tested different ways to set the virtual DS18 temperature values :

/*
 *    Example-Code that emulates a bunch of DS18B20 and puts values in it
 */

constexpr bool enable_debug = 1;

#include "OneWireHub.h"
#include "DS18B20.h"
#include <Wire.h>

constexpr uint8_t pin_onewire   { 2 };

auto hub    = OneWireHub(pin_onewire);

//               chip 0x10: DS 18 S 20
//                    0x22: DS 18   22
//                    0x28: DS 18 B 20
auto ds18b0 = DS18B20(0x28, 0x00, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b1 = DS18B20(0x28, 0x01, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b2 = DS18B20(0x28, 0x02, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b3 = DS18B20(0x28, 0x03, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b4 = DS18B20(0x10, 0x04, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b5 = DS18B20(0x10, 0x05, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b6 = DS18B20(0x22, 0x06, 0x55, 0x44, 0x33, 0x22, 0x11); 
auto ds18b7 = DS18B20(0x22, 0x07, 0x55, 0x44, 0x33, 0x22, 0x11); 

void setup()
{
    if (enable_debug)
    {
        Serial.begin(115200);
        Serial.println("OneWire-Hub DS18B20 Temperature-Sensor");
        Serial.flush();    
    }

    // Setup OneWire
    hub.attach(ds18b0);
    hub.attach(ds18b1);
    hub.attach(ds18b2);
    hub.attach(ds18b3);
    hub.attach(ds18b4);
    hub.attach(ds18b5);
    hub.attach(ds18b6);
    hub.attach(ds18b7);
}

void loop()
{
    const int8_t temperature_degC = 75;   // tried to declare vars in different manners, 
    static float _temperature = 20.0;     // thinking it was linked to that. Didn't work ;)
           float _temperature2 = 42.0f;

    // following function must be called periodically
    hub.poll();
    ds18b0.setTemperatureRaw(0);
    ds18b1.setTemperatureRaw(_temperature);
    ds18b2.setTemperature(_temperature2);
    ds18b3.setTemperature(_temperature2);
    ds18b4.setTemperatureRaw(40);
    ds18b5.setTemperature(int8_t(-50));
    ds18b6.setTemperatureRaw(temperature_degC);
    ds18b7.setTemperature(temperature_degC);
}

Or maybe it's in the OneWireHub library itself ?

findstr /s crc libraries\OneWireHub\src*

shows that the CRC for exemple, is handled in different files:

DS18B20.cpp: scratchpad[8] = crc8(scratchpad, 8); DS18B20.cpp: hub->recv(&scratchpad[2], 3); // dont return here, so crc gets updated even if write not complete ... OneWireHub.cpp OneWireHub.h OneWireItem.cpp OneWireItem.h

A last thing I tested, was using another slave library OneWireArduinoSlave with the DallasTemperature/Multiple master from Arduino IDE example sketches. The results are baffling: again, the hardware DS18 is ok, whereas the virtual ones have a CRC value equal 00 (instead of all FF for OneWireHub sensors), and no way to set the temperature values on the virtual DS18 either. I guess there is a common code-base somewhere :)

So, basically that's where I am stuck: The two Nanos communicate through the 1-Wire bus, but somehow I can't get the virtual temperature values transmitted.

git-Space commented 6 years ago

The following setup works:

2 Arduino Nanos (each simulating 8 DS18B20) plus 2 physical DS18B20 on the same bus, connected to a DS9097U, needs the following value changed to get almost 0 errors:

libraries\OneWireHub\src\OneWireHub_config.h constexpr timeOW_t ONEWIRE_TIME_MSG_HIGH_TIMEOUT = { 1500000_us };

300 1-Wire bus readings, over 5398 values retrieved (so only 2 readings were lost):

ds9097u