|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | +#ifndef MICROPY_INCLUDED_MIMXRT_IRQ_H |
| 27 | +#define MICROPY_INCLUDED_MIMXRT_IRQ_H |
| 28 | + |
| 29 | +#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003) |
| 30 | + |
| 31 | +static inline uint32_t query_irq(void) { |
| 32 | + return __get_PRIMASK(); |
| 33 | +} |
| 34 | + |
| 35 | +static inline uint32_t raise_irq_pri(uint32_t pri) { |
| 36 | + uint32_t basepri = __get_BASEPRI(); |
| 37 | + // If non-zero, the processor does not process any exception with a |
| 38 | + // priority value greater than or equal to BASEPRI. |
| 39 | + // When writing to BASEPRI_MAX the write goes to BASEPRI only if either: |
| 40 | + // - Rn is non-zero and the current BASEPRI value is 0 |
| 41 | + // - Rn is non-zero and less than the current BASEPRI value |
| 42 | + pri <<= (8 - __NVIC_PRIO_BITS); |
| 43 | + __ASM volatile ("msr basepri_max, %0" : : "r" (pri) : "memory"); |
| 44 | + return basepri; |
| 45 | +} |
| 46 | + |
| 47 | +// "basepri" should be the value returned from raise_irq_pri |
| 48 | +static inline void restore_irq_pri(uint32_t basepri) { |
| 49 | + __set_BASEPRI(basepri); |
| 50 | +} |
| 51 | + |
| 52 | +// IRQ priority definitions. |
| 53 | +// |
| 54 | +// Lower number implies higher interrupt priority. |
| 55 | +// |
| 56 | +// The default priority grouping used in this port is NVIC_PRIORITYGROUP_4. |
| 57 | +// This corresponds to 4 bits for the priority field and 0 bits for the |
| 58 | +// sub-priority field (which means that for all intents and purposes the |
| 59 | +// sub-priorities below are ignored). |
| 60 | +// |
| 61 | +// While a given interrupt is being processed, only higher priority (lower number) |
| 62 | +// interrupts will preempt a given interrupt. If sub-priorities are active |
| 63 | +// then the sub-priority determines the order that pending interrupts of |
| 64 | +// a given priority are executed. This is only meaningful if 2 or more |
| 65 | +// interrupts of the same priority are pending at the same time. |
| 66 | +// |
| 67 | +// The following interrupts are arranged from highest priority to lowest |
| 68 | +// priority to make it a bit easier to figure out. |
| 69 | + |
| 70 | +#define IRQ_PRI_SYSTICK NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 0, 0) |
| 71 | + |
| 72 | +#define IRQ_PRI_OTG_HS NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 6, 0) |
| 73 | + |
| 74 | +#define IRQ_PRI_EXTINT NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 14, 0) |
| 75 | + |
| 76 | +// PENDSV should be at the lowst priority so that other interrupts complete |
| 77 | +// before exception is raised. |
| 78 | +#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 15, 0) |
| 79 | + |
| 80 | +#endif // MICROPY_INCLUDED_MIMXRT_IRQ_H |
0 commit comments