onewire.h 5.9 KB

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