one_wire_slave.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include "one_wire_slave.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #define TH_TIMEOUT_MAX 15000 /* Maximum time before general timeout */
  5. typedef enum {
  6. OneWireSlaveErrorNone = 0,
  7. OneWireSlaveErrorResetInProgress,
  8. OneWireSlaveErrorPresenceConflict,
  9. OneWireSlaveErrorInvalidCommand,
  10. OneWireSlaveErrorTimeout,
  11. } OneWireSlaveError;
  12. typedef struct {
  13. uint16_t trstl_min; /* Minimum Reset Low time */
  14. uint16_t trstl_max; /* Maximum Reset Low time */
  15. uint16_t tpdh_typ; /* Typical Presence Detect High time */
  16. uint16_t tpdl_min; /* Minimum Presence Detect Low time */
  17. uint16_t tpdl_max; /* Maximum Presence Detect Low time */
  18. uint16_t tslot_min; /* Minimum Read/Write Slot time */
  19. uint16_t tslot_max; /* Maximum Read/Write Slot time */
  20. uint16_t tw1l_max; /* Maximum Master Write 1 time */
  21. uint16_t trl_tmsr_max; /* Maximum Master Read Low + Read Sample time */
  22. } OneWireSlaveTimings;
  23. struct OneWireSlave {
  24. const GpioPin* gpio_pin;
  25. const OneWireSlaveTimings* timings;
  26. OneWireSlaveError error;
  27. bool is_first_reset;
  28. bool is_short_reset;
  29. OneWireSlaveResetCallback reset_callback;
  30. OneWireSlaveCommandCallback command_callback;
  31. OneWireSlaveResultCallback result_callback;
  32. void* reset_callback_context;
  33. void* result_callback_context;
  34. void* command_callback_context;
  35. };
  36. static const OneWireSlaveTimings onewire_slave_timings_normal = {
  37. .trstl_min = 270,
  38. .trstl_max = 1200,
  39. .tpdh_typ = 20,
  40. .tpdl_min = 100,
  41. .tpdl_max = 480,
  42. .tslot_min = 60,
  43. .tslot_max = 135,
  44. .tw1l_max = 20,
  45. .trl_tmsr_max = 30,
  46. };
  47. static const OneWireSlaveTimings onewire_slave_timings_overdrive = {
  48. .trstl_min = 48,
  49. .trstl_max = 80,
  50. .tpdh_typ = 0,
  51. .tpdl_min = 8,
  52. .tpdl_max = 24,
  53. .tslot_min = 6,
  54. .tslot_max = 16,
  55. .tw1l_max = 2,
  56. .trl_tmsr_max = 3,
  57. };
  58. /*********************** PRIVATE ***********************/
  59. static bool
  60. onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time_us, const bool pin_value) {
  61. const uint32_t time_start = DWT->CYCCNT;
  62. const uint32_t time_ticks = time_us * furi_hal_cortex_instructions_per_microsecond();
  63. uint32_t time_elapsed;
  64. do { //-V1044
  65. time_elapsed = DWT->CYCCNT - time_start;
  66. if(furi_hal_gpio_read(bus->gpio_pin) != pin_value) {
  67. return time_ticks >= time_elapsed;
  68. }
  69. } while(time_elapsed < time_ticks);
  70. return false;
  71. }
  72. static inline bool onewire_slave_show_presence(OneWireSlave* bus) {
  73. const OneWireSlaveTimings* timings = bus->timings;
  74. // wait until the bus is high (might return immediately)
  75. onewire_slave_wait_while_gpio_is(bus, timings->trstl_max, false);
  76. // wait while master delay presence check
  77. furi_delay_us(timings->tpdh_typ);
  78. // show presence
  79. furi_hal_gpio_write(bus->gpio_pin, false);
  80. furi_delay_us(timings->tpdl_min);
  81. furi_hal_gpio_write(bus->gpio_pin, true);
  82. // somebody also can show presence
  83. const uint32_t wait_low_time = timings->tpdl_max - timings->tpdl_min;
  84. // so we will wait
  85. if(!onewire_slave_wait_while_gpio_is(bus, wait_low_time, false)) {
  86. bus->error = OneWireSlaveErrorPresenceConflict;
  87. return false;
  88. }
  89. return true;
  90. }
  91. static inline bool onewire_slave_receive_and_process_command(OneWireSlave* bus) {
  92. /* Reset condition detected, send a presence pulse and reset protocol state */
  93. if(bus->error == OneWireSlaveErrorResetInProgress) {
  94. if(!bus->is_first_reset) {
  95. /* Guess the reset type */
  96. bus->is_short_reset = onewire_slave_wait_while_gpio_is(
  97. bus,
  98. onewire_slave_timings_overdrive.trstl_max -
  99. onewire_slave_timings_overdrive.tslot_max,
  100. false);
  101. } else {
  102. bus->is_first_reset = false;
  103. }
  104. furi_assert(bus->reset_callback);
  105. if(bus->reset_callback(bus->is_short_reset, bus->reset_callback_context)) {
  106. if(onewire_slave_show_presence(bus)) {
  107. bus->error = OneWireSlaveErrorNone;
  108. return true;
  109. }
  110. }
  111. } else if(bus->error == OneWireSlaveErrorNone) {
  112. uint8_t command;
  113. if(onewire_slave_receive(bus, &command, sizeof(command))) {
  114. furi_assert(bus->command_callback);
  115. if(bus->command_callback(command, bus->command_callback_context)) {
  116. return true;
  117. }
  118. }
  119. return (bus->error == OneWireSlaveErrorResetInProgress);
  120. }
  121. return false;
  122. }
  123. static inline bool onewire_slave_bus_start(OneWireSlave* bus) {
  124. FURI_CRITICAL_ENTER();
  125. furi_hal_gpio_init(bus->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  126. while(onewire_slave_receive_and_process_command(bus))
  127. ;
  128. const bool result = (bus->error == OneWireSlaveErrorNone);
  129. furi_hal_gpio_init(bus->gpio_pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  130. FURI_CRITICAL_EXIT();
  131. return result;
  132. }
  133. static void onewire_slave_exti_callback(void* context) {
  134. OneWireSlave* bus = context;
  135. const volatile bool input_state = furi_hal_gpio_read(bus->gpio_pin);
  136. static uint32_t pulse_start = 0;
  137. if(input_state) {
  138. const uint32_t pulse_length =
  139. (DWT->CYCCNT - pulse_start) / furi_hal_cortex_instructions_per_microsecond();
  140. if((pulse_length >= onewire_slave_timings_overdrive.trstl_min) &&
  141. (pulse_length <= onewire_slave_timings_normal.trstl_max)) {
  142. /* Start in reset state in order to send a presence pulse immediately */
  143. bus->error = OneWireSlaveErrorResetInProgress;
  144. /* Determine reset type (chooses speed mode if supported by the emulated device) */
  145. bus->is_short_reset = pulse_length <= onewire_slave_timings_overdrive.trstl_max;
  146. /* Initial reset allows going directly into overdrive mode */
  147. bus->is_first_reset = true;
  148. const bool result = onewire_slave_bus_start(bus);
  149. if(result && bus->result_callback != NULL) {
  150. bus->result_callback(bus->result_callback_context);
  151. }
  152. }
  153. } else {
  154. pulse_start = DWT->CYCCNT;
  155. }
  156. };
  157. /*********************** PUBLIC ***********************/
  158. OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin) {
  159. OneWireSlave* bus = malloc(sizeof(OneWireSlave));
  160. bus->gpio_pin = gpio_pin;
  161. bus->timings = &onewire_slave_timings_normal;
  162. bus->error = OneWireSlaveErrorNone;
  163. return bus;
  164. }
  165. void onewire_slave_free(OneWireSlave* bus) {
  166. onewire_slave_stop(bus);
  167. free(bus);
  168. }
  169. void onewire_slave_start(OneWireSlave* bus) {
  170. furi_hal_gpio_add_int_callback(bus->gpio_pin, onewire_slave_exti_callback, bus);
  171. furi_hal_gpio_write(bus->gpio_pin, true);
  172. furi_hal_gpio_init(bus->gpio_pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  173. }
  174. void onewire_slave_stop(OneWireSlave* bus) {
  175. furi_hal_gpio_write(bus->gpio_pin, true);
  176. furi_hal_gpio_init(bus->gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  177. furi_hal_gpio_remove_int_callback(bus->gpio_pin);
  178. }
  179. void onewire_slave_set_reset_callback(
  180. OneWireSlave* bus,
  181. OneWireSlaveResetCallback callback,
  182. void* context) {
  183. bus->reset_callback = callback;
  184. bus->reset_callback_context = context;
  185. }
  186. void onewire_slave_set_command_callback(
  187. OneWireSlave* bus,
  188. OneWireSlaveCommandCallback callback,
  189. void* context) {
  190. bus->command_callback = callback;
  191. bus->command_callback_context = context;
  192. }
  193. void onewire_slave_set_result_callback(
  194. OneWireSlave* bus,
  195. OneWireSlaveResultCallback result_cb,
  196. void* context) {
  197. bus->result_callback = result_cb;
  198. bus->result_callback_context = context;
  199. }
  200. bool onewire_slave_receive_bit(OneWireSlave* bus) {
  201. const OneWireSlaveTimings* timings = bus->timings;
  202. // wait while bus is low
  203. if(!onewire_slave_wait_while_gpio_is(bus, timings->tslot_max, false)) {
  204. bus->error = OneWireSlaveErrorResetInProgress;
  205. return false;
  206. }
  207. // wait while bus is high
  208. if(!onewire_slave_wait_while_gpio_is(bus, TH_TIMEOUT_MAX, true)) {
  209. bus->error = OneWireSlaveErrorTimeout;
  210. return false;
  211. }
  212. // wait a time of zero
  213. return onewire_slave_wait_while_gpio_is(bus, timings->tw1l_max, false);
  214. }
  215. bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
  216. const OneWireSlaveTimings* timings = bus->timings;
  217. // wait while bus is low
  218. if(!onewire_slave_wait_while_gpio_is(bus, timings->tslot_max, false)) {
  219. bus->error = OneWireSlaveErrorResetInProgress;
  220. return false;
  221. }
  222. // wait while bus is high
  223. if(!onewire_slave_wait_while_gpio_is(bus, TH_TIMEOUT_MAX, true)) {
  224. bus->error = OneWireSlaveErrorTimeout;
  225. return false;
  226. }
  227. // choose write time
  228. uint32_t time;
  229. if(!value) {
  230. furi_hal_gpio_write(bus->gpio_pin, false);
  231. time = timings->trl_tmsr_max;
  232. } else {
  233. time = timings->tslot_min;
  234. }
  235. // hold line for ZERO or ONE time
  236. furi_delay_us(time);
  237. furi_hal_gpio_write(bus->gpio_pin, true);
  238. return true;
  239. }
  240. bool onewire_slave_send(OneWireSlave* bus, const uint8_t* data, size_t data_size) {
  241. furi_hal_gpio_write(bus->gpio_pin, true);
  242. size_t bytes_sent = 0;
  243. // bytes loop
  244. for(; bytes_sent < data_size; ++bytes_sent) {
  245. const uint8_t data_byte = data[bytes_sent];
  246. // bit loop
  247. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  248. if(!onewire_slave_send_bit(bus, bit_mask & data_byte)) {
  249. return false;
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, size_t data_size) {
  256. furi_hal_gpio_write(bus->gpio_pin, true);
  257. size_t bytes_received = 0;
  258. for(; bytes_received < data_size; ++bytes_received) {
  259. uint8_t value = 0;
  260. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  261. if(onewire_slave_receive_bit(bus)) {
  262. value |= bit_mask;
  263. }
  264. if(bus->error != OneWireSlaveErrorNone) {
  265. return false;
  266. }
  267. }
  268. data[bytes_received] = value;
  269. }
  270. return true;
  271. }
  272. void onewire_slave_set_overdrive(OneWireSlave* bus, bool set) {
  273. const OneWireSlaveTimings* new_timings = set ? &onewire_slave_timings_overdrive :
  274. &onewire_slave_timings_normal;
  275. if(bus->timings != new_timings) {
  276. /* Prevent erroneous reset by waiting for the previous time slot to finish */
  277. onewire_slave_wait_while_gpio_is(bus, bus->timings->tslot_max, false);
  278. bus->timings = new_timings;
  279. }
  280. }