Skip to content

Commit

Permalink
Merge pull request #790 from bjsowa/mbed-clock
Browse files Browse the repository at this point in the history
Implement clock in mbed port
  • Loading branch information
milyin authored Dec 6, 2024
2 parents 54191c8 + 355bb08 commit a4f07d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/zenoh-pico/system/platform/mbed.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef void *_z_mutex_t; // Workaround as MBED is a C++ library
typedef void *_z_condvar_t; // Workaround as MBED is a C++ library
#endif // Z_FEATURE_MULTI_THREAD == 1

typedef void *z_clock_t; // Not defined
typedef struct timespec z_clock_t;
typedef struct timeval z_time_t;

typedef struct BufferedSerial BufferedSerial; // Forward declaration to be used in _z_sys_net_socket_t
Expand Down
28 changes: 20 additions & 8 deletions src/system/mbed/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,35 @@ z_result_t z_sleep_s(size_t time) {

/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
// Not supported by default
return NULL;
auto now = Kernel::Clock::now();
auto duration = now.time_since_epoch();
auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration);
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(duration - secs);

z_clock_t ts;
ts.tv_sec = secs.count();
ts.tv_nsec = nanos.count();
return ts;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
// Not supported by default
return -1;
z_clock_t now = z_clock_now();
unsigned long elapsed =
(unsigned long)(1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
// Not supported by default
return -1;
z_clock_t now = z_clock_now();
unsigned long elapsed =
(unsigned long)(1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
// Not supported by default
return -1;
z_clock_t now = z_clock_now();
unsigned long elapsed = (unsigned long)(now.tv_sec - instant->tv_sec);
return elapsed;
}

/*------------------ Time ------------------*/
Expand Down

0 comments on commit a4f07d6

Please sign in to comment.