Skip to content

Commit 3f5976e

Browse files
iabdalkaderdpgeorge
authored andcommitted
mimxrt/irq: Move all IRQ related definitions to dedicated header.
Following other ports, IRQ priorities and related functions are moved to their own header, to simplify mpconfigport.h. Signed-off-by: iabdalkader <[email protected]>
1 parent a453b4f commit 3f5976e

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

ports/mimxrt/board_init.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include CLOCK_CONFIG_H
4141
#include "modmachine.h"
42+
#include "irq.h"
4243

4344
const uint8_t dcd_data[] = { 0x00 };
4445

@@ -63,7 +64,7 @@ void board_init(void) {
6364

6465
// 1ms tick timer
6566
SysTick_Config(SystemCoreClock / 1000);
66-
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 0, 0));
67+
NVIC_SetPriority(SysTick_IRQn, IRQ_PRI_SYSTICK);
6768

6869
// USB0
6970
usb_phy0_init(0b0111, 0b0110, 0b0110); // Configure nominal values for D_CAL and TXCAL45DP/DN

ports/mimxrt/irq.h

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

ports/mimxrt/mpconfigport.h

-17
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,6 @@ __attribute__((always_inline)) static inline uint32_t disable_irq(void) {
152152
return state;
153153
}
154154

155-
static inline uint32_t raise_irq_pri(uint32_t pri) {
156-
uint32_t basepri = __get_BASEPRI();
157-
// If non-zero, the processor does not process any exception with a
158-
// priority value greater than or equal to BASEPRI.
159-
// When writing to BASEPRI_MAX the write goes to BASEPRI only if either:
160-
// - Rn is non-zero and the current BASEPRI value is 0
161-
// - Rn is non-zero and less than the current BASEPRI value
162-
pri <<= (8 - __NVIC_PRIO_BITS);
163-
__ASM volatile ("msr basepri_max, %0" : : "r" (pri) : "memory");
164-
return basepri;
165-
}
166-
167-
// "basepri" should be the value returned from raise_irq_pri
168-
static inline void restore_irq_pri(uint32_t basepri) {
169-
__set_BASEPRI(basepri);
170-
}
171-
172155
#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq()
173156
#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state)
174157

ports/mimxrt/mphalport.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ticks.h"
3232
#include "py/ringbuf.h"
3333
#include "pin.h"
34+
#include "irq.h"
3435
#include "fsl_clock.h"
3536

3637
#define MICROPY_HAL_VERSION "2.8.0"

ports/mimxrt/pendsv.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828

2929
#include "py/runtime.h"
3030
#include "pendsv.h"
31-
32-
#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003)
33-
#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 15, 0)
31+
#include "irq.h"
3432

3533
#if defined(PENDSV_DISPATCH_NUM_SLOTS)
3634
pendsv_dispatch_t pendsv_dispatch_table[PENDSV_DISPATCH_NUM_SLOTS];

0 commit comments

Comments
 (0)