one_wire_slave.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "one_wire_timings.h"
  5. class OneWireDevice;
  6. typedef void (*OneWireSlaveResultCallback)(bool success, void* ctx);
  7. class OneWireSlave {
  8. private:
  9. enum class OneWireSlaveError : uint8_t {
  10. NO_ERROR = 0,
  11. READ_TIMESLOT_TIMEOUT,
  12. WRITE_TIMESLOT_TIMEOUT,
  13. WAIT_RESET_TIMEOUT,
  14. VERY_LONG_RESET,
  15. VERY_SHORT_RESET,
  16. PRESENCE_LOW_ON_LINE,
  17. READ_TIMESLOT_TIMEOUT_LOW,
  18. AWAIT_TIMESLOT_TIMEOUT_HIGH,
  19. PRESENCE_HIGH_ON_LINE,
  20. INCORRECT_ONEWIRE_CMD,
  21. INCORRECT_SLAVE_USAGE,
  22. TRIED_INCORRECT_WRITE,
  23. FIRST_TIMESLOT_TIMEOUT,
  24. FIRST_BIT_OF_BYTE_TIMEOUT,
  25. RESET_IN_PROGRESS
  26. };
  27. const GpioPin* one_wire_pin_record;
  28. // exti callback and its pointer
  29. void exti_callback(void* _ctx);
  30. void (*exti_cb)(void* _ctx);
  31. uint32_t __instructions_per_us;
  32. OneWireSlaveError error;
  33. OneWireDevice* device = nullptr;
  34. bool bus_start(void);
  35. void pin_set_float(void);
  36. void pin_set_low(void);
  37. void pin_init_interrupt_in_isr_ctx(void);
  38. void pin_init_opendrain_in_isr_ctx(void);
  39. OneWiteTimeType wait_while_gpio_is(OneWiteTimeType time, const bool pin_value);
  40. bool show_presence(void);
  41. bool receive_and_process_cmd(void);
  42. bool receive_bit(void);
  43. bool send_bit(bool value);
  44. void cmd_search_rom(void);
  45. OneWireSlaveResultCallback result_cb = nullptr;
  46. void* result_cb_ctx = nullptr;
  47. public:
  48. void start(void);
  49. void stop(void);
  50. bool send(const uint8_t* address, const uint8_t data_length);
  51. bool receive(uint8_t* data, const uint8_t data_length = 1);
  52. OneWireSlave(const GpioPin* pin);
  53. ~OneWireSlave();
  54. void attach(OneWireDevice* device);
  55. void deattach(void);
  56. void set_result_callback(OneWireSlaveResultCallback result_cb, void* ctx);
  57. };