1High-Level Interrupts
2=====================
3
4.. toctree::
5   :maxdepth: 1
6
7The Xtensa architecture has support for 32 interrupts, divided over 8 levels, plus an assortment of exceptions. On the {IDF_TARGET_NAME}, the interrupt mux allows most interrupt sources to be routed to these interrupts using the :doc:`interrupt allocator <../api-reference/system/intr_alloc>`. Normally, interrupts will be written in C, but ESP-IDF allows high-level interrupts to be written in assembly as well, allowing for very low interrupt latencies.
8
9Interrupt Levels
10----------------
11
12===== ================= ====================================================
13Level Symbol            Remark
14===== ================= ====================================================
151     N/A               Exception and level 0 interrupts. Handled by ESP-IDF
162-3   N/A               Medium level interrupts. Handled by ESP-IDF
174     xt_highint4       Normally used by ESP-IDF debug logic
185     xt_highint5       Free to use
19NMI   xt_nmi            Free to use
20dbg   xt_debugexception Debug exception. Called on e.g. a BREAK instruction.
21===== ================= ====================================================
22
23Using these symbols is done by creating an assembly file (suffix .S) and defining the named symbols, like this::
24
25        .section .iram1,"ax"
26        .global     xt_highint5
27        .type       xt_highint5,@function
28        .align      4
29    xt_highint5:
30        ... your code here
31        rsr     a0, EXCSAVE_5
32        rfi     5
33
34For a real-life example, see the :component_file:`esp_system/port/soc/{IDF_TARGET_PATH_NAME}/dport_panic_highint_hdl.S` file; the panic handler interrupt is implemented there.
35
36
37Notes
38-----
39
40 - Do not call C code from a high-level interrupt; because these interrupts still run in critical sections, this can cause crashes.
41   (The panic handler interrupt does call normal C code, but this is OK because there is no intention of returning to the normal code
42   flow afterwards.)
43
44 - Make sure your assembly code gets linked in. If the interrupt handler symbol is the only symbol the rest of the code uses from this
45   file, the linker will take the default ISR instead and not link the assembly file into the final project. To get around this, in the
46   assembly file, define a symbol, like this::
47
48            .global ld_include_my_isr_file
49       ld_include_my_isr_file:
50
51
52The symbol is called ``ld_include_my_isr_file`` here but can have any arbitrary name not defined anywhere else.
53
54Then, in the component CMakeLists.txt, add this file as an unresolved symbol to the ld command line arguments::
55
56   target_link_libraries(${COMPONENT_TARGET} "-u ld_include_my_isr_file")
57
58If using the legacy Make build system, add the following to component.mk, instead::
59
60   COMPONENT_ADD_LDFLAGS := -u ld_include_my_isr_file
61
62
63This should cause the linker to always include a file defining ``ld_include_my_isr_file``, causing the ISR to always be linked in.
64
65 - High-level interrupts can be routed and handled using esp_intr_alloc and associated functions. The handler and handler arguments
66   to esp_intr_alloc must be NULL, however.
67
68 - In theory, medium priority interrupts could also be handled in this way. For now, ESP-IDF does not support this.
69
70