gpio_exp.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* GDS Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #pragma once
  8. #include <stdint.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "driver/gpio.h"
  11. struct gpio_exp_s;
  12. typedef struct {
  13. char model[32];
  14. int intr;
  15. uint8_t count;
  16. uint32_t base;
  17. struct gpio_exp_phy_s {
  18. uint8_t addr;
  19. struct { // for I2C
  20. uint8_t port;
  21. };
  22. struct { // for SPI
  23. uint32_t speed;
  24. uint8_t host;
  25. uint8_t cs_pin;
  26. };
  27. } phy;
  28. } gpio_exp_config_t;
  29. // set <intr> to -1 and <queue> to NULL if there is no interrupt
  30. struct gpio_exp_s* gpio_exp_create(const gpio_exp_config_t *config);
  31. uint32_t gpio_exp_get_base(struct gpio_exp_s *expander);
  32. struct gpio_exp_s* gpio_exp_get_expander(int gpio);
  33. #define gpio_is_expanded(gpio) (gpio < GPIO_NUM_MAX)
  34. /*
  35. For all functions below when <expander> is provided, GPIO's can be numbered from 0. If <expander>
  36. is NULL, then GPIO must start from base OR be on-chip
  37. */
  38. esp_err_t gpio_exp_set_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander);
  39. esp_err_t gpio_exp_set_pull_mode(int gpio, gpio_pull_mode_t mode, struct gpio_exp_s *expander);
  40. int gpio_exp_get_level(int gpio, int age, struct gpio_exp_s *expander);
  41. esp_err_t gpio_exp_set_level(int gpio, int level, bool direct, struct gpio_exp_s *expander);
  42. esp_err_t gpio_exp_isr_handler_add(int gpio, gpio_isr_t isr, uint32_t debounce, void *arg, struct gpio_exp_s *expander);
  43. esp_err_t gpio_exp_isr_handler_remove(int gpio, struct gpio_exp_s *expander);
  44. // unified function to use either built-in or expanded GPIO
  45. esp_err_t gpio_set_direction_x(int gpio, gpio_mode_t mode);
  46. esp_err_t gpio_set_pull_mode_x(int gpio, gpio_pull_mode_t mode);
  47. int gpio_get_level_x(int gpio);
  48. esp_err_t gpio_set_level_x(int gpio, int level);
  49. esp_err_t gpio_isr_handler_add_x(int gpio, gpio_isr_t isr_handler, void* args);
  50. esp_err_t gpio_isr_handler_remove_x(int gpio);
  51. #define gpio_set_intr_type_x(gpio, type) do { if (gpio < GPIO_NUM_MAX) gpio_set_intr_type(gpio, type); } while (0)
  52. #define gpio_intr_enable_x(gpio) do { if (gpio < GPIO_NUM_MAX) gpio_intr_enable(gpio); } while (0)
  53. #define gpio_pad_select_gpio_x(gpio) do { if (gpio < GPIO_NUM_MAX) gpio_pad_select_gpio(gpio); } while (0)