esp_intr_alloc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_INTR_ALLOC_H__
  14. #define __ESP_INTR_ALLOC_H__
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "esp_err.h"
  18. #include "freertos/xtensa_api.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /** @addtogroup Intr_Alloc
  23. * @{
  24. */
  25. /** @brief Interrupt allocation flags
  26. *
  27. * These flags can be used to specify which interrupt qualities the
  28. * code calling esp_intr_alloc* needs.
  29. *
  30. */
  31. //Keep the LEVELx values as they are here; they match up with (1<<level)
  32. #define ESP_INTR_FLAG_LEVEL1 (1<<1) ///< Accept a Level 1 interrupt vector (lowest priority)
  33. #define ESP_INTR_FLAG_LEVEL2 (1<<2) ///< Accept a Level 2 interrupt vector
  34. #define ESP_INTR_FLAG_LEVEL3 (1<<3) ///< Accept a Level 3 interrupt vector
  35. #define ESP_INTR_FLAG_LEVEL4 (1<<4) ///< Accept a Level 4 interrupt vector
  36. #define ESP_INTR_FLAG_LEVEL5 (1<<5) ///< Accept a Level 5 interrupt vector
  37. #define ESP_INTR_FLAG_LEVEL6 (1<<6) ///< Accept a Level 6 interrupt vector
  38. #define ESP_INTR_FLAG_NMI (1<<7) ///< Accept a Level 7 interrupt vector (highest priority)
  39. #define ESP_INTR_FLAG_SHARED (1<<8) ///< Interrupt can be shared between ISRs
  40. #define ESP_INTR_FLAG_EDGE (1<<9) ///< Edge-triggered interrupt
  41. #define ESP_INTR_FLAG_IRAM (1<<10) ///< ISR can be called if cache is disabled
  42. #define ESP_INTR_FLAG_INTRDISABLED (1<<11) ///< Return with this interrupt disabled
  43. #define ESP_INTR_FLAG_LOWMED (ESP_INTR_FLAG_LEVEL1|ESP_INTR_FLAG_LEVEL2|ESP_INTR_FLAG_LEVEL3) ///< Low and medium prio interrupts. These can be handled in C.
  44. #define ESP_INTR_FLAG_HIGH (ESP_INTR_FLAG_LEVEL4|ESP_INTR_FLAG_LEVEL5|ESP_INTR_FLAG_LEVEL6|ESP_INTR_FLAG_NMI) ///< High level interrupts. Need to be handled in assembly.
  45. #define ESP_INTR_FLAG_LEVELMASK (ESP_INTR_FLAG_LEVEL1|ESP_INTR_FLAG_LEVEL2|ESP_INTR_FLAG_LEVEL3| \
  46. ESP_INTR_FLAG_LEVEL4|ESP_INTR_FLAG_LEVEL5|ESP_INTR_FLAG_LEVEL6| \
  47. ESP_INTR_FLAG_NMI) ///< Mask for all level flags
  48. /** @addtogroup Intr_Alloc_Pseudo_Src
  49. * @{
  50. */
  51. /**
  52. * The esp_intr_alloc* functions can allocate an int for all ETS_*_INTR_SOURCE interrupt sources that
  53. * are routed through the interrupt mux. Apart from these sources, each core also has some internal
  54. * sources that do not pass through the interrupt mux. To allocate an interrupt for these sources,
  55. * pass these pseudo-sources to the functions.
  56. */
  57. #define ETS_INTERNAL_TIMER0_INTR_SOURCE -1 ///< Xtensa timer 0 interrupt source
  58. #define ETS_INTERNAL_TIMER1_INTR_SOURCE -2 ///< Xtensa timer 1 interrupt source
  59. #define ETS_INTERNAL_TIMER2_INTR_SOURCE -3 ///< Xtensa timer 2 interrupt source
  60. #define ETS_INTERNAL_SW0_INTR_SOURCE -4 ///< Software int source 1
  61. #define ETS_INTERNAL_SW1_INTR_SOURCE -5 ///< Software int source 2
  62. #define ETS_INTERNAL_PROFILING_INTR_SOURCE -6 ///< Int source for profiling
  63. /**@}*/
  64. /** Provides SystemView with positive IRQ IDs, otherwise scheduler events are not shown properly
  65. */
  66. #define ETS_INTERNAL_INTR_SOURCE_OFF (-ETS_INTERNAL_PROFILING_INTR_SOURCE)
  67. /** Enable interrupt by interrupt number */
  68. #define ESP_INTR_ENABLE(inum) xt_ints_on((1<<inum))
  69. /** Disable interrupt by interrupt number */
  70. #define ESP_INTR_DISABLE(inum) xt_ints_off((1<<inum))
  71. /** Function prototype for interrupt handler function */
  72. typedef void (*intr_handler_t)(void *arg);
  73. /** Interrupt handler associated data structure */
  74. typedef struct intr_handle_data_t intr_handle_data_t;
  75. /** Handle to an interrupt handler */
  76. typedef intr_handle_data_t* intr_handle_t ;
  77. /**
  78. * @brief Mark an interrupt as a shared interrupt
  79. *
  80. * This will mark a certain interrupt on the specified CPU as
  81. * an interrupt that can be used to hook shared interrupt handlers
  82. * to.
  83. *
  84. * @param intno The number of the interrupt (0-31)
  85. * @param cpu CPU on which the interrupt should be marked as shared (0 or 1)
  86. * @param is_in_iram Shared interrupt is for handlers that reside in IRAM and
  87. * the int can be left enabled while the flash cache is disabled.
  88. *
  89. * @return ESP_ERR_INVALID_ARG if cpu or intno is invalid
  90. * ESP_OK otherwise
  91. */
  92. esp_err_t esp_intr_mark_shared(int intno, int cpu, bool is_in_iram);
  93. /**
  94. * @brief Reserve an interrupt to be used outside of this framework
  95. *
  96. * This will mark a certain interrupt on the specified CPU as
  97. * reserved, not to be allocated for any reason.
  98. *
  99. * @param intno The number of the interrupt (0-31)
  100. * @param cpu CPU on which the interrupt should be marked as shared (0 or 1)
  101. *
  102. * @return ESP_ERR_INVALID_ARG if cpu or intno is invalid
  103. * ESP_OK otherwise
  104. */
  105. esp_err_t esp_intr_reserve(int intno, int cpu);
  106. /**
  107. * @brief Allocate an interrupt with the given parameters.
  108. *
  109. * This finds an interrupt that matches the restrictions as given in the flags
  110. * parameter, maps the given interrupt source to it and hooks up the given
  111. * interrupt handler (with optional argument) as well. If needed, it can return
  112. * a handle for the interrupt as well.
  113. *
  114. * The interrupt will always be allocated on the core that runs this function.
  115. *
  116. * If ESP_INTR_FLAG_IRAM flag is used, and handler address is not in IRAM or
  117. * RTC_FAST_MEM, then ESP_ERR_INVALID_ARG is returned.
  118. *
  119. * @param source The interrupt source. One of the ETS_*_INTR_SOURCE interrupt mux
  120. * sources, as defined in soc/soc.h, or one of the internal
  121. * ETS_INTERNAL_*_INTR_SOURCE sources as defined in this header.
  122. * @param flags An ORred mask of the ESP_INTR_FLAG_* defines. These restrict the
  123. * choice of interrupts that this routine can choose from. If this value
  124. * is 0, it will default to allocating a non-shared interrupt of level
  125. * 1, 2 or 3. If this is ESP_INTR_FLAG_SHARED, it will allocate a shared
  126. * interrupt of level 1. Setting ESP_INTR_FLAG_INTRDISABLED will return
  127. * from this function with the interrupt disabled.
  128. * @param handler The interrupt handler. Must be NULL when an interrupt of level >3
  129. * is requested, because these types of interrupts aren't C-callable.
  130. * @param arg Optional argument for passed to the interrupt handler
  131. * @param ret_handle Pointer to an intr_handle_t to store a handle that can later be
  132. * used to request details or free the interrupt. Can be NULL if no handle
  133. * is required.
  134. *
  135. * @return ESP_ERR_INVALID_ARG if the combination of arguments is invalid.
  136. * ESP_ERR_NOT_FOUND No free interrupt found with the specified flags
  137. * ESP_OK otherwise
  138. */
  139. esp_err_t esp_intr_alloc(int source, int flags, intr_handler_t handler, void *arg, intr_handle_t *ret_handle);
  140. /**
  141. * @brief Allocate an interrupt with the given parameters.
  142. *
  143. *
  144. * This essentially does the same as esp_intr_alloc, but allows specifying a register and mask
  145. * combo. For shared interrupts, the handler is only called if a read from the specified
  146. * register, ANDed with the mask, returns non-zero. By passing an interrupt status register
  147. * address and a fitting mask, this can be used to accelerate interrupt handling in the case
  148. * a shared interrupt is triggered; by checking the interrupt statuses first, the code can
  149. * decide which ISRs can be skipped
  150. *
  151. * @param source The interrupt source. One of the ETS_*_INTR_SOURCE interrupt mux
  152. * sources, as defined in soc/soc.h, or one of the internal
  153. * ETS_INTERNAL_*_INTR_SOURCE sources as defined in this header.
  154. * @param flags An ORred mask of the ESP_INTR_FLAG_* defines. These restrict the
  155. * choice of interrupts that this routine can choose from. If this value
  156. * is 0, it will default to allocating a non-shared interrupt of level
  157. * 1, 2 or 3. If this is ESP_INTR_FLAG_SHARED, it will allocate a shared
  158. * interrupt of level 1. Setting ESP_INTR_FLAG_INTRDISABLED will return
  159. * from this function with the interrupt disabled.
  160. * @param intrstatusreg The address of an interrupt status register
  161. * @param intrstatusmask A mask. If a read of address intrstatusreg has any of the bits
  162. * that are 1 in the mask set, the ISR will be called. If not, it will be
  163. * skipped.
  164. * @param handler The interrupt handler. Must be NULL when an interrupt of level >3
  165. * is requested, because these types of interrupts aren't C-callable.
  166. * @param arg Optional argument for passed to the interrupt handler
  167. * @param ret_handle Pointer to an intr_handle_t to store a handle that can later be
  168. * used to request details or free the interrupt. Can be NULL if no handle
  169. * is required.
  170. *
  171. * @return ESP_ERR_INVALID_ARG if the combination of arguments is invalid.
  172. * ESP_ERR_NOT_FOUND No free interrupt found with the specified flags
  173. * ESP_OK otherwise
  174. */
  175. esp_err_t esp_intr_alloc_intrstatus(int source, int flags, uint32_t intrstatusreg, uint32_t intrstatusmask, intr_handler_t handler, void *arg, intr_handle_t *ret_handle);
  176. /**
  177. * @brief Disable and free an interrupt.
  178. *
  179. * Use an interrupt handle to disable the interrupt and release the resources
  180. * associated with it.
  181. *
  182. * @note
  183. * When the handler shares its source with other handlers, the interrupt status
  184. * bits it's responsible for should be managed properly before freeing it. see
  185. * ``esp_intr_disable`` for more details.
  186. *
  187. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  188. *
  189. * @return ESP_ERR_INVALID_ARG if handle is invalid, or esp_intr_free runs on another core than
  190. * where the interrupt is allocated on.
  191. * ESP_OK otherwise
  192. */
  193. esp_err_t esp_intr_free(intr_handle_t handle);
  194. /**
  195. * @brief Get CPU number an interrupt is tied to
  196. *
  197. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  198. *
  199. * @return The core number where the interrupt is allocated
  200. */
  201. int esp_intr_get_cpu(intr_handle_t handle);
  202. /**
  203. * @brief Get the allocated interrupt for a certain handle
  204. *
  205. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  206. *
  207. * @return The interrupt number
  208. */
  209. int esp_intr_get_intno(intr_handle_t handle);
  210. /**
  211. * @brief Disable the interrupt associated with the handle
  212. *
  213. * @note
  214. * 1. For local interrupts (ESP_INTERNAL_* sources), this function has to be called on the
  215. * CPU the interrupt is allocated on. Other interrupts have no such restriction.
  216. * 2. When several handlers sharing a same interrupt source, interrupt status bits, which are
  217. * handled in the handler to be disabled, should be masked before the disabling, or handled
  218. * in other enabled interrupts properly. Miss of interrupt status handling will cause infinite
  219. * interrupt calls and finally system crash.
  220. *
  221. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  222. *
  223. * @return ESP_ERR_INVALID_ARG if the combination of arguments is invalid.
  224. * ESP_OK otherwise
  225. */
  226. esp_err_t esp_intr_disable(intr_handle_t handle);
  227. /**
  228. * @brief Enable the interrupt associated with the handle
  229. *
  230. * @note For local interrupts (ESP_INTERNAL_* sources), this function has to be called on the
  231. * CPU the interrupt is allocated on. Other interrupts have no such restriction.
  232. *
  233. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  234. *
  235. * @return ESP_ERR_INVALID_ARG if the combination of arguments is invalid.
  236. * ESP_OK otherwise
  237. */
  238. esp_err_t esp_intr_enable(intr_handle_t handle);
  239. /**
  240. * @brief Set the "in IRAM" status of the handler.
  241. *
  242. * @note Does not work on shared interrupts.
  243. *
  244. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  245. * @param is_in_iram Whether the handler associated with this handle resides in IRAM.
  246. * Handlers residing in IRAM can be called when cache is disabled.
  247. *
  248. * @return ESP_ERR_INVALID_ARG if the combination of arguments is invalid.
  249. * ESP_OK otherwise
  250. */
  251. esp_err_t esp_intr_set_in_iram(intr_handle_t handle, bool is_in_iram);
  252. /**
  253. * @brief Disable interrupts that aren't specifically marked as running from IRAM
  254. */
  255. void esp_intr_noniram_disable(void);
  256. /**
  257. * @brief Re-enable interrupts disabled by esp_intr_noniram_disable
  258. */
  259. void esp_intr_noniram_enable(void);
  260. /**@}*/
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. #endif