ctime.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-08-21 zhangjun copy from minilibc
  9. * 2020-09-07 Meco Man combine gcc armcc iccarm
  10. * 2021-02-05 Meco Man add timegm()
  11. * 2021-02-07 Meco Man fixed gettimeofday()
  12. * 2021-02-08 Meco Man add settimeofday() stime()
  13. * 2021-02-10 Meco Man add ctime_r() and re-implement ctime()
  14. * 2021-02-11 Meco Man fix bug #3183 - align days[] and months[] to 4 bytes
  15. * 2021-02-12 Meco Man add errno
  16. * 2012-12-08 Bernard <clock_time.c> fix the issue of _timevalue.tv_usec initialization,
  17. * which found by Rob <rdent@iinet.net.au>
  18. * 2021-02-12 Meco Man move all of the functions located in <clock_time.c> to this file
  19. * 2021-03-15 Meco Man fixed a bug of leaking memory in asctime()
  20. * 2021-05-01 Meco Man support fixed timezone
  21. * 2021-07-21 Meco Man implement that change/set timezone APIs
  22. */
  23. #include "sys/time.h"
  24. #include <sys/errno.h>
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include <unistd.h>
  28. #ifdef RT_USING_SMART
  29. #include "lwp.h"
  30. #endif
  31. #ifdef RT_USING_POSIX_DELAY
  32. #include <delay.h>
  33. #endif
  34. #if defined( RT_USING_RTC ) || defined( RT_USING_CPUTIME)
  35. #include <rtdevice.h>
  36. #endif
  37. #define DBG_TAG "time"
  38. #define DBG_LVL DBG_INFO
  39. #include <rtdbg.h>
  40. #define _WARNING_NO_RTC "Cannot find a RTC device!"
  41. /* seconds per day */
  42. #define SPD 24*60*60
  43. /* days per month -- nonleap! */
  44. static const short __spm[13] =
  45. {
  46. 0,
  47. (31),
  48. (31 + 28),
  49. (31 + 28 + 31),
  50. (31 + 28 + 31 + 30),
  51. (31 + 28 + 31 + 30 + 31),
  52. (31 + 28 + 31 + 30 + 31 + 30),
  53. (31 + 28 + 31 + 30 + 31 + 30 + 31),
  54. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31),
  55. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
  56. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
  57. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30),
  58. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31),
  59. };
  60. rt_align(4) static const char *days = "Sun Mon Tue Wed Thu Fri Sat ";
  61. rt_align(4) static const char *months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
  62. static int __isleap(int year)
  63. {
  64. /* every fourth year is a leap year except for century years that are
  65. * not divisible by 400. */
  66. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  67. return (!(year % 4) && ((year % 100) || !(year % 400)));
  68. }
  69. static void num2str(char *c, int i)
  70. {
  71. c[0] = i / 10 + '0';
  72. c[1] = i % 10 + '0';
  73. }
  74. /**
  75. * Get time from RTC device (without timezone, UTC+0)
  76. * @param tv: struct timeval
  77. * @return the operation status, RT_EOK on successful
  78. */
  79. static rt_err_t get_timeval(struct timeval *tv)
  80. {
  81. #ifdef RT_USING_RTC
  82. static rt_device_t device = RT_NULL;
  83. rt_err_t rst = -RT_ERROR;
  84. if (tv == RT_NULL)
  85. return -RT_EINVAL;
  86. /* default is 0 */
  87. tv->tv_sec = 0;
  88. tv->tv_usec = 0;
  89. /* optimization: find rtc device only first */
  90. if (device == RT_NULL)
  91. {
  92. device = rt_device_find("rtc");
  93. }
  94. /* read timestamp from RTC device */
  95. if (device != RT_NULL)
  96. {
  97. if (rt_device_open(device, 0) == RT_EOK)
  98. {
  99. rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec);
  100. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv);
  101. rt_device_close(device);
  102. }
  103. }
  104. else
  105. {
  106. LOG_W(_WARNING_NO_RTC);
  107. return -RT_ENOSYS;
  108. }
  109. return rst;
  110. #else
  111. LOG_W(_WARNING_NO_RTC);
  112. return -RT_ENOSYS;
  113. #endif /* RT_USING_RTC */
  114. }
  115. /**
  116. * Set time to RTC device (without timezone)
  117. * @param tv: struct timeval
  118. * @return the operation status, RT_EOK on successful
  119. */
  120. static int set_timeval(struct timeval *tv)
  121. {
  122. #ifdef RT_USING_RTC
  123. static rt_device_t device = RT_NULL;
  124. rt_err_t rst = -RT_ERROR;
  125. if (tv == RT_NULL)
  126. return -RT_EINVAL;
  127. /* optimization: find rtc device only first */
  128. if (device == RT_NULL)
  129. {
  130. device = rt_device_find("rtc");
  131. }
  132. /* read timestamp from RTC device */
  133. if (device != RT_NULL)
  134. {
  135. if (rt_device_open(device, 0) == RT_EOK)
  136. {
  137. rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec);
  138. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIMEVAL, tv);
  139. rt_device_close(device);
  140. }
  141. }
  142. else
  143. {
  144. LOG_W(_WARNING_NO_RTC);
  145. return -RT_ENOSYS;
  146. }
  147. return rst;
  148. #else
  149. LOG_W(_WARNING_NO_RTC);
  150. return -RT_ENOSYS;
  151. #endif /* RT_USING_RTC */
  152. }
  153. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  154. {
  155. int i;
  156. int work = *timep % (SPD);
  157. if(timep == RT_NULL || r == RT_NULL)
  158. {
  159. rt_set_errno(EFAULT);
  160. return RT_NULL;
  161. }
  162. rt_memset(r, RT_NULL, sizeof(struct tm));
  163. r->tm_sec = work % 60;
  164. work /= 60;
  165. r->tm_min = work % 60;
  166. r->tm_hour = work / 60;
  167. work = (int)(*timep / (SPD));
  168. r->tm_wday = (4 + work) % 7;
  169. for (i = 1970;; ++i)
  170. {
  171. int k = __isleap(i) ? 366 : 365;
  172. if (work >= k)
  173. work -= k;
  174. else
  175. break;
  176. }
  177. r->tm_year = i - 1900;
  178. r->tm_yday = work;
  179. r->tm_mday = 1;
  180. if (__isleap(i) && (work > 58))
  181. {
  182. if (work == 59)
  183. r->tm_mday = 2; /* 29.2. */
  184. work -= 1;
  185. }
  186. for (i = 11; i && (__spm[i] > work); --i);
  187. r->tm_mon = i;
  188. r->tm_mday += work - __spm[i];
  189. r->tm_isdst = tz_is_dst();
  190. return r;
  191. }
  192. RTM_EXPORT(gmtime_r);
  193. struct tm* gmtime(const time_t* t)
  194. {
  195. static struct tm tmp;
  196. return gmtime_r(t, &tmp);
  197. }
  198. RTM_EXPORT(gmtime);
  199. struct tm* localtime_r(const time_t* t, struct tm* r)
  200. {
  201. time_t local_tz;
  202. local_tz = *t + (time_t)tz_get() * 3600;
  203. return gmtime_r(&local_tz, r);
  204. }
  205. RTM_EXPORT(localtime_r);
  206. struct tm* localtime(const time_t* t)
  207. {
  208. static struct tm tmp;
  209. return localtime_r(t, &tmp);
  210. }
  211. RTM_EXPORT(localtime);
  212. time_t mktime(struct tm * const t)
  213. {
  214. time_t timestamp;
  215. timestamp = timegm(t);
  216. timestamp = timestamp - 3600 * (time_t)tz_get();
  217. return timestamp;
  218. }
  219. RTM_EXPORT(mktime);
  220. char* asctime_r(const struct tm *t, char *buf)
  221. {
  222. if(t == RT_NULL || buf == RT_NULL)
  223. {
  224. rt_set_errno(EFAULT);
  225. return RT_NULL;
  226. }
  227. rt_memset(buf, RT_NULL, 26);
  228. /* Checking input validity */
  229. if ((int)rt_strlen(days) <= (t->tm_wday << 2) || (int)rt_strlen(months) <= (t->tm_mon << 2))
  230. {
  231. LOG_W("asctime_r: the input parameters exceeded the limit, please check it.");
  232. *(int*) buf = *(int*) days;
  233. *(int*) (buf + 4) = *(int*) months;
  234. num2str(buf + 8, t->tm_mday);
  235. if (buf[8] == '0')
  236. buf[8] = ' ';
  237. buf[10] = ' ';
  238. num2str(buf + 11, t->tm_hour);
  239. buf[13] = ':';
  240. num2str(buf + 14, t->tm_min);
  241. buf[16] = ':';
  242. num2str(buf + 17, t->tm_sec);
  243. buf[19] = ' ';
  244. num2str(buf + 20, 2000 / 100);
  245. num2str(buf + 22, 2000 % 100);
  246. buf[24] = '\n';
  247. buf[25] = '\0';
  248. return buf;
  249. }
  250. /* "Wed Jun 30 21:49:08 1993\n" */
  251. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  252. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  253. num2str(buf + 8, t->tm_mday);
  254. if (buf[8] == '0')
  255. buf[8] = ' ';
  256. buf[10] = ' ';
  257. num2str(buf + 11, t->tm_hour);
  258. buf[13] = ':';
  259. num2str(buf + 14, t->tm_min);
  260. buf[16] = ':';
  261. num2str(buf + 17, t->tm_sec);
  262. buf[19] = ' ';
  263. num2str(buf + 20, (t->tm_year + 1900) / 100);
  264. num2str(buf + 22, (t->tm_year + 1900) % 100);
  265. buf[24] = '\n';
  266. buf[25] = '\0';
  267. return buf;
  268. }
  269. RTM_EXPORT(asctime_r);
  270. char* asctime(const struct tm *timeptr)
  271. {
  272. static char buf[26];
  273. return asctime_r(timeptr, buf);
  274. }
  275. RTM_EXPORT(asctime);
  276. char *ctime_r(const time_t * tim_p, char * result)
  277. {
  278. struct tm tm;
  279. return asctime_r(localtime_r(tim_p, &tm), result);
  280. }
  281. RTM_EXPORT(ctime_r);
  282. char* ctime(const time_t *tim_p)
  283. {
  284. return asctime(localtime(tim_p));
  285. }
  286. RTM_EXPORT(ctime);
  287. #ifndef __ICCARM__
  288. double difftime(time_t time1, time_t time2)
  289. {
  290. return (double)(time1 - time2);
  291. }
  292. #endif /* __ICCARM__ */
  293. RTM_EXPORT(difftime);
  294. RTM_EXPORT(strftime); /* inherent in the toolchain */
  295. /**
  296. * Returns the current time.
  297. *
  298. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  299. *
  300. * @return The value ((time_t)-1) is returned if the calendar time is not available.
  301. * If timer is not a NULL pointer, the return value is also stored in timer.
  302. *
  303. */
  304. rt_weak time_t time(time_t *t)
  305. {
  306. struct timeval now;
  307. if(get_timeval(&now) == RT_EOK)
  308. {
  309. if (t)
  310. {
  311. *t = now.tv_sec;
  312. }
  313. return now.tv_sec;
  314. }
  315. else
  316. {
  317. rt_set_errno(EFAULT);
  318. return ((time_t)-1);
  319. }
  320. }
  321. RTM_EXPORT(time);
  322. rt_weak clock_t clock(void)
  323. {
  324. return rt_tick_get();
  325. }
  326. RTM_EXPORT(clock);
  327. int stime(const time_t *t)
  328. {
  329. struct timeval tv;
  330. if (t == RT_NULL)
  331. {
  332. rt_set_errno(EFAULT);
  333. return -1;
  334. }
  335. tv.tv_sec = *t;
  336. if (set_timeval(&tv) == RT_EOK)
  337. {
  338. return 0;
  339. }
  340. else
  341. {
  342. rt_set_errno(EFAULT);
  343. return -1;
  344. }
  345. }
  346. RTM_EXPORT(stime);
  347. time_t timegm(struct tm * const t)
  348. {
  349. time_t day;
  350. time_t i;
  351. time_t years;
  352. if(t == RT_NULL)
  353. {
  354. rt_set_errno(EFAULT);
  355. return (time_t)-1;
  356. }
  357. years = (time_t)t->tm_year - 70;
  358. if (t->tm_sec > 60) /* seconds after the minute - [0, 60] including leap second */
  359. {
  360. t->tm_min += t->tm_sec / 60;
  361. t->tm_sec %= 60;
  362. }
  363. if (t->tm_min >= 60) /* minutes after the hour - [0, 59] */
  364. {
  365. t->tm_hour += t->tm_min / 60;
  366. t->tm_min %= 60;
  367. }
  368. if (t->tm_hour >= 24) /* hours since midnight - [0, 23] */
  369. {
  370. t->tm_mday += t->tm_hour / 24;
  371. t->tm_hour %= 24;
  372. }
  373. if (t->tm_mon >= 12) /* months since January - [0, 11] */
  374. {
  375. t->tm_year += t->tm_mon / 12;
  376. t->tm_mon %= 12;
  377. }
  378. while (t->tm_mday > __spm[1 + t->tm_mon])
  379. {
  380. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  381. {
  382. --t->tm_mday;
  383. }
  384. t->tm_mday -= __spm[t->tm_mon];
  385. ++t->tm_mon;
  386. if (t->tm_mon > 11)
  387. {
  388. t->tm_mon = 0;
  389. ++t->tm_year;
  390. }
  391. }
  392. if (t->tm_year < 70)
  393. {
  394. rt_set_errno(EINVAL);
  395. return (time_t) -1;
  396. }
  397. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  398. day = years * 365 + (years + 1) / 4;
  399. /* After 2100 we have to substract 3 leap years for every 400 years
  400. This is not intuitive. Most mktime implementations do not support
  401. dates after 2059, anyway, so we might leave this out for it's
  402. bloat. */
  403. if (years >= 131)
  404. {
  405. years -= 131;
  406. years /= 100;
  407. day -= (years >> 2) * 3 + 1;
  408. if ((years &= 3) == 3)
  409. years--;
  410. day -= years;
  411. }
  412. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  413. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  414. /* day is now the number of days since 'Jan 1 1970' */
  415. i = 7;
  416. t->tm_wday = (int)((day + 4) % i); /* Sunday=0, Monday=1, ..., Saturday=6 */
  417. i = 24;
  418. day *= i;
  419. i = 60;
  420. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  421. }
  422. RTM_EXPORT(timegm);
  423. int gettimeofday(struct timeval *tv, struct timezone *tz)
  424. {
  425. /* The use of the timezone structure is obsolete;
  426. * the tz argument should normally be specified as NULL.
  427. * The tz_dsttime field has never been used under Linux.
  428. * Thus, the following is purely of historic interest.
  429. */
  430. if(tz != RT_NULL)
  431. {
  432. tz->tz_dsttime = DST_NONE;
  433. tz->tz_minuteswest = -(tz_get() * 60);
  434. }
  435. if (tv != RT_NULL && get_timeval(tv) == RT_EOK)
  436. {
  437. return 0;
  438. }
  439. else
  440. {
  441. rt_set_errno(EINVAL);
  442. return -1;
  443. }
  444. }
  445. RTM_EXPORT(gettimeofday);
  446. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  447. {
  448. /* The use of the timezone structure is obsolete;
  449. * the tz argument should normally be specified as NULL.
  450. * The tz_dsttime field has never been used under Linux.
  451. * Thus, the following is purely of historic interest.
  452. */
  453. if (tv != RT_NULL
  454. && tv->tv_usec >= 0
  455. && set_timeval((struct timeval *)tv) == RT_EOK)
  456. {
  457. return 0;
  458. }
  459. else
  460. {
  461. rt_set_errno(EINVAL);
  462. return -1;
  463. }
  464. }
  465. RTM_EXPORT(settimeofday);
  466. #ifdef RT_USING_POSIX_DELAY
  467. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  468. {
  469. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  470. {
  471. rt_set_errno(EINVAL);
  472. return -1;
  473. }
  474. #ifdef RT_USING_CPUTIME
  475. double unit = clock_cpu_getres();
  476. rt_uint64_t ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec;
  477. rt_uint64_t tick = ns / unit;
  478. rt_cputime_sleep(tick);
  479. if (rt_get_errno() == -RT_EINTR)
  480. {
  481. if (rmtp)
  482. {
  483. uint64_t rmtp_cpu_tick = tick - clock_cpu_gettime();
  484. rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  485. rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  486. }
  487. rt_set_errno(EINTR);
  488. return -1;
  489. }
  490. #else
  491. rt_tick_t tick, tick_old = rt_tick_get();
  492. tick = rqtp->tv_sec * RT_TICK_PER_SECOND + ((uint64_t)rqtp->tv_nsec * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  493. rt_thread_delay(tick);
  494. if (rt_get_errno() == -RT_EINTR)
  495. {
  496. if (rmtp)
  497. {
  498. tick = tick_old + tick - rt_tick_get();
  499. /* get the passed time */
  500. rmtp->tv_sec = tick / RT_TICK_PER_SECOND;
  501. rmtp->tv_nsec = (tick % RT_TICK_PER_SECOND) * (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND);
  502. }
  503. rt_set_errno(EINTR);
  504. return -1;
  505. }
  506. #endif
  507. return 0;
  508. }
  509. RTM_EXPORT(nanosleep);
  510. #endif /* RT_USING_POSIX_DELAY */
  511. #ifdef RT_USING_POSIX_CLOCK
  512. #ifdef RT_USING_RTC
  513. static volatile struct timeval _timevalue;
  514. static int _rt_clock_time_system_init(void)
  515. {
  516. rt_base_t level;
  517. time_t time = 0;
  518. rt_tick_t tick;
  519. rt_device_t device;
  520. device = rt_device_find("rtc");
  521. if (device != RT_NULL)
  522. {
  523. /* get realtime seconds */
  524. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
  525. {
  526. level = rt_hw_interrupt_disable();
  527. tick = rt_tick_get(); /* get tick */
  528. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  529. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  530. rt_hw_interrupt_enable(level);
  531. return 0;
  532. }
  533. }
  534. level = rt_hw_interrupt_disable();
  535. _timevalue.tv_usec = 0;
  536. _timevalue.tv_sec = 0;
  537. rt_hw_interrupt_enable(level);
  538. return -1;
  539. }
  540. INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
  541. #endif /* RT_USING_RTC */
  542. int clock_getres(clockid_t clockid, struct timespec *res)
  543. {
  544. #ifndef RT_USING_RTC
  545. LOG_W(_WARNING_NO_RTC);
  546. return -1;
  547. #else
  548. int ret = 0;
  549. if (res == RT_NULL)
  550. {
  551. rt_set_errno(EFAULT);
  552. return -1;
  553. }
  554. switch (clockid)
  555. {
  556. case CLOCK_REALTIME:
  557. res->tv_sec = 0;
  558. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  559. break;
  560. #ifdef RT_USING_CPUTIME
  561. case CLOCK_CPUTIME_ID:
  562. res->tv_sec = 0;
  563. res->tv_nsec = clock_cpu_getres();
  564. break;
  565. #endif
  566. default:
  567. res->tv_sec = 0;
  568. res->tv_nsec = 0;
  569. ret = -1;
  570. rt_set_errno(EINVAL);
  571. break;
  572. }
  573. return ret;
  574. #endif /* RT_USING_RTC */
  575. }
  576. RTM_EXPORT(clock_getres);
  577. int clock_gettime(clockid_t clockid, struct timespec *tp)
  578. {
  579. #ifndef RT_USING_RTC
  580. LOG_W(_WARNING_NO_RTC);
  581. return -1;
  582. #else
  583. int ret = 0;
  584. if (tp == RT_NULL)
  585. {
  586. rt_set_errno(EFAULT);
  587. return -1;
  588. }
  589. switch (clockid)
  590. {
  591. case CLOCK_REALTIME:
  592. {
  593. rt_tick_t tick;
  594. rt_base_t level;
  595. level = rt_hw_interrupt_disable();
  596. tick = rt_tick_get(); /* get tick */
  597. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  598. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  599. rt_hw_interrupt_enable(level);
  600. }
  601. break;
  602. #ifdef RT_USING_CPUTIME
  603. case CLOCK_MONOTONIC:
  604. case CLOCK_CPUTIME_ID:
  605. {
  606. double unit = 0;
  607. uint64_t cpu_tick;
  608. unit = clock_cpu_getres();
  609. cpu_tick = clock_cpu_gettime();
  610. tp->tv_sec = ((uint64_t)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  611. tp->tv_nsec = ((uint64_t)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  612. }
  613. break;
  614. #endif
  615. default:
  616. tp->tv_sec = 0;
  617. tp->tv_nsec = 0;
  618. rt_set_errno(EINVAL);
  619. ret = -1;
  620. }
  621. return ret;
  622. #endif /* RT_USING_RTC */
  623. }
  624. RTM_EXPORT(clock_gettime);
  625. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp)
  626. {
  627. #ifndef RT_USING_RTC
  628. LOG_W(_WARNING_NO_RTC);
  629. return -1;
  630. #else
  631. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  632. {
  633. rt_set_errno(EINVAL);
  634. return -1;
  635. }
  636. switch (clockid)
  637. {
  638. case CLOCK_REALTIME:
  639. {
  640. rt_tick_t tick, tick_old = rt_tick_get();
  641. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  642. {
  643. rt_int64_t ts = ((rqtp->tv_sec - _timevalue.tv_sec) * RT_TICK_PER_SECOND);
  644. rt_int64_t tns = (rqtp->tv_nsec - _timevalue.tv_usec * 1000) * (RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND);
  645. tick = ts + tns;
  646. rt_tick_t rt_tick = rt_tick_get();
  647. tick = tick < rt_tick ? 0 : tick - rt_tick;
  648. }
  649. else
  650. {
  651. tick = rqtp->tv_sec * RT_TICK_PER_SECOND + ((uint64_t)(rqtp->tv_nsec) * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  652. }
  653. rt_thread_delay(tick);
  654. if (rt_get_errno() == -RT_EINTR)
  655. {
  656. if (rmtp)
  657. {
  658. tick = tick_old + tick - rt_tick_get();
  659. /* get the passed time */
  660. rmtp->tv_sec = tick / RT_TICK_PER_SECOND;
  661. rmtp->tv_nsec = (tick % RT_TICK_PER_SECOND) * (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND);
  662. }
  663. rt_set_errno(EINTR);
  664. return -1;
  665. }
  666. }
  667. break;
  668. #ifdef RT_USING_CPUTIME
  669. case CLOCK_MONOTONIC:
  670. case CLOCK_CPUTIME_ID:
  671. {
  672. rt_uint64_t cpu_tick_old = clock_cpu_gettime();
  673. double unit = clock_cpu_getres();
  674. rt_uint64_t ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec;
  675. rt_uint64_t tick = ns / unit;
  676. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  677. tick -= cpu_tick_old;
  678. rt_cputime_sleep(tick);
  679. if (rt_get_errno() == -RT_EINTR)
  680. {
  681. if (rmtp)
  682. {
  683. uint64_t rmtp_cpu_tick = tick - clock_cpu_gettime();
  684. rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  685. rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  686. }
  687. rt_set_errno(EINTR);
  688. return -1;
  689. }
  690. }
  691. break;
  692. #endif
  693. default:
  694. rt_set_errno(EINVAL);
  695. return -1;
  696. }
  697. return 0;
  698. #endif
  699. }
  700. RTM_EXPORT(clock_nanosleep);
  701. int clock_settime(clockid_t clockid, const struct timespec *tp)
  702. {
  703. #ifndef RT_USING_RTC
  704. LOG_W(_WARNING_NO_RTC);
  705. return -1;
  706. #else
  707. rt_base_t level;
  708. int second;
  709. rt_tick_t tick;
  710. rt_device_t device;
  711. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  712. {
  713. rt_set_errno(EFAULT);
  714. return -1;
  715. }
  716. /* get second */
  717. second = tp->tv_sec;
  718. level = rt_hw_interrupt_disable();
  719. tick = rt_tick_get(); /* get tick */
  720. /* update timevalue */
  721. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  722. _timevalue.tv_sec = second - tick / RT_TICK_PER_SECOND - 1;
  723. rt_hw_interrupt_enable(level);
  724. /* update for RTC device */
  725. device = rt_device_find("rtc");
  726. if (device != RT_NULL)
  727. {
  728. /* set realtime seconds */
  729. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
  730. {
  731. return 0;
  732. }
  733. }
  734. return -1;
  735. #endif /* RT_USING_RTC */
  736. }
  737. RTM_EXPORT(clock_settime);
  738. int rt_timespec_to_tick(const struct timespec *time)
  739. {
  740. int tick;
  741. int nsecond, second;
  742. struct timespec tp = {0};
  743. RT_ASSERT(time != RT_NULL);
  744. tick = RT_WAITING_FOREVER;
  745. if (time != NULL)
  746. {
  747. /* get current tp */
  748. clock_gettime(CLOCK_REALTIME, &tp);
  749. if ((time->tv_nsec - tp.tv_nsec) < 0)
  750. {
  751. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  752. second = time->tv_sec - tp.tv_sec - 1;
  753. }
  754. else
  755. {
  756. nsecond = time->tv_nsec - tp.tv_nsec;
  757. second = time->tv_sec - tp.tv_sec;
  758. }
  759. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  760. if (tick < 0) tick = 0;
  761. }
  762. return tick;
  763. }
  764. RTM_EXPORT(rt_timespec_to_tick);
  765. #endif /* RT_USING_POSIX_CLOCK */
  766. #ifdef RT_USING_POSIX_TIMER
  767. #define ACTIVE 1
  768. #define NOT_ACTIVE 0
  769. struct timer_obj
  770. {
  771. union
  772. {
  773. struct rt_timer timer;
  774. #ifdef RT_USING_CPUTIME
  775. struct rt_cputimer cputimer;
  776. #endif
  777. };
  778. void (*sigev_notify_function)(union sigval val);
  779. union sigval val;
  780. struct timespec interval; /* Reload value */
  781. struct timespec value; /* Reload value */
  782. rt_uint64_t reload; /* Reload value in ms */
  783. rt_uint32_t status;
  784. int sigev_signo;
  785. clockid_t clockid;
  786. #ifdef RT_USING_SMART
  787. pid_t pid;
  788. #endif
  789. };
  790. static void rtthread_timer_wrapper(void *timerobj)
  791. {
  792. struct timer_obj *timer;
  793. timer = (struct timer_obj *)timerobj;
  794. if (timer->reload == 0U)
  795. {
  796. timer->status = NOT_ACTIVE;
  797. }
  798. #ifdef RT_USING_CPUTIME
  799. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  800. {
  801. timer->reload = (timer->interval.tv_sec * NANOSECOND_PER_SECOND + timer->interval.tv_nsec) / clock_cpu_getres();
  802. if (timer->reload)
  803. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  804. }
  805. else
  806. #endif /* RT_USING_CPUTIME */
  807. {
  808. timer->reload = (timer->interval.tv_sec * RT_TICK_PER_SECOND) + (timer->interval.tv_nsec * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  809. if (timer->reload)
  810. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  811. }
  812. #ifdef RT_USING_SMART
  813. sys_kill(timer->pid, timer->sigev_signo);
  814. #else
  815. if(timer->sigev_notify_function != RT_NULL)
  816. {
  817. (timer->sigev_notify_function)(timer->val);
  818. }
  819. #endif
  820. }
  821. #define TIMER_ID_MAX 50
  822. static struct timer_obj *_g_timerid[TIMER_ID_MAX];
  823. static int timerid_idx = 0;
  824. RT_DEFINE_SPINLOCK(_timer_id_lock);
  825. void timer_id_init(void)
  826. {
  827. for (int i = 0; i < TIMER_ID_MAX; i++)
  828. {
  829. _g_timerid[i] = NULL;
  830. }
  831. timerid_idx = 0;
  832. }
  833. int timer_id_alloc(void)
  834. {
  835. for (int i = 0; i < timerid_idx; i++)
  836. {
  837. if (_g_timerid[i] == NULL)
  838. return i;
  839. }
  840. if (timerid_idx < TIMER_ID_MAX)
  841. {
  842. timerid_idx++;
  843. return timerid_idx; /* todo */
  844. }
  845. return -1;
  846. }
  847. void timer_id_lock()
  848. {
  849. rt_hw_spin_lock(&_timer_id_lock);
  850. }
  851. void timer_id_unlock()
  852. {
  853. rt_hw_spin_unlock(&_timer_id_lock);
  854. }
  855. struct timer_obj *timer_id_get(int timerid)
  856. {
  857. struct timer_obj *timer;
  858. timer_id_lock();
  859. if (_g_timerid[timerid] == NULL)
  860. {
  861. timer_id_unlock();
  862. LOG_E("can not find timer!");
  863. return NULL;
  864. }
  865. timer = _g_timerid[timerid];
  866. timer_id_unlock();
  867. return timer;
  868. }
  869. int timer_id_put(int id)
  870. {
  871. if (_g_timerid[id] == NULL)
  872. return -1;
  873. _g_timerid[id] = NULL;
  874. return 0;
  875. }
  876. /**
  877. * @brief Create a per-process timer.
  878. *
  879. * This API does not accept SIGEV_THREAD as valid signal event notification
  880. * type.
  881. *
  882. * See IEEE 1003.1
  883. */
  884. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
  885. {
  886. static int num = 0;
  887. int _timerid = 0;
  888. struct timer_obj *timer;
  889. char timername[RT_NAME_MAX] = {0};
  890. if (clockid > CLOCK_ID_MAX ||
  891. (evp->sigev_notify != SIGEV_NONE &&
  892. evp->sigev_notify != SIGEV_SIGNAL))
  893. {
  894. rt_set_errno(EINVAL);
  895. return -1;
  896. }
  897. timer = rt_malloc(sizeof(struct timer_obj));
  898. if(timer == RT_NULL)
  899. {
  900. rt_set_errno(ENOMEM);
  901. return -1;
  902. }
  903. rt_snprintf(timername, RT_NAME_MAX, "psx_tm%02d", num++);
  904. num %= 100;
  905. timer->sigev_signo = evp->sigev_signo;
  906. #ifdef RT_USING_SMART
  907. timer->pid = lwp_self()->pid;
  908. #endif
  909. timer->sigev_notify_function = evp->sigev_notify_function;
  910. timer->val = evp->sigev_value;
  911. timer->interval.tv_sec = 0;
  912. timer->interval.tv_nsec = 0;
  913. timer->reload = 0U;
  914. timer->status = NOT_ACTIVE;
  915. timer->clockid = clockid;
  916. #ifdef RT_USING_CPUTIME
  917. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  918. {
  919. rt_cputimer_init(&timer->cputimer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  920. }
  921. else
  922. #endif /* RT_USING_CPUTIME */
  923. {
  924. if (evp->sigev_notify == SIGEV_NONE)
  925. rt_timer_init(&timer->timer, timername, RT_NULL, RT_NULL, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  926. else
  927. rt_timer_init(&timer->timer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  928. }
  929. timer_id_lock();
  930. _timerid = timer_id_alloc();
  931. if (_timerid < 0)
  932. {
  933. timer_id_unlock();
  934. LOG_E("_timerid overflow!");
  935. return -1; /* todo:memory leak */
  936. }
  937. _g_timerid[_timerid] = timer;
  938. *timerid = (timer_t)(rt_ubase_t)_timerid;
  939. timer_id_unlock();
  940. return 0;
  941. }
  942. RTM_EXPORT(timer_create);
  943. /**
  944. * @brief Delete a per-process timer.
  945. *
  946. * See IEEE 1003.1
  947. */
  948. int timer_delete(timer_t timerid)
  949. {
  950. struct timer_obj *timer;
  951. timer_id_lock();
  952. if (_g_timerid[(rt_ubase_t)timerid] == NULL)
  953. {
  954. timer_id_unlock();
  955. rt_set_errno(EINVAL);
  956. LOG_E("can not find timer!");
  957. return -1;
  958. }
  959. timer = _g_timerid[(rt_ubase_t)timerid];
  960. timer_id_put((rt_ubase_t)timerid);
  961. timer_id_unlock();
  962. if (timer == RT_NULL)
  963. {
  964. rt_set_errno(EINVAL);
  965. return -1;
  966. }
  967. #ifdef RT_USING_CPUTIME
  968. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  969. {
  970. if (timer->status == ACTIVE)
  971. {
  972. timer->status = NOT_ACTIVE;
  973. rt_cputimer_stop(&timer->cputimer);
  974. }
  975. rt_cputimer_detach(&timer->cputimer);
  976. }
  977. else
  978. #endif /* RT_USING_CPUTIME */
  979. {
  980. if (timer->status == ACTIVE)
  981. {
  982. timer->status = NOT_ACTIVE;
  983. rt_timer_stop(&timer->timer);
  984. }
  985. rt_timer_detach(&timer->timer);
  986. }
  987. rt_free(timer);
  988. return 0;
  989. }
  990. RTM_EXPORT(timer_delete);
  991. /**
  992. *
  993. * Return the overrun count for the last timer expiration.
  994. * It is subefficient to create a new structure to get overrun count.
  995. **/
  996. int timer_getoverrun(timer_t timerid)
  997. {
  998. rt_set_errno(ENOSYS);
  999. return -1;
  1000. }
  1001. /**
  1002. * @brief Get amount of time left for expiration on a per-process timer.
  1003. *
  1004. * See IEEE 1003.1
  1005. */
  1006. int timer_gettime(timer_t timerid, struct itimerspec *its)
  1007. {
  1008. struct timer_obj *timer = timer_id_get((rt_ubase_t)timerid);
  1009. rt_uint32_t seconds, nanoseconds;
  1010. if (timer == NULL)
  1011. {
  1012. rt_set_errno(EINVAL);
  1013. return -1;
  1014. }
  1015. if (its == NULL)
  1016. {
  1017. rt_set_errno(EFAULT);
  1018. return -1;
  1019. }
  1020. if (timer->status == ACTIVE)
  1021. {
  1022. #ifdef RT_USING_CPUTIME
  1023. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1024. {
  1025. rt_uint64_t remain_tick;
  1026. rt_uint64_t remaining;
  1027. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick);
  1028. remaining = (remain_tick - clock_cpu_gettime()) / clock_cpu_getres();
  1029. seconds = remaining / NANOSECOND_PER_SECOND;
  1030. nanoseconds = remaining % NANOSECOND_PER_SECOND;
  1031. }
  1032. else
  1033. #endif /* RT_USING_CPUTIME */
  1034. {
  1035. rt_tick_t remain_tick;
  1036. rt_tick_t remaining;
  1037. rt_timer_control(&timer->timer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick);
  1038. /* 'remain_tick' is minimum-unit in the RT-Thread' timer,
  1039. * so the seconds, nanoseconds will be calculated by 'remain_tick'.
  1040. */
  1041. remaining = remain_tick - rt_tick_get();
  1042. /* calculate 'second' */
  1043. seconds = remaining / RT_TICK_PER_SECOND;
  1044. /* calculate 'nanosecond'; To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
  1045. *
  1046. * remain_tick millisecond remain_tick * MILLISECOND_PER_SECOND
  1047. * ------------------------- = -------------------------- ---> millisecond = -------------------------------------------
  1048. * RT_TICK_PER_SECOND MILLISECOND_PER_SECOND RT_TICK_PER_SECOND
  1049. *
  1050. * remain_tick * MILLISECOND_PER_SECOND remain_tick * MILLISECOND_PER_SECOND * MICROSECOND_PER_SECOND
  1051. * millisecond = ---------------------------------------- ---> nanosecond = -------------------------------------------------------------------
  1052. * RT_TICK_PER_SECOND RT_TICK_PER_SECOND
  1053. *
  1054. */
  1055. nanoseconds = (((remaining % RT_TICK_PER_SECOND) * MILLISECOND_PER_SECOND) * MICROSECOND_PER_SECOND) / RT_TICK_PER_SECOND;
  1056. }
  1057. its->it_value.tv_sec = (rt_int32_t)seconds;
  1058. its->it_value.tv_nsec = (rt_int32_t)nanoseconds;
  1059. }
  1060. else
  1061. {
  1062. /* Timer is disarmed */
  1063. its->it_value.tv_sec = 0;
  1064. its->it_value.tv_nsec = 0;
  1065. }
  1066. /* The interval last set by timer_settime() */
  1067. its->it_interval = timer->interval;
  1068. return 0;
  1069. }
  1070. RTM_EXPORT(timer_gettime);
  1071. /**
  1072. * @brief Sets expiration time of per-process timer.
  1073. *
  1074. * See IEEE 1003.1
  1075. */
  1076. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
  1077. struct itimerspec *ovalue)
  1078. {
  1079. struct timer_obj *timer = timer_id_get((rt_ubase_t)timerid);
  1080. if (timer == NULL ||
  1081. value->it_interval.tv_nsec < 0 ||
  1082. value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND ||
  1083. value->it_interval.tv_sec < 0 ||
  1084. value->it_value.tv_nsec < 0 ||
  1085. value->it_value.tv_nsec >= NANOSECOND_PER_SECOND ||
  1086. value->it_value.tv_sec < 0)
  1087. {
  1088. rt_set_errno(EINVAL);
  1089. return -1;
  1090. }
  1091. /* Save time to expire and old reload value. */
  1092. if (ovalue != NULL)
  1093. {
  1094. timer_gettime(timerid, ovalue);
  1095. }
  1096. /* Stop the timer if the value is 0 */
  1097. if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0))
  1098. {
  1099. if (timer->status == ACTIVE)
  1100. {
  1101. #ifdef RT_USING_CPUTIME
  1102. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1103. rt_cputimer_stop(&timer->cputimer);
  1104. else
  1105. #endif /* RT_USING_CPUTIME */
  1106. rt_timer_stop(&timer->timer);
  1107. }
  1108. timer->status = NOT_ACTIVE;
  1109. return 0;
  1110. }
  1111. /* calculate timer period(tick); To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
  1112. *
  1113. * tick nanosecond nanosecond * RT_TICK_PER_SECOND
  1114. * ------------------------- = -------------------------- ---> tick = -------------------------------------
  1115. * RT_TICK_PER_SECOND NANOSECOND_PER_SECOND NANOSECOND_PER_SECOND
  1116. *
  1117. */
  1118. #ifdef RT_USING_CPUTIME
  1119. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1120. {
  1121. rt_uint64_t tick;
  1122. double unit = clock_cpu_getres();
  1123. tick = (value->it_value.tv_sec * NANOSECOND_PER_SECOND + value->it_value.tv_nsec) / unit;
  1124. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  1125. {
  1126. tick -= clock_cpu_gettime();
  1127. }
  1128. timer->reload = tick;
  1129. }
  1130. else
  1131. #endif /* RT_USING_CPUTIME */
  1132. {
  1133. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  1134. {
  1135. #ifndef RT_USING_RTC
  1136. LOG_W(_WARNING_NO_RTC);
  1137. return -1;
  1138. #else
  1139. rt_int64_t ts = ((value->it_value.tv_sec - _timevalue.tv_sec) * RT_TICK_PER_SECOND);
  1140. rt_int64_t tns = (value->it_value.tv_nsec - _timevalue.tv_usec * 1000) * (RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND);
  1141. rt_int64_t reload = ts + tns;
  1142. rt_tick_t rt_tick = rt_tick_get();
  1143. timer->reload = reload < rt_tick ? 0 : reload - rt_tick;
  1144. #endif
  1145. }
  1146. else
  1147. timer->reload = (value->it_value.tv_sec * RT_TICK_PER_SECOND) + value->it_value.tv_nsec * (RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND);
  1148. }
  1149. timer->interval.tv_sec = value->it_interval.tv_sec;
  1150. timer->interval.tv_nsec = value->it_interval.tv_nsec;
  1151. timer->value.tv_sec = value->it_value.tv_sec;
  1152. timer->value.tv_nsec = value->it_value.tv_nsec;
  1153. if (timer->status == ACTIVE)
  1154. {
  1155. #ifdef RT_USING_CPUTIME
  1156. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1157. rt_cputimer_stop(&timer->cputimer);
  1158. else
  1159. #endif /* RT_USING_CPUTIME */
  1160. rt_timer_stop(&timer->timer);
  1161. }
  1162. timer->status = ACTIVE;
  1163. #ifdef RT_USING_CPUTIME
  1164. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1165. {
  1166. if ((value->it_interval.tv_sec == 0) && (value->it_interval.tv_nsec == 0))
  1167. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_ONESHOT, RT_NULL);
  1168. else
  1169. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  1170. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  1171. rt_cputimer_start(&timer->cputimer);
  1172. }
  1173. else
  1174. #endif /* RT_USING_CPUTIME */
  1175. {
  1176. if ((value->it_interval.tv_sec == 0) && (value->it_interval.tv_nsec == 0))
  1177. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_ONESHOT, RT_NULL);
  1178. else
  1179. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  1180. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  1181. rt_timer_start(&timer->timer);
  1182. }
  1183. return 0;
  1184. }
  1185. RTM_EXPORT(timer_settime);
  1186. #endif /* RT_USING_POSIX_TIMER */
  1187. /* timezone */
  1188. #ifndef RT_LIBC_DEFAULT_TIMEZONE
  1189. #define RT_LIBC_DEFAULT_TIMEZONE 8
  1190. #endif
  1191. static volatile int8_t _current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
  1192. void tz_set(int8_t tz)
  1193. {
  1194. rt_base_t level;
  1195. level = rt_hw_interrupt_disable();
  1196. _current_timezone = tz;
  1197. rt_hw_interrupt_enable(level);
  1198. }
  1199. int8_t tz_get(void)
  1200. {
  1201. return _current_timezone;
  1202. }
  1203. int8_t tz_is_dst(void)
  1204. {
  1205. return 0;
  1206. }