Skip to content

Commit 4dff32a

Browse files
Merge pull request #5220 from c1728p9/sleep_lock_fixes
Fix deep sleep lock bugs
2 parents f47d8e9 + f854f3e commit 4dff32a

File tree

6 files changed

+175
-6
lines changed

6 files changed

+175
-6
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
/* mbed Microcontroller Library
3+
* Copyright (c) 2017 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#if !DEVICE_SLEEP
19+
#error [NOT_SUPPORTED] Sleep not supported for this target
20+
#endif
21+
22+
#include "utest/utest.h"
23+
#include "unity/unity.h"
24+
#include "greentea-client/test_env.h"
25+
26+
#include "mbed.h"
27+
28+
using namespace utest::v1;
29+
30+
void deep_sleep_lock_lock_test()
31+
{
32+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
33+
{
34+
// Check basic usage works
35+
DeepSleepLock lock;
36+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
37+
}
38+
39+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
40+
{
41+
// Check that unlock and lock change can deep sleep as expected
42+
DeepSleepLock lock;
43+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
44+
lock.unlock();
45+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
46+
lock.lock();
47+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
48+
}
49+
50+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
51+
{
52+
// Check that unlock releases sleep based on count
53+
DeepSleepLock lock;
54+
lock.lock();
55+
lock.lock();
56+
lock.unlock();
57+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
58+
}
59+
60+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
61+
{
62+
// Check that unbalanced locks do not leave deep sleep locked
63+
DeepSleepLock lock;
64+
lock.lock();
65+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
66+
}
67+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
68+
69+
}
70+
71+
void timer_lock_test()
72+
{
73+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
74+
{
75+
// Just creating a timer object does not lock sleep
76+
Timer timer;
77+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
78+
}
79+
80+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
81+
{
82+
// Starting a timer does lock sleep
83+
Timer timer;
84+
timer.start();
85+
TEST_ASSERT_EQUAL(false, sleep_manager_can_deep_sleep());
86+
}
87+
88+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
89+
{
90+
// Stopping a timer after starting it allows sleep
91+
Timer timer;
92+
timer.start();
93+
timer.stop();
94+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
95+
}
96+
97+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
98+
{
99+
// Starting a timer multiple times still lets you sleep
100+
Timer timer;
101+
timer.start();
102+
timer.start();
103+
}
104+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
105+
106+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
107+
{
108+
// Stopping a timer multiple times still lets you sleep
109+
Timer timer;
110+
timer.start();
111+
timer.stop();
112+
timer.stop();
113+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
114+
}
115+
TEST_ASSERT_EQUAL(true, sleep_manager_can_deep_sleep());
116+
}
117+
118+
Case cases[] = {
119+
Case("DeepSleepLock lock test", deep_sleep_lock_lock_test),
120+
Case("timer lock test", timer_lock_test),
121+
};
122+
123+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
124+
GREENTEA_SETUP(20, "default_auto");
125+
return greentea_test_setup_handler(number_of_cases);
126+
}
127+
128+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
129+
130+
int main() {
131+
Harness::run(specification);
132+
}

drivers/CAN.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() {
4646

4747
CAN::~CAN() {
4848
// No lock needed in destructor
49+
50+
// Detaching interrupts releases the sleep lock if it was locked
51+
for (int irq = 0; irq < IrqCnt; irq++) {
52+
attach(NULL, (IrqType)irq);
53+
}
4954
can_irq_free(&_can);
5055
can_free(&_can);
5156
}

drivers/SerialBase.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ void SerialBase:: unlock() {
133133
// Stub
134134
}
135135

136+
SerialBase::~SerialBase()
137+
{
138+
// No lock needed in destructor
139+
140+
// Detaching interrupts releases the sleep lock if it was locked
141+
for (int irq = 0; irq < IrqCnt; irq++) {
142+
attach(NULL, (IrqType)irq);
143+
}
144+
}
145+
136146
#if DEVICE_SERIAL_FC
137147
void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
138148
lock();

drivers/SerialBase.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ class SerialBase : private NonCopyable<SerialBase> {
241241

242242
protected:
243243
SerialBase(PinName tx, PinName rx, int baud);
244-
virtual ~SerialBase() {
245-
}
244+
virtual ~SerialBase();
246245

247246
int _base_getc();
248247
int _base_putc(int c);

drivers/Ticker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "platform/NonCopyable.h"
2323
#include "platform/mbed_sleep.h"
2424
#include "hal/lp_ticker_api.h"
25+
#include "platform/mbed_critical.h"
2526

2627
namespace mbed {
2728
/** \addtogroup drivers */
@@ -113,12 +114,14 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
113114
*
114115
*/
115116
void attach_us(Callback<void()> func, us_timestamp_t t) {
117+
core_util_critical_section_enter();
116118
// lock only for the initial callback setup and this is not low power ticker
117119
if(!_function && _lock_deepsleep) {
118120
sleep_manager_lock_deep_sleep();
119121
}
120122
_function = func;
121123
setup(t);
124+
core_util_critical_section_exit();
122125
}
123126

124127
/** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds

platform/DeepSleepLock.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#ifndef MBED_DEEPSLEEPLOCK_H
1717
#define MBED_DEEPSLEEPLOCK_H
1818

19+
#include <limits.h>
1920
#include "platform/mbed_sleep.h"
21+
#include "platform/mbed_critical.h"
2022

2123
namespace mbed {
2224

@@ -36,29 +38,47 @@ namespace mbed {
3638
* @endcode
3739
*/
3840
class DeepSleepLock {
41+
private:
42+
uint16_t _lock_count;
43+
3944
public:
40-
DeepSleepLock()
45+
DeepSleepLock(): _lock_count(1)
4146
{
4247
sleep_manager_lock_deep_sleep();
4348
}
4449

4550
~DeepSleepLock()
4651
{
47-
sleep_manager_unlock_deep_sleep();
52+
if (_lock_count) {
53+
sleep_manager_unlock_deep_sleep();
54+
}
4855
}
4956

5057
/** Mark the start of a locked deep sleep section
5158
*/
5259
void lock()
5360
{
54-
sleep_manager_lock_deep_sleep();
61+
uint16_t count = core_util_atomic_incr_u16(&_lock_count, 1);
62+
if (1 == count) {
63+
sleep_manager_lock_deep_sleep();
64+
}
65+
if (0 == count) {
66+
error("DeepSleepLock overflow (> USHRT_MAX)");
67+
}
5568
}
5669

5770
/** Mark the end of a locked deep sleep section
5871
*/
5972
void unlock()
6073
{
61-
sleep_manager_unlock_deep_sleep();
74+
uint16_t count = core_util_atomic_decr_u16(&_lock_count, 1);
75+
if (count == 0) {
76+
sleep_manager_unlock_deep_sleep();
77+
}
78+
if (count == USHRT_MAX) {
79+
core_util_critical_section_exit();
80+
error("DeepSleepLock underflow (< 0)");
81+
}
6282
}
6383
};
6484

0 commit comments

Comments
 (0)