Skip to content

Commit 01c864e

Browse files
Merge pull request #5099 from mprse/move_clock_function
Move clock() function from mbed_rtc_time.cpp to mbed_retarget.cpp.
2 parents 6177c01 + 035c089 commit 01c864e

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

platform/mbed_retarget.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include <time.h>
1617
#include "platform/platform.h"
1718
#include "platform/FilePath.h"
1819
#include "hal/serial_api.h"
20+
#include "hal/us_ticker_api.h"
1921
#include "platform/mbed_toolchain.h"
2022
#include "platform/mbed_semihost_api.h"
2123
#include "platform/mbed_interface.h"
@@ -24,6 +26,7 @@
2426
#include "platform/mbed_error.h"
2527
#include "platform/mbed_stats.h"
2628
#include "platform/mbed_critical.h"
29+
#include "platform/PlatformMutex.h"
2730
#include <stdlib.h>
2831
#include <string.h>
2932
#include <limits.h>
@@ -33,6 +36,8 @@
3336
#include <errno.h>
3437
#include "platform/mbed_retarget.h"
3538

39+
static SingletonPtr<PlatformMutex> _mutex;
40+
3641
#if defined(__ARMCC_VERSION)
3742
# if __ARMCC_VERSION >= 6010050
3843
# include <arm_compat.h>
@@ -1030,3 +1035,23 @@ void operator delete[](void *ptr)
10301035
free(ptr);
10311036
}
10321037
}
1038+
1039+
/* @brief standard c library clock() function.
1040+
*
1041+
* This function returns the number of clock ticks elapsed since the start of the program.
1042+
*
1043+
* @note Synchronization level: Thread safe
1044+
*
1045+
* @return
1046+
* the number of clock ticks elapsed since the start of the program.
1047+
*
1048+
* */
1049+
extern "C" clock_t clock()
1050+
{
1051+
_mutex->lock();
1052+
clock_t t = us_ticker_read();
1053+
t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
1054+
_mutex->unlock();
1055+
return t;
1056+
}
1057+

platform/mbed_rtc_time.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
*/
1616
#include "hal/rtc_api.h"
1717

18-
#include <time.h>
1918
#include "platform/mbed_critical.h"
2019
#include "platform/mbed_rtc_time.h"
21-
#include "hal/us_ticker_api.h"
2220
#include "platform/SingletonPtr.h"
2321
#include "platform/PlatformMutex.h"
2422

@@ -76,14 +74,6 @@ void set_time(time_t t) {
7674
_mutex->unlock();
7775
}
7876

79-
clock_t clock() {
80-
_mutex->lock();
81-
clock_t t = us_ticker_read();
82-
t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
83-
_mutex->unlock();
84-
return t;
85-
}
86-
8777
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
8878
_mutex->lock();
8979
_rtc_read = read_rtc;

0 commit comments

Comments
 (0)