Skip to content

Commit f3e5441

Browse files
committed
litex: ensure we don't re-enable interrups during ISR
During an interrupt handler, interrupts are implicitly disabled. They will be re-enabled when the interrupt handler returns. Due to some changes that were made, varous calls will re-enable interrupts after they're finished. Examples of this include calling `CALLBACK_CRITICAL_END` and getting the number of ticks with `port_get_raw_ticks()`. This patch prevents this from happening by doing two things: 1. Use standard calls in `port_get_raw_ticks()` to disable and re-enable interrupts, preventing nesting issues, and 2. Increase the nesting count inside `isr()`, reflecting the implicit call that is made by hardware when an interrupt is handled This helps to address adafruit#3841. Signed-off-by: Sean Cross <[email protected]>
1 parent 42ca57f commit f3e5441

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ports/litex/mphalport.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,31 @@ void mp_hal_delay_us(mp_uint_t delay) {
4444

4545
extern void SysTick_Handler(void);
4646

47+
// This value contains the number of times "common_hal_mcu_disable_interrupts()"
48+
// has been called without calling "common_hal_mcu_enable_interrupts()". Since
49+
// this is the interrupt handler, that means we're handling an interrupt, so
50+
// this value should be `0`.
51+
//
52+
// Interrupts should already be disabled when this handler is running, which means
53+
// this value is logically already `1`. If we didn't do this, then interrupts would
54+
// be prematurely enabled by interrupt handlers that enable and disable interrupts.
55+
extern volatile uint32_t nesting_count;
56+
4757
__attribute__((section(".ramtext")))
4858
void isr(void) {
4959
uint8_t irqs = irq_pending() & irq_getmask();
5060

61+
// Increase the "nesting count". Note: This should be going from 0 -> 1.
62+
nesting_count += 1;
5163
#ifdef CFG_TUSB_MCU
5264
if (irqs & (1 << USB_INTERRUPT))
5365
usb_irq_handler();
5466
#endif
5567
if (irqs & (1 << TIMER0_INTERRUPT))
5668
SysTick_Handler();
69+
70+
// Decrease the "nesting count". Note: This should be going from 1 -> 0.
71+
nesting_count -= 1;
5772
}
5873

5974
mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) {

ports/litex/supervisor/port.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "irq.h"
3333
#include "csr.h"
3434

35+
#include "shared-bindings/microcontroller/__init__.h"
36+
3537
// Global millisecond tick count. 1024 per second because most RTCs are clocked with 32.768khz
3638
// crystals.
3739
volatile uint64_t raw_ticks = 0;
@@ -129,9 +131,9 @@ uint32_t port_get_saved_word(void) {
129131

130132
uint64_t port_get_raw_ticks(uint8_t* subticks) {
131133
// Reading 64 bits may take two loads, so turn of interrupts while we do it.
132-
irq_setie(false);
134+
common_hal_mcu_disable_interrupts();
133135
uint64_t raw_tick_snapshot = raw_ticks;
134-
irq_setie(true);
136+
common_hal_mcu_enable_interrupts();
135137
return raw_tick_snapshot;
136138
}
137139

0 commit comments

Comments
 (0)