one_wire_host.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_write_bytes(OneWireHost* host, const uint8_t* buffer, uint16_t count) {
  94. for(uint16_t i = 0; i < count; ++i) {
  95. onewire_host_write(host, buffer[i]);
  96. }
  97. }
  98. void onewire_host_skip(OneWireHost* host) {
  99. onewire_host_write(host, 0xCC);
  100. }
  101. void onewire_host_start(OneWireHost* host) {
  102. furi_hal_gpio_write(host->gpio_pin, true);
  103. furi_hal_gpio_init(host->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  104. }
  105. void onewire_host_stop(OneWireHost* host) {
  106. furi_hal_gpio_write(host->gpio_pin, true);
  107. furi_hal_gpio_init(host->gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  108. }
  109. void onewire_host_reset_search(OneWireHost* host) {
  110. host->last_discrepancy = 0;
  111. host->last_device_flag = false;
  112. host->last_family_discrepancy = 0;
  113. for(int i = 7;; i--) {
  114. host->saved_rom[i] = 0;
  115. if(i == 0) break;
  116. }
  117. }
  118. void onewire_host_target_search(OneWireHost* host, uint8_t family_code) {
  119. host->saved_rom[0] = family_code;
  120. for(uint8_t i = 1; i < 8; i++) host->saved_rom[i] = 0;
  121. host->last_discrepancy = 64;
  122. host->last_family_discrepancy = 0;
  123. host->last_device_flag = false;
  124. }
  125. uint8_t onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode) {
  126. uint8_t id_bit_number;
  127. uint8_t last_zero, rom_byte_number, search_result;
  128. uint8_t id_bit, cmp_id_bit;
  129. unsigned char rom_byte_mask, search_direction;
  130. // initialize for search
  131. id_bit_number = 1;
  132. last_zero = 0;
  133. rom_byte_number = 0;
  134. rom_byte_mask = 1;
  135. search_result = 0;
  136. // if the last call was not the last one
  137. if(!host->last_device_flag) {
  138. // 1-Wire reset
  139. if(!onewire_host_reset(host)) {
  140. // reset the search
  141. host->last_discrepancy = 0;
  142. host->last_device_flag = false;
  143. host->last_family_discrepancy = 0;
  144. return false;
  145. }
  146. // issue the search command
  147. switch(mode) {
  148. case OneWireHostSearchModeConditional:
  149. onewire_host_write(host, 0xEC);
  150. break;
  151. case OneWireHostSearchModeNormal:
  152. onewire_host_write(host, 0xF0);
  153. break;
  154. }
  155. // loop to do the search
  156. do {
  157. // read a bit and its complement
  158. id_bit = onewire_host_read_bit(host);
  159. cmp_id_bit = onewire_host_read_bit(host);
  160. // check for no devices on 1-wire
  161. if((id_bit == 1) && (cmp_id_bit == 1))
  162. break;
  163. else {
  164. // all devices coupled have 0 or 1
  165. if(id_bit != cmp_id_bit)
  166. search_direction = id_bit; // bit write value for search
  167. else {
  168. // if this discrepancy if before the Last Discrepancy
  169. // on a previous next then pick the same as last time
  170. if(id_bit_number < host->last_discrepancy)
  171. search_direction =
  172. ((host->saved_rom[rom_byte_number] & rom_byte_mask) > 0);
  173. else
  174. // if equal to last pick 1, if not then pick 0
  175. search_direction = (id_bit_number == host->last_discrepancy);
  176. // if 0 was picked then record its position in LastZero
  177. if(search_direction == 0) {
  178. last_zero = id_bit_number;
  179. // check for Last discrepancy in family
  180. if(last_zero < 9) host->last_family_discrepancy = last_zero;
  181. }
  182. }
  183. // set or clear the bit in the ROM byte rom_byte_number
  184. // with mask rom_byte_mask
  185. if(search_direction == 1)
  186. host->saved_rom[rom_byte_number] |= rom_byte_mask;
  187. else
  188. host->saved_rom[rom_byte_number] &= ~rom_byte_mask;
  189. // serial number search direction write bit
  190. onewire_host_write_bit(host, search_direction);
  191. // increment the byte counter id_bit_number
  192. // and shift the mask rom_byte_mask
  193. id_bit_number++;
  194. rom_byte_mask <<= 1;
  195. // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
  196. if(rom_byte_mask == 0) {
  197. rom_byte_number++;
  198. rom_byte_mask = 1;
  199. }
  200. }
  201. } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
  202. // if the search was successful then
  203. if(!(id_bit_number < 65)) {
  204. // search successful so set last_Discrepancy, last_device_flag, search_result
  205. host->last_discrepancy = last_zero;
  206. // check for last device
  207. if(host->last_discrepancy == 0) host->last_device_flag = true;
  208. search_result = true;
  209. }
  210. }
  211. // if no device found then reset counters so next 'search' will be like a first
  212. if(!search_result || !host->saved_rom[0]) {
  213. host->last_discrepancy = 0;
  214. host->last_device_flag = false;
  215. host->last_family_discrepancy = 0;
  216. search_result = false;
  217. } else {
  218. for(int i = 0; i < 8; i++) new_addr[i] = host->saved_rom[i];
  219. }
  220. return search_result;
  221. }