Skip to content

Commit 1da259a

Browse files
committed
Fix RTC test failures by removing critical sect
Remove the critical section in mbed_rtc_time.c and instead use a mutex to protect this. This function does not need to be interrupt safe, just thread safe. This fixes crashes on the GCC_ARM toolchain on the RTC test due to trying to lock the GCC environment mutex while in a critical section. Prior to this patch, this failure was likely to occur on STM and LPC processor families.
1 parent 4047ff9 commit 1da259a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

hal/common/mbed_rtc_time.c renamed to hal/common/mbed_rtc_time.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "critical.h"
2020
#include "rtc_time.h"
2121
#include "us_ticker_api.h"
22+
#include "SingletonPtr.h"
23+
#include "PlatformMutex.h"
24+
25+
static SingletonPtr<PlatformMutex> _mutex;
2226

2327
#if DEVICE_RTC
2428
static void (*_rtc_init)(void) = rtc_init;
@@ -42,7 +46,7 @@ time_t time(time_t *timer)
4246
#endif
4347

4448
{
45-
core_util_critical_section_enter();
49+
_mutex->lock();
4650
if (_rtc_isenabled != NULL) {
4751
if (!(_rtc_isenabled())) {
4852
set_time(0);
@@ -57,36 +61,36 @@ time_t time(time_t *timer)
5761
if (timer != NULL) {
5862
*timer = t;
5963
}
60-
core_util_critical_section_exit();
64+
_mutex->unlock();
6165
return t;
6266
}
6367

6468
void set_time(time_t t) {
65-
core_util_critical_section_enter();
69+
_mutex->lock();
6670
if (_rtc_init != NULL) {
6771
_rtc_init();
6872
}
6973
if (_rtc_write != NULL) {
7074
_rtc_write(t);
7175
}
72-
core_util_critical_section_exit();
76+
_mutex->unlock();
7377
}
7478

7579
clock_t clock() {
76-
core_util_critical_section_enter();
80+
_mutex->lock();
7781
clock_t t = us_ticker_read();
7882
t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
79-
core_util_critical_section_exit();
83+
_mutex->unlock();
8084
return t;
8185
}
8286

8387
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
84-
core_util_critical_section_enter();
88+
_mutex->lock();
8589
_rtc_read = read_rtc;
8690
_rtc_write = write_rtc;
8791
_rtc_init = init_rtc;
8892
_rtc_isenabled = isenabled_rtc;
89-
core_util_critical_section_exit();
93+
_mutex->unlock();
9094
}
9195

9296

0 commit comments

Comments
 (0)