Repository: raspberrypi/pico-sdk
File: src/host/hardware_timer/timer.c (C)

Code before:
#include <sys/time.h>
#include <time.h>

#endif

// in our case not a busy wait
PICO_WEAK_FUNCTION_DEF(busy_wait_us)
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_us_32)(uint32_t delay_us) {
#if defined(__unix__) || defined(__APPLE__)
    usleep(delay_us);
#else
    assert(false);
#endif
}
PICO_WEAK_FUNCTION_DEF(busy_wait_us)
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_us)(uint64_t delay_us) {
    absolute_time_t t;
    update_us_since_boot(&t, time_us_64() + delay_us);
    busy_wait_until(t);
}

// this may or may not wrap
PICO_WEAK_FUNCTION_DEF(time_us_64)
uint64_t PICO_WEAK_FUNCTION_IMPL_NAME(time_us_64)() {
#if defined(__unix__) || defined(__APPLE__)
//    struct timeval tv;
//    gettimeofday(&tv, NULL);
//    return tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec;
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec * (uint64_t) 1000000 + ts.tv_nsec / 1000;
#else
    panic_unsupported();
#endif
}

PICO_WEAK_FUNCTION_DEF(timer_us_32)
uint32_t PICO_WEAK_FUNCTION_IMPL_NAME(timer_us_32)() {
    return (uint32_t) time_us_64();
}

PICO_WEAK_FUNCTION_DEF(time_reached)
bool PICO_WEAK_FUNCTION_IMPL_NAME(time_reached)(absolute_time_t t) {
    uint64_t target = to_us_since_boot(t);
    return time_us_64() >= target;
}

PICO_WEAK_FUNCTION_DEF(busy_wait_until)
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_until)(absolute_time_t target) {
#if defined(__unix__)
    struct timespec tspec;

Review comment (bug):
https://man7.org/linux/man-pages/man3/usleep.3.html says that `usleep` fails if "usec is greater than or equal to 1000000", which I guess means this `busy_wait_ms` function will fail if passed a `delay_ms` value >= 1000 ?
Is that something we ought to check for, or are we not really that concerned about the correct operation of "host platform mode"? :man_shrugging: 

Code after:
#include <sys/time.h>
#include <time.h>

#endif

// in our case not a busy wait
PICO_WEAK_FUNCTION_DEF(busy_wait_us_32)
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_us_32)(uint32_t delay_us) {
#if defined(__unix__) || defined(__APPLE__)
    usleep(delay_us);
#else
    assert(false);
#endif
}

PICO_WEAK_FUNCTION_DEF(busy_wait_us)
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_us)(uint64_t delay_us) {
    absolute_time_t t;
    update_us_since_boot(&t, time_us_64() + delay_us);
    busy_wait_until(t);
}

PICO_WEAK_FUNCTION_DEF(busy_wait_ms)
void PICO_WEAK_FUNCTION_IMPL_NAME(busy_wait_ms)(uint32_t delay_ms) {
#if defined(__unix__) || defined(__APPLE__)
    usleep(delay_ms * 1000);
#else
    assert(false);
#endif
}

// this may or may not wrap
PICO_WEAK_FUNCTION_DEF(time_us_64)
uint64_t PICO_WEAK_FUNCTION_IMPL_NAME(time_us_64)() {
#if defined(__unix__) || defined(__APPLE__)
//    struct timeval tv;
//    gettimeofday(&tv, NULL);
//    return tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec;
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return ts.tv_sec * (uint64_t) 1000000 + ts.tv_nsec / 1000;
#else
    panic_unsupported();
#endif
}

PICO_WEAK_FUNCTION_DEF(timer_us_32)
uint32_t PICO_WEAK_FUNCTION_IMPL_NAME(timer_us_32)() {
    return (uint32_t) time_us_64();
}