Skip to content

Commit 035c089

Browse files
committed
Move clock() function from mbed_rtc_time.cpp to mbed_retarget.cpp.
1 parent de6d291 commit 035c089

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
# include <rt_sys.h>
3843
# define PREFIX(x) _sys##x
@@ -985,3 +990,23 @@ void operator delete[](void *ptr)
985990
free(ptr);
986991
}
987992
}
993+
994+
/* @brief standard c library clock() function.
995+
*
996+
* This function returns the number of clock ticks elapsed since the start of the program.
997+
*
998+
* @note Synchronization level: Thread safe
999+
*
1000+
* @return
1001+
* the number of clock ticks elapsed since the start of the program.
1002+
*
1003+
* */
1004+
extern "C" clock_t clock()
1005+
{
1006+
_mutex->lock();
1007+
clock_t t = us_ticker_read();
1008+
t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
1009+
_mutex->unlock();
1010+
return t;
1011+
}
1012+

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)