owibus.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2012 Uwe Bonnes. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENce OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * \file app/owibus/owibus.c
  34. * \brief Example access to a DS18B20.
  35. *
  36. * The Onewire device needs to be connected to a port pin with pull-up
  37. * or the uart with TX low driving RX low and TX high not touching RX
  38. *
  39. * STM32 UART can do that by itself, with RX connected internal to TX
  40. * and TX driven Tristate
  41. *
  42. * \verbatim
  43. */
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <io.h>
  47. #include <cfg/arch.h>
  48. #include <stdint.h>
  49. #include <sys/timer.h>
  50. #include <dev/board.h>
  51. #include <dev/gpio.h>
  52. #if !defined(__GNUC__) || !defined(DEF_OWIBUS)
  53. int main(void)
  54. {
  55. uint32_t baud = 115200;
  56. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  57. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  58. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  59. #if !defined(__GNUC__)
  60. puts("This program requires a compiler that supports 64-bit integers.");
  61. #else
  62. puts("This program requires a configured OWIBUS.");
  63. #endif
  64. for (;;);
  65. return 0;
  66. }
  67. #else
  68. #include <dev/owibus.h>
  69. static char *banner = "\nNut/OS OWI Bus on " BOARDNAME
  70. " "__DATE__ " " __TIME__"\n";
  71. /*
  72. * UART sample.
  73. *
  74. */
  75. int main(void)
  76. {
  77. uint32_t baud = 115200;
  78. FILE *uart;
  79. int res, i = 0;
  80. uint64_t hid = 0;
  81. int32_t xcelsius;
  82. int run =0;
  83. uint8_t raw[2];
  84. uint8_t diff;
  85. uint8_t family;
  86. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  87. uart = fopen(DEV_CONSOLE.dev_name, "r+");
  88. _ioctl(_fileno(uart), UART_SETSPEED, &baud);
  89. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  90. fprintf(stdout, banner);
  91. res = OwiInit(&DEF_OWIBUS);
  92. if(res)
  93. {
  94. printf("Owi Error %d\n", res);
  95. while(1)
  96. NutSleep(10);
  97. }
  98. diff = OWI_SEARCH_FIRST;
  99. res = OwiRomSearch(&DEF_OWIBUS, &diff, &hid);
  100. if(res)
  101. {
  102. printf("OwiRomSearch failed\n");
  103. while(1)
  104. NutSleep(10);
  105. }
  106. fprintf(stdout, "Hardware ID of first device %08lx%08lx\n",
  107. (uint32_t)(hid>>32),
  108. (uint32_t)(hid &0xffffffff));
  109. family = hid & 0xff;
  110. if ((family != W1_THERM_DS18B20) && (family != W1_THERM_DS18S20))
  111. {
  112. fprintf(stdout, "One-wire device found, but family not handled"
  113. "in this example\n");
  114. while(1) NutSleep(10);
  115. }
  116. while(1)
  117. {
  118. res = OwiCommand(&DEF_OWIBUS, OWI_CONVERT_T, NULL);
  119. if (res)
  120. printf("OwiCommand convert_t error %d\n", res);
  121. NutSleep(750);
  122. while (!(res = OwiReadBlock(&DEF_OWIBUS, &diff, 1)) && !diff && i < 100)
  123. {
  124. NutSleep(10);
  125. i++;
  126. }
  127. if (i)
  128. {
  129. printf(
  130. "Conversion took additional %d poll cycles\n",
  131. i);
  132. i = 0;
  133. }
  134. res = OwiCommand(&DEF_OWIBUS, OWI_READ, NULL);
  135. if (res)
  136. printf("OwiCommand read error %d\n", res);
  137. res = OwiReadBlock(&DEF_OWIBUS, raw, 16);
  138. if (res)
  139. printf("OwiReadBlock error %d\n", res);
  140. if (family == W1_THERM_DS18B20 )
  141. xcelsius = ((raw[1]<<8) | raw[0]) * (int32_t)625;
  142. else
  143. xcelsius = ((raw[1]<< 8) | raw[0]) * (int32_t)5000;
  144. fprintf(stdout, "Run %3d: Temp %ld.%04ld\r",
  145. run++, xcelsius/10000, xcelsius%10000);
  146. }
  147. return 0;
  148. }
  149. #endif