owibus_uart.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2012/2014 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_uart.c
  36. * \brief Common implementation of the One-Wire via Uart.
  37. *
  38. * \verbatim
  39. * $Id: owibus_uart.c 5528 2014-01-07 18:17:58Z u_bonnes $
  40. * \endverbatim
  41. */
  42. #include <cfg/arch.h>
  43. #include <stdint.h>
  44. #include <fcntl.h>
  45. #include <dev/uart.h>
  46. #include <dev/owibus.h>
  47. #include <dev/owibus_uart.h>
  48. /*!
  49. * \addtogroup xgOwibusUart
  50. */
  51. /*@{*/
  52. int Uart_OwiInit(NUTOWIINFO_UART *owcb, NUTDEVICE *uart, int mode)
  53. {
  54. int uart_fd;
  55. uint32_t timeout = 5;
  56. uint32_t stopbits = 2;
  57. int res;
  58. uint8_t data[1];
  59. uart_fd = _open(uart->dev_name, _O_RDWR | _O_BINARY);
  60. if (uart_fd == -1) {
  61. return OWI_INVALID_HW;
  62. }
  63. res = _ioctl(uart_fd, UART_SETOWIMODE, &mode);
  64. if (res) {
  65. _close(uart_fd);
  66. return OWI_INVALID_HW;
  67. }
  68. _ioctl(uart_fd, UART_SETREADTIMEOUT, &timeout);
  69. _ioctl(uart_fd, UART_SETSTOPBITS, &stopbits);
  70. /* Empty RX buffer, as pin setup might have caused several
  71. transitions on RX */
  72. while(_read(uart_fd, data, 1) == 1);
  73. owcb->uart_fd = uart_fd;
  74. return OWI_SUCCESS;
  75. }
  76. /*!
  77. * \brief Reset the One-Wire bus and check if device(s) present.
  78. *
  79. * \param bus Specifies the One-Wire bus.
  80. *
  81. * \return OWI_SUCCESS on success, a negative value otherwise.
  82. */
  83. int Uart_OwiTouchReset(NUTOWIBUS *bus)
  84. {
  85. NUTOWIINFO_UART *owcb = (NUTOWIINFO_UART *) (bus->owibus_info);
  86. uint8_t send_data[1] = { OWI_UART_WRITE_RST };
  87. uint8_t rec_data[10] = { 0 };
  88. uint32_t baud_presence = OWI_UART_BAUD_RESET;
  89. uint32_t baud_owi_rwbit = OWI_UART_BAUD_RWBIT;
  90. int res;
  91. int uart_fd = owcb->uart_fd;
  92. _ioctl(uart_fd, UART_SETSPEED, &baud_presence);
  93. _write(uart_fd, send_data, 1);
  94. res = _read(uart_fd, rec_data, 1);
  95. if (res == -1) {
  96. return OWI_HW_ERROR;
  97. }
  98. _ioctl(uart_fd, UART_SETSPEED, &baud_owi_rwbit);
  99. if ((rec_data[0] != 0xf0) && !(rec_data[0] & 0x10) && (rec_data[0] != 0)) {
  100. return OWI_SUCCESS;
  101. }
  102. return OWI_PRESENCE_ERR;
  103. }
  104. /*!
  105. * \brief Exchange one bit on the One-Wire bus.
  106. *
  107. * \param bus Specifies the One-Wire bus.
  108. * \param bit Value for the bit to send.
  109. *
  110. * \return The bus state at the read slot on success, a negative value
  111. * otherwise.
  112. */
  113. int Uart_OwiRWBit(NUTOWIBUS *bus, uint_fast8_t bit)
  114. {
  115. NUTOWIINFO_UART *owcb = (NUTOWIINFO_UART *) (bus->owibus_info);
  116. uint8_t send_data[1] = { (bit) ? OWI_UART_WRITE_ONE : OWI_UART_WRITE_ZERO };
  117. uint8_t rec_data[1] = { 0 };
  118. _write(owcb->uart_fd, send_data, 1);
  119. if (_read(owcb->uart_fd, rec_data, 1) == -1) {
  120. return OWI_HW_ERROR;
  121. }
  122. if (rec_data[0] & OWI_UART_READ_ONE) {
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. /*!
  128. * \brief Write a block of data bits to the One-Wire bus.
  129. *
  130. * \param bus Specifies the One-Wire bus.
  131. * \param data Data bits to send.
  132. * \param len Number of bits to send.
  133. *
  134. * \return OWI_SUCCESS on success, a negative value otherwise.
  135. */
  136. int Uart_OwiWriteBlock(NUTOWIBUS *bus, uint8_t *data, uint_fast8_t len)
  137. {
  138. int res;
  139. int i;
  140. for (i = 0; i < len; i++) {
  141. res = Uart_OwiRWBit(bus, data[i >> 3] & (1 << (i & 0x7)));
  142. if (res < 0) {
  143. return OWI_HW_ERROR;
  144. }
  145. }
  146. return OWI_SUCCESS;
  147. }
  148. /*!
  149. * \brief Read a block of data bits from the One-Wire bus.
  150. *
  151. * \param bus Specifies the One-Wire bus.
  152. * \param data Data bits received.
  153. * \param len Number of bits to read.
  154. *
  155. * \return OWI_SUCCESS on success, a negative value otherwise.
  156. */
  157. int Uart_OwiReadBlock(NUTOWIBUS *bus, uint8_t *data, uint_fast8_t len)
  158. {
  159. int res;
  160. int i;
  161. memset(data, 0, ((len +7) >> 3));
  162. for (i = 0; i < len; i++) {
  163. res = Uart_OwiRWBit(bus, 1);
  164. if (res < 0) {
  165. return OWI_HW_ERROR;
  166. }
  167. data[i >> 3] |= (res << (i & 0x7));
  168. }
  169. return OWI_SUCCESS;
  170. }
  171. /*@}*/