Skip to content

Commit 6c59e13

Browse files
authored
Merge pull request #5581 from SiliconLabs/feature/remove-custom-sleepmodes
Remove custom Silicon Labs sleep management
2 parents 047455e + 5dd4613 commit 6c59e13

File tree

11 files changed

+41
-172
lines changed

11 files changed

+41
-172
lines changed

features/unsupported/USBDevice/targets/TARGET_Silicon_Labs/USBHAL_EFM32.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
#include "em_usbtypes.h"
2424
#include "em_usbhal.h"
2525
#include "em_usbd.h"
26-
27-
#include "sleepmodes.h"
26+
#include "mbed_sleep.h"
2827

2928
enum USBISRCommand {
3029
CMD_HANDLED = 0,
@@ -134,11 +133,6 @@ USBHAL::USBHAL(void)
134133
// be dynamically removed/reinstated to allow deeper sleep.
135134
usbhal_allow_em2(false);
136135

137-
// When in suspend / Vbus off we can go to EM2, but never below
138-
// that as long as USB is being used. Despite the name the call here
139-
// blocks entering modes _below_ EM2, but allows EM2.
140-
blockSleepMode(EM2);
141-
142136
epCallback[EP0OUT] = NULL;
143137
epCallback[EP0IN ] = NULL;
144138
epCallback[EP1OUT] = &USBHAL::EP1_OUT_callback;
@@ -193,17 +187,17 @@ USBHAL::~USBHAL(void)
193187
usbhal_free_buffers();
194188

195189
usbhal_allow_em2(true);
196-
unblockSleepMode(EM2);
197190
}
198191

199192
extern "C" void usbhal_allow_em2(bool allow_em2)
200193
{
201-
if (allow_em2) {
202-
// unblockSleepMode is safe to call even if we would unblock
203-
// an already unblocked mode, so no checks here.
204-
unblockSleepMode(EM1);
205-
} else {
206-
blockSleepMode(EM1);
194+
static bool blocked = false;
195+
if (allow_em2 && blocked) {
196+
sleep_manager_unlock_deep_sleep();
197+
blocked = false;
198+
} else if (!blocked) {
199+
sleep_manager_lock_deep_sleep();
200+
blocked = true;
207201
}
208202
}
209203

targets/TARGET_Silicon_Labs/TARGET_EFM32/gpio_api.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "pinmap.h"
2626
#include "em_cmu.h"
2727
#include "mbed_assert.h"
28-
#include "sleepmodes.h"
2928

3029

3130
void gpio_write(gpio_t *obj, int value)

targets/TARGET_Silicon_Labs/TARGET_EFM32/gpio_irq_api.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@
3131
#include "em_gpio.h"
3232
#include "em_cmu.h"
3333
#include "sleep_api.h"
34-
#include "sleepmodes.h"
3534

3635
#define NUM_GPIO_CHANNELS (16)
37-
#define GPIO_LEAST_ACTIVE_SLEEPMODE EM3
3836

3937
/* Macro return index of the LSB flag which is set. */
4038
#if ((__CORTEX_M == 3) || (__CORTEX_M == 4))
@@ -142,21 +140,16 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
142140
if(GPIO->IEN == 0) was_disabled = true;
143141

144142
GPIO_IntConfig((GPIO_Port_TypeDef)((obj->pin >> 4) & 0xF), obj->pin &0xF, obj->risingEdge, obj->fallingEdge, obj->risingEdge || obj->fallingEdge);
145-
if ((GPIO->IEN != 0) && (obj->risingEdge || obj->fallingEdge) && was_disabled) {
146-
blockSleepMode(GPIO_LEAST_ACTIVE_SLEEPMODE);
147-
}
148143
}
149144

150145
inline void gpio_irq_enable(gpio_irq_t *obj)
151146
{
152-
if(GPIO->IEN == 0) blockSleepMode(GPIO_LEAST_ACTIVE_SLEEPMODE);
153147
GPIO_IntEnable(1 << (obj->pin & 0xF)); // pin mask for pins to enable
154148
}
155149

156150
inline void gpio_irq_disable(gpio_irq_t *obj)
157151
{
158152
GPIO_IntDisable(1 << (obj->pin & 0xF)); // pin mask for pins to disable
159-
if(GPIO->IEN == 0) unblockSleepMode(GPIO_LEAST_ACTIVE_SLEEPMODE);
160153
}
161154

162155
/***************************************************************************//**

targets/TARGET_Silicon_Labs/TARGET_EFM32/i2c_api.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
#if DEVICE_I2C
2929

3030
#include "mbed_assert.h"
31+
#include "mbed_sleep.h"
3132
#include "i2c_api.h"
3233
#include "PeripheralPins.h"
3334
#include "pinmap_function.h"
34-
#include "sleepmodes.h"
3535

3636
#include "em_i2c.h"
3737
#include "em_cmu.h"
@@ -508,7 +508,7 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
508508
retval = I2C_TransferInit(obj->i2c.i2c, &(obj->i2c.xfer));
509509

510510
if(retval == i2cTransferInProgress) {
511-
blockSleepMode(EM1);
511+
sleep_manager_lock_deep_sleep();
512512
} else {
513513
// something happened, and the transfer did not go through
514514
// So, we need to clean up
@@ -541,23 +541,23 @@ uint32_t i2c_irq_handler_asynch(i2c_t *obj)
541541
// Disable interrupt
542542
i2c_enable_interrupt(obj, 0, false);
543543

544-
unblockSleepMode(EM1);
544+
sleep_manager_unlock_deep_sleep();
545545

546546
return I2C_EVENT_TRANSFER_COMPLETE & obj->i2c.events;
547547
case i2cTransferNack:
548548
// A NACK has been received while an ACK was expected. This is usually because the slave did not respond to the address.
549549
// Disable interrupt
550550
i2c_enable_interrupt(obj, 0, false);
551551

552-
unblockSleepMode(EM1);
552+
sleep_manager_unlock_deep_sleep();
553553

554554
return I2C_EVENT_ERROR_NO_SLAVE & obj->i2c.events;
555555
default:
556556
// An error situation has arisen.
557557
// Disable interrupt
558558
i2c_enable_interrupt(obj, 0, false);
559559

560-
unblockSleepMode(EM1);
560+
sleep_manager_unlock_deep_sleep();
561561

562562
// return error
563563
return I2C_EVENT_ERROR & obj->i2c.events;
@@ -578,19 +578,19 @@ uint8_t i2c_active(i2c_t *obj)
578578
*/
579579
void i2c_abort_asynch(i2c_t *obj)
580580
{
581-
// Do not deactivate I2C twice
582-
if (!i2c_active(obj)) return;
583-
584581
// Disable interrupt
585582
i2c_enable_interrupt(obj, 0, false);
586583

584+
// Do not deactivate I2C twice
585+
if (!i2c_active(obj)) return;
586+
587587
// Abort
588588
obj->i2c.i2c->CMD = I2C_CMD_STOP | I2C_CMD_ABORT;
589589

590590
// Block until free
591591
while(i2c_active(obj));
592592

593-
unblockSleepMode(EM1);
593+
sleep_manager_unlock_deep_sleep();
594594
}
595595

596596
#endif //DEVICE_I2C ASYNCH

targets/TARGET_Silicon_Labs/TARGET_EFM32/pwmout_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
#if DEVICE_PWMOUT
2727

2828
#include "mbed_assert.h"
29+
#include "mbed_sleep.h"
2930
#include "pwmout_api.h"
3031
#include "pinmap.h"
3132
#include "PeripheralPins.h"
3233
#include "device_peripherals.h"
33-
#include "sleepmodes.h"
3434

3535
#include "em_cmu.h"
3636
#include "em_gpio.h"
@@ -180,7 +180,7 @@ void pwmout_init(pwmout_t *obj, PinName pin)
180180
return;
181181
} else {
182182
pwmout_set_channel_route(pwmout_get_channel_route(obj->channel));
183-
blockSleepMode(EM1);
183+
sleep_manager_lock_deep_sleep();
184184
pwmout_enable(obj, true);
185185
pwmout_enable_pins(obj, true);
186186
}
@@ -226,7 +226,7 @@ void pwmout_free(pwmout_t *obj)
226226
{
227227
if(pwmout_disable_channel_route(pwmout_get_channel_route(obj->channel))) {
228228
//Channel was previously enabled, so do housekeeping
229-
unblockSleepMode(EM1);
229+
sleep_manager_unlock_deep_sleep();
230230
} else {
231231
//This channel was disabled already
232232
}

targets/TARGET_Silicon_Labs/TARGET_EFM32/rtc_api.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "rtc_api_HAL.h"
2929
#include "em_cmu.h"
3030
#include "sleep_api.h"
31-
#include "sleepmodes.h"
3231

3332
#if (defined RTC_COUNT) && (RTC_COUNT > 0)
3433
#include "em_rtc.h"
@@ -48,8 +47,6 @@ static void (*comp0_handler)(void) = NULL;
4847
#ifndef RTCC_COUNT
4948

5049
/* Using RTC API */
51-
52-
#define RTC_LEAST_ACTIVE_SLEEPMODE EM2
5350
#define RTC_NUM_BITS (24)
5451

5552
void RTC_IRQHandler(void)
@@ -111,7 +108,6 @@ void rtc_init_real(uint32_t flags)
111108
/* Initialize */
112109
RTC_Init(&init);
113110

114-
blockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE);
115111
rtc_inited = true;
116112
}
117113
}
@@ -131,16 +127,13 @@ void rtc_free_real(uint32_t flags)
131127
NVIC_DisableIRQ(RTC_IRQn);
132128
RTC_Reset();
133129
CMU_ClockEnable(cmuClock_RTC, false);
134-
unblockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE);
135130
rtc_inited = false;
136131
}
137132
}
138133

139134
#else
140135

141136
/* Using RTCC API */
142-
143-
#define RTCC_LEAST_ACTIVE_SLEEPMODE EM2
144137
#define RTCC_NUM_BITS (32)
145138

146139
void RTCC_IRQHandler(void)
@@ -204,8 +197,6 @@ void rtc_init_real(uint32_t flags)
204197

205198
/* Initialize */
206199
RTCC_Init(&init);
207-
208-
blockSleepMode(RTCC_LEAST_ACTIVE_SLEEPMODE);
209200
rtc_inited = true;
210201
}
211202
}
@@ -225,7 +216,6 @@ void rtc_free_real(uint32_t flags)
225216
NVIC_DisableIRQ(RTCC_IRQn);
226217
RTCC_Reset();
227218
CMU_ClockEnable(cmuClock_RTCC, false);
228-
unblockSleepMode(RTCC_LEAST_ACTIVE_SLEEPMODE);
229219
rtc_inited = false;
230220
}
231221
}

targets/TARGET_Silicon_Labs/TARGET_EFM32/serial_api.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#if DEVICE_SERIAL
2727

2828
#include "mbed_assert.h"
29+
#include "mbed_sleep.h"
2930
#include "serial_api.h"
3031
#include "serial_api_HAL.h"
3132
#include <string.h>
@@ -44,10 +45,6 @@
4445
#include "dma_api.h"
4546
#include "sleep_api.h"
4647
#include "buffer.h"
47-
#include "sleepmodes.h"
48-
49-
#define SERIAL_LEAST_ACTIVE_SLEEPMODE EM1
50-
#define SERIAL_LEAST_ACTIVE_SLEEPMODE_LEUART EM2
5148

5249
/** Validation of LEUART register block pointer reference
5350
* for assert statements. */
@@ -2239,13 +2236,11 @@ static void serial_unblock_sleep(serial_t *obj)
22392236
{
22402237
if( obj->serial.sleep_blocked > 0 ) {
22412238
#ifdef LEUART_USING_LFXO
2242-
if(LEUART_REF_VALID(obj->serial.periph.leuart) && (LEUART_BaudrateGet(obj->serial.periph.leuart) <= (LEUART_LF_REF_FREQ/2))){
2243-
unblockSleepMode(SERIAL_LEAST_ACTIVE_SLEEPMODE_LEUART);
2244-
}else{
2245-
unblockSleepMode(SERIAL_LEAST_ACTIVE_SLEEPMODE);
2239+
if(!LEUART_REF_VALID(obj->serial.periph.leuart) || (LEUART_BaudrateGet(obj->serial.periph.leuart) > (LEUART_LF_REF_FREQ/2))){
2240+
sleep_manager_unlock_deep_sleep();
22462241
}
22472242
#else
2248-
unblockSleepMode(SERIAL_LEAST_ACTIVE_SLEEPMODE);
2243+
sleep_manager_unlock_deep_sleep();
22492244
#endif
22502245
obj->serial.sleep_blocked--;
22512246
}
@@ -2255,13 +2250,13 @@ static void serial_block_sleep(serial_t *obj)
22552250
{
22562251
obj->serial.sleep_blocked++;
22572252
#ifdef LEUART_USING_LFXO
2258-
if(LEUART_REF_VALID(obj->serial.periph.leuart) && (LEUART_BaudrateGet(obj->serial.periph.leuart) <= (LEUART_LF_REF_FREQ/2))){
2259-
blockSleepMode(SERIAL_LEAST_ACTIVE_SLEEPMODE_LEUART);
2260-
}else{
2261-
blockSleepMode(SERIAL_LEAST_ACTIVE_SLEEPMODE);
2253+
if(!LEUART_REF_VALID(obj->serial.periph.leuart) || (LEUART_BaudrateGet(obj->serial.periph.leuart) > (LEUART_LF_REF_FREQ/2))){
2254+
/* LEUART configured to a baudrate triggering the use of HFCLK, so prevent HFCLK from getting turned off */
2255+
sleep_manager_lock_deep_sleep();
22622256
}
22632257
#else
2264-
blockSleepMode(SERIAL_LEAST_ACTIVE_SLEEPMODE);
2258+
/* HFCLK unavailable in deepsleep */
2259+
sleep_manager_lock_deep_sleep();
22652260
#endif
22662261
}
22672262

targets/TARGET_Silicon_Labs/TARGET_EFM32/sleep.c

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,15 @@
2525
#if DEVICE_SLEEP
2626

2727
#include "sleep_api.h"
28-
#include "sleepmodes.h"
2928
#include "em_emu.h"
30-
#include "mbed_critical.h"
31-
32-
uint32_t sleep_block_counter[NUM_SLEEP_MODES] = {0};
3329

3430
/**
3531
* Sleep mode.
36-
* Enter the lowest possible sleep mode that is not blocked by ongoing activity.
32+
* Stop the core clock using a WFI.
3733
*/
3834
void hal_sleep(void)
3935
{
40-
if (sleep_block_counter[0] > 0) {
41-
/* Blocked everything below EM0, so just return */
42-
return;
43-
} else if (sleep_block_counter[1] > 0) {
44-
/* Blocked everything below EM1, enter EM1 */
45-
EMU_EnterEM1();
46-
} else if (sleep_block_counter[2] > 0) {
47-
/* Blocked everything below EM2, enter EM2 */
48-
EMU_EnterEM2(true);
49-
} else {
50-
/* Blocked everything below EM3, enter EM3 */
51-
EMU_EnterEM3(true);
52-
} /* Never enter EM4, as mbed has no way of configuring EM4 wakeup */
53-
return;
36+
EMU_EnterEM1();
5437
}
5538

5639
/**
@@ -69,35 +52,4 @@ void hal_deepsleep(void)
6952
EMU_EnterEM2(true);
7053
}
7154

72-
/** Block the microcontroller from sleeping below a certain mode
73-
*
74-
* This will block sleep() from entering an energy mode below the one given.
75-
* -- To be called by peripheral HAL's --
76-
*
77-
* After the peripheral is finished with the operation, it should call unblock with the same state
78-
*
79-
*/
80-
void blockSleepMode(sleepstate_enum minimumMode)
81-
{
82-
core_util_critical_section_enter();
83-
sleep_block_counter[minimumMode]++;
84-
core_util_critical_section_exit();
85-
}
86-
87-
/** Unblock the microcontroller from sleeping below a certain mode
88-
*
89-
* This will unblock sleep() from entering an energy mode below the one given.
90-
* -- To be called by peripheral HAL's --
91-
*
92-
* This should be called after all transactions on a peripheral are done.
93-
*/
94-
void unblockSleepMode(sleepstate_enum minimumMode)
95-
{
96-
core_util_critical_section_enter();
97-
if(sleep_block_counter[minimumMode] > 0) {
98-
sleep_block_counter[minimumMode]--;
99-
}
100-
core_util_critical_section_exit();
101-
}
102-
10355
#endif

0 commit comments

Comments
 (0)