Skip to content

Commit 4f9adfa

Browse files
committed
Re-evaluate interrupt priority values
Can be redefined thanks variant.h or build_opt.h Fix stm32duino#472 Signed-off-by: Frederic.Pillon <[email protected]> [USBD] Allow IRQ priority redefinition Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 4c29c3a commit 4f9adfa

File tree

12 files changed

+74
-28
lines changed

12 files changed

+74
-28
lines changed

cores/arduino/stm32/interrupt.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
*
3636
******************************************************************************
3737
*/
38-
#include "stm32_def.h"
3938
#include "interrupt.h"
4039

4140
/* Private Types */
@@ -163,8 +162,8 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_
163162

164163
gpio_irq_conf[id].callback = callback;
165164

166-
// Enable and set Button EXTI Interrupt to the lowest priority
167-
HAL_NVIC_SetPriority(gpio_irq_conf[id].irqnb, 0x06, 0);
165+
// Enable and set EXTI Interrupt
166+
HAL_NVIC_SetPriority(gpio_irq_conf[id].irqnb, EXTI_IRQ_PRIO, EXTI_IRQ_SUBPRIO);
168167
HAL_NVIC_EnableIRQ(gpio_irq_conf[id].irqnb);
169168
}
170169

cores/arduino/stm32/interrupt.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,23 @@
4040
#define __INTERRUPT_H
4141

4242
/* Includes ------------------------------------------------------------------*/
43-
#include "stm32_def.h"
44-
#include "PinNames.h"
43+
#include "variant.h"
4544

4645
#if defined(STM32F3xx)
4746
#define EXTI2_IRQn EXTI2_TSC_IRQn
4847
#endif
4948

49+
#ifndef EXTI_IRQ_PRIO
50+
#if (__CORTEX_M == 0x00U)
51+
#define EXTI_IRQ_PRIO 3
52+
#else
53+
#define EXTI_IRQ_PRIO 6
54+
#endif /* __CORTEX_M */
55+
#endif /* EXTI_IRQ_PRIO */
56+
#ifndef EXTI_IRQ_SUBPRIO
57+
#define EXTI_IRQ_SUBPRIO 0
58+
#endif
59+
5060
#ifdef __cplusplus
5161
#include <functional>
5262

cores/arduino/stm32/rtc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
335335
HAL_RTCEx_EnableBypassShadow(&RtcHandle);
336336
#endif /* !STM32F1xx && !STM32F2xx */
337337

338-
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 2, 0);
338+
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
339339
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
340340
/* Ensure backup domain is enabled */
341341
enableBackupRegister();

cores/arduino/stm32/rtc.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define __RTC_H
4141

4242
/* Includes ------------------------------------------------------------------*/
43-
#include <stdbool.h>
43+
#include "variant.h"
4444
#include "backup.h"
4545
#include "clock.h"
4646

@@ -77,6 +77,14 @@ typedef enum {
7777
typedef void(*voidCallbackPtr)(void *);
7878

7979
/* Exported constants --------------------------------------------------------*/
80+
/* Interrupt priority */
81+
#ifndef RTC_IRQ_PRIO
82+
#define RTC_IRQ_PRIO 2
83+
#endif
84+
#ifndef RTC_IRQ_SUBPRIO
85+
#define RTC_IRQ_SUBPRIO 0
86+
#endif
87+
8088

8189
#define HSE_RTC_MAX 1000000U
8290

cores/arduino/stm32/timer.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base)
142142
{
143143
timer_enable_clock(htim_base);
144144

145-
HAL_NVIC_SetPriority(getTimerIrq(htim_base->Instance), 15, 0);
145+
HAL_NVIC_SetPriority(getTimerIrq(htim_base->Instance), TIM_IRQ_PRIO, TIM_IRQ_SUBPRIO);
146146
HAL_NVIC_EnableIRQ(getTimerIrq(htim_base->Instance));
147147
}
148148

@@ -1131,7 +1131,7 @@ void attachIntHandleOC(stimer_t *obj, void (*irqHandle)(void), uint16_t timChann
11311131
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
11321132
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
11331133
#endif
1134-
HAL_NVIC_SetPriority(getTimerIrq(obj->timer), 14, 0);
1134+
HAL_NVIC_SetPriority(getTimerIrq(obj->timer), TIM_IRQ_PRIO, TIM_IRQ_SUBPRIO);
11351135
HAL_NVIC_EnableIRQ(getTimerIrq(obj->timer));
11361136

11371137
if (HAL_TIM_OC_Init(handle) != HAL_OK) {

cores/arduino/stm32/timer.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
#define __TIMER_H
4141

4242
/* Includes ------------------------------------------------------------------*/
43-
#include "stm32_def.h"
44-
#include "PeripheralPins.h"
43+
#include "variant.h"
4544

4645
#ifdef __cplusplus
4746
extern "C" {
@@ -77,6 +76,17 @@ struct timer_s {
7776
};
7877

7978
/* Exported constants --------------------------------------------------------*/
79+
#ifndef TIM_IRQ_PRIO
80+
#if (__CORTEX_M == 0x00U)
81+
#define TIM_IRQ_PRIO 3
82+
#else
83+
#define TIM_IRQ_PRIO 14
84+
#endif /* __CORTEX_M */
85+
#endif /* TIM_IRQ_PRIO */
86+
#ifndef TIM_IRQ_SUBPRIO
87+
#define TIM_IRQ_SUBPRIO 0
88+
#endif
89+
8090
#define MAX_FREQ 65535
8191

8292
#if defined(TIM1_BASE) && !defined(TIM1_IRQn)

cores/arduino/stm32/twi.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
******************************************************************************
3737
*/
3838
#include "core_debug.h"
39-
#include "stm32_def.h"
4039
#include "twi.h"
4140
#include "PinAF_STM32F1.h"
4241

@@ -198,10 +197,10 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
198197

199198
handle->State = HAL_I2C_STATE_RESET;
200199

201-
HAL_NVIC_SetPriority(obj->irq, 0, 1);
200+
HAL_NVIC_SetPriority(obj->irq, I2C_IRQ_PRIO, I2C_IRQ_SUBPRIO);
202201
HAL_NVIC_EnableIRQ(obj->irq);
203202
#if !defined(STM32F0xx) && !defined(STM32L0xx)
204-
HAL_NVIC_SetPriority(obj->irqER, 0, 1);
203+
HAL_NVIC_SetPriority(obj->irqER, I2C_IRQ_PRIO, I2C_IRQ_SUBPRIO);
205204
HAL_NVIC_EnableIRQ(obj->irqER);
206205
#endif // !defined(STM32F0xx) && !defined(STM32L0xx)
207206

cores/arduino/stm32/twi.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
#define __TWI_H__
4242

4343
/* Includes ------------------------------------------------------------------*/
44-
#include "stm32_def.h"
45-
#include "PeripheralPins.h"
44+
#include "variant.h"
4645

4746
#ifdef __cplusplus
4847
extern "C" {
@@ -52,6 +51,14 @@ extern "C" {
5251
/* offsetof is a gcc built-in function, this is the manual implementation */
5352
#define OFFSETOF(type, member) ((uint32_t) (&(((type *)(0))->member)))
5453

54+
/* Interrupt priority */
55+
#ifndef I2C_IRQ_PRIO
56+
#define I2C_IRQ_PRIO 2
57+
#endif
58+
#ifndef I2C_IRQ_SUBPRIO
59+
#define I2C_IRQ_SUBPRIO 0
60+
#endif
61+
5562
/* I2C Tx/Rx buffer size */
5663
#define I2C_TXRX_BUFFER_SIZE 32
5764

cores/arduino/stm32/uart.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *))
703703
HAL_UART_Receive_IT(uart_handlers[obj->index], &(obj->recv), 1);
704704

705705
/* Enable interrupt */
706-
HAL_NVIC_SetPriority(obj->irq, 0, 1);
706+
HAL_NVIC_SetPriority(obj->irq, UART_IRQ_PRIO, UART_IRQ_SUBPRIO);
707707
HAL_NVIC_EnableIRQ(obj->irq);
708708
}
709709

@@ -728,7 +728,7 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
728728
HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1);
729729

730730
/* Enable interrupt */
731-
HAL_NVIC_SetPriority(obj->irq, 0, 2);
731+
HAL_NVIC_SetPriority(obj->irq, UART_IRQ_PRIO, UART_IRQ_SUBPRIO);
732732
HAL_NVIC_EnableIRQ(obj->irq);
733733
}
734734

cores/arduino/stm32/uart.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#define __UART_H
3939

4040
/* Includes ------------------------------------------------------------------*/
41-
#include "stm32_def.h"
4241
#include "variant.h"
4342

4443
#ifdef __cplusplus
@@ -49,6 +48,13 @@ extern "C" {
4948
#define serial_t void*
5049
#else
5150

51+
#ifndef UART_IRQ_PRIO
52+
#define UART_IRQ_PRIO 1
53+
#endif
54+
#ifndef UART_IRQ_SUBPRIO
55+
#define UART_IRQ_SUBPRIO 0
56+
#endif
57+
5258
/* Exported types ------------------------------------------------------------*/
5359
typedef struct serial_s serial_t;
5460

cores/arduino/stm32/usb/usbd_conf.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
8989
#endif
9090

9191
#if defined(STM32WBxx)
92-
HAL_NVIC_SetPriority(USB_HP_IRQn, 5, 0);
92+
HAL_NVIC_SetPriority(USB_HP_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
9393
HAL_NVIC_EnableIRQ(USB_HP_IRQn);
94-
HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
94+
HAL_NVIC_SetPriority(USB_LP_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
9595
HAL_NVIC_EnableIRQ(USB_LP_IRQn);
9696
#else
9797
/* Set USB FS Interrupt priority */
98-
HAL_NVIC_SetPriority(USB_IRQn, 5, 0);
98+
HAL_NVIC_SetPriority(USB_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
9999

100100
/* Enable USB FS Interrupt */
101101
HAL_NVIC_EnableIRQ(USB_IRQn);
@@ -134,7 +134,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
134134
__HAL_RCC_USB_OTG_FS_CLK_ENABLE();
135135

136136
/* Set USB FS Interrupt priority */
137-
HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
137+
HAL_NVIC_SetPriority(OTG_FS_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
138138

139139
/* Enable USB FS Interrupt */
140140
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
@@ -146,7 +146,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
146146
__HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT();
147147
#if !defined(STM32L4xx)
148148
/* Set EXTI Wakeup Interrupt priority */
149-
HAL_NVIC_SetPriority(OTG_FS_WKUP_IRQn, 0, 0);
149+
HAL_NVIC_SetPriority(OTG_FS_WKUP_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
150150

151151
/* Enable EXTI Interrupt */
152152
HAL_NVIC_EnableIRQ(OTG_FS_WKUP_IRQn);
@@ -169,8 +169,8 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
169169
/* Enable USB HS Clocks */
170170
__HAL_RCC_USB_OTG_HS_CLK_ENABLE();
171171

172-
/* Set USBHS Interrupt to the lowest priority */
173-
HAL_NVIC_SetPriority(OTG_HS_IRQn, 5, 0);
172+
/* Set USBHS Interrupt priority */
173+
HAL_NVIC_SetPriority(OTG_HS_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
174174

175175
/* Enable USB HS Interrupt */
176176
HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
@@ -182,7 +182,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
182182
__HAL_USB_OTG_HS_WAKEUP_EXTI_ENABLE_IT();
183183

184184
/* Set EXTI Wakeup Interrupt priority */
185-
HAL_NVIC_SetPriority(OTG_HS_WKUP_IRQn, 0, 0);
185+
HAL_NVIC_SetPriority(OTG_HS_WKUP_IRQn, USBD_IRQ_PRIO, USBD_IRQ_SUBPRIO);
186186

187187
/* Enable EXTI Interrupt */
188188
HAL_NVIC_EnableIRQ(OTG_HS_WKUP_IRQn);

cores/arduino/stm32/usb/usbd_conf.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extern "C" {
2727

2828
#ifdef USBCON
2929
/* Includes ------------------------------------------------------------------*/
30-
#include "stm32_def.h"
3130
#include "variant.h"
3231

3332
#if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE)
@@ -105,6 +104,14 @@ extern "C" {
105104
#define USBD_AUDIO_FREQ 22100U
106105
#endif /* USBD_AUDIO_FREQ */
107106

107+
/* Interrupt priority */
108+
#ifndef USBD_IRQ_PRIO
109+
#define USBD_IRQ_PRIO 1
110+
#endif /* USBD_IRQ_PRIO */
111+
#ifndef USBD_IRQ_SUBPRIO
112+
#define USBD_IRQ_SUBPRIO 0
113+
#endif /* USBD_IRQ_SUBPRIO */
114+
108115
/* Memory management macros */
109116
#ifndef USBD_malloc
110117
#define USBD_malloc malloc

0 commit comments

Comments
 (0)