OneWire.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef OneWire_h
  2. #define OneWire_h
  3. #ifdef __cplusplus
  4. #include <stdint.h>
  5. #if defined(__AVR__)
  6. #include <util/crc16.h>
  7. #endif
  8. #if ARDUINO >= 100
  9. #include <Arduino.h> // for delayMicroseconds, digitalPinToBitMask, etc
  10. #else
  11. #include "WProgram.h" // for delayMicroseconds
  12. #include "pins_arduino.h" // for digitalPinToBitMask, etc
  13. #endif
  14. // You can exclude certain features from OneWire. In theory, this
  15. // might save some space. In practice, the compiler automatically
  16. // removes unused code (technically, the linker, using -fdata-sections
  17. // and -ffunction-sections when compiling, and Wl,--gc-sections
  18. // when linking), so most of these will not result in any code size
  19. // reduction. Well, unless you try to use the missing features
  20. // and redesign your program to not need them! ONEWIRE_CRC8_TABLE
  21. // is the exception, because it selects a fast but large algorithm
  22. // or a small but slow algorithm.
  23. // you can exclude onewire_search by defining that to 0
  24. #ifndef ONEWIRE_SEARCH
  25. #define ONEWIRE_SEARCH 1
  26. #endif
  27. // You can exclude CRC checks altogether by defining this to 0
  28. #ifndef ONEWIRE_CRC
  29. #define ONEWIRE_CRC 1
  30. #endif
  31. // Select the table-lookup method of computing the 8-bit CRC
  32. // by setting this to 1. The lookup table enlarges code size by
  33. // about 250 bytes. It does NOT consume RAM (but did in very
  34. // old versions of OneWire). If you disable this, a slower
  35. // but very compact algorithm is used.
  36. #ifndef ONEWIRE_CRC8_TABLE
  37. #define ONEWIRE_CRC8_TABLE 1
  38. #endif
  39. // You can allow 16-bit CRC checks by defining this to 1
  40. // (Note that ONEWIRE_CRC must also be 1.)
  41. #ifndef ONEWIRE_CRC16
  42. #define ONEWIRE_CRC16 1
  43. #endif
  44. // Board-specific macros for direct GPIO
  45. #include "util/OneWire_direct_regtype.h"
  46. class OneWire
  47. {
  48. private:
  49. IO_REG_TYPE bitmask;
  50. volatile IO_REG_TYPE *baseReg;
  51. #if ONEWIRE_SEARCH
  52. // global search state
  53. unsigned char ROM_NO[8];
  54. uint8_t LastDiscrepancy;
  55. uint8_t LastFamilyDiscrepancy;
  56. bool LastDeviceFlag;
  57. #endif
  58. public:
  59. OneWire() { }
  60. OneWire(uint8_t pin) { begin(pin); }
  61. void begin(uint8_t pin);
  62. // Perform a 1-Wire reset cycle. Returns 1 if a device responds
  63. // with a presence pulse. Returns 0 if there is no device or the
  64. // bus is shorted or otherwise held low for more than 250uS
  65. uint8_t reset(void);
  66. // Issue a 1-Wire rom select command, you do the reset first.
  67. void select(const uint8_t rom[8]);
  68. // Issue a 1-Wire rom skip command, to address all on bus.
  69. void skip(void);
  70. // Write a byte. If 'power' is one then the wire is held high at
  71. // the end for parasitically powered devices. You are responsible
  72. // for eventually depowering it by calling depower() or doing
  73. // another read or write.
  74. void write(uint8_t v, uint8_t power = 0);
  75. void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
  76. // Read a byte.
  77. uint8_t read(void);
  78. void read_bytes(uint8_t *buf, uint16_t count);
  79. // Write a bit. The bus is always left powered at the end, see
  80. // note in write() about that.
  81. void write_bit(uint8_t v);
  82. // Read a bit.
  83. uint8_t read_bit(void);
  84. // Stop forcing power onto the bus. You only need to do this if
  85. // you used the 'power' flag to write() or used a write_bit() call
  86. // and aren't about to do another read or write. You would rather
  87. // not leave this powered if you don't have to, just in case
  88. // someone shorts your bus.
  89. void depower(void);
  90. #if ONEWIRE_SEARCH
  91. // Clear the search state so that if will start from the beginning again.
  92. void reset_search();
  93. // Setup the search to find the device type 'family_code' on the next call
  94. // to search(*newAddr) if it is present.
  95. void target_search(uint8_t family_code);
  96. // Look for the next device. Returns 1 if a new address has been
  97. // returned. A zero might mean that the bus is shorted, there are
  98. // no devices, or you have already retrieved all of them. It
  99. // might be a good idea to check the CRC to make sure you didn't
  100. // get garbage. The order is deterministic. You will always get
  101. // the same devices in the same order.
  102. bool search(uint8_t *newAddr, bool search_mode = true);
  103. #endif
  104. #if ONEWIRE_CRC
  105. // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
  106. // ROM and scratchpad registers.
  107. static uint8_t crc8(const uint8_t *addr, uint8_t len);
  108. #if ONEWIRE_CRC16
  109. // Compute the 1-Wire CRC16 and compare it against the received CRC.
  110. // Example usage (reading a DS2408):
  111. // // Put everything in a buffer so we can compute the CRC easily.
  112. // uint8_t buf[13];
  113. // buf[0] = 0xF0; // Read PIO Registers
  114. // buf[1] = 0x88; // LSB address
  115. // buf[2] = 0x00; // MSB address
  116. // WriteBytes(net, buf, 3); // Write 3 cmd bytes
  117. // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
  118. // if (!CheckCRC16(buf, 11, &buf[11])) {
  119. // // Handle error.
  120. // }
  121. //
  122. // @param input - Array of bytes to checksum.
  123. // @param len - How many bytes to use.
  124. // @param inverted_crc - The two CRC16 bytes in the received data.
  125. // This should just point into the received data,
  126. // *not* at a 16-bit integer.
  127. // @param crc - The crc starting value (optional)
  128. // @return True, iff the CRC matches.
  129. static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
  130. // Compute a Dallas Semiconductor 16 bit CRC. This is required to check
  131. // the integrity of data received from many 1-Wire devices. Note that the
  132. // CRC computed here is *not* what you'll get from the 1-Wire network,
  133. // for two reasons:
  134. // 1) The CRC is transmitted bitwise inverted.
  135. // 2) Depending on the endian-ness of your processor, the binary
  136. // representation of the two-byte return value may have a different
  137. // byte order than the two bytes you get from 1-Wire.
  138. // @param input - Array of bytes to checksum.
  139. // @param len - How many bytes to use.
  140. // @param crc - The crc starting value (optional)
  141. // @return The CRC16, as defined by Dallas Semiconductor.
  142. static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
  143. #endif
  144. #endif
  145. };
  146. // Prevent this name from leaking into Arduino sketches
  147. #ifdef IO_REG_TYPE
  148. #undef IO_REG_TYPE
  149. #endif
  150. #endif // __cplusplus
  151. #endif // OneWire_h