one_wire_host.c 7.5 KB

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