OneWire.h 20 KB

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