one_wire_slave_gpio.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "flipper.h"
  3. #include "flipper_v2.h"
  4. #include "one_wire_timings.h"
  5. // TODO fix GPL compability
  6. // currently we use rework of OneWireHub
  7. #define ONE_WIRE_MAX_DEVICES 1
  8. #define ONE_WIRE_TREE_SIZE ((2 * ONE_WIRE_MAX_DEVICES) - 1)
  9. #define OWET OneWireEmulateTiming
  10. class OneWireDevice;
  11. enum class OneWireGpioSlaveError : uint8_t {
  12. NO_ERROR = 0,
  13. READ_TIMESLOT_TIMEOUT = 1,
  14. WRITE_TIMESLOT_TIMEOUT = 2,
  15. WAIT_RESET_TIMEOUT = 3,
  16. VERY_LONG_RESET = 4,
  17. VERY_SHORT_RESET = 5,
  18. PRESENCE_LOW_ON_LINE = 6,
  19. READ_TIMESLOT_TIMEOUT_LOW = 7,
  20. AWAIT_TIMESLOT_TIMEOUT_HIGH = 8,
  21. PRESENCE_HIGH_ON_LINE = 9,
  22. INCORRECT_ONEWIRE_CMD = 10,
  23. INCORRECT_SLAVE_USAGE = 11,
  24. TRIED_INCORRECT_WRITE = 12,
  25. FIRST_TIMESLOT_TIMEOUT = 13,
  26. FIRST_BIT_OF_BYTE_TIMEOUT = 14,
  27. RESET_IN_PROGRESS = 15
  28. };
  29. class OneWireGpioSlave {
  30. private:
  31. const GpioPin* gpio;
  32. bool overdrive_mode = false;
  33. uint8_t cmd;
  34. OneWireGpioSlaveError error;
  35. uint8_t error_place;
  36. uint8_t devices_count;
  37. OneWireDevice* devices[ONE_WIRE_MAX_DEVICES];
  38. OneWireDevice* device_selected;
  39. struct IDTree {
  40. uint8_t device_selected; // for which slave is this jump-command relevant
  41. uint8_t id_position; // where does the algorithm has to look for a junction
  42. uint8_t got_zero; // if 0 switch to which tree branch
  43. uint8_t got_one; // if 1 switch to which tree branch
  44. } id_tree[ONE_WIRE_TREE_SIZE];
  45. public:
  46. OneWireGpioSlave(const GpioPin* one_wire_gpio);
  47. ~OneWireGpioSlave();
  48. void start(void);
  49. void stop(void);
  50. bool emulate();
  51. bool check_reset(void);
  52. bool show_presence(void);
  53. bool receive_and_process_cmd(void);
  54. bool receive(uint8_t* data, const uint8_t data_length = 1);
  55. bool receive_bit(void);
  56. bool send_bit(bool value);
  57. bool send(const uint8_t* address, const uint8_t data_length = 1);
  58. OneWiteTimeType wait_while_gpio_is(volatile OneWiteTimeType retries, const bool pin_value);
  59. // set pin state
  60. inline void pin_set_float();
  61. inline void pin_set_low();
  62. // get error text
  63. const char* decode_error();
  64. // devices managment
  65. uint8_t attach(OneWireDevice& device);
  66. bool detach(const OneWireDevice& device);
  67. bool detach(uint8_t device_number);
  68. uint8_t get_next_device_index(const uint8_t index_start = 0) const;
  69. // id tree managment
  70. uint8_t build_id_tree(void);
  71. uint8_t build_id_tree(uint8_t id_bit_position, uint32_t device_mask);
  72. uint8_t get_first_bit_set_position(uint32_t mask) const;
  73. uint8_t get_first_id_tree_el_position(void) const;
  74. // commands
  75. void cmd_search_rom(void);
  76. };