one_wire_slave.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @file one_wire_slave.h
  3. *
  4. * 1-Wire slave library.
  5. */
  6. #pragma once
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <furi_hal_gpio.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef struct OneWireDevice OneWireDevice;
  15. typedef struct OneWireSlave OneWireSlave;
  16. typedef bool (*OneWireSlaveResetCallback)(bool is_short, void* context);
  17. typedef bool (*OneWireSlaveCommandCallback)(uint8_t command, void* context);
  18. typedef void (*OneWireSlaveResultCallback)(void* context);
  19. /**
  20. * Allocate OneWireSlave instance
  21. * @param [in] gpio_pin connection pin
  22. * @return pointer to OneWireSlave instance
  23. */
  24. OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin);
  25. /**
  26. * Destroy OneWireSlave instance, free resources
  27. * @param [in] bus pointer to OneWireSlave instance
  28. */
  29. void onewire_slave_free(OneWireSlave* bus);
  30. /**
  31. * Start working with the bus
  32. * @param [in] bus pointer to OneWireSlave instance
  33. */
  34. void onewire_slave_start(OneWireSlave* bus);
  35. /**
  36. * Stop working with the bus
  37. * @param [in] bus pointer to OneWireSlave instance
  38. */
  39. void onewire_slave_stop(OneWireSlave* bus);
  40. /**
  41. * Receive one bit
  42. * @param [in] bus pointer to OneWireSlave instance
  43. * @return received bit value
  44. */
  45. bool onewire_slave_receive_bit(OneWireSlave* bus);
  46. /**
  47. * Send one bit
  48. * @param [in] bus pointer to OneWireSlave instance
  49. * @param [in] value bit value to send
  50. * @return true on success, false on failure
  51. */
  52. bool onewire_slave_send_bit(OneWireSlave* bus, bool value);
  53. /**
  54. * Send one or more bytes of data
  55. * @param [in] bus pointer to OneWireSlave instance
  56. * @param [in] data pointer to the data to send
  57. * @param [in] data_size size of the data to send
  58. * @return true on success, false on failure
  59. */
  60. bool onewire_slave_send(OneWireSlave* bus, const uint8_t* data, size_t data_size);
  61. /**
  62. * Receive one or more bytes of data
  63. * @param [in] bus pointer to OneWireSlave instance
  64. * @param [out] data pointer to the receive buffer
  65. * @param [in] data_size number of bytes to receive
  66. * @return true on success, false on failure
  67. */
  68. bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, size_t data_size);
  69. /**
  70. * Enable overdrive mode
  71. * @param [in] bus pointer to OneWireSlave instance
  72. * @param [in] set true to turn overdrive on, false to turn it off
  73. */
  74. void onewire_slave_set_overdrive(OneWireSlave* bus, bool set);
  75. /**
  76. * Set a callback function to be called on each reset.
  77. * The return value of the callback determines whether the emulated device
  78. * supports the short reset (passed as the is_short parameter).
  79. * In most applications, it should also call onewire_slave_set_overdrive()
  80. * to set the appropriate speed mode.
  81. *
  82. * @param [in] bus pointer to OneWireSlave instance
  83. * @param [in] callback pointer to a callback function
  84. * @param [in] context additional parameter to be passed to the callback
  85. */
  86. void onewire_slave_set_reset_callback(
  87. OneWireSlave* bus,
  88. OneWireSlaveResetCallback callback,
  89. void* context);
  90. /**
  91. * Set a callback function to be called on each command.
  92. * The return value of the callback determines whether further operation
  93. * is possible. As a rule of thumb, return true unless a critical error happened.
  94. *
  95. * @param [in] bus pointer to OneWireSlave instance
  96. * @param [in] callback pointer to a callback function
  97. * @param [in] context additional parameter to be passed to the callback
  98. */
  99. void onewire_slave_set_command_callback(
  100. OneWireSlave* bus,
  101. OneWireSlaveCommandCallback callback,
  102. void* context);
  103. /**
  104. * Set a callback to report emulation success
  105. * @param [in] bus pointer to OneWireSlave instance
  106. * @param [in] result_cb pointer to a callback function
  107. * @param [in] context additional parameter to be passed to the callback
  108. */
  109. void onewire_slave_set_result_callback(
  110. OneWireSlave* bus,
  111. OneWireSlaveResultCallback result_cb,
  112. void* context);
  113. #ifdef __cplusplus
  114. }
  115. #endif