one_wire_host.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <furi.h>
  2. #include "one_wire_host.h"
  3. #include "one_wire_host_timing.h"
  4. struct OneWireHost {
  5. const GpioPin* gpio_pin;
  6. unsigned char saved_rom[8]; /** < global search state */
  7. uint8_t last_discrepancy;
  8. uint8_t last_family_discrepancy;
  9. bool last_device_flag;
  10. };
  11. OneWireHost* onewire_host_alloc(const GpioPin* gpio_pin) {
  12. OneWireHost* host = malloc(sizeof(OneWireHost));
  13. host->gpio_pin = gpio_pin;
  14. onewire_host_reset_search(host);
  15. return host;
  16. }
  17. void onewire_host_free(OneWireHost* host) {
  18. onewire_host_stop(host);
  19. free(host);
  20. }
  21. bool onewire_host_reset(OneWireHost* host) {
  22. uint8_t r;
  23. uint8_t retries = 125;
  24. // wait until the gpio is high
  25. furi_hal_gpio_write(host->gpio_pin, true);
  26. do {
  27. if(--retries == 0) return 0;
  28. furi_delay_us(2);
  29. } while(!furi_hal_gpio_read(host->gpio_pin));
  30. // pre delay
  31. furi_delay_us(OWH_RESET_DELAY_PRE);
  32. // drive low
  33. furi_hal_gpio_write(host->gpio_pin, false);
  34. furi_delay_us(OWH_RESET_DRIVE);
  35. // release
  36. furi_hal_gpio_write(host->gpio_pin, true);
  37. furi_delay_us(OWH_RESET_RELEASE);
  38. // read and post delay
  39. r = !furi_hal_gpio_read(host->gpio_pin);
  40. furi_delay_us(OWH_RESET_DELAY_POST);
  41. return r;
  42. }
  43. bool onewire_host_read_bit(OneWireHost* host) {
  44. bool result;
  45. // drive low
  46. furi_hal_gpio_write(host->gpio_pin, false);
  47. furi_delay_us(OWH_READ_DRIVE);
  48. // release
  49. furi_hal_gpio_write(host->gpio_pin, true);
  50. furi_delay_us(OWH_READ_RELEASE);
  51. // read and post delay
  52. result = furi_hal_gpio_read(host->gpio_pin);
  53. furi_delay_us(OWH_READ_DELAY_POST);
  54. return result;
  55. }
  56. uint8_t onewire_host_read(OneWireHost* host) {
  57. uint8_t result = 0;
  58. for(uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
  59. if(onewire_host_read_bit(host)) {
  60. result |= bitMask;
  61. }
  62. }
  63. return result;
  64. }
  65. void onewire_host_read_bytes(OneWireHost* host, uint8_t* buffer, uint16_t count) {
  66. for(uint16_t i = 0; i < count; i++) {
  67. buffer[i] = onewire_host_read(host);
  68. }
  69. }
  70. void onewire_host_write_bit(OneWireHost* host, bool value) {
  71. if(value) {
  72. // drive low
  73. furi_hal_gpio_write(host->gpio_pin, false);
  74. furi_delay_us(OWH_WRITE_1_DRIVE);
  75. // release
  76. furi_hal_gpio_write(host->gpio_pin, true);
  77. furi_delay_us(OWH_WRITE_1_RELEASE);
  78. } else {
  79. // drive low
  80. furi_hal_gpio_write(host->gpio_pin, false);
  81. furi_delay_us(OWH_WRITE_0_DRIVE);
  82. // release
  83. furi_hal_gpio_write(host->gpio_pin, true);
  84. furi_delay_us(OWH_WRITE_0_RELEASE);
  85. }
  86. }
  87. void onewire_host_write(OneWireHost* host, uint8_t value) {
  88. uint8_t bitMask;
  89. for(bitMask = 0x01; bitMask; bitMask <<= 1) {
  90. onewire_host_write_bit(host, (bitMask & value) ? 1 : 0);
  91. }
  92. }
  93. void onewire_host_skip(OneWireHost* host) {
  94. onewire_host_write(host, 0xCC);
  95. }
  96. void onewire_host_start(OneWireHost* host) {
  97. furi_hal_gpio_write(host->gpio_pin, true);
  98. furi_hal_gpio_init(host->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  99. }
  100. void onewire_host_stop(OneWireHost* host) {
  101. furi_hal_gpio_write(host->gpio_pin, true);
  102. furi_hal_gpio_init(host->gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  103. }
  104. void onewire_host_reset_search(OneWireHost* host) {
  105. host->last_discrepancy = 0;
  106. host->last_device_flag = false;
  107. host->last_family_discrepancy = 0;
  108. for(int i = 7;; i--) {
  109. host->saved_rom[i] = 0;
  110. if(i == 0) break;
  111. }
  112. }
  113. void onewire_host_target_search(OneWireHost* host, uint8_t family_code) {
  114. host->saved_rom[0] = family_code;
  115. for(uint8_t i = 1; i < 8; i++) host->saved_rom[i] = 0;
  116. host->last_discrepancy = 64;
  117. host->last_family_discrepancy = 0;
  118. host->last_device_flag = false;
  119. }
  120. uint8_t onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode) {
  121. uint8_t id_bit_number;
  122. uint8_t last_zero, rom_byte_number, search_result;
  123. uint8_t id_bit, cmp_id_bit;
  124. unsigned char rom_byte_mask, search_direction;
  125. // initialize for search
  126. id_bit_number = 1;
  127. last_zero = 0;
  128. rom_byte_number = 0;
  129. rom_byte_mask = 1;
  130. search_result = 0;
  131. // if the last call was not the last one
  132. if(!host->last_device_flag) {
  133. // 1-Wire reset
  134. if(!onewire_host_reset(host)) {
  135. // reset the search
  136. host->last_discrepancy = 0;
  137. host->last_device_flag = false;
  138. host->last_family_discrepancy = 0;
  139. return false;
  140. }
  141. // issue the search command
  142. switch(mode) {
  143. case CONDITIONAL_SEARCH:
  144. onewire_host_write(host, 0xEC);
  145. break;
  146. case NORMAL_SEARCH:
  147. onewire_host_write(host, 0xF0);
  148. break;
  149. }
  150. // loop to do the search
  151. do {
  152. // read a bit and its complement
  153. id_bit = onewire_host_read_bit(host);
  154. cmp_id_bit = onewire_host_read_bit(host);
  155. // check for no devices on 1-wire
  156. if((id_bit == 1) && (cmp_id_bit == 1))
  157. break;
  158. else {
  159. // all devices coupled have 0 or 1
  160. if(id_bit != cmp_id_bit)
  161. search_direction = id_bit; // bit write value for search
  162. else {
  163. // if this discrepancy if before the Last Discrepancy
  164. // on a previous next then pick the same as last time
  165. if(id_bit_number < host->last_discrepancy)
  166. search_direction =
  167. ((host->saved_rom[rom_byte_number] & rom_byte_mask) > 0);
  168. else
  169. // if equal to last pick 1, if not then pick 0
  170. search_direction = (id_bit_number == host->last_discrepancy);
  171. // if 0 was picked then record its position in LastZero
  172. if(search_direction == 0) {
  173. last_zero = id_bit_number;
  174. // check for Last discrepancy in family
  175. if(last_zero < 9) host->last_family_discrepancy = last_zero;
  176. }
  177. }
  178. // set or clear the bit in the ROM byte rom_byte_number
  179. // with mask rom_byte_mask
  180. if(search_direction == 1)
  181. host->saved_rom[rom_byte_number] |= rom_byte_mask;
  182. else
  183. host->saved_rom[rom_byte_number] &= ~rom_byte_mask;
  184. // serial number search direction write bit
  185. onewire_host_write_bit(host, search_direction);
  186. // increment the byte counter id_bit_number
  187. // and shift the mask rom_byte_mask
  188. id_bit_number++;
  189. rom_byte_mask <<= 1;
  190. // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
  191. if(rom_byte_mask == 0) {
  192. rom_byte_number++;
  193. rom_byte_mask = 1;
  194. }
  195. }
  196. } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
  197. // if the search was successful then
  198. if(!(id_bit_number < 65)) {
  199. // search successful so set last_Discrepancy, last_device_flag, search_result
  200. host->last_discrepancy = last_zero;
  201. // check for last device
  202. if(host->last_discrepancy == 0) host->last_device_flag = true;
  203. search_result = true;
  204. }
  205. }
  206. // if no device found then reset counters so next 'search' will be like a first
  207. if(!search_result || !host->saved_rom[0]) {
  208. host->last_discrepancy = 0;
  209. host->last_device_flag = false;
  210. host->last_family_discrepancy = 0;
  211. search_result = false;
  212. } else {
  213. for(int i = 0; i < 8; i++) new_addr[i] = host->saved_rom[i];
  214. }
  215. return search_result;
  216. }