onewire.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. ChibiOS/RT - Copyright (C) 2014 Uladzimir Pylinsky aka barthess
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <string.h>
  14. #include "hal.h"
  15. #include "boarddef.h"
  16. /*
  17. ******************************************************************************
  18. * DEFINES
  19. ******************************************************************************
  20. */
  21. /*
  22. ******************************************************************************
  23. * EXTERNS
  24. ******************************************************************************
  25. */
  26. /*
  27. ******************************************************************************
  28. * PROTOTYPES
  29. ******************************************************************************
  30. */
  31. /*
  32. * Forward declarations
  33. */
  34. #if ONEWIRE_USE_STRONG_PULLUP
  35. static void strong_pullup_assert(void);
  36. static void strong_pullup_release(void);
  37. #endif
  38. /*
  39. ******************************************************************************
  40. * GLOBAL VARIABLES
  41. ******************************************************************************
  42. */
  43. static uint8_t testbuf[12];
  44. /* stores 3 temperature values in millicelsius */
  45. static int32_t temperature[3];
  46. static size_t devices_on_bus = 0;
  47. /*
  48. * Config for underlying PWM driver.
  49. * Note! It is NOT constant because 1-wire driver needs to change them
  50. * during functioning.
  51. */
  52. static PWMConfig pwm_cfg = {
  53. 0,
  54. 0,
  55. NULL,
  56. {
  57. {PWM_OUTPUT_DISABLED, NULL},
  58. {PWM_OUTPUT_DISABLED, NULL},
  59. {PWM_OUTPUT_DISABLED, NULL},
  60. {PWM_OUTPUT_DISABLED, NULL}
  61. },
  62. 0,
  63. #if STM32_PWM_USE_ADVANCED
  64. 0,
  65. #endif
  66. 0,
  67. 0
  68. };
  69. /*
  70. *
  71. */
  72. static const onewireConfig ow_cfg = {
  73. &PWMD3,
  74. &pwm_cfg,
  75. PWM_OUTPUT_ACTIVE_LOW,
  76. ONEWIRE_MASTER_CHANNEL,
  77. ONEWIRE_SAMPLE_CHANNEL,
  78. ONEWIRE_PORT,
  79. ONEWIRE_PIN,
  80. #if defined(STM32F1XX)
  81. ONEWIRE_PAD_MODE_IDLE,
  82. #endif
  83. ONEWIRE_PAD_MODE_ACTIVE,
  84. #if ONEWIRE_USE_STRONG_PULLUP
  85. strong_pullup_assert,
  86. strong_pullup_release
  87. #endif
  88. };
  89. /*
  90. ******************************************************************************
  91. ******************************************************************************
  92. * LOCAL FUNCTIONS
  93. ******************************************************************************
  94. ******************************************************************************
  95. */
  96. #if ONEWIRE_USE_STRONG_PULLUP
  97. /**
  98. *
  99. */
  100. static void strong_pullup_assert(void) {
  101. palSetPadMode(ONEWIRE_PORT, ONEWIRE_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  102. }
  103. /**
  104. *
  105. */
  106. static void strong_pullup_release(void) {
  107. palSetPadMode(ONEWIRE_PORT, ONEWIRE_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  108. }
  109. #endif /* ONEWIRE_USE_STRONG_PULLUP */
  110. /*
  111. ******************************************************************************
  112. * EXPORTED FUNCTIONS
  113. ******************************************************************************
  114. */
  115. /*
  116. *
  117. */
  118. void onewireTest(void) {
  119. int16_t tmp;
  120. uint8_t rombuf[24];
  121. size_t i = 0;
  122. bool presence;
  123. onewireObjectInit(&OWD1);
  124. onewireStart(&OWD1, &ow_cfg);
  125. #if ONEWIRE_SYNTH_SEARCH_TEST
  126. synthSearchRomTest(&OWD1);
  127. #endif
  128. for (i=0; i<3; i++)
  129. temperature[i] = -666;
  130. if (true == onewireReset(&OWD1)){
  131. memset(rombuf, 0x55, sizeof(rombuf));
  132. search_led_on();
  133. devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
  134. search_led_off();
  135. osalDbgCheck(devices_on_bus <= 3);
  136. osalDbgCheck(devices_on_bus > 0);
  137. if (1 == devices_on_bus){
  138. /* test read rom command */
  139. presence = onewireReset(&OWD1);
  140. osalDbgCheck(true == presence);
  141. testbuf[0] = ONEWIRE_CMD_READ_ROM;
  142. onewireWrite(&OWD1, testbuf, 1, 0);
  143. onewireRead(&OWD1, testbuf, 8);
  144. osalDbgCheck(testbuf[7] == onewireCRC(testbuf, 7));
  145. osalDbgCheck(0 == memcmp(rombuf, testbuf, 8));
  146. }
  147. /* start temperature measurement on all connected devices at once */
  148. presence = onewireReset(&OWD1);
  149. osalDbgCheck(true == presence);
  150. testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
  151. testbuf[1] = ONEWIRE_CMD_CONVERT_TEMP;
  152. #if ONEWIRE_USE_STRONG_PULLUP
  153. onewireWrite(&OWD1, testbuf, 2, TIME_MS2I(750));
  154. #else
  155. onewireWrite(&OWD1, testbuf, 2, 0);
  156. /* poll bus waiting ready signal from all connected devices */
  157. testbuf[0] = 0;
  158. while (testbuf[0] == 0){
  159. osalThreadSleepMilliseconds(50);
  160. onewireRead(&OWD1, testbuf, 1);
  161. }
  162. #endif
  163. for (i=0; i<devices_on_bus; i++) {
  164. /* read temperature device by device from their scratchpads */
  165. presence = onewireReset(&OWD1);
  166. osalDbgCheck(true == presence);
  167. testbuf[0] = ONEWIRE_CMD_MATCH_ROM;
  168. memcpy(&testbuf[1], &rombuf[i*8], 8);
  169. testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD;
  170. onewireWrite(&OWD1, testbuf, 10, 0);
  171. onewireRead(&OWD1, testbuf, 9);
  172. osalDbgCheck(testbuf[8] == onewireCRC(testbuf, 8));
  173. memcpy(&tmp, &testbuf, 2);
  174. temperature[i] = ((int32_t)tmp * 625) / 10;
  175. }
  176. }
  177. else {
  178. devices_on_bus = 0;
  179. }
  180. osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
  181. onewireStop(&OWD1);
  182. }
  183. uint8_t onewireGetDevicesNum(void) {
  184. return (uint8_t)devices_on_bus;
  185. }
  186. int32_t onewireGetTemperature(const size_t num) {
  187. if (num > devices_on_bus || num < devices_on_bus) {
  188. return -999;
  189. } else {
  190. return temperature[num-1];
  191. }
  192. }