Skip to content

Commit 6a045a4

Browse files
committed
Platform: Add sleep/deepsleep user facing functions
Add sleep/deepsleep functions to platform layer which are replacing HAL functions with the same name, rename existing symbols in HAL layer to hal_sleep/hal_deepsleep. This way sleep functions are always available, even if target doesn't implement them, which makes the code using sleep clearer. It also enables us to make decision on in which builds (debug/release) the sleep will be enabled.
1 parent 04a31f3 commit 6a045a4

File tree

31 files changed

+161
-67
lines changed

31 files changed

+161
-67
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// In this case, bits which are equal to 0 are the bits reserved in this register
2424
#define SCB_ICSR_RESERVED_BITS_MASK 0x9E43F03F
2525

26-
void sleep(void)
26+
void hal_sleep(void)
2727
{
2828
// ensure debug is disconnected if semihost is enabled....
2929

@@ -64,7 +64,7 @@ void sleep(void)
6464
}
6565
}
6666

67-
void deepsleep(void)
67+
void hal_deepsleep(void)
6868
{
6969
sleep();
7070
// NRF_POWER->SYSTEMOFF=1;

hal/sleep_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extern "C" {
4141
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
4242
* able to access the LocalFileSystem
4343
*/
44-
void sleep(void);
44+
void hal_sleep(void);
4545

4646
/** Send the microcontroller to deep sleep
4747
*
@@ -56,7 +56,7 @@ void sleep(void);
5656
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
5757
* able to access the LocalFileSystem
5858
*/
59-
void deepsleep(void);
59+
void hal_deepsleep(void);
6060

6161
#ifdef __cplusplus
6262
}

mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#include "drivers/InterruptIn.h"
9393
#include "platform/wait_api.h"
9494
#include "hal/sleep_api.h"
95+
#include "platform/sleep.h"
9596
#include "platform/rtc_time.h"
9697

9798
// mbed Non-hardware components

platform/sleep.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
/** \addtogroup platform */
3+
/** @{*/
4+
/* mbed Microcontroller Library
5+
* Copyright (c) 2006-2017 ARM Limited
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
#ifndef MBED_SLEEP_H
20+
#define MBED_SLEEP_H
21+
22+
#include "sleep_api.h"
23+
24+
#if !DEVICE_SLEEP
25+
#warning Sleep is not supported on this platform.
26+
#else /* DEVICE_SLEEP */
27+
#ifndef NDEBUG
28+
#warning Sleep is disabled for debug builds.
29+
#endif /* NDEBUG */
30+
#endif /* DEVICE_SLEEP */
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
/** Send the microcontroller to sleep
37+
*
38+
* @note This function can be a noop if not implemented by the platform.
39+
* @note This function will only put device to sleep in release mode (small profile or when NDEBUG is defined).
40+
*
41+
* The processor is setup ready for sleep, and sent to sleep using __WFI(). In this mode, the
42+
* system clock to the core is stopped until a reset or an interrupt occurs. This eliminates
43+
* dynamic power used by the processor, memory systems and buses. The processor, peripheral and
44+
* memory state are maintained, and the peripherals continue to work and can generate interrupts.
45+
*
46+
* The processor can be woken up by any internal peripheral interrupt or external pin interrupt.
47+
*
48+
* @note
49+
* The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
50+
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
51+
* able to access the LocalFileSystem
52+
*/
53+
__INLINE void sleep(void)
54+
{
55+
#ifdef NDEBUG
56+
#if DEVICE_SLEEP
57+
hal_sleep();
58+
#endif /* DEVICE_SLEEP */
59+
#endif /* NDEBUG */
60+
}
61+
62+
/** Send the microcontroller to deep sleep
63+
*
64+
* @note This function can be a noop if not implemented by the platform.
65+
* @note This function will only put device to sleep in release mode (small profile or when NDEBUG is defined).
66+
*
67+
* This processor is setup ready for deep sleep, and sent to sleep using __WFI(). This mode
68+
* has the same sleep features as sleep plus it powers down peripherals and clocks. All state
69+
* is still maintained.
70+
*
71+
* The processor can only be woken up by an external interrupt on a pin or a watchdog timer.
72+
*
73+
* @note
74+
* The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
75+
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
76+
* able to access the LocalFileSystem
77+
*/
78+
__INLINE void deepsleep(void)
79+
{
80+
#ifdef NDEBUG
81+
#if DEVICE_SLEEP
82+
hal_deepsleep();
83+
#endif /* DEVICE_SLEEP */
84+
#endif /* NDEBUG */
85+
}
86+
87+
#ifdef __cplusplus
88+
}
89+
#endif
90+
91+
#endif
92+
93+
/** @}*/

targets/TARGET_ARM_SSG/TARGET_BEETLE/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
#include "sleep_api.h"
1717
#include "cmsis.h"
1818

19-
void sleep(void)
19+
void hal_sleep(void)
2020
{
2121
SystemPowerSuspend(POWER_MODE_SLEEP);
2222
SystemPowerResume(POWER_MODE_SLEEP);
2323
}
2424

25-
void deepsleep(void)
25+
void hal_deepsleep(void)
2626
{
2727
SystemPowerSuspend(POWER_MODE_DEEP_SLEEP);
2828
SystemPowerResume(POWER_MODE_DEEP_SLEEP);

targets/TARGET_Atmel/TARGET_SAM_CortexM0P/sleep_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @param[void] void
2626
* @return void
2727
*/
28-
void sleep(void)
28+
void hal_sleep(void)
2929
{
3030
#if (SAMD21) || (SAMR21)
3131
system_set_sleepmode(SYSTEM_SLEEPMODE_IDLE_2);
@@ -43,7 +43,7 @@ void sleep(void)
4343
* @param[void] void
4444
* @return void
4545
*/
46-
void deepsleep(void)
46+
void hal_deepsleep(void)
4747
{
4848
system_set_sleepmode(SYSTEM_SLEEPMODE_STANDBY);
4949
system_sleep();

targets/TARGET_Atmel/TARGET_SAM_CortexM4/sleep_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @param[void] void
2525
* @return void
2626
*/
27-
void sleep(void)
27+
void hal_sleep(void)
2828
{
2929
enum sleepmgr_mode sleep_mode;
3030

@@ -40,10 +40,10 @@ void sleep(void)
4040
* @param[void] void
4141
* @return void
4242
*/
43-
void deepsleep(void)
43+
void hal_deepsleep(void)
4444
{
4545
enum sleepmgr_mode sleep_mode;
4646

4747
sleep_mode = SLEEPMGR_SLEEP_WFE;
4848
sleepmgr_sleep(sleep_mode);
49-
}
49+
}

targets/TARGET_Freescale/TARGET_K20XX/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "cmsis.h"
1818

1919
//Normal wait mode
20-
void sleep(void)
20+
void hal_sleep(void)
2121
{
2222
SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
2323

@@ -27,7 +27,7 @@ void sleep(void)
2727
}
2828

2929
//Very low-power stop mode
30-
void deepsleep(void)
30+
void hal_deepsleep(void)
3131
{
3232
//Check if ADC is enabled and HS mode is set, if yes disable it (lowers power consumption by 60uA)
3333
uint8_t ADC_HSC = 0;

targets/TARGET_Freescale/TARGET_KLXX/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "PeripheralPins.h"
1919

2020
//Normal wait mode
21-
void sleep(void)
21+
void hal_sleep(void)
2222
{
2323
SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
2424

@@ -28,7 +28,7 @@ void sleep(void)
2828
}
2929

3030
//Very low-power stop mode
31-
void deepsleep(void)
31+
void hal_deepsleep(void)
3232
{
3333
//Check if ADC is enabled and HS mode is set, if yes disable it (lowers power consumption by 60uA)
3434
uint8_t ADC_HSC = 0;

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#include "fsl_smc.h"
1919
#include "fsl_clock_config.h"
2020

21-
void sleep(void)
21+
void hal_sleep(void)
2222
{
2323
SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
2424

2525
SMC_SetPowerModeWait(SMC);
2626
}
2727

28-
void deepsleep(void)
28+
void hal_deepsleep(void)
2929
{
3030
#if (defined(FSL_FEATURE_SOC_MCG_COUNT) && FSL_FEATURE_SOC_MCG_COUNT)
3131
mcg_mode_t mode = CLOCK_GetMode();

targets/TARGET_Maxim/TARGET_MAX32600/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART;
4242

4343
// Normal wait mode
44-
void sleep(void)
44+
void hal_sleep(void)
4545
{
4646
// Normal sleep mode for ARM core
4747
SCB->SCR = 0;
@@ -70,7 +70,7 @@ static void clearAllGPIOWUD(void)
7070
}
7171

7272
// Low-power stop mode
73-
void deepsleep(void)
73+
void hal_deepsleep(void)
7474
{
7575
__disable_irq();
7676

targets/TARGET_Maxim/TARGET_MAX32610/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART;
4242

4343
// Normal wait mode
44-
void sleep(void)
44+
void hal_sleep(void)
4545
{
4646
// Normal sleep mode for ARM core
4747
SCB->SCR = 0;
@@ -70,7 +70,7 @@ static void clearAllGPIOWUD(void)
7070
}
7171

7272
// Low-power stop mode
73-
void deepsleep(void)
73+
void hal_deepsleep(void)
7474
{
7575
__disable_irq();
7676

targets/TARGET_Maxim/TARGET_MAX32620/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static mxc_uart_regs_t *stdio_uart = (mxc_uart_regs_t*)STDIO_UART;
5454
static int restore_usb;
5555
static usb_state_t usb_state;
5656

57-
void sleep(void)
57+
void hal_sleep(void)
5858
{
5959
// Normal sleep mode for ARM core
6060
SCB->SCR = 0;
@@ -109,7 +109,7 @@ static void usb_wakeup(void)
109109
}
110110

111111
// Low-power stop mode
112-
void deepsleep(void)
112+
void hal_deepsleep(void)
113113
{
114114
unsigned int part_rev = MXC_PWRMAN->mask_id0 & MXC_F_PWRMAN_MASK_ID0_REVISION_ID;
115115

targets/TARGET_Maxim/TARGET_MAX32625/sleep.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
#include "sleep_api.h"
3535
#include "lp.h"
3636

37-
void sleep(void)
37+
void hal_sleep(void)
3838
{
3939
LP_EnterLP2();
4040
}
4141

4242
// Low-power stop mode
43-
void deepsleep(void)
43+
void hal_deepsleep(void)
4444
{
45-
sleep();
45+
hal_sleep();
4646
}

targets/TARGET_NORDIC/TARGET_MCU_NRF51822/sleep.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
#include "mbed_interface.h"
1919
#include "toolchain.h"
2020

21-
MBED_WEAK void sleep(void)
21+
MBED_WEAK void hal_sleep(void)
2222
{
2323
// ensure debug is disconnected if semihost is enabled....
2424
NRF_POWER->TASKS_LOWPWR = 1;
2525
// wait for interrupt
2626
__WFE();
2727
}
2828

29-
MBED_WEAK void deepsleep(void)
29+
MBED_WEAK void hal_deepsleep(void)
3030
{
31-
sleep();
31+
hal_sleep();
3232
// NRF_POWER->SYSTEMOFF=1;
3333
}

targets/TARGET_NORDIC/TARGET_NRF5/sleep.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#define FPU_EXCEPTION_MASK 0x0000009F
2727

28-
void sleep(void)
28+
void hal_sleep(void)
2929
{
3030
// ensure debug is disconnected if semihost is enabled....
3131

@@ -73,8 +73,8 @@ void sleep(void)
7373
}
7474
}
7575

76-
void deepsleep(void)
76+
void hal_deepsleep(void)
7777
{
78-
sleep();
78+
hal_sleep();
7979
// NRF_POWER->SYSTEMOFF=1;
8080
}

targets/TARGET_NUVOTON/TARGET_M451/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int pwmout_allow_powerdown(void);
3838
/**
3939
* Enter Idle mode.
4040
*/
41-
void sleep(void)
41+
void hal_sleep(void)
4242
{
4343
struct sleep_s sleep_obj;
4444
sleep_obj.powerdown = 0;
@@ -49,7 +49,7 @@ void sleep(void)
4949
/**
5050
* Enter Power-down mode while no peripheral is active; otherwise, enter Idle mode.
5151
*/
52-
void deepsleep(void)
52+
void hal_deepsleep(void)
5353
{
5454
struct sleep_s sleep_obj;
5555
sleep_obj.powerdown = 1;

targets/TARGET_NUVOTON/TARGET_NUC472/sleep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int pwmout_allow_powerdown(void);
3838
/**
3939
* Enter Idle mode.
4040
*/
41-
void sleep(void)
41+
void hal_sleep(void)
4242
{
4343
struct sleep_s sleep_obj;
4444
sleep_obj.powerdown = 0;
@@ -49,7 +49,7 @@ void sleep(void)
4949
/**
5050
* Enter Power-down mode while no peripheral is active; otherwise, enter Idle mode.
5151
*/
52-
void deepsleep(void)
52+
void hal_deepsleep(void)
5353
{
5454
struct sleep_s sleep_obj;
5555
sleep_obj.powerdown = 1;

0 commit comments

Comments
 (0)