Skip to content

Commit a4f07d6

Browse files
authored
Merge pull request #790 from bjsowa/mbed-clock
Implement clock in mbed port
2 parents 54191c8 + 355bb08 commit a4f07d6

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

include/zenoh-pico/system/platform/mbed.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef void *_z_mutex_t; // Workaround as MBED is a C++ library
3333
typedef void *_z_condvar_t; // Workaround as MBED is a C++ library
3434
#endif // Z_FEATURE_MULTI_THREAD == 1
3535

36-
typedef void *z_clock_t; // Not defined
36+
typedef struct timespec z_clock_t;
3737
typedef struct timeval z_time_t;
3838

3939
typedef struct BufferedSerial BufferedSerial; // Forward declaration to be used in _z_sys_net_socket_t

src/system/mbed/system.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,35 @@ z_result_t z_sleep_s(size_t time) {
136136

137137
/*------------------ Instant ------------------*/
138138
z_clock_t z_clock_now(void) {
139-
// Not supported by default
140-
return NULL;
139+
auto now = Kernel::Clock::now();
140+
auto duration = now.time_since_epoch();
141+
auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration);
142+
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(duration - secs);
143+
144+
z_clock_t ts;
145+
ts.tv_sec = secs.count();
146+
ts.tv_nsec = nanos.count();
147+
return ts;
141148
}
142149

143150
unsigned long z_clock_elapsed_us(z_clock_t *instant) {
144-
// Not supported by default
145-
return -1;
151+
z_clock_t now = z_clock_now();
152+
unsigned long elapsed =
153+
(unsigned long)(1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
154+
return elapsed;
146155
}
147156

148157
unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
149-
// Not supported by default
150-
return -1;
158+
z_clock_t now = z_clock_now();
159+
unsigned long elapsed =
160+
(unsigned long)(1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
161+
return elapsed;
151162
}
152163

153164
unsigned long z_clock_elapsed_s(z_clock_t *instant) {
154-
// Not supported by default
155-
return -1;
165+
z_clock_t now = z_clock_now();
166+
unsigned long elapsed = (unsigned long)(now.tv_sec - instant->tv_sec);
167+
return elapsed;
156168
}
157169

158170
/*------------------ Time ------------------*/

0 commit comments

Comments
 (0)