Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement clock in mbed port #790

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading