From 8d1508487285bf379d4e9235fe247a0a29cc777e Mon Sep 17 00:00:00 2001 From: Jacob Dahl Date: Fri, 12 Jun 2026 19:00:19 -0600 Subject: [PATCH] perf(main): write dshot NVIC priorities only on actual change The dshot telemetry priority swap wrote three or four NVIC registers on every main loop iteration even though the scheme flips rarely. Track the current scheme in input_priority_is_high and skip the writes when nothing changed. The 0xFF boot sentinel forces the first pass to write, preserving the old boot behavior exactly. input_priority_is_high is a static local scoped to the priority block rather than a file global, so it also disappears on the MCU_G031 and NEED_INPUT_READY targets that compile the block out. --- Src/main.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Src/main.c b/Src/main.c index fc22dfae9..86dd98ab4 100644 --- a/Src/main.c +++ b/Src/main.c @@ -2038,8 +2038,13 @@ if(zero_crosses < 5){ } #if !defined(MCU_G031) && !defined(NEED_INPUT_READY) + // only touch the NVIC when the priority scheme actually changes + static uint8_t input_priority_is_high = 0xFF; // unknown at boot, first pass sets it + uint8_t want_input_priority = dshot_telemetry && (commutation_interval > DSHOT_PRIORITY_THRESHOLD); + if (want_input_priority != input_priority_is_high) { + input_priority_is_high = want_input_priority; #ifdef NXP - if (dshot_telemetry && (commutation_interval > DSHOT_PRIORITY_THRESHOLD)) { + if (want_input_priority) { NVIC_SetPriority(IC_DMA_IRQ_NAME, 0); NVIC_SetPriority(COM_TIMER_IRQ, 1); NVIC_SetPriority(COMP0_IRQ, 1); @@ -2051,7 +2056,7 @@ if(zero_crosses < 5){ NVIC_SetPriority(COMP1_IRQ, 0); } #else - if (dshot_telemetry && (commutation_interval > DSHOT_PRIORITY_THRESHOLD)) { + if (want_input_priority) { NVIC_SetPriority(IC_DMA_IRQ_NAME, 0); NVIC_SetPriority(COM_TIMER_IRQ, 1); NVIC_SetPriority(COMPARATOR_IRQ, 1); @@ -2061,6 +2066,7 @@ if(zero_crosses < 5){ NVIC_SetPriority(COMPARATOR_IRQ, 0); } #endif + } #endif if (send_telemetry) { #ifdef USE_SERIAL_TELEMETRY