one_wire_slave_gpio.h 2.6 KB

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