time.c 656 B

123456789101112131415161718192021222324252627282930
  1. /* Copyright 2019 SiFive, Inc */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #include <metal/time.h>
  4. #include <metal/timer.h>
  5. int metal_gettimeofday(struct timeval *tp, void *tzp)
  6. {
  7. int rv;
  8. unsigned long long mcc, timebase;
  9. if (rv = metal_timer_get_cyclecount(0, &mcc)) {
  10. return -1;
  11. }
  12. if (rv = metal_timer_get_timebase_frequency(0, &timebase)) {
  13. return -1;
  14. }
  15. tp->tv_sec = mcc / timebase;
  16. tp->tv_usec = mcc % timebase * 1000000 / timebase;
  17. return 0;
  18. }
  19. time_t metal_time (void)
  20. {
  21. struct timeval now;
  22. if (metal_gettimeofday(&now, NULL) < 0)
  23. now.tv_sec = (time_t) -1;
  24. return now.tv_sec;
  25. }