1wire_internal.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /****************************************************************************
  2. * drivers/1wire/1wire_internal.h
  3. *
  4. * Copyright (C) 2018 Haltian Ltd. All rights reserved.
  5. * Author: Juha Niskanen <juha.niskanen@haltian.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __DRIVERS_1WIRE_1WIRE_INTERNAL_H
  36. #define __DRIVERS_1WIRE_1WIRE_INTERNAL_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <stdbool.h>
  42. #include <nuttx/semaphore.h>
  43. #include <nuttx/power/pm.h>
  44. /****************************************************************************
  45. * Public Types
  46. ****************************************************************************/
  47. struct onewire_dev_s;
  48. struct onewire_sem_s
  49. {
  50. sem_t sem;
  51. pid_t holder; /* The current holder of the semaphore */
  52. int16_t count; /* Number of counts held */
  53. };
  54. struct onewire_master_s
  55. {
  56. FAR struct onewire_dev_s *dev; /* 1-Wire lower half */
  57. struct onewire_sem_s devsem; /* Re-entrant semaphore */
  58. int nslaves; /* Number of 1-wire slaves */
  59. int maxslaves; /* Maximum number of 1-wire slaves */
  60. bool insearch; /* If we are in middle of 1-wire search */
  61. #ifdef CONFIG_PM
  62. struct pm_callback_s pm_cb; /* PM callbacks */
  63. #endif
  64. };
  65. struct onewire_slave_s
  66. {
  67. FAR struct onewire_master_s *master; /* Slave's bus master */
  68. uint64_t romcode; /* Unique ROM id in bus */
  69. };
  70. /****************************************************************************
  71. * Public Function Prototypes
  72. ****************************************************************************/
  73. /* Additional CRC helpers from 1wire_crc.c */
  74. bool onewire_valid_rom(uint64_t rom);
  75. /* Rest are from 1wire.c */
  76. int onewire_sem_wait(FAR struct onewire_master_s *master);
  77. void onewire_sem_post(FAR struct onewire_master_s *master);
  78. int onewire_addslave(FAR struct onewire_master_s *master,
  79. FAR struct onewire_slave_s *slave);
  80. int onewire_removeslave(FAR struct onewire_master_s *master,
  81. FAR struct onewire_slave_s *slave);
  82. /****************************************************************************
  83. * Name: onewire_reset_resume
  84. *
  85. * Description:
  86. *
  87. ****************************************************************************/
  88. int onewire_reset_resume(FAR struct onewire_master_s *master);
  89. /****************************************************************************
  90. * Name: onewire_reset_select
  91. *
  92. * Description:
  93. *
  94. ****************************************************************************/
  95. int onewire_reset_select(FAR struct onewire_master_s *master,
  96. uint64_t romcode);
  97. /****************************************************************************
  98. * Name: onewire_triplet
  99. *
  100. * Description:
  101. * Used by 1-wire search algorithm. Reads two bits and writes
  102. * one based on comparison of read bits.
  103. *
  104. * Input Parameters:
  105. * search_bit - Bit to write if both id_bit and cmp_id_bit match
  106. *
  107. * Output Parameters:
  108. * taken_bit - Bit indicating the direction where the search is
  109. * progressing.
  110. *
  111. * Return Value:
  112. * Number of valid bits or negative on error.
  113. *
  114. ****************************************************************************/
  115. int onewire_triplet(FAR struct onewire_master_s *master,
  116. uint8_t search_bit,
  117. FAR uint8_t *taken_bit);
  118. /****************************************************************************
  119. * Name: onewire_search
  120. *
  121. * Description:
  122. * Search all devices from a 1-wire network. This is the 1-wire search
  123. * algorithm from Maxim Application Note 187. Note! This is an atomic
  124. * operation. The callback 'cb_search' can't execute any function that will
  125. * lock this bus, because of the locked state as long the search is active.
  126. *
  127. * Input Parameters:
  128. * master - Pointer to the allocated 1-wire interface
  129. * family - Limit search to devices of matching family
  130. * alarmonly - Limit search to devices on alarm state
  131. * cb_search - Callback to call on each device found
  132. * arg - Argument passed to cb_search
  133. *
  134. * Return Value:
  135. * Number of slaves present and matching family.
  136. *
  137. ****************************************************************************/
  138. int onewire_search(FAR struct onewire_master_s *master,
  139. int family,
  140. bool alarmonly,
  141. CODE void (*cb_search)(int family,
  142. uint64_t romcode,
  143. FAR void *arg),
  144. FAR void *arg);
  145. #endif /* __DRIVERS_1WIRE_1WIRE_INTERNAL_H */