2021-08-05 10:30:10 -04:00
/*
* SPDX - FileCopyrightText : 2015 - 2021 Espressif Systems ( Shanghai ) CO LTD
*
* SPDX - License - Identifier : Apache - 2.0
*/
2016-11-25 04:33:51 -05:00
2020-12-28 23:31:54 -05:00
# pragma once
2016-11-25 04:33:51 -05:00
# include <stdint.h>
# include <stdbool.h>
# include "esp_err.h"
# ifdef __cplusplus
extern " C " {
# endif
/** @addtogroup Intr_Alloc
* @ {
*/
/** @brief Interrupt allocation flags
*
* These flags can be used to specify which interrupt qualities the
* code calling esp_intr_alloc * needs .
*
*/
//Keep the LEVELx values as they are here; they match up with (1<<level)
2020-12-28 23:31:54 -05:00
# define ESP_INTR_FLAG_LEVEL1 (1<<1) ///< Accept a Level 1 interrupt vector (lowest priority)
# define ESP_INTR_FLAG_LEVEL2 (1<<2) ///< Accept a Level 2 interrupt vector
# define ESP_INTR_FLAG_LEVEL3 (1<<3) ///< Accept a Level 3 interrupt vector
# define ESP_INTR_FLAG_LEVEL4 (1<<4) ///< Accept a Level 4 interrupt vector
# define ESP_INTR_FLAG_LEVEL5 (1<<5) ///< Accept a Level 5 interrupt vector
# define ESP_INTR_FLAG_LEVEL6 (1<<6) ///< Accept a Level 6 interrupt vector
# define ESP_INTR_FLAG_NMI (1<<7) ///< Accept a Level 7 interrupt vector (highest priority)
# define ESP_INTR_FLAG_SHARED (1<<8) ///< Interrupt can be shared between ISRs
# define ESP_INTR_FLAG_EDGE (1<<9) ///< Edge-triggered interrupt
# define ESP_INTR_FLAG_IRAM (1<<10) ///< ISR can be called if cache is disabled
# define ESP_INTR_FLAG_INTRDISABLED (1<<11) ///< Return with this interrupt disabled
# 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.
# 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.
# define ESP_INTR_FLAG_LEVELMASK (ESP_INTR_FLAG_LEVEL1|ESP_INTR_FLAG_LEVEL2|ESP_INTR_FLAG_LEVEL3| \
ESP_INTR_FLAG_LEVEL4 | ESP_INTR_FLAG_LEVEL5 | ESP_INTR_FLAG_LEVEL6 | \
ESP_INTR_FLAG_NMI ) ///< Mask for all level flags
2016-11-25 04:33:51 -05:00
/** @addtogroup Intr_Alloc_Pseudo_Src
* @ {
*/
/**
* The esp_intr_alloc * functions can allocate an int for all ETS_ * _INTR_SOURCE interrupt sources that
* are routed through the interrupt mux . Apart from these sources , each core also has some internal
* sources that do not pass through the interrupt mux . To allocate an interrupt for these sources ,
* pass these pseudo - sources to the functions .
*/
2020-12-28 23:31:54 -05:00
# define ETS_INTERNAL_TIMER0_INTR_SOURCE -1 ///< Platform timer 0 interrupt source
# define ETS_INTERNAL_TIMER1_INTR_SOURCE -2 ///< Platform timer 1 interrupt source
# define ETS_INTERNAL_TIMER2_INTR_SOURCE -3 ///< Platform timer 2 interrupt source
# define ETS_INTERNAL_SW0_INTR_SOURCE -4 ///< Software int source 1
# define ETS_INTERNAL_SW1_INTR_SOURCE -5 ///< Software int source 2
# define ETS_INTERNAL_PROFILING_INTR_SOURCE -6 ///< Int source for profiling
2016-11-25 04:33:51 -05:00
/**@}*/
2020-01-28 17:31:14 -05:00
/** Provides SystemView with positive IRQ IDs, otherwise scheduler events are not shown properly
*/
2020-12-28 23:31:54 -05:00
# define ETS_INTERNAL_INTR_SOURCE_OFF (-ETS_INTERNAL_PROFILING_INTR_SOURCE)
2017-03-21 23:07:37 -04:00
2020-01-28 17:31:14 -05:00
/** Enable interrupt by interrupt number */
2020-09-29 19:44:12 -04:00
# define ESP_INTR_ENABLE(inum) esp_intr_enable_source(inum)
2020-01-28 17:31:14 -05:00
/** Disable interrupt by interrupt number */
2020-09-29 19:44:12 -04:00
# define ESP_INTR_DISABLE(inum) esp_intr_disable_source(inum)
2019-03-26 04:30:43 -04:00
2020-01-28 17:31:14 -05:00
/** Function prototype for interrupt handler function */
2019-03-26 04:30:43 -04:00
typedef void ( * intr_handler_t ) ( void * arg ) ;
2016-11-25 04:33:51 -05:00
2020-01-28 17:31:14 -05:00
/** Interrupt handler associated data structure */
2016-12-07 08:30:21 -05:00
typedef struct intr_handle_data_t intr_handle_data_t ;
2020-01-28 17:31:14 -05:00
/** Handle to an interrupt handler */
2020-12-28 23:31:54 -05:00
typedef intr_handle_data_t * intr_handle_t ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Mark an interrupt as a shared interrupt
2019-03-26 04:30:43 -04:00
*
2016-11-25 04:33:51 -05:00
* This will mark a certain interrupt on the specified CPU as
* an interrupt that can be used to hook shared interrupt handlers
* to .
*
* @ param intno The number of the interrupt ( 0 - 31 )
* @ param cpu CPU on which the interrupt should be marked as shared ( 0 or 1 )
* @ param is_in_iram Shared interrupt is for handlers that reside in IRAM and
2016-12-07 23:04:26 -05:00
* the int can be left enabled while the flash cache is disabled .
2016-11-25 04:33:51 -05:00
*
* @ return ESP_ERR_INVALID_ARG if cpu or intno is invalid
* ESP_OK otherwise
*/
esp_err_t esp_intr_mark_shared ( int intno , int cpu , bool is_in_iram ) ;
/**
2017-07-23 20:33:20 -04:00
* @ brief Reserve an interrupt to be used outside of this framework
2019-03-26 04:30:43 -04:00
*
2016-11-25 04:33:51 -05:00
* This will mark a certain interrupt on the specified CPU as
* reserved , not to be allocated for any reason .
*
* @ param intno The number of the interrupt ( 0 - 31 )
* @ param cpu CPU on which the interrupt should be marked as shared ( 0 or 1 )
*
* @ return ESP_ERR_INVALID_ARG if cpu or intno is invalid
* ESP_OK otherwise
*/
esp_err_t esp_intr_reserve ( int intno , int cpu ) ;
/**
* @ brief Allocate an interrupt with the given parameters .
*
* This finds an interrupt that matches the restrictions as given in the flags
* parameter , maps the given interrupt source to it and hooks up the given
* interrupt handler ( with optional argument ) as well . If needed , it can return
* a handle for the interrupt as well .
*
* The interrupt will always be allocated on the core that runs this function .
*
2017-01-10 12:14:18 -05:00
* If ESP_INTR_FLAG_IRAM flag is used , and handler address is not in IRAM or
* RTC_FAST_MEM , then ESP_ERR_INVALID_ARG is returned .
*
2016-11-25 04:33:51 -05:00
* @ param source The interrupt source . One of the ETS_ * _INTR_SOURCE interrupt mux
* sources , as defined in soc / soc . h , or one of the internal
* ETS_INTERNAL_ * _INTR_SOURCE sources as defined in this header .
* @ param flags An ORred mask of the ESP_INTR_FLAG_ * defines . These restrict the
* choice of interrupts that this routine can choose from . If this value
* is 0 , it will default to allocating a non - shared interrupt of level
* 1 , 2 or 3. If this is ESP_INTR_FLAG_SHARED , it will allocate a shared
2019-03-26 04:30:43 -04:00
* interrupt of level 1. Setting ESP_INTR_FLAG_INTRDISABLED will return
2016-12-07 23:04:26 -05:00
* from this function with the interrupt disabled .
2016-11-25 04:33:51 -05:00
* @ p aram handler The interrupt handler . Must be NULL when an interrupt of level > 3
* is requested , because these types of interrupts aren ' t C - callable .
* @ param arg Optional argument for passed to the interrupt handler
2016-12-07 08:30:21 -05:00
* @ param ret_handle Pointer to an intr_handle_t to store a handle that can later be
2016-11-25 04:33:51 -05:00
* used to request details or free the interrupt . Can be NULL if no handle
* is required .
*
* @ return ESP_ERR_INVALID_ARG if the combination of arguments is invalid .
* ESP_ERR_NOT_FOUND No free interrupt found with the specified flags
* ESP_OK otherwise
*/
2016-12-07 08:30:21 -05:00
esp_err_t esp_intr_alloc ( int source , int flags , intr_handler_t handler , void * arg , intr_handle_t * ret_handle ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Allocate an interrupt with the given parameters .
*
*
* This essentially does the same as esp_intr_alloc , but allows specifying a register and mask
2019-03-26 04:30:43 -04:00
* combo . For shared interrupts , the handler is only called if a read from the specified
2016-11-25 04:33:51 -05:00
* register , ANDed with the mask , returns non - zero . By passing an interrupt status register
* address and a fitting mask , this can be used to accelerate interrupt handling in the case
* a shared interrupt is triggered ; by checking the interrupt statuses first , the code can
* decide which ISRs can be skipped
*
* @ param source The interrupt source . One of the ETS_ * _INTR_SOURCE interrupt mux
* sources , as defined in soc / soc . h , or one of the internal
* ETS_INTERNAL_ * _INTR_SOURCE sources as defined in this header .
* @ param flags An ORred mask of the ESP_INTR_FLAG_ * defines . These restrict the
* choice of interrupts that this routine can choose from . If this value
* is 0 , it will default to allocating a non - shared interrupt of level
* 1 , 2 or 3. If this is ESP_INTR_FLAG_SHARED , it will allocate a shared
2019-03-26 04:30:43 -04:00
* interrupt of level 1. Setting ESP_INTR_FLAG_INTRDISABLED will return
2016-12-07 23:04:26 -05:00
* from this function with the interrupt disabled .
2016-11-25 04:33:51 -05:00
* @ param intrstatusreg The address of an interrupt status register
* @ param intrstatusmask A mask . If a read of address intrstatusreg has any of the bits
* that are 1 in the mask set , the ISR will be called . If not , it will be
* skipped .
* @ param handler The interrupt handler . Must be NULL when an interrupt of level > 3
* is requested , because these types of interrupts aren ' t C - callable .
* @ param arg Optional argument for passed to the interrupt handler
2016-12-07 08:30:21 -05:00
* @ param ret_handle Pointer to an intr_handle_t to store a handle that can later be
2016-11-25 04:33:51 -05:00
* used to request details or free the interrupt . Can be NULL if no handle
* is required .
*
* @ return ESP_ERR_INVALID_ARG if the combination of arguments is invalid .
* ESP_ERR_NOT_FOUND No free interrupt found with the specified flags
* ESP_OK otherwise
*/
2016-12-07 08:30:21 -05:00
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 ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Disable and free an interrupt .
*
2018-09-20 00:13:43 -04:00
* Use an interrupt handle to disable the interrupt and release the resources associated with it .
* If the current core is not the core that registered this interrupt , this routine will be assigned to
* the core that allocated this interrupt , blocking and waiting until the resource is successfully released .
2016-11-25 04:33:51 -05:00
*
2019-03-26 04:30:43 -04:00
* @ note
* When the handler shares its source with other handlers , the interrupt status
* bits it ' s responsible for should be managed properly before freeing it . see
2018-09-20 00:13:43 -04:00
* ` ` esp_intr_disable ` ` for more details . Please do not call this function in ` ` esp_ipc_call_blocking ` ` .
2017-08-18 03:15:47 -04:00
*
2016-11-25 04:33:51 -05:00
* @ param handle The handle , as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
*
2018-09-20 00:13:43 -04:00
* @ return ESP_ERR_INVALID_ARG the handle is NULL
* ESP_FAIL failed to release this handle
2016-11-25 04:33:51 -05:00
* ESP_OK otherwise
*/
2016-12-07 08:30:21 -05:00
esp_err_t esp_intr_free ( intr_handle_t handle ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Get CPU number an interrupt is tied to
2019-03-26 04:30:43 -04:00
*
2016-11-25 04:33:51 -05:00
* @ param handle The handle , as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
*
* @ return The core number where the interrupt is allocated
*/
2016-12-07 08:30:21 -05:00
int esp_intr_get_cpu ( intr_handle_t handle ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Get the allocated interrupt for a certain handle
2019-03-26 04:30:43 -04:00
*
2016-11-25 04:33:51 -05:00
* @ param handle The handle , as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
*
* @ return The interrupt number
*/
2016-12-07 08:30:21 -05:00
int esp_intr_get_intno ( intr_handle_t handle ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Disable the interrupt associated with the handle
2019-03-26 04:30:43 -04:00
*
* @ note
2017-08-18 03:15:47 -04:00
* 1. For local interrupts ( ESP_INTERNAL_ * sources ) , this function has to be called on the
* CPU the interrupt is allocated on . Other interrupts have no such restriction .
2019-03-26 04:30:43 -04:00
* 2. When several handlers sharing a same interrupt source , interrupt status bits , which are
2017-08-18 03:15:47 -04:00
* handled in the handler to be disabled , should be masked before the disabling , or handled
2019-03-26 04:30:43 -04:00
* in other enabled interrupts properly . Miss of interrupt status handling will cause infinite
2017-08-18 03:15:47 -04:00
* interrupt calls and finally system crash .
2016-11-25 04:33:51 -05:00
*
* @ param handle The handle , as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
*
* @ return ESP_ERR_INVALID_ARG if the combination of arguments is invalid .
* ESP_OK otherwise
*/
2016-12-07 08:30:21 -05:00
esp_err_t esp_intr_disable ( intr_handle_t handle ) ;
2016-11-25 04:33:51 -05:00
/**
2017-11-09 06:21:39 -05:00
* @ brief Enable the interrupt associated with the handle
*
2016-12-07 23:04:26 -05:00
* @ note For local interrupts ( ESP_INTERNAL_ * sources ) , this function has to be called on the
* CPU the interrupt is allocated on . Other interrupts have no such restriction .
2016-11-25 04:33:51 -05:00
*
* @ param handle The handle , as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
*
* @ return ESP_ERR_INVALID_ARG if the combination of arguments is invalid .
* ESP_OK otherwise
*/
2016-12-07 08:30:21 -05:00
esp_err_t esp_intr_enable ( intr_handle_t handle ) ;
2016-11-25 04:33:51 -05:00
2017-11-09 06:21:39 -05:00
/**
* @ brief Set the " in IRAM " status of the handler .
*
* @ note Does not work on shared interrupts .
*
* @ param handle The handle , as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
* @ param is_in_iram Whether the handler associated with this handle resides in IRAM .
* Handlers residing in IRAM can be called when cache is disabled .
*
* @ return ESP_ERR_INVALID_ARG if the combination of arguments is invalid .
* ESP_OK otherwise
*/
esp_err_t esp_intr_set_in_iram ( intr_handle_t handle , bool is_in_iram ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Disable interrupts that aren ' t specifically marked as running from IRAM
*/
2019-07-16 05:33:30 -04:00
void esp_intr_noniram_disable ( void ) ;
2016-11-25 04:33:51 -05:00
/**
* @ brief Re - enable interrupts disabled by esp_intr_noniram_disable
*/
2019-07-16 05:33:30 -04:00
void esp_intr_noniram_enable ( void ) ;
2016-11-25 04:33:51 -05:00
2020-09-29 19:44:12 -04:00
/**
* @ brief enable the interrupt source based on its number
* @ param inum interrupt number from 0 to 31
2020-11-10 02:40:01 -05:00
*/
2020-09-29 19:44:12 -04:00
void esp_intr_enable_source ( int inum ) ;
/**
* @ brief disable the interrupt source based on its number
* @ param inum interrupt number from 0 to 31
2020-11-10 02:40:01 -05:00
*/
2020-09-29 19:44:12 -04:00
void esp_intr_disable_source ( int inum ) ;
2020-12-28 23:31:54 -05:00
/**
* @ brief Get the lowest interrupt level from the flags
* @ param flags The same flags that pass to ` esp_intr_alloc_intrstatus ` API
*/
static inline int esp_intr_flags_to_level ( int flags )
{
return __builtin_ffs ( ( flags & ESP_INTR_FLAG_LEVELMASK ) > > 1 ) + 1 ;
}
2016-11-25 04:33:51 -05:00
/**@}*/
# ifdef __cplusplus
}
# endif