OneWire.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef OneWire_h
  2. #define OneWire_h
  3. #include <inttypes.h>
  4. #if ARDUINO >= 100
  5. #include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc
  6. #else
  7. #include "WProgram.h" // for delayMicroseconds
  8. #include "pins_arduino.h" // for digitalPinToBitMask, etc
  9. #endif
  10. // You can exclude certain features from OneWire. In theory, this
  11. // might save some space. In practice, the compiler automatically
  12. // removes unused code (technically, the linker, using -fdata-sections
  13. // and -ffunction-sections when compiling, and Wl,--gc-sections
  14. // when linking), so most of these will not result in any code size
  15. // reduction. Well, unless you try to use the missing features
  16. // and redesign your program to not need them! ONEWIRE_CRC8_TABLE
  17. // is the exception, because it selects a fast but large algorithm
  18. // or a small but slow algorithm.
  19. // you can exclude onewire_search by defining that to 0
  20. #ifndef ONEWIRE_SEARCH
  21. #define ONEWIRE_SEARCH 1
  22. #endif
  23. // You can exclude CRC checks altogether by defining this to 0
  24. #ifndef ONEWIRE_CRC
  25. #define ONEWIRE_CRC 1
  26. #endif
  27. // Select the table-lookup method of computing the 8-bit CRC
  28. // by setting this to 1. The lookup table enlarges code size by
  29. // about 250 bytes. It does NOT consume RAM (but did in very
  30. // old versions of OneWire). If you disable this, a slower
  31. // but very compact algorithm is used.
  32. #ifndef ONEWIRE_CRC8_TABLE
  33. #define ONEWIRE_CRC8_TABLE 1
  34. #endif
  35. // You can allow 16-bit CRC checks by defining this to 1
  36. // (Note that ONEWIRE_CRC must also be 1.)
  37. #ifndef ONEWIRE_CRC16
  38. #define ONEWIRE_CRC16 1
  39. #endif
  40. #define FALSE 0
  41. #define TRUE 1
  42. // Platform specific I/O definitions
  43. #if defined(__AVR__)
  44. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  45. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  46. #define IO_REG_TYPE uint8_t
  47. #define IO_REG_ASM asm("r30")
  48. #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  49. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
  50. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
  51. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
  52. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
  53. #elif defined(__MK20DX128__)
  54. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  55. #define PIN_TO_BITMASK(pin) (1)
  56. #define IO_REG_TYPE uint8_t
  57. #define IO_REG_ASM
  58. #define DIRECT_READ(base, mask) (*((base)+512))
  59. #define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
  60. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
  61. #define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
  62. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
  63. #elif defined(__SAM3X8E__)
  64. // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
  65. // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
  66. // If you have trouble with OneWire on Arduino Due, please check the
  67. // status of delayMicroseconds() before reporting a bug in OneWire!
  68. #define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
  69. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  70. #define IO_REG_TYPE uint32_t
  71. #define IO_REG_ASM
  72. #define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
  73. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
  74. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
  75. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
  76. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
  77. #ifndef PROGMEM
  78. #define PROGMEM
  79. #endif
  80. #ifndef pgm_read_byte
  81. #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
  82. #endif
  83. #elif defined(__PIC32MX__)
  84. #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
  85. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  86. #define IO_REG_TYPE uint32_t
  87. #define IO_REG_ASM
  88. #define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
  89. #define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
  90. #define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
  91. #define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
  92. #define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28
  93. #elif defined(ARDUINO_ARCH_ESP8266)
  94. #define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO)
  95. #define PIN_TO_BITMASK(pin) (1 << pin)
  96. #define IO_REG_TYPE uint32_t
  97. #define IO_REG_ASM
  98. #define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS
  99. #define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS
  100. #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS
  101. #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS
  102. #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
  103. #elif defined(__SAMD21G18A__)
  104. #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
  105. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  106. #define IO_REG_TYPE uint32_t
  107. #define IO_REG_ASM
  108. #define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0)
  109. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask))
  110. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask))
  111. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask))
  112. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask))
  113. #else
  114. #error "Please define I/O register types here"
  115. #endif
  116. class OneWire
  117. {
  118. private:
  119. IO_REG_TYPE bitmask;
  120. volatile IO_REG_TYPE *baseReg;
  121. #if ONEWIRE_SEARCH
  122. // global search state
  123. unsigned char ROM_NO[8];
  124. uint8_t LastDiscrepancy;
  125. uint8_t LastFamilyDiscrepancy;
  126. uint8_t LastDeviceFlag;
  127. #endif
  128. public:
  129. OneWire( uint8_t pin);
  130. // Perform a 1-Wire reset cycle. Returns 1 if a device responds
  131. // with a presence pulse. Returns 0 if there is no device or the
  132. // bus is shorted or otherwise held low for more than 250uS
  133. uint8_t reset(void);
  134. // Issue a 1-Wire rom select command, you do the reset first.
  135. void select(const uint8_t rom[8]);
  136. // Issue a 1-Wire rom skip command, to address all on bus.
  137. void skip(void);
  138. // Write a byte. If 'power' is one then the wire is held high at
  139. // the end for parasitically powered devices. You are responsible
  140. // for eventually depowering it by calling depower() or doing
  141. // another read or write.
  142. void write(uint8_t v, uint8_t power = 0);
  143. void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
  144. // Read a byte.
  145. uint8_t read(void);
  146. void read_bytes(uint8_t *buf, uint16_t count);
  147. // Write a bit. The bus is always left powered at the end, see
  148. // note in write() about that.
  149. void write_bit(uint8_t v);
  150. // Read a bit.
  151. uint8_t read_bit(void);
  152. // Stop forcing power onto the bus. You only need to do this if
  153. // you used the 'power' flag to write() or used a write_bit() call
  154. // and aren't about to do another read or write. You would rather
  155. // not leave this powered if you don't have to, just in case
  156. // someone shorts your bus.
  157. void depower(void);
  158. #if ONEWIRE_SEARCH
  159. // Clear the search state so that if will start from the beginning again.
  160. void reset_search();
  161. // Setup the search to find the device type 'family_code' on the next call
  162. // to search(*newAddr) if it is present.
  163. void target_search(uint8_t family_code);
  164. // Look for the next device. Returns 1 if a new address has been
  165. // returned. A zero might mean that the bus is shorted, there are
  166. // no devices, or you have already retrieved all of them. It
  167. // might be a good idea to check the CRC to make sure you didn't
  168. // get garbage. The order is deterministic. You will always get
  169. // the same devices in the same order.
  170. uint8_t search(uint8_t *newAddr);
  171. #endif
  172. #if ONEWIRE_CRC
  173. // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
  174. // ROM and scratchpad registers.
  175. static uint8_t crc8(const uint8_t *addr, uint8_t len);
  176. #if ONEWIRE_CRC16
  177. // Compute the 1-Wire CRC16 and compare it against the received CRC.
  178. // Example usage (reading a DS2408):
  179. // // Put everything in a buffer so we can compute the CRC easily.
  180. // uint8_t buf[13];
  181. // buf[0] = 0xF0; // Read PIO Registers
  182. // buf[1] = 0x88; // LSB address
  183. // buf[2] = 0x00; // MSB address
  184. // WriteBytes(net, buf, 3); // Write 3 cmd bytes
  185. // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
  186. // if (!CheckCRC16(buf, 11, &buf[11])) {
  187. // // Handle error.
  188. // }
  189. //
  190. // @param input - Array of bytes to checksum.
  191. // @param len - How many bytes to use.
  192. // @param inverted_crc - The two CRC16 bytes in the received data.
  193. // This should just point into the received data,
  194. // *not* at a 16-bit integer.
  195. // @param crc - The crc starting value (optional)
  196. // @return True, iff the CRC matches.
  197. static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
  198. // Compute a Dallas Semiconductor 16 bit CRC. This is required to check
  199. // the integrity of data received from many 1-Wire devices. Note that the
  200. // CRC computed here is *not* what you'll get from the 1-Wire network,
  201. // for two reasons:
  202. // 1) The CRC is transmitted bitwise inverted.
  203. // 2) Depending on the endian-ness of your processor, the binary
  204. // representation of the two-byte return value may have a different
  205. // byte order than the two bytes you get from 1-Wire.
  206. // @param input - Array of bytes to checksum.
  207. // @param len - How many bytes to use.
  208. // @param crc - The crc starting value (optional)
  209. // @return The CRC16, as defined by Dallas Semiconductor.
  210. static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
  211. #endif
  212. #endif
  213. };
  214. #endif