File tree 4 files changed +29
-4
lines changed
common-hal/microcontroller 4 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,11 @@ SECTIONS
51
51
*(.text.tu_edpt_dir)
52
52
*(.text.tu_fifo_empty)
53
53
*(.text.usbd_edpt_busy)
54
+ *(.text.usb_irq_handler)
55
+ *(.text.supervisor_tick)
56
+ *(.text.port_get_raw_ticks)
57
+ *(.text.__modsi3)
58
+ *(.text.__udivsi3)
54
59
*(.text.irq_getmask)
55
60
*(.text.irq_setmask)
56
61
*(.text.irq_pending)
Original file line number Diff line number Diff line change @@ -59,12 +59,15 @@ void common_hal_mcu_delay_us(uint32_t delay) {
59
59
60
60
volatile uint32_t nesting_count = 0 ;
61
61
62
+ __attribute__((section (".ramtext" )))
62
63
void common_hal_mcu_disable_interrupts (void ) {
63
- irq_setie (0 );
64
- // __DMB();
64
+ if (nesting_count == 0 ) {
65
+ irq_setie (0 );
66
+ }
65
67
nesting_count ++ ;
66
68
}
67
69
70
+ __attribute__((section (".ramtext" )))
68
71
void common_hal_mcu_enable_interrupts (void ) {
69
72
if (nesting_count == 0 ) {
70
73
// This is very very bad because it means there was mismatched disable/enables so we
Original file line number Diff line number Diff line change @@ -44,16 +44,31 @@ void mp_hal_delay_us(mp_uint_t delay) {
44
44
45
45
extern void SysTick_Handler (void );
46
46
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
+
47
57
__attribute__((section (".ramtext" )))
48
58
void isr (void ) {
49
59
uint8_t irqs = irq_pending () & irq_getmask ();
50
60
61
+ // Increase the "nesting count". Note: This should be going from 0 -> 1.
62
+ nesting_count += 1 ;
51
63
#ifdef CFG_TUSB_MCU
52
64
if (irqs & (1 << USB_INTERRUPT ))
53
65
usb_irq_handler ();
54
66
#endif
55
67
if (irqs & (1 << TIMER0_INTERRUPT ))
56
68
SysTick_Handler ();
69
+
70
+ // Decrease the "nesting count". Note: This should be going from 1 -> 0.
71
+ nesting_count -= 1 ;
57
72
}
58
73
59
74
mp_uint_t cpu_get_regs_and_sp (mp_uint_t * regs ) {
Original file line number Diff line number Diff line change 32
32
#include "irq.h"
33
33
#include "csr.h"
34
34
35
+ #include "shared-bindings/microcontroller/__init__.h"
36
+
35
37
// Global millisecond tick count. 1024 per second because most RTCs are clocked with 32.768khz
36
38
// crystals.
37
39
volatile uint64_t raw_ticks = 0 ;
@@ -129,9 +131,9 @@ uint32_t port_get_saved_word(void) {
129
131
130
132
uint64_t port_get_raw_ticks (uint8_t * subticks ) {
131
133
// 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 ( );
133
135
uint64_t raw_tick_snapshot = raw_ticks ;
134
- irq_setie (true );
136
+ common_hal_mcu_enable_interrupts ( );
135
137
return raw_tick_snapshot ;
136
138
}
137
139
You can’t perform that action at this time.
0 commit comments