one_wire_host.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. uint8_t r;
  23. uint8_t retries = 125;
  24. // wait until the gpio is high
  25. furi_hal_ibutton_pin_high();
  26. do {
  27. if(--retries == 0) return 0;
  28. furi_hal_delay_us(2);
  29. } while(!furi_hal_ibutton_pin_get_level());
  30. // pre delay
  31. furi_hal_delay_us(OWH_RESET_DELAY_PRE);
  32. // drive low
  33. furi_hal_ibutton_pin_low();
  34. furi_hal_delay_us(OWH_RESET_DRIVE);
  35. // release
  36. furi_hal_ibutton_pin_high();
  37. furi_hal_delay_us(OWH_RESET_RELEASE);
  38. // read and post delay
  39. r = !furi_hal_ibutton_pin_get_level();
  40. furi_hal_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_ibutton_pin_low();
  47. furi_hal_delay_us(OWH_READ_DRIVE);
  48. // release
  49. furi_hal_ibutton_pin_high();
  50. furi_hal_delay_us(OWH_READ_RELEASE);
  51. // read and post delay
  52. result = furi_hal_ibutton_pin_get_level();
  53. furi_hal_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_ibutton_pin_low();
  74. furi_hal_delay_us(OWH_WRITE_1_DRIVE);
  75. // release
  76. furi_hal_ibutton_pin_high();
  77. furi_hal_delay_us(OWH_WRITE_1_RELEASE);
  78. } else {
  79. // drive low
  80. furi_hal_ibutton_pin_low();
  81. furi_hal_delay_us(OWH_WRITE_0_DRIVE);
  82. // release
  83. furi_hal_ibutton_pin_high();
  84. furi_hal_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_ibutton_start_drive();
  98. }
  99. void onewire_host_stop(OneWireHost* host) {
  100. furi_hal_ibutton_stop();
  101. }
  102. void onewire_host_reset_search(OneWireHost* host) {
  103. host->last_discrepancy = 0;
  104. host->last_device_flag = false;
  105. host->last_family_discrepancy = 0;
  106. for(int i = 7;; i--) {
  107. host->saved_rom[i] = 0;
  108. if(i == 0) break;
  109. }
  110. }
  111. void onewire_host_target_search(OneWireHost* host, uint8_t family_code) {
  112. host->saved_rom[0] = family_code;
  113. for(uint8_t i = 1; i < 8; i++) host->saved_rom[i] = 0;
  114. host->last_discrepancy = 64;
  115. host->last_family_discrepancy = 0;
  116. host->last_device_flag = false;
  117. }
  118. uint8_t onewire_host_search(OneWireHost* host, uint8_t* newAddr, OneWireHostSearchMode mode) {
  119. uint8_t id_bit_number;
  120. uint8_t last_zero, rom_byte_number, search_result;
  121. uint8_t id_bit, cmp_id_bit;
  122. unsigned char rom_byte_mask, search_direction;
  123. // initialize for search
  124. id_bit_number = 1;
  125. last_zero = 0;
  126. rom_byte_number = 0;
  127. rom_byte_mask = 1;
  128. search_result = 0;
  129. // if the last call was not the last one
  130. if(!host->last_device_flag) {
  131. // 1-Wire reset
  132. if(!onewire_host_reset(host)) {
  133. // reset the search
  134. host->last_discrepancy = 0;
  135. host->last_device_flag = false;
  136. host->last_family_discrepancy = 0;
  137. return false;
  138. }
  139. // issue the search command
  140. switch(mode) {
  141. case CONDITIONAL_SEARCH:
  142. onewire_host_write(host, 0xEC);
  143. break;
  144. case NORMAL_SEARCH:
  145. onewire_host_write(host, 0xF0);
  146. break;
  147. }
  148. // loop to do the search
  149. do {
  150. // read a bit and its complement
  151. id_bit = onewire_host_read_bit(host);
  152. cmp_id_bit = onewire_host_read_bit(host);
  153. // check for no devices on 1-wire
  154. if((id_bit == 1) && (cmp_id_bit == 1))
  155. break;
  156. else {
  157. // all devices coupled have 0 or 1
  158. if(id_bit != cmp_id_bit)
  159. search_direction = id_bit; // bit write value for search
  160. else {
  161. // if this discrepancy if before the Last Discrepancy
  162. // on a previous next then pick the same as last time
  163. if(id_bit_number < host->last_discrepancy)
  164. search_direction =
  165. ((host->saved_rom[rom_byte_number] & rom_byte_mask) > 0);
  166. else
  167. // if equal to last pick 1, if not then pick 0
  168. search_direction = (id_bit_number == host->last_discrepancy);
  169. // if 0 was picked then record its position in LastZero
  170. if(search_direction == 0) {
  171. last_zero = id_bit_number;
  172. // check for Last discrepancy in family
  173. if(last_zero < 9) host->last_family_discrepancy = last_zero;
  174. }
  175. }
  176. // set or clear the bit in the ROM byte rom_byte_number
  177. // with mask rom_byte_mask
  178. if(search_direction == 1)
  179. host->saved_rom[rom_byte_number] |= rom_byte_mask;
  180. else
  181. host->saved_rom[rom_byte_number] &= ~rom_byte_mask;
  182. // serial number search direction write bit
  183. onewire_host_write_bit(host, search_direction);
  184. // increment the byte counter id_bit_number
  185. // and shift the mask rom_byte_mask
  186. id_bit_number++;
  187. rom_byte_mask <<= 1;
  188. // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
  189. if(rom_byte_mask == 0) {
  190. rom_byte_number++;
  191. rom_byte_mask = 1;
  192. }
  193. }
  194. } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
  195. // if the search was successful then
  196. if(!(id_bit_number < 65)) {
  197. // search successful so set last_Discrepancy, last_device_flag, search_result
  198. host->last_discrepancy = last_zero;
  199. // check for last device
  200. if(host->last_discrepancy == 0) host->last_device_flag = true;
  201. search_result = true;
  202. }
  203. }
  204. // if no device found then reset counters so next 'search' will be like a first
  205. if(!search_result || !host->saved_rom[0]) {
  206. host->last_discrepancy = 0;
  207. host->last_device_flag = false;
  208. host->last_family_discrepancy = 0;
  209. search_result = false;
  210. } else {
  211. for(int i = 0; i < 8; i++) newAddr[i] = host->saved_rom[i];
  212. }
  213. return search_result;
  214. }