owibus_gpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2013 by Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file dev/owibus_gpio.c
  36. * \brief OWI bus for GPIO code include file.
  37. *
  38. * \verbatim
  39. * $Id: owibus_gpio.c 5356 2013-09-27 09:58:08Z u_bonnes $
  40. * \endverbatim
  41. */
  42. /*!
  43. * \addtogroup xgOwibusBb
  44. */
  45. /*@{*/
  46. /*!
  47. * \brief Perform One-Wire transaction.
  48. *
  49. * \param bus Specifies the One-Wire bus.
  50. * \param command Either OWI_CMD_RESET or OWI_CMD_RWBIT.
  51. * \param value The value to send.
  52. *
  53. * \return The value read on success, a negative value otherwise.
  54. */
  55. static int Gpio_OwiTransaction(NUTOWIBUS *bus, int_fast8_t command, int_fast8_t value)
  56. {
  57. int res = 0;
  58. int16_t delay1;
  59. int16_t delay2;
  60. int16_t delay3 =
  61. (owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_RELEASE] -
  62. owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_RW]) >> 2;
  63. if (value) {
  64. delay1 =
  65. (owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_SYNC_PULSE] -
  66. owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_SETUP]) >> 2;
  67. delay2 =
  68. (owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_RW] -
  69. owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_SYNC_PULSE]) >> 2;
  70. }
  71. else {
  72. delay1 =
  73. (owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_SYNC_PULSE_LOW] -
  74. owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_SETUP]) >> 2;
  75. delay2 =
  76. (owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_RELEASE] -
  77. owi_timervalues_250ns[bus->mode & OWI_OVERDRIVE][command][OWI_PHASE_SYNC_PULSE_LOW]) >> 2;
  78. }
  79. /* Be nice! Allow other thing to happen now before we block
  80. * cooperative multitasking for up to 480 us
  81. */
  82. NutSleep(0);
  83. OWI_LO();
  84. NutMicroDelay(delay1);
  85. OWI_HI();
  86. if (value) {
  87. NutMicroDelay(delay2);
  88. res = OWI_GET();
  89. NutMicroDelay(delay3);
  90. }
  91. else
  92. NutMicroDelay(delay2);
  93. return res;
  94. }
  95. /*!
  96. * \brief Reset the One-Wire bus and check if device(s) present.
  97. *
  98. * \param bus Specifies the One-Wire bus.
  99. *
  100. * \return OWI_SUCCESS on success, a negative value otherwise.
  101. */
  102. static int Gpio_OwiTouchReset(NUTOWIBUS *bus)
  103. {
  104. return Gpio_OwiTransaction(bus, OWI_CMD_RESET, 1);
  105. }
  106. /*!
  107. * \brief Exchange one bit on the One-Wire bus.
  108. *
  109. * \param bus Specifies the One-Wire bus.
  110. * \param bit Value for the bit to send.
  111. *
  112. * \return The bus state at the read slot on success, a negative value
  113. * otherwise.
  114. */
  115. static int Gpio_OwiRWBit(NUTOWIBUS *bus, uint_fast8_t bit)
  116. {
  117. return Gpio_OwiTransaction(bus, OWI_CMD_RWBIT, bit);
  118. }
  119. /*!
  120. * \brief Write a block of data bits to the One-Wire bus.
  121. *
  122. * \param bus Specifies the One-Wire bus.
  123. * \param data Data bits to send.
  124. * \param len Number of bits to send.
  125. *
  126. * \return OWI_SUCCESS on success, a negative value otherwise.
  127. */
  128. static int Gpio_OwiWriteBlock(NUTOWIBUS *bus, uint8_t *data, uint_fast8_t len)
  129. {
  130. int res;
  131. int i;
  132. for (i = 0; i < len; i++) {
  133. res = Gpio_OwiRWBit(bus, data[i >> 3] & (1 << (i & 0x7)));
  134. if (res < 0)
  135. return OWI_HW_ERROR;
  136. }
  137. return OWI_SUCCESS;
  138. }
  139. /*!
  140. * \brief Read a block of data bits from the One-Wire bus.
  141. *
  142. * \param bus Specifies the One-Wire bus.
  143. * \param data Data bits received.
  144. * \param len Number of bits to read.
  145. *
  146. * \return OWI_SUCCESS on success, a negative value otherwise.
  147. */
  148. static int Gpio_OwiReadBlock(NUTOWIBUS *bus, uint8_t *data, uint_fast8_t len)
  149. {
  150. int res;
  151. int i;
  152. memset(data, 0, (len >> 3) + 1);
  153. for (i = 0; i < len; i++) {
  154. res = Gpio_OwiRWBit(bus, 1);
  155. if (res < 0)
  156. return OWI_HW_ERROR;
  157. data[i >> 3] |= (res << (i & 0x7));
  158. }
  159. return OWI_SUCCESS;
  160. }
  161. /*!
  162. * \brief Read a block of data bits from the One-Wire bus.
  163. *
  164. * \param bus Specifies the One-Wire bus.
  165. * \param data Data bits received.
  166. * \param len Number of bits to read.
  167. *
  168. * \return OWI_SUCCESS on success, a negative value otherwise.
  169. */
  170. static int Gpio_Setup(NUTOWIBUS *bus)
  171. {
  172. (void) bus;
  173. #if !defined(OWI_HW)
  174. return OWI_INVALID_HW;
  175. #else
  176. OWI_INIT();
  177. return OWI_SUCCESS;
  178. #endif
  179. }
  180. /*@}*/