onewire.h 5.8 KB

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