Skip to content

Commit 18ed12c

Browse files
committed
Drivers: locking sleep fix for attach/detach
As attach provides API to change a callback, lock/unlock should only happen if we are doing the first/last callback (start-stop).
1 parent 7b0e937 commit 18ed12c

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

drivers/CAN.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle
115115
void CAN::attach(Callback<void()> func, IrqType type) {
116116
lock();
117117
if (func) {
118-
sleep_manager_lock_deep_sleep();
118+
// lock deep sleep only the first time
119+
if (_irq[(CanIrqType)type] == callback(donothing)) {
120+
sleep_manager_lock_deep_sleep();
121+
}
119122
_irq[(CanIrqType)type] = func;
120123
can_irq_set(&_can, (CanIrqType)type, 1);
121124
} else {

drivers/SerialBase.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ void SerialBase::attach(Callback<void()> func, IrqType type) {
7676
// Disable interrupts when attaching interrupt handler
7777
core_util_critical_section_enter();
7878
if (func) {
79-
sleep_manager_lock_deep_sleep();
79+
// lock deep sleep only the first time
80+
if (_irq[type] == donothing) {
81+
sleep_manager_lock_deep_sleep();
82+
}
8083
_irq[type] = func;
8184
serial_irq_set(&_serial, (SerialIrq)type, 1);
8285
} else {

drivers/Ticker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ namespace mbed {
2525
void Ticker::detach() {
2626
core_util_critical_section_enter();
2727
remove();
28+
// unlocked only if we were attached (we locked it)
29+
if (_function) {
30+
sleep_manager_unlock_deep_sleep();
31+
}
2832
_function = 0;
29-
sleep_manager_unlock_deep_sleep();
3033
core_util_critical_section_exit();
3134
}
3235

drivers/Ticker.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ namespace mbed {
6464
class Ticker : public TimerEvent, private NonCopyable<Ticker> {
6565

6666
public:
67-
Ticker() : TimerEvent() {
67+
Ticker() : TimerEvent(), _function(0) {
6868
}
6969

70-
Ticker(const ticker_data_t *data) : TimerEvent(data) {
70+
Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0) {
7171
data->interface->init();
7272
}
7373

@@ -103,8 +103,11 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
103103
* @param t the time between calls in micro-seconds
104104
*/
105105
void attach_us(Callback<void()> func, us_timestamp_t t) {
106+
// lock only for the initial callback setup
107+
if (!_function) {
108+
sleep_manager_lock_deep_sleep();
109+
}
106110
_function = func;
107-
sleep_manager_lock_deep_sleep();
108111
setup(t);
109112
}
110113

drivers/Timer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ void Timer::start() {
4141
void Timer::stop() {
4242
core_util_critical_section_enter();
4343
_time += slicetime();
44+
if (_running) {
45+
sleep_manager_unlock_deep_sleep();
46+
}
4447
_running = 0;
45-
sleep_manager_unlock_deep_sleep();
4648
core_util_critical_section_exit();
4749
}
4850

0 commit comments

Comments
 (0)