diff --git a/changelog.md b/changelog.md index 617fe5f5..cb9d8667 100644 --- a/changelog.md +++ b/changelog.md @@ -4,7 +4,8 @@ - Add HardwarePWM class to support up to 12 channels and compatible with Neopixel library - Add waitForEvent() as alias to __WFE() instruction -- Enable FreeRTOS's Idle hook, add rtos_idle_callback() as optional callback for user +- Enable FreeRTOS's Idle hook, and call waitForEvent() in the Idle hook. +- Add rtos_idle_callback() as optional callback for user to handle background task - Add mutex to prevent uart conflict - Change FreeRTOS tick source from systick to RTC for low-power mode diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 7f8fc54d..def95dcf 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -111,7 +111,7 @@ uint32_t setLoopStacksize(void); #define digitalPinHasPWM(P) ( true ) // Hint instruction to tell CPU go to low power mode -#define waitForEvent() sd_app_evt_wait() // __WFE() +#define waitForEvent() __WFE() // sd_app_evt_wait() /* * digitalPinToTimer(..) is AVR-specific and is not defined for nRF52 diff --git a/cores/nRF5/rtos.cpp b/cores/nRF5/rtos.cpp index 8129e2af..fb3f96e8 100644 --- a/cores/nRF5/rtos.cpp +++ b/cores/nRF5/rtos.cpp @@ -94,6 +94,9 @@ void vApplicationIdleHook( void ) // Weak symbol do nothing if is not implementation rtos_idle_callback(); + + // All done. Instruct CPU to enter low-power state until an event/interrupt occurred + waitForEvent(); } } diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino b/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino index 7771bb47..6a766ea9 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino @@ -110,22 +110,24 @@ void disconnect_callback(uint8_t reason) /** * RTOS Idle callback is automatically invoked by FreeRTOS - * when there is no active threads. This function must not - * call any blocking FreeRTOS API such as delay(), xSemaphoreTake() etc .. + * when there is no active threads. E.g loop() call delay() and + * there is no bluetooth or hw event. This is a greate place to handle + * background data. * - * Notes: Should be used for background work. Great place to call - * waitForEvent() (see comment below) + * NOTE: After this callback returns, MCU will enter low-power mode + * by waitForEvent() internally. THerefore user SHOULD NOT call + * waitForEvent() in this callback. + * + * WARNING: This function MUST NOT call any blocking FreeRTOS API + * such as delay(), xSemaphoreTake() etc ... for more information + * http://www.freertos.org/a00016.html */ void rtos_idle_callback(void) { // Background task here - - // Instruct CPU to enter low-power state until an event/interrupt occured - // Note: This will turn off hardware PWM and maybe other peripherlas !! - // Should only call there is no active PWM devices - if ( !(PWM0.enabled() || PWM1.enabled() || PWM2.enabled() ) ) - { - waitForEvent(); - } + + + // waitForEvent() will be invoked by internal code when this callback return. + // Don't call waitForEvent() in this function nor any other blocking API() } diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino index 458fc40c..e5ed6d40 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino @@ -109,3 +109,25 @@ void loop() waitForEvent(); } +/** + * RTOS Idle callback is automatically invoked by FreeRTOS + * when there is no active threads. E.g loop() call delay() and + * there is no bluetooth or hw event. This is a greate place to handle + * background data. + * + * NOTE: After this callback returns, MCU will enter low-power mode + * by waitForEvent() internally. THerefore user SHOULD NOT call + * waitForEvent() in this callback. + * + * WARNING: This function MUST NOT call any blocking FreeRTOS API + * such as delay(), xSemaphoreTake() etc ... for more information + * http://www.freertos.org/a00016.html + */ +void rtos_idle_callback(void) +{ + // Background task here + + + // waitForEvent() will be invoked by internal code when this callback return. + // Don't call waitForEvent() in this function nor any other blocking API() +}