1-Wire bus search algorithm, new C implementation

ow_search.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Created by MightyPork on 2018/02/01
  3. // MIT license
  4. //
  5. #ifndef OW_SEARCH_H
  6. #define OW_SEARCH_H
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. // --------------------------------------------------------------------------------------
  10. // External API functions for interfacing the bus
  11. // Customize as needed (and also update calls in the search function)
  12. /**
  13. * Reset the 1-wire bus
  14. *
  15. * @return presence pulse received
  16. */
  17. extern bool ow_reset(void);
  18. /**
  19. * Write a byte to the bus
  20. *
  21. * @param[in] value byte value
  22. */
  23. extern void ow_write_u8(uint8_t value);
  24. /**
  25. * Write a bit to the 1-wire bus
  26. *
  27. * @param[in] value bit value
  28. */
  29. extern void ow_write_bit(bool value);
  30. /**
  31. * Read a bit from the 1-wire bus
  32. *
  33. * @return bit value
  34. */
  35. extern bool ow_read_bit(void);
  36. // --------------------------------------------------------------------------------------
  37. /**
  38. * Data type holding a romcode
  39. */
  40. typedef uint8_t ow_romcode_t[8];
  41. /**
  42. * Get a single bit from a romcode
  43. */
  44. #define ow_code_getbit(code, index) (bool)((code)[(index) >> 3] & (1 << ((index) & 7)))
  45. /**
  46. * Convert to unsigned 64-bit integer
  47. * (works only on little-endian systems - eg. OK on x86/x86_64, not on PowerPC)
  48. */
  49. #define ow_romcode_to_u64(code) (*((uint64_t *) (void *)(code)))
  50. /**
  51. * States of the search algorithm
  52. */
  53. enum ow_search_result {
  54. OW_SEARCH_DONE = 0,
  55. OW_SEARCH_MORE = 1,
  56. OW_SEARCH_FAILED = 2,
  57. };
  58. /**
  59. * Internal state of the search algorithm.
  60. * Check status to see if more remain to be read or an error occurred.
  61. */
  62. struct ow_search_state {
  63. int8_t prev_last_fork;
  64. ow_romcode_t prev_code;
  65. uint8_t command;
  66. enum ow_search_result status;
  67. bool first;
  68. bool test_checksums;
  69. };
  70. /**
  71. * Init the search algorithm state structure
  72. *
  73. * @param[out] state - inited struct
  74. * @param[in] command - command to send for requesting the search (e.g. SEARCH_ROM)
  75. * @param[in] test_checksums - verify checksums of all read romcodes
  76. */
  77. void ow_search_init(struct ow_search_state *state, uint8_t command, bool test_checksums);
  78. /**
  79. * Perform a search of the 1-wire bus, with a state struct pre-inited
  80. * using ow_search_init().
  81. *
  82. * Romcodes are stored in the provided array in a numerically ascending order.
  83. *
  84. * This function may be called repeatedly to retrieve more addresses than could fit
  85. * in the address buffer.
  86. *
  87. * @param[in,out] state - search state, used for multiple calls with limited buffer size
  88. * @param[out] codes - buffer for found romcodes
  89. * @param[in] capacity - buffer capacity
  90. * @return number of romcodes found. Search status is stored in state->status
  91. */
  92. uint16_t ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capacity);
  93. /**
  94. * Compute a 1-wire type checksum.
  95. * If the buffer includes the checksum, the result should be 0.
  96. *
  97. * (this function may be used externally, or you can delete the implementation
  98. * from the c file if another implementation is already available)
  99. *
  100. * @param[in] buf - buffer of bytes to verify
  101. * @param[in] len - buffer length
  102. * @return checksum
  103. */
  104. uint8_t ow_checksum(const uint8_t *buf, uint16_t len);
  105. #endif //OW_SEARCH_H