esp_intr_alloc.h 13 KB

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