1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! ESP32 specific interrupt handling
//!
//! ESP32 uses 2-level interrupt handling: peripheral interrupts are mapped to cpu interrupts.
//! This module redirects the cpu interrupts handler to registered peripheral interrupt handlers.
//!
//! Interrupt handlers are defined using the [Interrupt](attr.interrupt.html) attribute.
//! (Note that this is a distinct attribute from the one in the [xtensa_lx_rt](xtensa_lx_rt)
//! crate.)
//!
//! To enable the interrupt and assign to a specific interrupt level use
//! the [enable] or [enable_with_priority] functions. (This is in addition to enabling the
//! interrupt in the respective peripherals.)
//!
//! To have lowest latency possible you can use the
//! [Interrupt](../../xtensa_lx_rt/attr.interrupt.html) attribute from the xtensa_lx_rt crate
//! to define low level/naked interrupt handlers. (This will override the interrupt
//! handling offered by this crate for that specific interrupt level. This should especially be
//! considered when using Level 7 = Non Maskable Interrupt level as these will not be turned off
//! during [interrupt::free](interrupt::free) sections.)
//!
//! **Note: If multiple edge triggered interrupts are assigned to the same [level][InterruptLevel],
//!   it is not possible to detect which peripheral triggered the interrupt. Therefore all
//!   registered handlers will be called.**
//!
//! **Note: Edge triggered interrupts can be lost when triggered after handling of another edge
//!   triggered interrupt has started.**
//!
//! *Note: routines and variables in this module are stored in RAM because otherwise it may lead
//! to exceptions when the flash is programmed or erased while the interrupt is called.*
use crate::ram;

use crate::prelude::*;
pub use crate::target::{
    self,
    Interrupt::{self, *},
    DPORT,
};
use crate::Core::{self, APP, PRO};
use bare_metal::Nr;
pub use proc_macros::interrupt;
pub use xtensa_lx::interrupt::{self, free};

/// Interrupt errors
#[derive(Debug)]
pub enum Error {
    InvalidCore,
    InvalidCPUInterrupt,
    InvalidInterruptLevel,
    InternalInterruptsCannotBeMapped,
    InvalidInterrupt,
}

/// Interrupt level.
///
/// Valid levels are 1 through 7. Level 6 is typically used for debug exceptions
/// and level 7 for Non-Maskable Interrupts (NMI).
///
/// Level 0 is used to disable interrupts.
///
/// **Note: Level 7 (NMI) will not be disabled by the
/// [interrupt::free](interrupt::free) section. This risks race conditions in
/// various places.
/// **
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
pub struct InterruptLevel(pub usize);

#[ram]
const CPU_INTERRUPT_EDGE: u32 = 0b_0111_0000_0100_0000_0000_1100_1000_0000;

#[ram]
const INTERRUPT_EDGE: u128 =
    0b_0000_0000_0000_0000_0000_0000_0000_0000__0000_0000_0000_0000_0000_0000_0000_0011__1111_1100_0000_0000_0000_0000_0000_0000__0000_0000_0000_0000_0000_0000_0000_0000;

#[ram]
const CPU_INTERRUPT_INTERNAL: u32 = 0b_0010_0000_0000_0001_1000_1000_1100_0000;

#[ram]
const CPU_INTERRUPT_LEVELS: [u32; 8] = [
    0b_0000_0000_0000_0000_0000_0000_0000_0000, // Dummy level 0
    0b_0000_0000_0000_0110_0011_0111_1111_1111, // Level_1
    0b_0000_0000_0011_1000_0000_0000_0000_0000, // Level 2
    0b_0010_1000_1100_0000_1000_1000_0000_0000, // Level 3
    0b_0101_0011_0000_0000_0000_0000_0000_0000, // Level 4
    0b_1000_0100_0000_0001_0000_0000_0000_0000, // Level 5
    0b_0000_0000_0000_0000_0000_0000_0000_0000, // Level 6
    0b_0000_0000_0000_0000_0100_0000_0000_0000, // Level 7
];

#[ram]
fn interrupt_is_edge(interrupt: Interrupt) -> bool {
    [
        TG0_T0_EDGE_INTR,
        TG0_T1_EDGE_INTR,
        TG0_WDT_EDGE_INTR,
        TG0_LACT_EDGE_INTR,
        TG1_T0_EDGE_INTR,
        TG1_T1_EDGE_INTR,
        TG1_WDT_EDGE_INTR,
        TG1_LACT_EDGE_INTR,
    ]
    .contains(&interrupt)
}

#[ram]
fn interrupt_level_to_cpu_interrupt(
    interrupt_level: InterruptLevel,
    edge: bool,
) -> Result<CPUInterrupt, Error> {
    #[ram]
    const INTERRUPT_LEVEL_TO_CPU_INTERRUPT_EDGE: [Option<CPUInterrupt>; 8] = [
        Some(CPUInterrupt(6)),  // Disable (assign to internal interrupt)
        Some(CPUInterrupt(10)), // Level 1 edge triggered
        None,                   // Level 2 edge triggered not supported
        Some(CPUInterrupt(22)), // Level 3 edge triggered
        Some(CPUInterrupt(28)), // Level 4 edge triggered
        None,                   // Level 5 edge triggered not supported
        None,                   // Level 6 = Debug not supported for peripherals
        Some(CPUInterrupt(14)), // Level 7 = NMI edge triggered
    ];
    #[ram]
    const INTERRUPT_LEVEL_TO_CPU_INTERRUPT_LEVEL: [Option<CPUInterrupt>; 8] = [
        Some(CPUInterrupt(6)),  // Disable (assign to internal interrupt)
        Some(CPUInterrupt(0)),  // Level 1 level triggered
        Some(CPUInterrupt(19)), // Level 2 level triggered
        Some(CPUInterrupt(23)), // Level 3 level triggered
        Some(CPUInterrupt(24)), // Level 4 level triggered
        Some(CPUInterrupt(31)), // Level 5 level triggered
        None,                   // Level 6 = Debug not supported for peripherals
        Some(CPUInterrupt(14)), // Level 7 = NMI level triggered (not supported for peripherals,
                                //                                      forward to edge interrupt)
    ];
    if edge {
        INTERRUPT_LEVEL_TO_CPU_INTERRUPT_EDGE[interrupt_level.0].ok_or(Error::InvalidInterruptLevel)
    } else {
        INTERRUPT_LEVEL_TO_CPU_INTERRUPT_LEVEL[interrupt_level.0]
            .ok_or(Error::InvalidInterruptLevel)
    }
}

#[ram]
const CPU_INTERRUPT_USED_LEVELS: u32 = 0b_1001_0001_1100_1000_0100_0100_0000_0001;

#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
struct CPUInterrupt(pub usize);

fn cpu_interrupt_to_interrupt(cpu_interrupt: CPUInterrupt) -> Result<target::Interrupt, Error> {
    #[ram]
    const CPU_INTERRUPT_TO_INTERRUPT: [Option<target::Interrupt>; 32] = [
        None,
        None,
        None,
        None,
        None,
        None,
        Some(target::Interrupt::INTERNAL_TIMER0_INTR),
        Some(target::Interrupt::INTERNAL_SOFTWARE_LEVEL_1_INTR),
        None,
        None,
        None,
        Some(target::Interrupt::INTERNAL_PROFILING_INTR),
        None,
        None,
        None,
        Some(target::Interrupt::INTERNAL_TIMER1_INTR),
        Some(target::Interrupt::INTERNAL_TIMER2_INTR),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        Some(target::Interrupt::INTERNAL_SOFTWARE_LEVEL_3_INTR),
        None,
        None,
    ];

    CPU_INTERRUPT_TO_INTERRUPT[cpu_interrupt.0].ok_or(Error::InvalidCPUInterrupt)
}

#[ram]
fn interrupt_to_cpu_interrupt(interrupt: target::Interrupt) -> Result<CPUInterrupt, Error> {
    match interrupt {
        target::Interrupt::INTERNAL_TIMER0_INTR => Ok(CPUInterrupt(6)),
        target::Interrupt::INTERNAL_SOFTWARE_LEVEL_1_INTR => Ok(CPUInterrupt(7)),
        target::Interrupt::INTERNAL_PROFILING_INTR => Ok(CPUInterrupt(11)),
        target::Interrupt::INTERNAL_TIMER1_INTR => Ok(CPUInterrupt(15)),
        target::Interrupt::INTERNAL_TIMER2_INTR => Ok(CPUInterrupt(16)),
        target::Interrupt::INTERNAL_SOFTWARE_LEVEL_3_INTR => Ok(CPUInterrupt(29)),
        _ => Err(Error::InvalidCPUInterrupt),
    }
}

#[ram]
fn cpu_interrupt_to_level(cpu_interrupt: CPUInterrupt) -> InterruptLevel {
    #[ram]
    const CPU_INTERRUPT_TO_LEVEL: [usize; 32] = [
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 7, 3, 5, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 3, 4, 3,
        4, 5,
    ];
    InterruptLevel(CPU_INTERRUPT_TO_LEVEL[cpu_interrupt.0 as usize])
}

#[ram]
static mut INTERRUPT_LEVELS: [u128; 8] = [0u128; 8];

#[ram]
static INTERRUPT_LEVELS_MUTEX: CriticalSectionSpinLockMutex<bool> =
    CriticalSectionSpinLockMutex::new(false);

#[xtensa_lx_rt::interrupt(1)]
#[ram]
unsafe fn level_1_handler(level: u32) {
    handle_interrupts(level)
}

#[xtensa_lx_rt::interrupt(2)]
#[ram]
unsafe fn level_2_handler(level: u32) {
    handle_interrupts(level)
}

#[xtensa_lx_rt::interrupt(3)]
#[ram]
unsafe fn level_3_handler(level: u32) {
    handle_interrupts(level)
}

#[xtensa_lx_rt::interrupt(4)]
#[ram]
unsafe fn level_4_handler(level: u32) {
    handle_interrupts(level)
}

#[xtensa_lx_rt::interrupt(5)]
#[ram]
unsafe fn level_5_handler(level: u32) {
    handle_interrupts(level)
}

#[xtensa_lx_rt::interrupt(6)]
#[ram]
unsafe fn level_6_handler(level: u32) {
    handle_interrupts(level)
}

#[xtensa_lx_rt::interrupt(7)]
#[ram]
unsafe fn level_7_handler(level: u32) {
    handle_interrupts(level)
}

#[ram]
unsafe fn handle_interrupt(level: u32, interrupt: Interrupt) {
    let handler = target::__INTERRUPTS[interrupt.nr() as usize]._handler;
    if handler as *const _ == DefaultHandler as *const unsafe extern "C" fn() {
        DefaultHandler(level, interrupt);
    } else {
        handler();
    }
}

#[inline(always)]
#[ram]
unsafe fn handle_interrupts(level: u32) {
    let cpu_interrupt_mask =
        interrupt::get() & interrupt::get_mask() & CPU_INTERRUPT_LEVELS[level as usize];

    if cpu_interrupt_mask & CPU_INTERRUPT_INTERNAL != 0 {
        let cpu_interrupt_mask = cpu_interrupt_mask & CPU_INTERRUPT_INTERNAL;
        let cpu_interrupt_nr = cpu_interrupt_mask.trailing_zeros();

        if (cpu_interrupt_mask & CPU_INTERRUPT_EDGE) != 0 {
            interrupt::clear(1 << cpu_interrupt_nr);
        }

        // cpu_interrupt_to_interrupt can fail if interrupt already de-asserted: silently ignore
        if let Ok(interrupt) = cpu_interrupt_to_interrupt(CPUInterrupt(cpu_interrupt_nr as usize)) {
            handle_interrupt(level, interrupt);
        }
    } else {
        let cpu_interrupt_mask = cpu_interrupt_mask & !CPU_INTERRUPT_INTERNAL;

        if (cpu_interrupt_mask & CPU_INTERRUPT_EDGE) != 0 {
            let cpu_interrupt_mask = cpu_interrupt_mask & CPU_INTERRUPT_EDGE;
            let cpu_interrupt_nr = cpu_interrupt_mask.trailing_zeros();
            interrupt::clear(1 << cpu_interrupt_nr);

            // for edge interrupts cannot rely on the interrupt status register, therefore call all
            // registered handlers for current level
            let mut interrupt_mask = INTERRUPT_LEVELS[level as usize] & INTERRUPT_EDGE;
            loop {
                let interrupt_nr = interrupt_mask.trailing_zeros();
                if let Ok(interrupt) = target::Interrupt::try_from(interrupt_nr as u8) {
                    handle_interrupt(level, interrupt)
                } else {
                    break;
                }
                interrupt_mask &= !(1u128 << interrupt_nr);
            }
        } else {
            let interrupt_mask =
                get_interrupt_status(crate::get_core()) & INTERRUPT_LEVELS[level as usize];
            let interrupt_nr = interrupt_mask.trailing_zeros();

            // target::Interrupt::try_from can fail if interrupt already de-asserted: silently ignore
            if let Ok(interrupt) = target::Interrupt::try_from(interrupt_nr as u8) {
                handle_interrupt(level, interrupt);
            }
        }
    }
}

#[no_mangle]
#[ram]
extern "C" fn DefaultHandler(level: u32, interrupt: target::Interrupt) {
    crate::dprintln!("Unhandled interrupt (level {}): {:?}", level, interrupt);
}

/// Get status of peripheral interrupts
#[ram]
pub fn get_interrupt_status(core: Core) -> u128 {
    unsafe {
        match core {
            PRO => {
                ((*DPORT::ptr()).pro_intr_status_0.read().bits() as u128)
                    | ((*DPORT::ptr()).pro_intr_status_1.read().bits() as u128) << 32
                    | ((*DPORT::ptr()).pro_intr_status_2.read().bits() as u128) << 64
            }
            APP => {
                ((*DPORT::ptr()).app_intr_status_0.read().bits() as u128)
                    | ((*DPORT::ptr()).app_intr_status_1.read().bits() as u128) << 32
                    | ((*DPORT::ptr()).app_intr_status_2.read().bits() as u128) << 64
            }
        }
    }
}

/// Map an interrupt to a CPU interrupt
#[ram]
fn map_interrupt(
    core: crate::Core,
    interrupt: Interrupt,
    cpu_interrupt: CPUInterrupt,
) -> Result<(), Error> {
    if cpu_interrupt.0 >= 32 {
        return Err(Error::InvalidCPUInterrupt);
    }
    if interrupt.nr() >= Interrupt::INTERNAL_TIMER0_INTR.nr() {
        return Err(Error::InternalInterruptsCannotBeMapped);
    }
    unsafe {
        let base_reg = match core {
            crate::Core::PRO => (*DPORT::ptr()).pro_mac_intr_map.as_ptr(),
            crate::Core::APP => (*DPORT::ptr()).app_mac_intr_map.as_ptr(),
        };

        let reg = base_reg.add(interrupt.nr() as usize);
        *reg = cpu_interrupt.0 as u32;
    };
    Ok(())
}

/// Enable interrupt and set priority for a particular core
///
/// Valid levels are 1-7. Level 0 is used to disable the interrupt.
///
/// *Note: CPU internal interrupts can only be set on the current core.*
///
/// *Note: take care when mapping multiple peripheral edge triggered interrupts to the same level:
/// this will cause all handlers to be called.*
#[ram]
pub fn enable_with_priority(
    core: crate::Core,
    interrupt: Interrupt,
    level: InterruptLevel,
) -> Result<(), Error> {
    match interrupt_to_cpu_interrupt(interrupt) {
        Ok(cpu_interrupt) => {
            if core != crate::get_core() {
                return Err(Error::InvalidCore);
            }
            if level == InterruptLevel(0) {
                interrupt::disable_mask(1 << cpu_interrupt.0);
                return Ok(());
            } else if level == cpu_interrupt_to_level(cpu_interrupt) {
                unsafe { interrupt::enable_mask(1 << cpu_interrupt.0) };
                return Ok(());
            } else {
                return Err(Error::InvalidInterruptLevel);
            }
        }
        Err(_) => {
            let cpu_interrupt =
                interrupt_level_to_cpu_interrupt(level, interrupt_is_edge(interrupt))?;

            return (&INTERRUPT_LEVELS_MUTEX).lock(|_| unsafe {
                for i in 0..=7 {
                    INTERRUPT_LEVELS[i] &= !(1 << interrupt.nr());
                }
                INTERRUPT_LEVELS[level.0 as usize] |= 1 << interrupt.nr();

                interrupt::enable_mask(CPU_INTERRUPT_USED_LEVELS);

                map_interrupt(core, interrupt, cpu_interrupt)
            });
        }
    }
}

/// Enable interrupt
///
/// For CPU internal interrupts use the default level, for others use level 1
///
/// *Note: CPU internal interrupts can only be set on the current core.*
///
/// *Note: take care when mapping multiple peripheral edge triggered interrupts to the same level:
/// this will cause all handlers to be called.*
#[ram]
pub fn enable(interrupt: Interrupt) -> Result<(), Error> {
    match interrupt_to_cpu_interrupt(interrupt) {
        Ok(cpu_interrupt) => {
            unsafe { interrupt::enable_mask(1 << cpu_interrupt.0) };
            return Ok(());
        }
        Err(_) => enable_with_priority(crate::get_core(), interrupt, InterruptLevel(1)),
    }
}

/// Disable interrupt
#[ram]
pub fn disable(interrupt: Interrupt) -> Result<(), Error> {
    match interrupt_to_cpu_interrupt(interrupt) {
        Ok(cpu_interrupt) => {
            unsafe { interrupt::enable_mask(1 << cpu_interrupt.0) };
            return Ok(());
        }
        Err(_) => enable_with_priority(crate::get_core(), interrupt, InterruptLevel(0)),
    }
}

/// Trigger a (cross-)core interrupt
///
/// Valid interrupts are FROM_CPU_INTR[0-3],
/// INTERNAL_SOFTWARE_LEVEL_1_INTR and INTERNAL_SOFTWARE_LEVEL_3_INTR.
#[ram]
pub fn set_software_interrupt(interrupt: Interrupt) -> Result<(), Error> {
    unsafe {
        match interrupt {
            FROM_CPU_INTR0 => (*DPORT::ptr())
                .cpu_intr_from_cpu_0
                .write(|w| w.cpu_intr_from_cpu_0().set_bit()),
            FROM_CPU_INTR1 => (*DPORT::ptr())
                .cpu_intr_from_cpu_1
                .write(|w| w.cpu_intr_from_cpu_1().set_bit()),
            FROM_CPU_INTR2 => (*DPORT::ptr())
                .cpu_intr_from_cpu_2
                .write(|w| w.cpu_intr_from_cpu_2().set_bit()),
            FROM_CPU_INTR3 => (*DPORT::ptr())
                .cpu_intr_from_cpu_3
                .write(|w| w.cpu_intr_from_cpu_3().set_bit()),
            INTERNAL_SOFTWARE_LEVEL_1_INTR | INTERNAL_SOFTWARE_LEVEL_3_INTR => {
                interrupt::set(1 << interrupt_to_cpu_interrupt(interrupt)?.0)
            }

            _ => return Err(Error::InvalidInterrupt),
        }
    };
    Ok(())
}

/// Clear a (cross-)core interrupt
///
/// Valid interrupts are FROM_CPU_INTR[0-3],
/// INTERNAL_SOFTWARE_LEVEL_1_INTR and INTERNAL_SOFTWARE_LEVEL_3_INTR.
#[ram]
pub fn clear_software_interrupt(interrupt: Interrupt) -> Result<(), Error> {
    unsafe {
        match interrupt {
            FROM_CPU_INTR0 => (*DPORT::ptr())
                .cpu_intr_from_cpu_0
                .write(|w| w.cpu_intr_from_cpu_0().clear_bit()),
            FROM_CPU_INTR1 => (*DPORT::ptr())
                .cpu_intr_from_cpu_1
                .write(|w| w.cpu_intr_from_cpu_1().clear_bit()),
            FROM_CPU_INTR2 => (*DPORT::ptr())
                .cpu_intr_from_cpu_2
                .write(|w| w.cpu_intr_from_cpu_2().clear_bit()),
            FROM_CPU_INTR3 => (*DPORT::ptr())
                .cpu_intr_from_cpu_3
                .write(|w| w.cpu_intr_from_cpu_3().clear_bit()),
            INTERNAL_SOFTWARE_LEVEL_1_INTR | INTERNAL_SOFTWARE_LEVEL_3_INTR => {
                interrupt::clear(1 << interrupt_to_cpu_interrupt(interrupt)?.0)
            }

            _ => return Err(Error::InvalidInterrupt),
        }
    };
    Ok(())
}