Skip to content

Commit

Permalink
Add demo for tickless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bulislaw committed Mar 24, 2018
1 parent 7b8c994 commit 98494f7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hal/mbed_sleep_manager.c → hal/mbed_sleep_manager.cpp
Expand Up @@ -23,8 +23,13 @@
#include <limits.h>
#include <stdio.h>

#include "mbed.h"

#if DEVICE_SLEEP

extern DigitalOut pin_sleep;
extern DigitalOut pin_deepsleep;

// deep sleep locking counter. A target is allowed to deep sleep if counter == 0
static uint16_t deep_sleep_lock = 0U;

Expand Down Expand Up @@ -152,9 +157,13 @@ void sleep_manager_sleep_auto(void)
hal_sleep();
#else
if (sleep_manager_can_deep_sleep()) {
pin_deepsleep = 1;
hal_deepsleep();
pin_deepsleep = 0;
} else {
pin_sleep = 1;
hal_sleep();
pin_sleep = 0;
}
#endif
core_util_critical_section_exit();
Expand Down
37 changes: 37 additions & 0 deletions main.cpp
@@ -0,0 +1,37 @@
#include "mbed.h"

/* Tickless challenge
*
* Intstructtions:
*
* - Cherry pick support for your board for HAL Sleep and Low Power Ticker APIs from
* feature-hal-spec-sleep and feature-hal-spec-ticker on top of tickless_challenge.
*
* - Connect Logic Analyser to the board
* - CH0 <> D0
* - CH1 <> D1
* - CH2 <> D2
* - GND <> GND
*
* run: mbed compile -f --profile release
*
* Explanations:
* CH0 is HIGH when the board is running LOW is when the board is sleeping, it also illustrates systick
* CH1 is HIGH when the board is in shallow sleep
* CH2 is HIGH when the board is in deep sleep.
*/

DigitalOut pin_run(D0, 1);
DigitalOut pin_sleep(D1, 0);
DigitalOut pin_deepsleep(D2, 0);

DigitalOut led1(LED1);

// main() runs in its own thread in the OS
int main() {
while (true) {
led1 = !led1;
Thread::wait(500);
}
}

8 changes: 8 additions & 0 deletions rtos/TARGET_CORTEX/mbed_rtx_idle.cpp
Expand Up @@ -32,6 +32,10 @@ extern "C" {
#include "rtx_lib.h"
}

#include "mbed.h"

extern DigitalOut pin_run;

using namespace mbed;

#if (defined(MBED_TICKLESS) && defined(DEVICE_LPTICKER))
Expand Down Expand Up @@ -98,6 +102,7 @@ static void default_idle_hook(void)
uint32_t elapsed_ticks = 0;

core_util_critical_section_enter();
pin_run = 0;
uint32_t ticks_to_sleep = svcRtxKernelSuspend();
if (ticks_to_sleep) {
os_timer->schedule_tick(ticks_to_sleep);
Expand All @@ -109,6 +114,7 @@ static void default_idle_hook(void)
elapsed_ticks = os_timer->update_tick();
}
svcRtxKernelResume(elapsed_ticks);
pin_run = 1;
core_util_critical_section_exit();
}

Expand All @@ -126,9 +132,11 @@ static void default_idle_hook(void)
{
// critical section to complete sleep with locked deepsleep
core_util_critical_section_enter();
pin_run = 0;
sleep_manager_lock_deep_sleep();
sleep();
sleep_manager_unlock_deep_sleep();
pin_run = 1;
core_util_critical_section_exit();
}

Expand Down

0 comments on commit 98494f7

Please sign in to comment.