libraries / OneWire / OneWire.hon commit Added link to project report (97a3ba0)
   1#ifndef OneWire_h
   2#define OneWire_h
   3
   4#include <inttypes.h>
   5
   6#if defined(__AVR__)
   7#include <util/crc16.h>
   8#endif
   9
  10#if ARDUINO >= 100
  11#include "Arduino.h"       // for delayMicroseconds, digitalPinToBitMask, etc
  12#else
  13#include "WProgram.h"      // for delayMicroseconds
  14#include "pins_arduino.h"  // for digitalPinToBitMask, etc
  15#endif
  16
  17// You can exclude certain features from OneWire.  In theory, this
  18// might save some space.  In practice, the compiler automatically
  19// removes unused code (technically, the linker, using -fdata-sections
  20// and -ffunction-sections when compiling, and Wl,--gc-sections
  21// when linking), so most of these will not result in any code size
  22// reduction.  Well, unless you try to use the missing features
  23// and redesign your program to not need them!  ONEWIRE_CRC8_TABLE
  24// is the exception, because it selects a fast but large algorithm
  25// or a small but slow algorithm.
  26
  27// you can exclude onewire_search by defining that to 0
  28#ifndef ONEWIRE_SEARCH
  29#define ONEWIRE_SEARCH 1
  30#endif
  31
  32// You can exclude CRC checks altogether by defining this to 0
  33#ifndef ONEWIRE_CRC
  34#define ONEWIRE_CRC 1
  35#endif
  36
  37// Select the table-lookup method of computing the 8-bit CRC
  38// by setting this to 1.  The lookup table enlarges code size by
  39// about 250 bytes.  It does NOT consume RAM (but did in very
  40// old versions of OneWire).  If you disable this, a slower
  41// but very compact algorithm is used.
  42#ifndef ONEWIRE_CRC8_TABLE
  43#define ONEWIRE_CRC8_TABLE 1
  44#endif
  45
  46// You can allow 16-bit CRC checks by defining this to 1
  47// (Note that ONEWIRE_CRC must also be 1.)
  48#ifndef ONEWIRE_CRC16
  49#define ONEWIRE_CRC16 1
  50#endif
  51
  52#ifndef FALSE
  53#define FALSE 0
  54#endif
  55#ifndef TRUE
  56#define TRUE  1
  57#endif
  58
  59// Platform specific I/O definitions
  60
  61#if defined(__AVR__)
  62#define PIN_TO_BASEREG(pin)             (portInputRegister(digitalPinToPort(pin)))
  63#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
  64#define IO_REG_TYPE uint8_t
  65#define IO_REG_ASM asm("r30")
  66#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)
  67#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) &= ~(mask))
  68#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+1)) |= (mask))
  69#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))
  70#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))
  71
  72#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__)
  73#define PIN_TO_BASEREG(pin)             (portOutputRegister(pin))
  74#define PIN_TO_BITMASK(pin)             (1)
  75#define IO_REG_TYPE uint8_t
  76#define IO_REG_ASM
  77#define DIRECT_READ(base, mask)         (*((base)+512))
  78#define DIRECT_MODE_INPUT(base, mask)   (*((base)+640) = 0)
  79#define DIRECT_MODE_OUTPUT(base, mask)  (*((base)+640) = 1)
  80#define DIRECT_WRITE_LOW(base, mask)    (*((base)+256) = 1)
  81#define DIRECT_WRITE_HIGH(base, mask)   (*((base)+128) = 1)
  82
  83#elif defined(__MKL26Z64__)
  84#define PIN_TO_BASEREG(pin)             (portOutputRegister(pin))
  85#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
  86#define IO_REG_TYPE uint8_t
  87#define IO_REG_ASM
  88#define DIRECT_READ(base, mask)         ((*((base)+16) & (mask)) ? 1 : 0)
  89#define DIRECT_MODE_INPUT(base, mask)   (*((base)+20) &= ~(mask))
  90#define DIRECT_MODE_OUTPUT(base, mask)  (*((base)+20) |= (mask))
  91#define DIRECT_WRITE_LOW(base, mask)    (*((base)+8) = (mask))
  92#define DIRECT_WRITE_HIGH(base, mask)   (*((base)+4) = (mask))
  93
  94#elif defined(__SAM3X8E__)
  95// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
  96// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
  97// If you have trouble with OneWire on Arduino Due, please check the
  98// status of delayMicroseconds() before reporting a bug in OneWire!
  99#define PIN_TO_BASEREG(pin)             (&(digitalPinToPort(pin)->PIO_PER))
 100#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
 101#define IO_REG_TYPE uint32_t
 102#define IO_REG_ASM
 103#define DIRECT_READ(base, mask)         (((*((base)+15)) & (mask)) ? 1 : 0)
 104#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+5)) = (mask))
 105#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+4)) = (mask))
 106#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+13)) = (mask))
 107#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+12)) = (mask))
 108#ifndef PROGMEM
 109#define PROGMEM
 110#endif
 111#ifndef pgm_read_byte
 112#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
 113#endif
 114
 115#elif defined(__PIC32MX__)
 116#define PIN_TO_BASEREG(pin)             (portModeRegister(digitalPinToPort(pin)))
 117#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
 118#define IO_REG_TYPE uint32_t
 119#define IO_REG_ASM
 120#define DIRECT_READ(base, mask)         (((*(base+4)) & (mask)) ? 1 : 0)  //PORTX + 0x10
 121#define DIRECT_MODE_INPUT(base, mask)   ((*(base+2)) = (mask))            //TRISXSET + 0x08
 122#define DIRECT_MODE_OUTPUT(base, mask)  ((*(base+1)) = (mask))            //TRISXCLR + 0x04
 123#define DIRECT_WRITE_LOW(base, mask)    ((*(base+8+1)) = (mask))          //LATXCLR  + 0x24
 124#define DIRECT_WRITE_HIGH(base, mask)   ((*(base+8+2)) = (mask))          //LATXSET + 0x28
 125
 126#elif defined(ARDUINO_ARCH_ESP8266)
 127#define PIN_TO_BASEREG(pin)             ((volatile uint32_t*) GPO)
 128#define PIN_TO_BITMASK(pin)             (1 << pin)
 129#define IO_REG_TYPE uint32_t
 130#define IO_REG_ASM
 131#define DIRECT_READ(base, mask)         ((GPI & (mask)) ? 1 : 0)    //GPIO_IN_ADDRESS
 132#define DIRECT_MODE_INPUT(base, mask)   (GPE &= ~(mask))            //GPIO_ENABLE_W1TC_ADDRESS
 133#define DIRECT_MODE_OUTPUT(base, mask)  (GPE |= (mask))             //GPIO_ENABLE_W1TS_ADDRESS
 134#define DIRECT_WRITE_LOW(base, mask)    (GPOC = (mask))             //GPIO_OUT_W1TC_ADDRESS
 135#define DIRECT_WRITE_HIGH(base, mask)   (GPOS = (mask))             //GPIO_OUT_W1TS_ADDRESS
 136
 137#elif defined(__SAMD21G18A__)
 138#define PIN_TO_BASEREG(pin)             portModeRegister(digitalPinToPort(pin))
 139#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
 140#define IO_REG_TYPE uint32_t
 141#define IO_REG_ASM
 142#define DIRECT_READ(base, mask)         (((*((base)+8)) & (mask)) ? 1 : 0)
 143#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) = (mask))
 144#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+2)) = (mask))
 145#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+5)) = (mask))
 146#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+6)) = (mask))
 147
 148#elif defined(RBL_NRF51822)
 149#define PIN_TO_BASEREG(pin)             (0)
 150#define PIN_TO_BITMASK(pin)             (pin)
 151#define IO_REG_TYPE uint32_t
 152#define IO_REG_ASM
 153#define DIRECT_READ(base, pin)          nrf_gpio_pin_read(pin)
 154#define DIRECT_WRITE_LOW(base, pin)     nrf_gpio_pin_clear(pin)
 155#define DIRECT_WRITE_HIGH(base, pin)    nrf_gpio_pin_set(pin)
 156#define DIRECT_MODE_INPUT(base, pin)    nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL)
 157#define DIRECT_MODE_OUTPUT(base, pin)   nrf_gpio_cfg_output(pin)
 158
 159#elif defined(__arc__) /* Arduino101/Genuino101 specifics */
 160
 161#include "scss_registers.h"
 162#include "portable.h"
 163#include "avr/pgmspace.h"
 164
 165#define GPIO_ID(pin)                    (g_APinDescription[pin].ulGPIOId)
 166#define GPIO_TYPE(pin)                  (g_APinDescription[pin].ulGPIOType)
 167#define GPIO_BASE(pin)                  (g_APinDescription[pin].ulGPIOBase)
 168#define DIR_OFFSET_SS                   0x01
 169#define DIR_OFFSET_SOC                  0x04
 170#define EXT_PORT_OFFSET_SS              0x0A
 171#define EXT_PORT_OFFSET_SOC             0x50
 172
 173/* GPIO registers base address */
 174#define PIN_TO_BASEREG(pin)             ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase)
 175#define PIN_TO_BITMASK(pin)             pin
 176#define IO_REG_TYPE                     uint32_t
 177#define IO_REG_ASM
 178
 179static inline __attribute__((always_inline))
 180IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
 181{
 182    IO_REG_TYPE ret;
 183    if (SS_GPIO == GPIO_TYPE(pin)) {
 184        ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS));
 185    } else {
 186        ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC);
 187    }
 188    return ((ret >> GPIO_ID(pin)) & 0x01);
 189}
 190
 191static inline __attribute__((always_inline))
 192void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
 193{
 194    if (SS_GPIO == GPIO_TYPE(pin)) {
 195        WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)),
 196                        ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
 197    } else {
 198        MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin));
 199    }
 200}
 201
 202static inline __attribute__((always_inline))
 203void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
 204{
 205    if (SS_GPIO == GPIO_TYPE(pin)) {
 206        WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)),
 207                        ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
 208    } else {
 209        MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin));
 210    }
 211}
 212
 213static inline __attribute__((always_inline))
 214void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
 215{
 216    if (SS_GPIO == GPIO_TYPE(pin)) {
 217        WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base);
 218    } else {
 219        MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin));
 220    }
 221}
 222
 223static inline __attribute__((always_inline))
 224void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
 225{
 226    if (SS_GPIO == GPIO_TYPE(pin)) {
 227        WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base);
 228    } else {
 229        MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin));
 230    }
 231}
 232
 233#define DIRECT_READ(base, pin)          directRead(base, pin)
 234#define DIRECT_MODE_INPUT(base, pin)    directModeInput(base, pin)
 235#define DIRECT_MODE_OUTPUT(base, pin)   directModeOutput(base, pin)
 236#define DIRECT_WRITE_LOW(base, pin)     directWriteLow(base, pin)
 237#define DIRECT_WRITE_HIGH(base, pin)    directWriteHigh(base, pin)
 238
 239#else
 240#define PIN_TO_BASEREG(pin)             (0)
 241#define PIN_TO_BITMASK(pin)             (pin)
 242#define IO_REG_TYPE unsigned int
 243#define IO_REG_ASM
 244#define DIRECT_READ(base, pin)          digitalRead(pin)
 245#define DIRECT_WRITE_LOW(base, pin)     digitalWrite(pin, LOW)
 246#define DIRECT_WRITE_HIGH(base, pin)    digitalWrite(pin, HIGH)
 247#define DIRECT_MODE_INPUT(base, pin)    pinMode(pin,INPUT)
 248#define DIRECT_MODE_OUTPUT(base, pin)   pinMode(pin,OUTPUT)
 249#warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture."
 250
 251#endif
 252
 253
 254class OneWire
 255{
 256  private:
 257    IO_REG_TYPE bitmask;
 258    volatile IO_REG_TYPE *baseReg;
 259
 260#if ONEWIRE_SEARCH
 261    // global search state
 262    unsigned char ROM_NO[8];
 263    uint8_t LastDiscrepancy;
 264    uint8_t LastFamilyDiscrepancy;
 265    uint8_t LastDeviceFlag;
 266#endif
 267
 268  public:
 269    OneWire( uint8_t pin);
 270
 271    // Perform a 1-Wire reset cycle. Returns 1 if a device responds
 272    // with a presence pulse.  Returns 0 if there is no device or the
 273    // bus is shorted or otherwise held low for more than 250uS
 274    uint8_t reset(void);
 275
 276    // Issue a 1-Wire rom select command, you do the reset first.
 277    void select(const uint8_t rom[8]);
 278
 279    // Issue a 1-Wire rom skip command, to address all on bus.
 280    void skip(void);
 281
 282    // Write a byte. If 'power' is one then the wire is held high at
 283    // the end for parasitically powered devices. You are responsible
 284    // for eventually depowering it by calling depower() or doing
 285    // another read or write.
 286    void write(uint8_t v, uint8_t power = 0);
 287
 288    void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
 289
 290    // Read a byte.
 291    uint8_t read(void);
 292
 293    void read_bytes(uint8_t *buf, uint16_t count);
 294
 295    // Write a bit. The bus is always left powered at the end, see
 296    // note in write() about that.
 297    void write_bit(uint8_t v);
 298
 299    // Read a bit.
 300    uint8_t read_bit(void);
 301
 302    // Stop forcing power onto the bus. You only need to do this if
 303    // you used the 'power' flag to write() or used a write_bit() call
 304    // and aren't about to do another read or write. You would rather
 305    // not leave this powered if you don't have to, just in case
 306    // someone shorts your bus.
 307    void depower(void);
 308
 309#if ONEWIRE_SEARCH
 310    // Clear the search state so that if will start from the beginning again.
 311    void reset_search();
 312
 313    // Setup the search to find the device type 'family_code' on the next call
 314    // to search(*newAddr) if it is present.
 315    void target_search(uint8_t family_code);
 316
 317    // Look for the next device. Returns 1 if a new address has been
 318    // returned. A zero might mean that the bus is shorted, there are
 319    // no devices, or you have already retrieved all of them.  It
 320    // might be a good idea to check the CRC to make sure you didn't
 321    // get garbage.  The order is deterministic. You will always get
 322    // the same devices in the same order.
 323    uint8_t search(uint8_t *newAddr, bool search_mode = true);
 324#endif
 325
 326#if ONEWIRE_CRC
 327    // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
 328    // ROM and scratchpad registers.
 329    static uint8_t crc8(const uint8_t *addr, uint8_t len);
 330
 331#if ONEWIRE_CRC16
 332    // Compute the 1-Wire CRC16 and compare it against the received CRC.
 333    // Example usage (reading a DS2408):
 334    //    // Put everything in a buffer so we can compute the CRC easily.
 335    //    uint8_t buf[13];
 336    //    buf[0] = 0xF0;    // Read PIO Registers
 337    //    buf[1] = 0x88;    // LSB address
 338    //    buf[2] = 0x00;    // MSB address
 339    //    WriteBytes(net, buf, 3);    // Write 3 cmd bytes
 340    //    ReadBytes(net, buf+3, 10);  // Read 6 data bytes, 2 0xFF, 2 CRC16
 341    //    if (!CheckCRC16(buf, 11, &buf[11])) {
 342    //        // Handle error.
 343    //    }     
 344    //          
 345    // @param input - Array of bytes to checksum.
 346    // @param len - How many bytes to use.
 347    // @param inverted_crc - The two CRC16 bytes in the received data.
 348    //                       This should just point into the received data,
 349    //                       *not* at a 16-bit integer.
 350    // @param crc - The crc starting value (optional)
 351    // @return True, iff the CRC matches.
 352    static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
 353
 354    // Compute a Dallas Semiconductor 16 bit CRC.  This is required to check
 355    // the integrity of data received from many 1-Wire devices.  Note that the
 356    // CRC computed here is *not* what you'll get from the 1-Wire network,
 357    // for two reasons:
 358    //   1) The CRC is transmitted bitwise inverted.
 359    //   2) Depending on the endian-ness of your processor, the binary
 360    //      representation of the two-byte return value may have a different
 361    //      byte order than the two bytes you get from 1-Wire.
 362    // @param input - Array of bytes to checksum.
 363    // @param len - How many bytes to use.
 364    // @param crc - The crc starting value (optional)
 365    // @return The CRC16, as defined by Dallas Semiconductor.
 366    static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
 367#endif
 368#endif
 369};
 370
 371#endif