dedic_gpio.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "esp_attr.h"
  11. #include "soc/soc_caps.h"
  12. #if SOC_DEDICATED_GPIO_SUPPORTED
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /**
  17. * @brief Type of Dedicated GPIO bundle
  18. */
  19. typedef struct dedic_gpio_bundle_t *dedic_gpio_bundle_handle_t;
  20. /**
  21. * @brief Type of Dedicated GPIO bundle configuration
  22. */
  23. typedef struct {
  24. const int *gpio_array; /*!< Array of GPIO numbers, gpio_array[0] ~ gpio_array[size-1] <=> low_dedic_channel_num ~ high_dedic_channel_num */
  25. size_t array_size; /*!< Number of GPIOs in gpio_array */
  26. struct {
  27. unsigned int in_en: 1; /*!< Enable input */
  28. unsigned int in_invert: 1; /*!< Invert input signal */
  29. unsigned int out_en: 1; /*!< Enable output */
  30. unsigned int out_invert: 1; /*!< Invert output signal */
  31. } flags; /*!< Flags to control specific behaviour of GPIO bundle */
  32. } dedic_gpio_bundle_config_t;
  33. /**
  34. * @brief Create GPIO bundle and return the handle
  35. *
  36. * @param[in] config Configuration of GPIO bundle
  37. * @param[out] ret_bundle Returned handle of the new created GPIO bundle
  38. * @return
  39. * - ESP_OK: Create GPIO bundle successfully
  40. * - ESP_ERR_INVALID_ARG: Create GPIO bundle failed because of invalid argument
  41. * - ESP_ERR_NO_MEM: Create GPIO bundle failed because of no capable memory
  42. * - ESP_ERR_NOT_FOUND: Create GPIO bundle failed because of no enough continuous dedicated channels
  43. * - ESP_FAIL: Create GPIO bundle failed because of other error
  44. *
  45. * @note One has to enable at least input or output mode in "config" parameter.
  46. */
  47. esp_err_t dedic_gpio_new_bundle(const dedic_gpio_bundle_config_t *config, dedic_gpio_bundle_handle_t *ret_bundle);
  48. /**
  49. * @brief Destory GPIO bundle
  50. *
  51. * @param[in] bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  52. * @return
  53. * - ESP_OK: Destory GPIO bundle successfully
  54. * - ESP_ERR_INVALID_ARG: Destory GPIO bundle failed because of invalid argument
  55. * - ESP_FAIL: Destory GPIO bundle failed because of other error
  56. */
  57. esp_err_t dedic_gpio_del_bundle(dedic_gpio_bundle_handle_t bundle);
  58. /**@{*/
  59. /**
  60. * @brief Get allocated channel mask
  61. *
  62. * @param[in] bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  63. * @param[out] mask Returned mask value for on specific direction (in or out)
  64. * @return
  65. * - ESP_OK: Get channel mask successfully
  66. * - ESP_ERR_INVALID_ARG: Get channel mask failed because of invalid argument
  67. * - ESP_FAIL: Get channel mask failed because of other error
  68. *
  69. * @note Each bundle should have at least one mask (in or/and out), based on bundle configuration.
  70. * @note With the returned mask, user can directly invoke LL function like "cpu_ll_write_dedic_gpio_mask"
  71. * or write assembly code with dedicated GPIO instructions, to get better performance on GPIO manipulation.
  72. */
  73. esp_err_t dedic_gpio_get_out_mask(dedic_gpio_bundle_handle_t bundle, uint32_t *mask);
  74. esp_err_t dedic_gpio_get_in_mask(dedic_gpio_bundle_handle_t bundle, uint32_t *mask);
  75. /**@}*/
  76. /**
  77. * @brief Write value to GPIO bundle
  78. *
  79. * @param[in] bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  80. * @param[in] mask Mask of the GPIOs to be written in the given bundle
  81. * @param[in] value Value to write to given GPIO bundle, low bit represents low member in the bundle
  82. *
  83. * @note The mask is seen from the view of GPIO bundle.
  84. * For example, bundleA contains [GPIO10, GPIO12, GPIO17], to set GPIO17 individually, the mask should be 0x04.
  85. * @note For performance reasons, this function doesn't check the validity of any parameters, and is placed in IRAM.
  86. */
  87. void dedic_gpio_bundle_write(dedic_gpio_bundle_handle_t bundle, uint32_t mask, uint32_t value) IRAM_ATTR;
  88. /**
  89. * @brief Read the value that output from the given GPIO bundle
  90. *
  91. * @param[in] bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  92. * @return Value that output from the GPIO bundle, low bit represents low member in the bundle
  93. *
  94. * @note For performance reasons, this function doesn't check the validity of any parameters, and is placed in IRAM.
  95. */
  96. uint32_t dedic_gpio_bundle_read_out(dedic_gpio_bundle_handle_t bundle) IRAM_ATTR;
  97. /**
  98. * @brief Read the value that input to the given GPIO bundle
  99. *
  100. * @param[in] bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  101. * @return Value that input to the GPIO bundle, low bit represents low member in the bundle
  102. *
  103. * @note For performance reasons, this function doesn't check the validity of any parameters, and is placed in IRAM.
  104. */
  105. uint32_t dedic_gpio_bundle_read_in(dedic_gpio_bundle_handle_t bundle) IRAM_ATTR;
  106. #if SOC_DEDIC_GPIO_HAS_INTERRUPT
  107. /**
  108. * @brief Supported type of dedicated GPIO interrupt
  109. */
  110. typedef enum {
  111. DEDIC_GPIO_INTR_NONE, /*!< No interrupt */
  112. DEDIC_GPIO_INTR_LOW_LEVEL = 2, /*!< Interrupt on low level */
  113. DEDIC_GPIO_INTR_HIGH_LEVEL, /*!< Interrupt on high level */
  114. DEDIC_GPIO_INTR_NEG_EDGE, /*!< Interrupt on negedge */
  115. DEDIC_GPIO_INTR_POS_EDGE, /*!< Interrupt on posedge */
  116. DEDIC_GPIO_INTR_BOTH_EDGE /*!< Interrupt on both negedge and posedge */
  117. } dedic_gpio_intr_type_t;
  118. /**
  119. * @brief Type of dedicated GPIO ISR callback function
  120. *
  121. * @param bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  122. * @param index Index of the GPIO in its corresponding bundle (count from 0)
  123. * @param args User defined arguments for the callback function. It's passed through `dedic_gpio_bundle_set_interrupt_and_callback`
  124. * @return If a high priority task is woken up by the callback function
  125. */
  126. typedef bool (*dedic_gpio_isr_callback_t)(dedic_gpio_bundle_handle_t bundle, uint32_t index, void *args);
  127. /**
  128. * @brief Set interrupt and callback function for GPIO bundle
  129. *
  130. * @param[in] bundle Handle of GPIO bundle that returned from "dedic_gpio_new_bundle"
  131. * @param[in] mask Mask of the GPIOs in the given bundle
  132. * @param[in] intr_type Interrupt type, set to DEDIC_GPIO_INTR_NONE can disable interrupt
  133. * @param[in] cb_isr Callback function, which got invoked in ISR context. A NULL pointer here will bypass the callback
  134. * @param[in] cb_args User defined argument to be passed to the callback function
  135. *
  136. * @note This function is only valid for bundle with input mode enabled. See "dedic_gpio_bundle_config_t"
  137. * @note The mask is seen from the view of GPIO Bundle.
  138. * For example, bundleA contains [GPIO10, GPIO12, GPIO17], to set GPIO17 individually, the mask should be 0x04.
  139. *
  140. * @return
  141. * - ESP_OK: Set GPIO interrupt and callback function successfully
  142. * - ESP_ERR_INVALID_ARG: Set GPIO interrupt and callback function failed because of invalid argument
  143. * - ESP_FAIL: Set GPIO interrupt and callback function failed because of other error
  144. */
  145. esp_err_t dedic_gpio_bundle_set_interrupt_and_callback(dedic_gpio_bundle_handle_t bundle, uint32_t mask, dedic_gpio_intr_type_t intr_type, dedic_gpio_isr_callback_t cb_isr, void *cb_args);
  146. #endif // SOC_DEDIC_GPIO_HAS_INTERRUPT
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif //SOC_DEDICATED_GPIO_SUPPORTED