ctime.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. tv.tv_usec = 0;
  337. if (set_timeval(&tv) == RT_EOK)
  338. {
  339. return 0;
  340. }
  341. else
  342. {
  343. rt_set_errno(EFAULT);
  344. return -1;
  345. }
  346. }
  347. RTM_EXPORT(stime);
  348. time_t timegm(struct tm * const t)
  349. {
  350. time_t day;
  351. time_t i;
  352. time_t years;
  353. if(t == RT_NULL)
  354. {
  355. rt_set_errno(EFAULT);
  356. return (time_t)-1;
  357. }
  358. years = (time_t)t->tm_year - 70;
  359. if (t->tm_sec > 60) /* seconds after the minute - [0, 60] including leap second */
  360. {
  361. t->tm_min += t->tm_sec / 60;
  362. t->tm_sec %= 60;
  363. }
  364. if (t->tm_min >= 60) /* minutes after the hour - [0, 59] */
  365. {
  366. t->tm_hour += t->tm_min / 60;
  367. t->tm_min %= 60;
  368. }
  369. if (t->tm_hour >= 24) /* hours since midnight - [0, 23] */
  370. {
  371. t->tm_mday += t->tm_hour / 24;
  372. t->tm_hour %= 24;
  373. }
  374. if (t->tm_mon >= 12) /* months since January - [0, 11] */
  375. {
  376. t->tm_year += t->tm_mon / 12;
  377. t->tm_mon %= 12;
  378. }
  379. while (t->tm_mday > __spm[1 + t->tm_mon])
  380. {
  381. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  382. {
  383. --t->tm_mday;
  384. }
  385. t->tm_mday -= __spm[t->tm_mon];
  386. ++t->tm_mon;
  387. if (t->tm_mon > 11)
  388. {
  389. t->tm_mon = 0;
  390. ++t->tm_year;
  391. }
  392. }
  393. if (t->tm_year < 70)
  394. {
  395. rt_set_errno(EINVAL);
  396. return (time_t) -1;
  397. }
  398. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  399. day = years * 365 + (years + 1) / 4;
  400. /* After 2100 we have to substract 3 leap years for every 400 years
  401. This is not intuitive. Most mktime implementations do not support
  402. dates after 2059, anyway, so we might leave this out for it's
  403. bloat. */
  404. if (years >= 131)
  405. {
  406. years -= 131;
  407. years /= 100;
  408. day -= (years >> 2) * 3 + 1;
  409. if ((years &= 3) == 3)
  410. years--;
  411. day -= years;
  412. }
  413. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  414. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  415. /* day is now the number of days since 'Jan 1 1970' */
  416. i = 7;
  417. t->tm_wday = (int)((day + 4) % i); /* Sunday=0, Monday=1, ..., Saturday=6 */
  418. i = 24;
  419. day *= i;
  420. i = 60;
  421. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  422. }
  423. RTM_EXPORT(timegm);
  424. int gettimeofday(struct timeval *tv, struct timezone *tz)
  425. {
  426. /* The use of the timezone structure is obsolete;
  427. * the tz argument should normally be specified as NULL.
  428. * The tz_dsttime field has never been used under Linux.
  429. * Thus, the following is purely of historic interest.
  430. */
  431. if(tz != RT_NULL)
  432. {
  433. tz->tz_dsttime = DST_NONE;
  434. tz->tz_minuteswest = -(tz_get() * 60);
  435. }
  436. if (tv != RT_NULL && get_timeval(tv) == RT_EOK)
  437. {
  438. return 0;
  439. }
  440. else
  441. {
  442. rt_set_errno(EINVAL);
  443. return -1;
  444. }
  445. }
  446. RTM_EXPORT(gettimeofday);
  447. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  448. {
  449. /* The use of the timezone structure is obsolete;
  450. * the tz argument should normally be specified as NULL.
  451. * The tz_dsttime field has never been used under Linux.
  452. * Thus, the following is purely of historic interest.
  453. */
  454. if (tv != RT_NULL
  455. && tv->tv_usec >= 0
  456. && set_timeval((struct timeval *)tv) == RT_EOK)
  457. {
  458. return 0;
  459. }
  460. else
  461. {
  462. rt_set_errno(EINVAL);
  463. return -1;
  464. }
  465. }
  466. RTM_EXPORT(settimeofday);
  467. #ifdef RT_USING_POSIX_DELAY
  468. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  469. {
  470. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  471. {
  472. rt_set_errno(EINVAL);
  473. return -1;
  474. }
  475. #ifdef RT_USING_CPUTIME
  476. double unit = clock_cpu_getres();
  477. rt_uint64_t ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec;
  478. rt_uint64_t tick = ns / unit;
  479. rt_cputime_sleep(tick);
  480. if (rt_get_errno() == -RT_EINTR)
  481. {
  482. if (rmtp)
  483. {
  484. uint64_t rmtp_cpu_tick = tick - clock_cpu_gettime();
  485. rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  486. rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  487. }
  488. rt_set_errno(EINTR);
  489. return -1;
  490. }
  491. #else
  492. rt_tick_t tick, tick_old = rt_tick_get();
  493. tick = rqtp->tv_sec * RT_TICK_PER_SECOND + ((uint64_t)rqtp->tv_nsec * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  494. rt_thread_delay(tick);
  495. if (rt_get_errno() == -RT_EINTR)
  496. {
  497. if (rmtp)
  498. {
  499. tick = tick_old + tick - rt_tick_get();
  500. /* get the passed time */
  501. rmtp->tv_sec = tick / RT_TICK_PER_SECOND;
  502. rmtp->tv_nsec = (tick % RT_TICK_PER_SECOND) * (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND);
  503. }
  504. rt_set_errno(EINTR);
  505. return -1;
  506. }
  507. #endif
  508. return 0;
  509. }
  510. RTM_EXPORT(nanosleep);
  511. #endif /* RT_USING_POSIX_DELAY */
  512. #ifdef RT_USING_POSIX_CLOCK
  513. #ifdef RT_USING_RTC
  514. static volatile struct timeval _timevalue;
  515. static int _rt_clock_time_system_init(void)
  516. {
  517. rt_base_t level;
  518. time_t time = 0;
  519. rt_tick_t tick;
  520. rt_device_t device;
  521. device = rt_device_find("rtc");
  522. if (device != RT_NULL)
  523. {
  524. /* get realtime seconds */
  525. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
  526. {
  527. level = rt_hw_interrupt_disable();
  528. tick = rt_tick_get(); /* get tick */
  529. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  530. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  531. rt_hw_interrupt_enable(level);
  532. return 0;
  533. }
  534. }
  535. level = rt_hw_interrupt_disable();
  536. _timevalue.tv_usec = 0;
  537. _timevalue.tv_sec = 0;
  538. rt_hw_interrupt_enable(level);
  539. return -1;
  540. }
  541. INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
  542. #endif /* RT_USING_RTC */
  543. int clock_getres(clockid_t clockid, struct timespec *res)
  544. {
  545. #ifndef RT_USING_RTC
  546. LOG_W(_WARNING_NO_RTC);
  547. return -1;
  548. #else
  549. int ret = 0;
  550. if (res == RT_NULL)
  551. {
  552. rt_set_errno(EFAULT);
  553. return -1;
  554. }
  555. switch (clockid)
  556. {
  557. case CLOCK_REALTIME:
  558. res->tv_sec = 0;
  559. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  560. break;
  561. #ifdef RT_USING_CPUTIME
  562. case CLOCK_CPUTIME_ID:
  563. res->tv_sec = 0;
  564. res->tv_nsec = clock_cpu_getres();
  565. break;
  566. #endif
  567. default:
  568. res->tv_sec = 0;
  569. res->tv_nsec = 0;
  570. ret = -1;
  571. rt_set_errno(EINVAL);
  572. break;
  573. }
  574. return ret;
  575. #endif /* RT_USING_RTC */
  576. }
  577. RTM_EXPORT(clock_getres);
  578. int clock_gettime(clockid_t clockid, struct timespec *tp)
  579. {
  580. #ifndef RT_USING_RTC
  581. LOG_W(_WARNING_NO_RTC);
  582. return -1;
  583. #else
  584. int ret = 0;
  585. if (tp == RT_NULL)
  586. {
  587. rt_set_errno(EFAULT);
  588. return -1;
  589. }
  590. switch (clockid)
  591. {
  592. case CLOCK_REALTIME:
  593. {
  594. rt_tick_t tick;
  595. rt_base_t level;
  596. level = rt_hw_interrupt_disable();
  597. tick = rt_tick_get(); /* get tick */
  598. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  599. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  600. rt_hw_interrupt_enable(level);
  601. }
  602. break;
  603. #ifdef RT_USING_CPUTIME
  604. case CLOCK_MONOTONIC:
  605. case CLOCK_CPUTIME_ID:
  606. {
  607. double unit = 0;
  608. uint64_t cpu_tick;
  609. unit = clock_cpu_getres();
  610. cpu_tick = clock_cpu_gettime();
  611. tp->tv_sec = ((uint64_t)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  612. tp->tv_nsec = ((uint64_t)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  613. }
  614. break;
  615. #endif
  616. default:
  617. tp->tv_sec = 0;
  618. tp->tv_nsec = 0;
  619. rt_set_errno(EINVAL);
  620. ret = -1;
  621. }
  622. return ret;
  623. #endif /* RT_USING_RTC */
  624. }
  625. RTM_EXPORT(clock_gettime);
  626. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp)
  627. {
  628. #ifndef RT_USING_RTC
  629. LOG_W(_WARNING_NO_RTC);
  630. return -1;
  631. #else
  632. if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NANOSECOND_PER_SECOND)
  633. {
  634. rt_set_errno(EINVAL);
  635. return -1;
  636. }
  637. switch (clockid)
  638. {
  639. case CLOCK_REALTIME:
  640. {
  641. rt_tick_t tick, tick_old = rt_tick_get();
  642. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  643. {
  644. rt_int64_t ts = ((rqtp->tv_sec - _timevalue.tv_sec) * RT_TICK_PER_SECOND);
  645. rt_int64_t tns = (rqtp->tv_nsec - _timevalue.tv_usec * 1000) * (RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND);
  646. tick = ts + tns;
  647. rt_tick_t rt_tick = rt_tick_get();
  648. tick = tick < rt_tick ? 0 : tick - rt_tick;
  649. }
  650. else
  651. {
  652. tick = rqtp->tv_sec * RT_TICK_PER_SECOND + ((uint64_t)(rqtp->tv_nsec) * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  653. }
  654. rt_thread_delay(tick);
  655. if (rt_get_errno() == -RT_EINTR)
  656. {
  657. if (rmtp)
  658. {
  659. tick = tick_old + tick - rt_tick_get();
  660. /* get the passed time */
  661. rmtp->tv_sec = tick / RT_TICK_PER_SECOND;
  662. rmtp->tv_nsec = (tick % RT_TICK_PER_SECOND) * (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND);
  663. }
  664. rt_set_errno(EINTR);
  665. return -1;
  666. }
  667. }
  668. break;
  669. #ifdef RT_USING_CPUTIME
  670. case CLOCK_MONOTONIC:
  671. case CLOCK_CPUTIME_ID:
  672. {
  673. rt_uint64_t cpu_tick_old = clock_cpu_gettime();
  674. double unit = clock_cpu_getres();
  675. rt_uint64_t ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec;
  676. rt_uint64_t tick = ns / unit;
  677. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  678. tick -= cpu_tick_old;
  679. rt_cputime_sleep(tick);
  680. if (rt_get_errno() == -RT_EINTR)
  681. {
  682. if (rmtp)
  683. {
  684. uint64_t rmtp_cpu_tick = tick - clock_cpu_gettime();
  685. rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  686. rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  687. }
  688. rt_set_errno(EINTR);
  689. return -1;
  690. }
  691. }
  692. break;
  693. #endif
  694. default:
  695. rt_set_errno(EINVAL);
  696. return -1;
  697. }
  698. return 0;
  699. #endif
  700. }
  701. RTM_EXPORT(clock_nanosleep);
  702. int clock_settime(clockid_t clockid, const struct timespec *tp)
  703. {
  704. #ifndef RT_USING_RTC
  705. LOG_W(_WARNING_NO_RTC);
  706. return -1;
  707. #else
  708. rt_base_t level;
  709. int second;
  710. rt_tick_t tick;
  711. rt_device_t device;
  712. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  713. {
  714. rt_set_errno(EFAULT);
  715. return -1;
  716. }
  717. /* get second */
  718. second = tp->tv_sec;
  719. level = rt_hw_interrupt_disable();
  720. tick = rt_tick_get(); /* get tick */
  721. /* update timevalue */
  722. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  723. _timevalue.tv_sec = second - tick / RT_TICK_PER_SECOND - 1;
  724. rt_hw_interrupt_enable(level);
  725. /* update for RTC device */
  726. device = rt_device_find("rtc");
  727. if (device != RT_NULL)
  728. {
  729. /* set realtime seconds */
  730. if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
  731. {
  732. return 0;
  733. }
  734. }
  735. return -1;
  736. #endif /* RT_USING_RTC */
  737. }
  738. RTM_EXPORT(clock_settime);
  739. int rt_timespec_to_tick(const struct timespec *time)
  740. {
  741. int tick;
  742. int nsecond, second;
  743. struct timespec tp = {0};
  744. RT_ASSERT(time != RT_NULL);
  745. tick = RT_WAITING_FOREVER;
  746. if (time != NULL)
  747. {
  748. /* get current tp */
  749. clock_gettime(CLOCK_REALTIME, &tp);
  750. if ((time->tv_nsec - tp.tv_nsec) < 0)
  751. {
  752. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  753. second = time->tv_sec - tp.tv_sec - 1;
  754. }
  755. else
  756. {
  757. nsecond = time->tv_nsec - tp.tv_nsec;
  758. second = time->tv_sec - tp.tv_sec;
  759. }
  760. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  761. if (tick < 0) tick = 0;
  762. }
  763. return tick;
  764. }
  765. RTM_EXPORT(rt_timespec_to_tick);
  766. #endif /* RT_USING_POSIX_CLOCK */
  767. #ifdef RT_USING_POSIX_TIMER
  768. #define ACTIVE 1
  769. #define NOT_ACTIVE 0
  770. struct timer_obj
  771. {
  772. union
  773. {
  774. struct rt_timer timer;
  775. #ifdef RT_USING_CPUTIME
  776. struct rt_cputimer cputimer;
  777. #endif
  778. };
  779. void (*sigev_notify_function)(union sigval val);
  780. union sigval val;
  781. struct timespec interval; /* Reload value */
  782. struct timespec value; /* Reload value */
  783. rt_uint64_t reload; /* Reload value in ms */
  784. rt_uint32_t status;
  785. int sigev_signo;
  786. clockid_t clockid;
  787. #ifdef RT_USING_SMART
  788. pid_t pid;
  789. #endif
  790. };
  791. static void rtthread_timer_wrapper(void *timerobj)
  792. {
  793. struct timer_obj *timer;
  794. timer = (struct timer_obj *)timerobj;
  795. if (timer->reload == 0U)
  796. {
  797. timer->status = NOT_ACTIVE;
  798. }
  799. #ifdef RT_USING_CPUTIME
  800. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  801. {
  802. timer->reload = (timer->interval.tv_sec * NANOSECOND_PER_SECOND + timer->interval.tv_nsec) / clock_cpu_getres();
  803. if (timer->reload)
  804. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  805. }
  806. else
  807. #endif /* RT_USING_CPUTIME */
  808. {
  809. timer->reload = (timer->interval.tv_sec * RT_TICK_PER_SECOND) + (timer->interval.tv_nsec * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND;
  810. if (timer->reload)
  811. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  812. }
  813. #ifdef RT_USING_SMART
  814. sys_kill(timer->pid, timer->sigev_signo);
  815. #else
  816. if(timer->sigev_notify_function != RT_NULL)
  817. {
  818. (timer->sigev_notify_function)(timer->val);
  819. }
  820. #endif
  821. }
  822. #define TIMER_ID_MAX 50
  823. static struct timer_obj *_g_timerid[TIMER_ID_MAX];
  824. static int timerid_idx = 0;
  825. RT_DEFINE_SPINLOCK(_timer_id_lock);
  826. void timer_id_init(void)
  827. {
  828. for (int i = 0; i < TIMER_ID_MAX; i++)
  829. {
  830. _g_timerid[i] = NULL;
  831. }
  832. timerid_idx = 0;
  833. }
  834. int timer_id_alloc(void)
  835. {
  836. for (int i = 0; i < timerid_idx; i++)
  837. {
  838. if (_g_timerid[i] == NULL)
  839. return i;
  840. }
  841. if (timerid_idx < TIMER_ID_MAX)
  842. {
  843. timerid_idx++;
  844. return timerid_idx; /* todo */
  845. }
  846. return -1;
  847. }
  848. void timer_id_lock()
  849. {
  850. rt_hw_spin_lock(&_timer_id_lock);
  851. }
  852. void timer_id_unlock()
  853. {
  854. rt_hw_spin_unlock(&_timer_id_lock);
  855. }
  856. struct timer_obj *timer_id_get(rt_ubase_t timerid)
  857. {
  858. struct timer_obj *timer;
  859. if (timerid < 0 || timerid >= TIMER_ID_MAX)
  860. {
  861. return NULL;
  862. }
  863. timer_id_lock();
  864. if (_g_timerid[timerid] == NULL)
  865. {
  866. timer_id_unlock();
  867. LOG_E("can not find timer!");
  868. return NULL;
  869. }
  870. timer = _g_timerid[timerid];
  871. timer_id_unlock();
  872. return timer;
  873. }
  874. int timer_id_put(int id)
  875. {
  876. if (_g_timerid[id] == NULL)
  877. return -1;
  878. _g_timerid[id] = NULL;
  879. return 0;
  880. }
  881. /**
  882. * @brief Create a per-process timer.
  883. *
  884. * This API does not accept SIGEV_THREAD as valid signal event notification
  885. * type.
  886. *
  887. * See IEEE 1003.1
  888. */
  889. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
  890. {
  891. static int num = 0;
  892. int _timerid = 0;
  893. struct timer_obj *timer;
  894. char timername[RT_NAME_MAX] = {0};
  895. if (clockid > CLOCK_ID_MAX ||
  896. (evp->sigev_notify != SIGEV_NONE &&
  897. evp->sigev_notify != SIGEV_SIGNAL))
  898. {
  899. rt_set_errno(EINVAL);
  900. return -1;
  901. }
  902. timer = rt_malloc(sizeof(struct timer_obj));
  903. if(timer == RT_NULL)
  904. {
  905. rt_set_errno(ENOMEM);
  906. return -1;
  907. }
  908. rt_snprintf(timername, RT_NAME_MAX, "psx_tm%02d", num++);
  909. num %= 100;
  910. timer->sigev_signo = evp->sigev_signo;
  911. #ifdef RT_USING_SMART
  912. timer->pid = lwp_self()->pid;
  913. #endif
  914. timer->sigev_notify_function = evp->sigev_notify_function;
  915. timer->val = evp->sigev_value;
  916. timer->interval.tv_sec = 0;
  917. timer->interval.tv_nsec = 0;
  918. timer->reload = 0U;
  919. timer->status = NOT_ACTIVE;
  920. timer->clockid = clockid;
  921. #ifdef RT_USING_CPUTIME
  922. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  923. {
  924. rt_cputimer_init(&timer->cputimer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  925. }
  926. else
  927. #endif /* RT_USING_CPUTIME */
  928. {
  929. if (evp->sigev_notify == SIGEV_NONE)
  930. rt_timer_init(&timer->timer, timername, RT_NULL, RT_NULL, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  931. else
  932. rt_timer_init(&timer->timer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  933. }
  934. timer_id_lock();
  935. _timerid = timer_id_alloc();
  936. if (_timerid < 0)
  937. {
  938. timer_id_unlock();
  939. LOG_E("_timerid overflow!");
  940. return -1; /* todo:memory leak */
  941. }
  942. _g_timerid[_timerid] = timer;
  943. *timerid = (timer_t)(rt_ubase_t)_timerid;
  944. timer_id_unlock();
  945. return 0;
  946. }
  947. RTM_EXPORT(timer_create);
  948. /**
  949. * @brief Delete a per-process timer.
  950. *
  951. * See IEEE 1003.1
  952. */
  953. int timer_delete(timer_t timerid)
  954. {
  955. struct timer_obj *timer;
  956. rt_ubase_t ktimerid;
  957. ktimerid = (rt_ubase_t)timerid;
  958. if (ktimerid < 0 || ktimerid >= TIMER_ID_MAX)
  959. {
  960. rt_set_errno(EINVAL);
  961. return -1;
  962. }
  963. timer_id_lock();
  964. if (_g_timerid[ktimerid] == NULL)
  965. {
  966. timer_id_unlock();
  967. rt_set_errno(EINVAL);
  968. LOG_E("can not find timer!");
  969. return -1;
  970. }
  971. timer = _g_timerid[ktimerid];
  972. timer_id_put(ktimerid);
  973. timer_id_unlock();
  974. if (timer == RT_NULL)
  975. {
  976. rt_set_errno(EINVAL);
  977. return -1;
  978. }
  979. #ifdef RT_USING_CPUTIME
  980. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  981. {
  982. if (timer->status == ACTIVE)
  983. {
  984. timer->status = NOT_ACTIVE;
  985. rt_cputimer_stop(&timer->cputimer);
  986. }
  987. rt_cputimer_detach(&timer->cputimer);
  988. }
  989. else
  990. #endif /* RT_USING_CPUTIME */
  991. {
  992. if (timer->status == ACTIVE)
  993. {
  994. timer->status = NOT_ACTIVE;
  995. rt_timer_stop(&timer->timer);
  996. }
  997. rt_timer_detach(&timer->timer);
  998. }
  999. rt_free(timer);
  1000. return 0;
  1001. }
  1002. RTM_EXPORT(timer_delete);
  1003. /**
  1004. *
  1005. * Return the overrun count for the last timer expiration.
  1006. * It is subefficient to create a new structure to get overrun count.
  1007. **/
  1008. int timer_getoverrun(timer_t timerid)
  1009. {
  1010. rt_set_errno(ENOSYS);
  1011. return -1;
  1012. }
  1013. /**
  1014. * @brief Get amount of time left for expiration on a per-process timer.
  1015. *
  1016. * See IEEE 1003.1
  1017. */
  1018. int timer_gettime(timer_t timerid, struct itimerspec *its)
  1019. {
  1020. struct timer_obj *timer;
  1021. rt_uint32_t seconds, nanoseconds;
  1022. timer = timer_id_get((rt_ubase_t)timerid);
  1023. if (timer == NULL)
  1024. {
  1025. rt_set_errno(EINVAL);
  1026. return -1;
  1027. }
  1028. if (its == NULL)
  1029. {
  1030. rt_set_errno(EFAULT);
  1031. return -1;
  1032. }
  1033. if (timer->status == ACTIVE)
  1034. {
  1035. #ifdef RT_USING_CPUTIME
  1036. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1037. {
  1038. rt_uint64_t remain_tick;
  1039. rt_uint64_t remaining;
  1040. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick);
  1041. remaining = (remain_tick - clock_cpu_gettime()) / clock_cpu_getres();
  1042. seconds = remaining / NANOSECOND_PER_SECOND;
  1043. nanoseconds = remaining % NANOSECOND_PER_SECOND;
  1044. }
  1045. else
  1046. #endif /* RT_USING_CPUTIME */
  1047. {
  1048. rt_tick_t remain_tick;
  1049. rt_tick_t remaining;
  1050. rt_timer_control(&timer->timer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick);
  1051. /* 'remain_tick' is minimum-unit in the RT-Thread' timer,
  1052. * so the seconds, nanoseconds will be calculated by 'remain_tick'.
  1053. */
  1054. remaining = remain_tick - rt_tick_get();
  1055. /* calculate 'second' */
  1056. seconds = remaining / RT_TICK_PER_SECOND;
  1057. /* calculate 'nanosecond'; To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
  1058. *
  1059. * remain_tick millisecond remain_tick * MILLISECOND_PER_SECOND
  1060. * ------------------------- = -------------------------- ---> millisecond = -------------------------------------------
  1061. * RT_TICK_PER_SECOND MILLISECOND_PER_SECOND RT_TICK_PER_SECOND
  1062. *
  1063. * remain_tick * MILLISECOND_PER_SECOND remain_tick * MILLISECOND_PER_SECOND * MICROSECOND_PER_SECOND
  1064. * millisecond = ---------------------------------------- ---> nanosecond = -------------------------------------------------------------------
  1065. * RT_TICK_PER_SECOND RT_TICK_PER_SECOND
  1066. *
  1067. */
  1068. nanoseconds = (((remaining % RT_TICK_PER_SECOND) * MILLISECOND_PER_SECOND) * MICROSECOND_PER_SECOND) / RT_TICK_PER_SECOND;
  1069. }
  1070. its->it_value.tv_sec = (rt_int32_t)seconds;
  1071. its->it_value.tv_nsec = (rt_int32_t)nanoseconds;
  1072. }
  1073. else
  1074. {
  1075. /* Timer is disarmed */
  1076. its->it_value.tv_sec = 0;
  1077. its->it_value.tv_nsec = 0;
  1078. }
  1079. /* The interval last set by timer_settime() */
  1080. its->it_interval = timer->interval;
  1081. return 0;
  1082. }
  1083. RTM_EXPORT(timer_gettime);
  1084. /**
  1085. * @brief Sets expiration time of per-process timer.
  1086. *
  1087. * See IEEE 1003.1
  1088. */
  1089. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
  1090. struct itimerspec *ovalue)
  1091. {
  1092. struct timer_obj *timer = timer_id_get((rt_ubase_t)timerid);
  1093. if (timer == NULL ||
  1094. value->it_interval.tv_nsec < 0 ||
  1095. value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND ||
  1096. value->it_interval.tv_sec < 0 ||
  1097. value->it_value.tv_nsec < 0 ||
  1098. value->it_value.tv_nsec >= NANOSECOND_PER_SECOND ||
  1099. value->it_value.tv_sec < 0)
  1100. {
  1101. rt_set_errno(EINVAL);
  1102. return -1;
  1103. }
  1104. /* Save time to expire and old reload value. */
  1105. if (ovalue != NULL)
  1106. {
  1107. timer_gettime(timerid, ovalue);
  1108. }
  1109. /* Stop the timer if the value is 0 */
  1110. if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0))
  1111. {
  1112. if (timer->status == ACTIVE)
  1113. {
  1114. #ifdef RT_USING_CPUTIME
  1115. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1116. rt_cputimer_stop(&timer->cputimer);
  1117. else
  1118. #endif /* RT_USING_CPUTIME */
  1119. rt_timer_stop(&timer->timer);
  1120. }
  1121. timer->status = NOT_ACTIVE;
  1122. return 0;
  1123. }
  1124. /* calculate timer period(tick); To avoid lost of accuracy, because "RT_TICK_PER_SECOND" maybe 100, 1000, 1024 and so on.
  1125. *
  1126. * tick nanosecond nanosecond * RT_TICK_PER_SECOND
  1127. * ------------------------- = -------------------------- ---> tick = -------------------------------------
  1128. * RT_TICK_PER_SECOND NANOSECOND_PER_SECOND NANOSECOND_PER_SECOND
  1129. *
  1130. */
  1131. #ifdef RT_USING_CPUTIME
  1132. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1133. {
  1134. rt_uint64_t tick;
  1135. double unit = clock_cpu_getres();
  1136. tick = (value->it_value.tv_sec * NANOSECOND_PER_SECOND + value->it_value.tv_nsec) / unit;
  1137. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  1138. {
  1139. tick -= clock_cpu_gettime();
  1140. }
  1141. timer->reload = tick;
  1142. }
  1143. else
  1144. #endif /* RT_USING_CPUTIME */
  1145. {
  1146. if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME)
  1147. {
  1148. #ifndef RT_USING_RTC
  1149. LOG_W(_WARNING_NO_RTC);
  1150. return -1;
  1151. #else
  1152. rt_int64_t ts = ((value->it_value.tv_sec - _timevalue.tv_sec) * RT_TICK_PER_SECOND);
  1153. rt_int64_t tns = (value->it_value.tv_nsec - _timevalue.tv_usec * 1000) * (RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND);
  1154. rt_int64_t reload = ts + tns;
  1155. rt_tick_t rt_tick = rt_tick_get();
  1156. timer->reload = reload < rt_tick ? 0 : reload - rt_tick;
  1157. #endif
  1158. }
  1159. else
  1160. timer->reload = (value->it_value.tv_sec * RT_TICK_PER_SECOND) + value->it_value.tv_nsec * (RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND);
  1161. }
  1162. timer->interval.tv_sec = value->it_interval.tv_sec;
  1163. timer->interval.tv_nsec = value->it_interval.tv_nsec;
  1164. timer->value.tv_sec = value->it_value.tv_sec;
  1165. timer->value.tv_nsec = value->it_value.tv_nsec;
  1166. if (timer->status == ACTIVE)
  1167. {
  1168. #ifdef RT_USING_CPUTIME
  1169. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1170. rt_cputimer_stop(&timer->cputimer);
  1171. else
  1172. #endif /* RT_USING_CPUTIME */
  1173. rt_timer_stop(&timer->timer);
  1174. }
  1175. timer->status = ACTIVE;
  1176. #ifdef RT_USING_CPUTIME
  1177. if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout())
  1178. {
  1179. if ((value->it_interval.tv_sec == 0) && (value->it_interval.tv_nsec == 0))
  1180. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_ONESHOT, RT_NULL);
  1181. else
  1182. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  1183. rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  1184. rt_cputimer_start(&timer->cputimer);
  1185. }
  1186. else
  1187. #endif /* RT_USING_CPUTIME */
  1188. {
  1189. if ((value->it_interval.tv_sec == 0) && (value->it_interval.tv_nsec == 0))
  1190. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_ONESHOT, RT_NULL);
  1191. else
  1192. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
  1193. rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
  1194. rt_timer_start(&timer->timer);
  1195. }
  1196. return 0;
  1197. }
  1198. RTM_EXPORT(timer_settime);
  1199. #endif /* RT_USING_POSIX_TIMER */
  1200. /* timezone */
  1201. #ifndef RT_LIBC_DEFAULT_TIMEZONE
  1202. #define RT_LIBC_DEFAULT_TIMEZONE 8
  1203. #endif
  1204. static volatile int8_t _current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
  1205. void tz_set(int8_t tz)
  1206. {
  1207. rt_base_t level;
  1208. level = rt_hw_interrupt_disable();
  1209. _current_timezone = tz;
  1210. rt_hw_interrupt_enable(level);
  1211. }
  1212. int8_t tz_get(void)
  1213. {
  1214. return _current_timezone;
  1215. }
  1216. int8_t tz_is_dst(void)
  1217. {
  1218. return 0;
  1219. }