alt_settod.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /******************************************************************************
  2. * *
  3. * License Agreement *
  4. * *
  5. * Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
  6. * All rights reserved. *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining a *
  9. * copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  24. * DEALINGS IN THE SOFTWARE. *
  25. * *
  26. * This agreement shall be governed in all respects by the laws of the State *
  27. * of California and by the laws of the United States of America. *
  28. * *
  29. * Altera does not recommend, suggest or require that this reference design *
  30. * file be used in conjunction or combination with any other product. *
  31. ******************************************************************************/
  32. #include <sys/time.h>
  33. #include <sys/times.h>
  34. #include "sys/alt_errno.h"
  35. #include "sys/alt_alarm.h"
  36. #include "os/alt_syscall.h"
  37. /*
  38. * "alt_timezone" and "alt_resettime" are the values of the the reset time and
  39. * time zone set through the last call to settimeofday(). By default they are
  40. * zero initialised.
  41. */
  42. extern struct timezone alt_timezone;
  43. extern struct timeval alt_resettime;
  44. /*
  45. * Macro defining the number of micoseconds in a second.
  46. */
  47. #define ALT_US (1000000)
  48. /*
  49. * settimeofday() can be called to calibrate the system clock, so that
  50. * subsequent calls to gettimeofday() will return the elapsed "wall clock"
  51. * time.
  52. *
  53. * This is done by updating the global structures "alt_resettime" and
  54. * "alt_timezone" so that an immediate call to gettimeofday() would return
  55. * the value specified by "t" and "tz".
  56. *
  57. * Warning: if this function is called concurrently with a call to
  58. * gettimeofday(), the value returned by gettimeofday() will be unreliable.
  59. *
  60. * ALT_SETTIMEOFDAY is mapped onto the settimeofday() system call in
  61. * alt_syscall.h
  62. */
  63. int ALT_SETTIMEOFDAY (const struct timeval *t,
  64. const struct timezone *tz)
  65. {
  66. alt_u32 nticks = alt_nticks ();
  67. alt_u32 tick_rate = alt_ticks_per_second ();
  68. /* If there is a system clock available, update the current time */
  69. if (tick_rate)
  70. {
  71. alt_resettime.tv_sec = t->tv_sec - nticks/tick_rate;
  72. alt_resettime.tv_usec = t->tv_usec -
  73. ((nticks*(ALT_US/tick_rate))%ALT_US);
  74. alt_timezone.tz_minuteswest = tz->tz_minuteswest;
  75. alt_timezone.tz_dsttime = tz->tz_dsttime;
  76. return 0;
  77. }
  78. /* There's no system clock available */
  79. ALT_ERRNO = ENOSYS;
  80. return -1;
  81. }