Skip to content

Commit e4bfc88

Browse files
committed
ports/mimxrt: Add IRQ priority definitions header.
Following other ports, IRQ priorities and related functions are moved to their own header, to simplify mpconfigport.h. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 706ddc6 commit e4bfc88

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

ports/mimxrt/board_init.c

Lines changed: 2 additions & 1 deletion
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

Lines changed: 80 additions & 0 deletions
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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,6 @@ __attribute__((always_inline)) static inline uint32_t disable_irq(void) {
154154
return state;
155155
}
156156

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

ports/mimxrt/mphalport.h

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 3 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy