one_wire_slave.h 1.9 KB

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