ck_rtc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file ck_rtc.c
  18. * @brief CSI Source File for RTC Driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #include <stdbool.h>
  23. #include "ck_rtc.h"
  24. #include "csi_core.h"
  25. #include "drv_rtc.h"
  26. #include "soc.h"
  27. #define ERR_RTC(errno) (CSI_DRV_ERRNO_RTC_BASE | errno)
  28. #define RTC_NULL_PARAM_CHK(para) \
  29. do { \
  30. if (para == NULL) { \
  31. return ERR_RTC(EDRV_PARAMETER); \
  32. } \
  33. } while (0)
  34. typedef struct {
  35. uint32_t base;
  36. uint32_t irq;
  37. rtc_event_cb_t cb_event;
  38. struct tm rtc_base;
  39. } ck_rtc_priv_t;
  40. extern void mdelay(uint32_t ms);
  41. static ck_rtc_priv_t rtc_instance[CONFIG_RTC_NUM];
  42. static uint8_t leap_year[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  43. static uint8_t noleap_year[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  44. static const uint16_t g_noleap_daysbeforemonth[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  45. static const uint16_t g_leap_daysbeforemonth[13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
  46. static const rtc_capabilities_t rtc_capabilities = {
  47. .interrupt_mode = 1, /* supports Interrupt mode */
  48. .wrap_mode = 0 /* supports wrap mode */
  49. };
  50. static inline int clock_isleapyear(int year)
  51. {
  52. return (year % 400) ? ((year % 100) ? ((year % 4) ? 0 : 1) : 0) : 1;
  53. }
  54. static inline int32_t ck_rtc_enable(ck_rtc_reg_t *addr)
  55. {
  56. uint32_t value;
  57. value = addr->RTC_CCR;
  58. value |= 1 << 2;
  59. addr->RTC_CCR = value;
  60. return 0;
  61. }
  62. static inline int32_t ck_rtc_disable(ck_rtc_reg_t *addr)
  63. {
  64. uint32_t value;
  65. value = addr->RTC_CCR;
  66. value &= ~(1 << 2);
  67. addr->RTC_CCR = value;
  68. return 0;
  69. }
  70. static int ck_rtc_settime(ck_rtc_reg_t *addr, uint32_t settime)
  71. {
  72. if (settime < 0) {
  73. return ERR_RTC(EDRV_PARAMETER);
  74. }
  75. uint64_t time = settime;
  76. time = time * 20000000 / 16384;
  77. time = time / 1000;
  78. addr->RTC_CLR = (uint32_t)time;
  79. return 0;
  80. }
  81. static time_t ck_rtc_readtime(ck_rtc_reg_t *addr)
  82. {
  83. uint64_t time = addr->RTC_CCVR ;
  84. time = time * 16384 / 20000000;
  85. time = time * 1000;
  86. return (time_t)time;
  87. }
  88. static int clock_daysbeforemonth(int month, bool leapyear)
  89. {
  90. int retval = g_noleap_daysbeforemonth[month];
  91. if (month >= 2 && leapyear) {
  92. retval++;
  93. }
  94. return retval;
  95. }
  96. static time_t clock_calendar2utc(int year, int month, int day)
  97. {
  98. time_t days;
  99. /* Years since epoch in units of days (ignoring leap years). */
  100. days = (year - 1970) * 365;
  101. /* Add in the extra days for the leap years prior to the current year. */
  102. days += (year - 1969) >> 2;
  103. /* Add in the days up to the beginning of this month. */
  104. days += (time_t)clock_daysbeforemonth(month, clock_isleapyear(year));
  105. /* Add in the days since the beginning of this month (days are 1-based). */
  106. days += day - 1;
  107. /* Then convert the seconds and add in hours, minutes, and seconds */
  108. return days;
  109. }
  110. time_t _mktime(struct tm *tp)
  111. {
  112. time_t ret;
  113. time_t jdn;
  114. /* Get the EPOCH-relative julian date from the calendar year,
  115. * month, and date
  116. */
  117. jdn = clock_calendar2utc(tp->tm_year + 1900, tp->tm_mon, tp->tm_mday);
  118. /* Return the seconds into the julian day. */
  119. ret = ((jdn * 24 + tp->tm_hour) * 60 + tp->tm_min) * 60 + tp->tm_sec;
  120. return ret;
  121. }
  122. static void clock_utc2calendar(time_t days, int *year, int *month,
  123. int *day)
  124. {
  125. /* There is one leap year every four years, so we can get close with the
  126. * following:
  127. */
  128. int value = days / (4 * 365 + 1); /* Number of 4-years periods since the epoch */
  129. days -= value * (4 * 365 + 1); /* Remaining days */
  130. value <<= 2; /* Years since the epoch */
  131. /* Then we will brute force the next 0-3 years */
  132. bool leapyear;
  133. int tmp;
  134. for (; ;) {
  135. /* Is this year a leap year (we'll need this later too) */
  136. leapyear = clock_isleapyear(value + 1970);
  137. /* Get the number of days in the year */
  138. tmp = (leapyear ? 366 : 365);
  139. /* Do we have that many days? */
  140. if (days >= tmp) {
  141. /* Yes.. bump up the year */
  142. value++;
  143. days -= tmp;
  144. } else {
  145. /* Nope... then go handle months */
  146. break;
  147. }
  148. }
  149. /* At this point, value has the year and days has number days into this year */
  150. *year = 1970 + value;
  151. /* Handle the month (zero based) */
  152. int min = 0;
  153. int max = 11;
  154. do {
  155. /* Get the midpoint */
  156. value = (min + max) >> 1;
  157. /* Get the number of days that occurred before the beginning of the month
  158. * following the midpoint.
  159. */
  160. tmp = clock_daysbeforemonth(value + 1, leapyear);
  161. /* Does the number of days before this month that equal or exceed the
  162. * number of days we have remaining?
  163. */
  164. if (tmp > days) {
  165. /* Yes.. then the month we want is somewhere from 'min' and to the
  166. * midpoint, 'value'. Could it be the midpoint?
  167. */
  168. tmp = clock_daysbeforemonth(value, leapyear);
  169. if (tmp > days) {
  170. /* No... The one we want is somewhere between min and value-1 */
  171. max = value - 1;
  172. } else {
  173. /* Yes.. 'value' contains the month that we want */
  174. break;
  175. }
  176. } else {
  177. /* No... The one we want is somwhere between value+1 and max */
  178. min = value + 1;
  179. }
  180. /* If we break out of the loop because min == max, then we want value
  181. * to be equal to min == max.
  182. */
  183. value = min;
  184. } while (min < max);
  185. /* The selected month number is in value. Subtract the number of days in the
  186. * selected month
  187. */
  188. days -= clock_daysbeforemonth(value, leapyear);
  189. /* At this point, value has the month into this year (zero based) and days has
  190. * number of days into this month (zero based)
  191. */
  192. *month = value + 1; /* 1-based */
  193. *day = days + 1; /* 1-based */
  194. }
  195. struct tm *gmtime_r(const time_t *timer, struct tm *result)
  196. {
  197. time_t epoch;
  198. time_t jdn;
  199. int year;
  200. int month;
  201. int day;
  202. int hour;
  203. int min;
  204. int sec;
  205. /* Get the seconds since the EPOCH */
  206. epoch = *timer;
  207. /* Convert to days, hours, minutes, and seconds since the EPOCH */
  208. jdn = epoch / SEC_PER_DAY;
  209. epoch -= SEC_PER_DAY * jdn;
  210. hour = epoch / SEC_PER_HOUR;
  211. epoch -= SEC_PER_HOUR * hour;
  212. min = epoch / SEC_PER_MIN;
  213. epoch -= SEC_PER_MIN * min;
  214. sec = epoch;
  215. /* Convert the days since the EPOCH to calendar day */
  216. clock_utc2calendar(jdn, &year, &month, &day);
  217. /* Then return the struct tm contents */
  218. result->tm_year = (int)year - 1900; /* Relative to 1900 */
  219. result->tm_mon = (int)month - 1; /* zero-based */
  220. result->tm_mday = (int)day; /* one-based */
  221. result->tm_hour = (int)hour;
  222. result->tm_min = (int)min;
  223. result->tm_sec = (int)sec;
  224. return result;
  225. }
  226. static int ck_rtc_setmarchtime(ck_rtc_reg_t *addr, int64_t settime)
  227. {
  228. int64_t time = settime;
  229. if (settime < 0 || time >= 0x8000000000000000) {
  230. return ERR_RTC(EDRV_PARAMETER);
  231. }
  232. time = time * 20000 / 16384;
  233. time += addr->RTC_CCVR;
  234. addr->RTC_CMR = (uint32_t)time;
  235. return 0;
  236. }
  237. static int32_t ck_rtc_int_enable(ck_rtc_reg_t *addr)
  238. {
  239. int value = addr->RTC_CCR;
  240. value |= 1 << 0;
  241. addr->RTC_CCR = value;
  242. return 0;
  243. }
  244. static int32_t ck_rtc_int_disable(ck_rtc_reg_t *addr)
  245. {
  246. uint32_t value = addr->RTC_CCR;
  247. value &= ~(1 << 0);
  248. addr->RTC_CCR = value;
  249. ck_rtc_setmarchtime(addr, ck_rtc_readtime(addr));
  250. return 0;
  251. }
  252. void ck_rtc_irqhandler(int32_t idx)
  253. {
  254. ck_rtc_priv_t *rtc_priv = &rtc_instance[idx];
  255. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  256. addr->RTC_EOI;
  257. if (rtc_priv->cb_event) {
  258. rtc_priv->cb_event(RTC_EVENT_TIMER_INTRERRUPT);
  259. }
  260. }
  261. int32_t __attribute__((weak)) target_get_rtc_count(void)
  262. {
  263. return 0;
  264. }
  265. int32_t __attribute__((weak)) target_get_rtc(uint32_t idx, uint32_t *base, uint32_t *irq)
  266. {
  267. return NULL;
  268. }
  269. /**
  270. \brief get rtc instance count.
  271. \return rtc instance count
  272. */
  273. int32_t csi_rtc_get_instance_count(void)
  274. {
  275. return target_get_rtc_count();
  276. }
  277. /**
  278. \brief Initialize RTC Interface. 1. Initializes the resources needed for the RTC interface 2.registers event callback function
  279. \param[in] idx must not exceed return value of csi_rtc_get_instance_count()
  280. \param[in] cb_event Pointer to \ref rtc_event_cb_t
  281. \return pointer to rtc instance
  282. */
  283. rtc_handle_t csi_rtc_initialize(int32_t idx, rtc_event_cb_t cb_event)
  284. {
  285. if (idx < 0 || idx >= CONFIG_RTC_NUM) {
  286. return NULL;
  287. }
  288. int32_t real_idx;
  289. uint32_t base = 0u;
  290. uint32_t irq;
  291. real_idx = target_get_rtc(idx, &base, &irq);
  292. if (real_idx != idx) {
  293. return NULL;
  294. }
  295. ck_rtc_priv_t *rtc_priv;
  296. rtc_priv = &rtc_instance[idx];
  297. rtc_priv->base = base;
  298. rtc_priv->irq = irq;
  299. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  300. rtc_priv->cb_event = cb_event;
  301. addr->RTC_CCR = 0;
  302. drv_nvic_enable_irq(rtc_priv->irq);
  303. return (rtc_handle_t)rtc_priv;
  304. }
  305. /**
  306. \brief De-initialize RTC Interface. stops operation and releases the software resources used by the interface
  307. \param[in] handle rtc handle to operate.
  308. \return \ref execution_status
  309. */
  310. int32_t csi_rtc_uninitialize(rtc_handle_t handle)
  311. {
  312. RTC_NULL_PARAM_CHK(handle);
  313. ck_rtc_priv_t *rtc_priv = handle;
  314. rtc_priv->cb_event = NULL;
  315. drv_nvic_disable_irq(rtc_priv->irq);
  316. return 0;
  317. }
  318. /**
  319. \brief Get driver capabilities.
  320. \param[in] handle rtc handle to operate.
  321. \return \ref rtc_capabilities_t
  322. */
  323. rtc_capabilities_t csi_rtc_get_capabilities(rtc_handle_t handle)
  324. {
  325. return rtc_capabilities;
  326. }
  327. /**
  328. \brief Set RTC timer.
  329. \param[in] handle rtc handle to operate.
  330. \param[in] rtctime \ref struct tm
  331. \return \ref execution_status
  332. */
  333. int32_t csi_rtc_set_time(rtc_handle_t handle, const struct tm *rtctime)
  334. {
  335. RTC_NULL_PARAM_CHK(handle);
  336. RTC_NULL_PARAM_CHK(rtctime);
  337. ck_rtc_priv_t *rtc_priv = handle;
  338. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  339. if (rtctime->tm_year < 70 || rtctime->tm_year >= 200) {
  340. goto error_time;
  341. }
  342. int32_t leap = 1;
  343. leap = clock_isleapyear(rtctime->tm_year + 1900);
  344. if (rtctime->tm_sec < 0 || rtctime->tm_sec >= 60) {
  345. goto error_time;
  346. }
  347. if (rtctime->tm_min < 0 || rtctime->tm_min >= 60) {
  348. goto error_time;
  349. }
  350. if (rtctime->tm_hour < 0 || rtctime->tm_hour >= 24) {
  351. goto error_time;
  352. }
  353. if (rtctime->tm_mon < 0 || rtctime->tm_mon >= 12) {
  354. goto error_time;
  355. }
  356. if (leap) {
  357. if (rtctime->tm_mday < 1 || rtctime->tm_mday > leap_year[rtctime->tm_mon]) {
  358. goto error_time;
  359. }
  360. } else {
  361. if (rtctime->tm_mday < 1 || rtctime->tm_mday > noleap_year[rtctime->tm_mon]) {
  362. goto error_time;
  363. }
  364. }
  365. rtc_priv->rtc_base.tm_sec = rtctime->tm_sec;
  366. rtc_priv->rtc_base.tm_min = rtctime->tm_min;
  367. rtc_priv->rtc_base.tm_hour = rtctime->tm_hour;
  368. rtc_priv->rtc_base.tm_mday = rtctime->tm_mday;
  369. rtc_priv->rtc_base.tm_mon = rtctime->tm_mon;
  370. rtc_priv->rtc_base.tm_year = rtctime->tm_year;
  371. ck_rtc_settime(addr, 0);
  372. return 0;
  373. error_time:
  374. return ERR_RTC(EDRV_RTC_TIME);
  375. }
  376. /**
  377. \brief Get RTC timer.
  378. \param[in] handle rtc handle to operate.
  379. \param[in] rtctime \ref struct tm
  380. \return \ref execution_status
  381. */
  382. int32_t csi_rtc_get_time(rtc_handle_t handle, struct tm *rtctime)
  383. {
  384. RTC_NULL_PARAM_CHK(handle);
  385. ck_rtc_priv_t *rtc_priv = handle;
  386. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  387. time_t time = ck_rtc_readtime(addr);
  388. time = time / 1000;
  389. time += _mktime(&(rtc_priv->rtc_base));
  390. gmtime_r(&time, rtctime);
  391. return 0;
  392. }
  393. /**
  394. \brief Start RTC timer.
  395. \param[in] handle rtc handle to operate.
  396. \return \ref execution_status
  397. */
  398. int32_t csi_rtc_start(rtc_handle_t handle)
  399. {
  400. RTC_NULL_PARAM_CHK(handle);
  401. ck_rtc_priv_t *rtc_priv = handle;
  402. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  403. ck_rtc_enable(addr);
  404. return 0;
  405. }
  406. /**
  407. \brief Stop RTC timer.
  408. \param[in] handle rtc handle to operate.
  409. \return \ref execution_status
  410. */
  411. int32_t csi_rtc_stop(rtc_handle_t handle)
  412. {
  413. RTC_NULL_PARAM_CHK(handle);
  414. ck_rtc_priv_t *rtc_priv = handle;
  415. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  416. ck_rtc_disable(addr);
  417. return 0;
  418. }
  419. /**
  420. \brief Get RTC status.
  421. \param[in] handle rtc handle to operate.
  422. \return RTC status \ref rtc_status_t
  423. */
  424. rtc_status_t csi_rtc_get_status(rtc_handle_t handle)
  425. {
  426. rtc_status_t rtc_status = {0};
  427. if (handle == NULL) {
  428. return rtc_status;
  429. }
  430. ck_rtc_priv_t *rtc_priv = handle;
  431. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  432. if (addr->RTC_RSTAT & 0x1) {
  433. rtc_status.active = 1;
  434. }
  435. return rtc_status;
  436. }
  437. /**
  438. \brief config RTC timer.
  439. \param[in] handle rtc handle to operate.
  440. \param[in] rtctime time to wake up
  441. \return error code
  442. */
  443. int32_t csi_rtc_timer_config(rtc_handle_t handle, const struct tm *rtctime)
  444. {
  445. RTC_NULL_PARAM_CHK(handle);
  446. ck_rtc_priv_t *rtc_priv = handle;
  447. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  448. if (rtctime->tm_year < 70 || rtctime->tm_year >= 200) {
  449. goto error_time;
  450. }
  451. int32_t leap = clock_isleapyear(rtctime->tm_year + 1900);
  452. if (rtctime->tm_sec < 0 || rtctime->tm_sec >= 60) {
  453. goto error_time;
  454. }
  455. if (rtctime->tm_min < 0 || rtctime->tm_min >= 60) {
  456. goto error_time;
  457. }
  458. if (rtctime->tm_hour < 0 || rtctime->tm_hour >= 24) {
  459. goto error_time;
  460. }
  461. if (rtctime->tm_mon < 0 || rtctime->tm_mon >= 12) {
  462. goto error_time;
  463. }
  464. if (leap) {
  465. if (rtctime->tm_mday < 1 || rtctime->tm_mday > leap_year[rtctime->tm_mon]) {
  466. goto error_time;
  467. }
  468. } else {
  469. if (rtctime->tm_mday < 1 || rtctime->tm_mday > noleap_year[rtctime->tm_mon]) {
  470. goto error_time;
  471. }
  472. }
  473. /* after setting RTC counter load register(RTC_CLT), loading to RTC_CCVR need two rtc clocks.
  474. * two rtc clocks about 3ms. setting 10ms to ensure operation is completed.
  475. */
  476. mdelay(10);
  477. struct tm current_time;
  478. int32_t ret = csi_rtc_get_time(handle, &current_time);
  479. if (ret < 0) {
  480. return ERR_RTC(EDRV_PARAMETER);
  481. }
  482. int64_t settime = 0;
  483. settime += ((int64_t)(rtctime->tm_year) - (int64_t)(rtc_priv->rtc_base.tm_year)) * (365 + (int64_t)leap) * 24 * 3600 * 1000;
  484. if (settime < 0) {
  485. goto error_time;
  486. }
  487. if (leap) {
  488. settime += (int64_t)(g_leap_daysbeforemonth[rtctime->tm_mon + 1] + rtctime->tm_mday - g_leap_daysbeforemonth[current_time.tm_mon + 1] - current_time.tm_mday) * 24 * 3600 * 1000;
  489. if (settime < 0) {
  490. goto error_time;
  491. }
  492. } else {
  493. settime += (int64_t)(g_noleap_daysbeforemonth[rtctime->tm_mon + 1] + rtctime->tm_mday - g_noleap_daysbeforemonth[current_time.tm_mon + 1] - current_time.tm_mday) * 24 * 3600 * 1000;
  494. if (settime < 0) {
  495. goto error_time;
  496. }
  497. }
  498. settime += (int64_t)(rtctime->tm_hour - current_time.tm_hour) * 3600 * 1000;
  499. if (settime < 0) {
  500. goto error_time;
  501. }
  502. settime += (int64_t)(rtctime->tm_min - current_time.tm_min) * 60 * 1000;
  503. if (settime < 0) {
  504. goto error_time;
  505. }
  506. settime += (int64_t)(rtctime->tm_sec - current_time.tm_sec) * 1000;
  507. if (settime < 0) {
  508. goto error_time;
  509. }
  510. ck_rtc_setmarchtime(addr, settime);
  511. return 0;
  512. error_time:
  513. return ERR_RTC(EDRV_RTC_TIME);
  514. }
  515. /**
  516. \brief disable or enable RTC timer.
  517. \param[in] handle rtc handle to operate.
  518. \param[in] en set 1 enable for rtc timer
  519. \return error code
  520. */
  521. int32_t csi_rtc_timer_enable(rtc_handle_t handle, uint8_t en)
  522. {
  523. RTC_NULL_PARAM_CHK(handle);
  524. ck_rtc_priv_t *rtc_priv = handle;
  525. ck_rtc_reg_t *addr = (ck_rtc_reg_t *)(rtc_priv->base);
  526. if (en == 1) {
  527. ck_rtc_int_enable(addr);
  528. } else if (en == 0) {
  529. ck_rtc_int_disable(addr);
  530. } else {
  531. return ERR_RTC(EDRV_PARAMETER);
  532. }
  533. return 0;
  534. }