From a882b709d1af5973778bb9f112395857266bab38 Mon Sep 17 00:00:00 2001 From: Juan Antonio Date: Sat, 16 Dec 2023 00:54:00 +0100 Subject: [PATCH] Put the core to rest, do not spin --- Makefile | 5 ++++- driver/bk4819.c | 15 ++++++++------- driver/system.c | 8 ++++---- driver/systick.c | 35 +++++++++++++++++++++++------------ main.c | 47 ++++++++++++++++++++++++++++++----------------- 5 files changed, 69 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 58d7c8480..5629b272b 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ ENABLE_CLANG ?= 0 ENABLE_SWD ?= 0 ENABLE_OVERLAY ?= 0 ENABLE_LTO ?= 1 - +ENABLE_WFI ?= 1 # ---- STOCK QUANSHENG FERATURES ---- ENABLE_UART ?= 1 ENABLE_AIRCOPY ?= 0 @@ -365,6 +365,9 @@ ifeq ($(ENABLE_FLASHLIGHT),1) CFLAGS += -DENABLE_FLASHLIGHT endif +ifeq ($(ENABLE_WFI),1) + CFLAGS += -DENABLE_WFI +endif LDFLAGS = LDFLAGS += -z noexecstack -mcpu=cortex-m0 -nostartfiles -Wl,-T,firmware.ld -Wl,--gc-sections diff --git a/driver/bk4819.c b/driver/bk4819.c index bc01a7f90..f7ac28a52 100644 --- a/driver/bk4819.c +++ b/driver/bk4819.c @@ -16,13 +16,15 @@ #include // NULL -#include "audio.h" +#include "../audio.h" +#include "../bsp/dp32g030/gpio.h" +#include "../bsp/dp32g030/portcon.h" + #include "bk4819.h" -#include "bsp/dp32g030/gpio.h" -#include "bsp/dp32g030/portcon.h" -#include "driver/gpio.h" -#include "driver/system.h" -#include "driver/systick.h" +#include "gpio.h" +#include "system.h" +#include "systick.h" + #ifndef ARRAY_SIZE #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) @@ -127,7 +129,6 @@ static uint16_t BK4819_ReadU16(void) PORTCON_PORTC_IE = (PORTCON_PORTC_IE & ~PORTCON_PORTC_IE_C2_MASK) | PORTCON_PORTC_IE_C2_BITS_ENABLE; GPIOC->DIR = (GPIOC->DIR & ~GPIO_DIR_2_MASK) | GPIO_DIR_2_BITS_INPUT; SYSTICK_DelayUs(1); - Value = 0; for (i = 0; i < 16; i++) { diff --git a/driver/system.c b/driver/system.c index abcfb06ca..813ddec26 100644 --- a/driver/system.c +++ b/driver/system.c @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "bsp/dp32g030/pmu.h" -#include "bsp/dp32g030/syscon.h" -#include "driver/system.h" -#include "driver/systick.h" +#include "../bsp/dp32g030/pmu.h" +#include "../bsp/dp32g030/syscon.h" +#include "system.h" +#include "systick.h" void SYSTEM_DelayMs(uint32_t Delay) { diff --git a/driver/systick.c b/driver/systick.c index 5971d74f6..4542e9236 100644 --- a/driver/systick.c +++ b/driver/systick.c @@ -15,8 +15,8 @@ */ #include "ARMCM0.h" -#include "driver/systick.h" -#include "misc.h" +#include "systick.h" +#include "../misc.h" // 0x20000324 static uint32_t gTickMultiplier; @@ -29,17 +29,28 @@ void SYSTICK_Init(void) void SYSTICK_DelayUs(uint32_t Delay) { - const uint32_t ticks = Delay * gTickMultiplier; - uint32_t i = 0; - uint32_t Start = SysTick->LOAD; - uint32_t Previous = SysTick->VAL; + const uint32_t ticks = Delay * gTickMultiplier; + uint32_t elapsed_ticks = 0; + uint32_t Start = SysTick->LOAD; + uint32_t Previous = SysTick->VAL; do { uint32_t Current; - uint32_t Delta; - while ((Current = SysTick->VAL) == Previous) {} - Delta = (Current < Previous) ? -Current : Start - Current; - i += Delta + Previous; + + do { + Current = SysTick->VAL; + } while (Current == Previous); + + uint32_t Delta = ((Current < Previous) ? - Current : Start - Current); + + elapsed_ticks += Delta + Previous; + Previous = Current; - } while (i < ticks); -} +#ifdef ENABLE_WFI + if (((int32_t)(ticks - elapsed_ticks)) >= 48 * 10000) { + __WFI(); + } +#endif + + } while (elapsed_ticks < ticks); +} diff --git a/main.c b/main.c index d35a32cd3..8f9c2fd82 100644 --- a/main.c +++ b/main.c @@ -14,18 +14,27 @@ * limitations under the License. */ +#include #include #include // NULL +#include "ARMCM0.h" + #ifdef ENABLE_AM_FIX #include "am_fix.h" #endif +#include "misc.h" +#include "radio.h" +#include "settings.h" +#include "version.h" +#include "board.h" +#include "audio.h" + #include "app/app.h" #include "app/dtmf.h" -#include "audio.h" #include "bsp/dp32g030/gpio.h" #include "bsp/dp32g030/syscon.h" -#include "board.h" + #include "driver/backlight.h" #include "driver/bk4819.h" #include "driver/gpio.h" @@ -34,16 +43,13 @@ #ifdef ENABLE_UART #include "driver/uart.h" #endif + #include "helper/battery.h" #include "helper/boot.h" -#include "misc.h" -#include "radio.h" -#include "settings.h" + #include "ui/lock.h" #include "ui/welcome.h" #include "ui/menu.h" -#include "version.h" - void _putchar(__attribute__((unused)) char c) { @@ -102,9 +108,9 @@ void Main(void) BATTERY_GetReadings(false); - #ifdef ENABLE_AM_FIX - AM_fix_init(); - #endif +#ifdef ENABLE_AM_FIX + AM_fix_init(); +#endif const BOOT_Mode_t BootMode = BOOT_GetMode(); @@ -215,18 +221,25 @@ void Main(void) // ****************** } - while (1) - { + while (true) { APP_Update(); - - if (gNextTimeslice) - { +#ifdef ENABLE_WFI + /* + * Do not spin mindlessly. Wait until the next interrupt is raised. + * Could be SysTick, then gNextTimeslice would be true, or maybe there's + * something else that is seeking attention that would or should be handled + * by APP_Update(), who knows... + * Longest-case-scenario: we are woken up by SysTick, and this loop runs at 10Mhz. + * Should save quite a bit of battery, I guess. + */ + __WFI(); +#endif + if (gNextTimeslice) { APP_TimeSlice10ms(); gNextTimeslice = false; } - if (gNextTimeslice_500ms) - { + if (gNextTimeslice_500ms) { APP_TimeSlice500ms(); gNextTimeslice_500ms = false; }