diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 5497dbbd..33e2dda7 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -36,6 +36,7 @@ void setup( void ) ; void loop( void ) ; void suspendLoop(void); +void resumeLoop(void); #include "WVariant.h" 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 8f60b2c3..44f90ac1 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,13 @@ 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) +#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); @@ -193,7 +199,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. @@ -227,15 +232,34 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) diff = xExpectedIdleTime; } - if (diff > 0) - { - vTaskStepTick(diff); + // 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 - 1); + // If dwt cycle count is enable, adjust it as welll if ( (CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && (DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) ) { DWT->CYCCNT += ((diff * 1000000) / configTICK_RATE_HZ) * 64; } + + 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(); } } } diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index 62266ac7..00e6593b 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -100,6 +100,18 @@ void suspendLoop(void) vTaskSuspend(_loopHandle); } +void resumeLoop(void) +{ + if ( isInISR() ) + { + xTaskResumeFromISR(_loopHandle); + } + else + { + vTaskResume(_loopHandle); + } +} + extern "C" { diff --git a/libraries/Adafruit_LittleFS/src/littlefs/lfs_util.h b/libraries/Adafruit_LittleFS/src/littlefs/lfs_util.h index e992ee6f..14e26530 100644 --- a/libraries/Adafruit_LittleFS/src/littlefs/lfs_util.h +++ b/libraries/Adafruit_LittleFS/src/littlefs/lfs_util.h @@ -168,6 +168,7 @@ void lfs_crc(uint32_t *crc, const void *buffer, size_t size); // Allocate memory, only used if buffers are not provided to littlefs static inline void *lfs_malloc(size_t size) { #ifndef LFS_NO_MALLOC + extern void *pvPortMalloc( size_t xWantedSize ); return pvPortMalloc(size); #else (void)size; @@ -178,6 +179,7 @@ static inline void *lfs_malloc(size_t size) { // Deallocate memory, only used if buffers are not provided to littlefs static inline void lfs_free(void *p) { #ifndef LFS_NO_MALLOC + extern void vPortFree( void *pv ); vPortFree(p); #else (void)p; diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index ba993d1c..2100c9d2 100755 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -39,6 +39,7 @@ class TwoWire : public Stream void begin(uint8_t); void end(); void setClock(uint32_t); + void setPins(uint8_t pinSDA, uint8_t pinSCL); void beginTransmission(uint8_t); uint8_t endTransmission(bool stopBit); diff --git a/libraries/Wire/Wire_nRF52.cpp b/libraries/Wire/Wire_nRF52.cpp index 39043059..427f56c9 100755 --- a/libraries/Wire/Wire_nRF52.cpp +++ b/libraries/Wire/Wire_nRF52.cpp @@ -127,6 +127,12 @@ void TwoWire::setClock(uint32_t baudrate) { } } +void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL) +{ + this->_uc_pinSDA = g_ADigitalPinMap[pinSDA]; + this->_uc_pinSCL = g_ADigitalPinMap[pinSCL]; +} + void TwoWire::end() { if (master) {