esp_intr_alloc.h 13 KB

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