From a5afbec143bf5cf7253a4b75e396ea2dea0aaf97 Mon Sep 17 00:00:00 2001 From: Petr Nejedly Date: Mon, 10 Aug 2020 20:37:35 -0700 Subject: [PATCH 1/3] Nenik: Fix scheduling at the end of delay(), #537 See https://devzone.nordicsemi.com/f/nordic-q-a/63828/vtaskdelay-on-nrf52-freertos-port-wastes-cpu-power for reference --- .../portable/CMSIS/nrf52/port_cmsis_systick.c | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c index 21dec1fc..ec01aa10 100644 --- a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c +++ b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c @@ -227,9 +227,27 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) diff = xExpectedIdleTime; } - if (diff > 0) + // nRF-provided fix for delay() wakeup 1ms spin-loop power waste + // See https://devzone.nordicsemi.com/f/nordic-q-a/63828/vtaskdelay-on-nrf52-freertos-port-wastes-cpu-power + BaseType_t switch_req = pdFALSE; + + if (diff > 1) { - vTaskStepTick(diff); + vTaskStepTick(diff - 1); + switch_req = xTaskIncrementTick(); + } + else if (diff == 1) + { + switch_req = xTaskIncrementTick(); + } + + /* Increment the RTOS tick as usual which checks if there is a need for rescheduling */ + if ( switch_req != pdFALSE ) + { + /* A context switch is required. Context switching is performed in + the PendSV interrupt. Pend the PendSV interrupt. */ + SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; + __SEV(); } } } From 3ada760cef7af9c5ad11091ec14ef26e5bbcefa4 Mon Sep 17 00:00:00 2001 From: Petr Nejedly Date: Mon, 10 Aug 2020 20:48:02 -0700 Subject: [PATCH 2/3] Nenik: Re-enable SD specific sleep path, related to #165 When the SoftDevice is active (e.g. as soon as you call Bluefruit.begin()), standby by __WFE seems to keep external Xtal oscillator running (at least on nRF52832), increasing the standby power consumption by 500-800uA. The SD-enabled path was apparently disabled mistakenly by Nordic, see Nordic post for reference: https://devzone.nordicsemi.com/f/nordic-q-a/35073/high-power-consumption-of-softdevice/136048#136048 --- cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c index ec01aa10..8cd5967b 100644 --- a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c +++ b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c @@ -182,7 +182,6 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); if ( xModifiableIdleTime > 0 ) { -#if 0 // With FreeRTOS sd_app_evt_wait increases power consumption with FreeRTOS compared to _WFE (NRFFOSDK-11174) #ifdef SOFTDEVICE_PRESENT // TODO uint8_t sd_en = 0; (void) sd_softdevice_is_enabled(&sd_en); @@ -193,7 +192,6 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) } else #endif -#endif // (NRFFOSDK-11174) { /* No SD - we would just block interrupts globally. * BASEPRI cannot be used for that because it would prevent WFE from wake up. From 2970c507756b567c484d5f56cc0788dec889fd04 Mon Sep 17 00:00:00 2001 From: Petr Nejedly Date: Mon, 10 Aug 2020 21:29:48 -0700 Subject: [PATCH 3/3] Nenik: Fix FPU-caused power consumption issue, related to #165 Apply the workaround from the Errata 87 of nRF52832 See https://infocenter.nordicsemi.com/topic/errata_nRF52832_Rev2/ERR/nRF52832/Rev2/latest/anomaly_832_87.html?cp=4_2_1_0_1_24 --- .../freertos/portable/CMSIS/nrf52/port_cmsis_systick.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c index 8cd5967b..29dfb57d 100644 --- a/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c +++ b/cores/nRF5/freertos/portable/CMSIS/nrf52/port_cmsis_systick.c @@ -182,6 +182,13 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); if ( xModifiableIdleTime > 0 ) { +#if (__FPU_USED == 1) + // nRF52832 errata 87: prevent FPU from keeping CPU on + // https://infocenter.nordicsemi.com/topic/errata_nRF52832_Rev2/ERR/nRF52832/Rev2/latest/anomaly_832_87.html?cp=4_2_1_0_1_24 + __set_FPSCR(__get_FPSCR() & ~(0x0000009F)); + (void) __get_FPSCR(); + NVIC_ClearPendingIRQ(FPU_IRQn); +#endif #ifdef SOFTDEVICE_PRESENT // TODO uint8_t sd_en = 0; (void) sd_softdevice_is_enabled(&sd_en);