dedic_gpio.h 7.9 KB

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