drv_rtc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-06-19 klcheng First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined (BSP_USING_RTC)
  14. #include <rtdevice.h>
  15. #include <sys/time.h>
  16. #include "NuMicro.h"
  17. /* Private define ---------------------------------------------------------------*/
  18. /* convert the real year and month value to the format of struct tm. */
  19. #define CONV_TO_TM_YEAR(year) ((year) - 1900)
  20. #define CONV_TO_TM_MON(mon) ((mon) - 1)
  21. /* convert the tm_year and tm_mon from struct tm to the real value. */
  22. #define CONV_FROM_TM_YEAR(tm_year) ((tm_year) + 1900)
  23. #define CONV_FROM_TM_MON(tm_mon) ((tm_mon) + 1)
  24. /* rtc date upper bound reaches the year of 2099. */
  25. #define RTC_TM_UPPER_BOUND \
  26. { .tm_year = CONV_TO_TM_YEAR(2038), \
  27. .tm_mon = CONV_TO_TM_MON(1), \
  28. .tm_mday = 19, \
  29. .tm_hour = 3, \
  30. .tm_min = 14, \
  31. .tm_sec = 07, \
  32. }
  33. /* rtc date lower bound reaches the year of 2000. */
  34. #define RTC_TM_LOWER_BOUND \
  35. { .tm_year = CONV_TO_TM_YEAR(2000), \
  36. .tm_mon = CONV_TO_TM_MON(1), \
  37. .tm_mday = 1, \
  38. .tm_hour = 0, \
  39. .tm_min = 0, \
  40. .tm_sec = 0, \
  41. }
  42. /* Private typedef --------------------------------------------------------------*/
  43. /* Private functions ------------------------------------------------------------*/
  44. static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args);
  45. #if defined (NU_RTC_SUPPORT_IO_RW)
  46. static rt_size_t nu_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  47. static rt_size_t nu_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  48. #endif
  49. static rt_err_t nu_rtc_is_date_valid(const time_t t);
  50. static rt_err_t nu_rtc_init(void);
  51. #if defined(RT_USING_ALARM)
  52. static void nu_rtc_alarm_reset(void);
  53. #endif
  54. /* Public functions -------------------------------------------------------------*/
  55. #if defined (NU_RTC_SUPPORT_MSH_CMD)
  56. extern rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
  57. extern rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
  58. #endif
  59. /* Private variables ------------------------------------------------------------*/
  60. static struct rt_device device_rtc;
  61. static rt_err_t nu_rtc_init(void)
  62. {
  63. /* hw rtc initialise */
  64. RTC_Open(NULL);
  65. RTC_DisableInt(RTC_INTEN_ALMIEN_Msk | RTC_INTEN_TICKIEN_Msk | RTC_INTEN_TAMP0IEN_Msk |
  66. RTC_INTEN_TAMP1IEN_Msk | RTC_INTEN_TAMP2IEN_Msk | RTC_INTEN_TAMP3IEN_Msk |
  67. RTC_INTEN_TAMP4IEN_Msk | RTC_INTEN_TAMP5IEN_Msk | RTC_INTEN_CLKFIEN_Msk |
  68. RTC_INTEN_CLKSTIEN_Msk);
  69. #if defined(RT_USING_ALARM)
  70. nu_rtc_alarm_reset();
  71. RTC_EnableInt(RTC_INTEN_ALMIEN_Msk);
  72. NVIC_EnableIRQ(RTC_IRQn);
  73. #endif
  74. return RT_EOK;
  75. }
  76. #if defined(RT_USING_ALARM)
  77. /* Reset alarm settings to avoid the unwanted values remain in rtc registers. */
  78. static void nu_rtc_alarm_reset(void)
  79. {
  80. S_RTC_TIME_DATA_T alarm;
  81. /* Reset alarm time and calendar. */
  82. alarm.u32Year = RTC_YEAR2000;
  83. alarm.u32Month = 0;
  84. alarm.u32Day = 0;
  85. alarm.u32Hour = 0;
  86. alarm.u32Minute = 0;
  87. alarm.u32Second = 0;
  88. alarm.u32TimeScale = RTC_CLOCK_24;
  89. RTC_SetAlarmDateAndTime(&alarm);
  90. /* Reset alarm time mask and calendar mask. */
  91. RTC_SetAlarmDateMask(0, 0, 0, 0, 0, 0);
  92. RTC_SetAlarmTimeMask(0, 0, 0, 0, 0, 0);
  93. /* Clear alarm flag for safe */
  94. RTC_CLEAR_ALARM_INT_FLAG(RTC);
  95. }
  96. #endif
  97. /* rtc device driver initialise. */
  98. int rt_hw_rtc_init(void)
  99. {
  100. rt_err_t ret;
  101. nu_rtc_init();
  102. /* register rtc device IO operations */
  103. device_rtc.type = RT_Device_Class_RTC;
  104. device_rtc.init = NULL;
  105. device_rtc.open = NULL;
  106. device_rtc.close = NULL;
  107. device_rtc.control = nu_rtc_control;
  108. #if defined (NU_RTC_SUPPORT_IO_RW)
  109. device_rtc.read = nu_rtc_read;
  110. device_rtc.write = nu_rtc_write;
  111. #else
  112. device_rtc.read = NULL;
  113. device_rtc.write = NULL;
  114. #endif
  115. device_rtc.user_data = RT_NULL;
  116. device_rtc.rx_indicate = RT_NULL;
  117. device_rtc.tx_complete = RT_NULL;
  118. ret = rt_device_register(&device_rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  119. return (int)ret;
  120. }
  121. INIT_BOARD_EXPORT(rt_hw_rtc_init);
  122. #if defined (NU_RTC_SUPPORT_IO_RW)
  123. /* Register rt-thread device.read() entry. */
  124. static rt_size_t nu_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  125. {
  126. (void) pos;
  127. nu_rtc_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, buffer);
  128. return size;
  129. }
  130. #endif
  131. #if defined (NU_RTC_SUPPORT_IO_RW)
  132. /* Register rt-thread device.write() entry. */
  133. static rt_size_t nu_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  134. {
  135. (void) pos;
  136. nu_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
  137. return size;
  138. }
  139. #endif
  140. static rt_err_t nu_rtc_is_date_valid(const time_t t)
  141. {
  142. static struct tm tm_upper = RTC_TM_UPPER_BOUND;
  143. static struct tm tm_lower = RTC_TM_LOWER_BOUND;
  144. static time_t t_upper, t_lower;
  145. static rt_bool_t initialised = RT_FALSE;
  146. if (!initialised)
  147. {
  148. t_upper = timegm((struct tm *)&tm_upper);
  149. t_lower = timegm((struct tm *)&tm_lower);
  150. initialised = RT_TRUE;
  151. }
  152. /* check the date is supported by rtc. */
  153. if ((t > t_upper) || (t < t_lower))
  154. return -(RT_EINVAL);
  155. return RT_EOK;
  156. }
  157. /* Register rt-thread device.control() entry. */
  158. static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
  159. {
  160. struct tm tm_out, tm_in;
  161. time_t *time;
  162. S_RTC_TIME_DATA_T hw_time;
  163. #if defined(RT_USING_ALARM)
  164. struct rt_rtc_wkalarm *wkalarm;
  165. S_RTC_TIME_DATA_T hw_alarm;
  166. #endif
  167. if ((dev == NULL) || (args == NULL))
  168. return -(RT_EINVAL);
  169. switch (cmd)
  170. {
  171. case RT_DEVICE_CTRL_RTC_GET_TIME:
  172. time = (time_t *)args;
  173. RTC_GetDateAndTime(&hw_time);
  174. tm_out.tm_year = CONV_TO_TM_YEAR(hw_time.u32Year);
  175. tm_out.tm_mon = CONV_TO_TM_MON(hw_time.u32Month);
  176. tm_out.tm_mday = hw_time.u32Day;
  177. tm_out.tm_hour = hw_time.u32Hour;
  178. tm_out.tm_min = hw_time.u32Minute;
  179. tm_out.tm_sec = hw_time.u32Second;
  180. *time = timegm(&tm_out);
  181. break;
  182. case RT_DEVICE_CTRL_RTC_SET_TIME:
  183. time = (time_t *) args;
  184. if (nu_rtc_is_date_valid(*time) != RT_EOK)
  185. return -(RT_ERROR);
  186. gmtime_r(time, &tm_in);
  187. hw_time.u32Year = CONV_FROM_TM_YEAR(tm_in.tm_year);
  188. hw_time.u32Month = CONV_FROM_TM_MON(tm_in.tm_mon);
  189. hw_time.u32Day = tm_in.tm_mday;
  190. hw_time.u32Hour = tm_in.tm_hour;
  191. hw_time.u32Minute = tm_in.tm_min;
  192. hw_time.u32Second = tm_in.tm_sec;
  193. hw_time.u32TimeScale = RTC_CLOCK_24;
  194. hw_time.u32AmPm = 0;
  195. RTC_SetDateAndTime(&hw_time);
  196. break;
  197. #if defined(RT_USING_ALARM)
  198. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  199. wkalarm = (struct rt_rtc_wkalarm *) args;
  200. RTC_GetAlarmDateAndTime(&hw_alarm);
  201. wkalarm->tm_hour = hw_alarm.u32Hour;
  202. wkalarm->tm_min = hw_alarm.u32Minute;
  203. wkalarm->tm_sec = hw_alarm.u32Second;
  204. break;
  205. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  206. wkalarm = (struct rt_rtc_wkalarm *) args;
  207. hw_alarm.u32Hour = wkalarm->tm_hour;
  208. hw_alarm.u32Minute = wkalarm->tm_min;
  209. hw_alarm.u32Second = wkalarm->tm_sec;
  210. RTC_SetAlarmDateMask(1, 1, 1, 1, 1, 1);
  211. RTC_SetAlarmDateAndTime(&hw_alarm);
  212. break;
  213. default:
  214. return -(RT_EINVAL);
  215. #endif
  216. }
  217. return RT_EOK;
  218. }
  219. #if defined (NU_RTC_SUPPORT_MSH_CMD)
  220. /* Support "rtc_det_date" command line in msh mode */
  221. static rt_err_t msh_rtc_set_date(int argc, char **argv)
  222. {
  223. rt_uint32_t index, len, arg[3];
  224. rt_memset(arg, 0, sizeof(arg));
  225. len = (argc >= 4) ? 4 : argc;
  226. /* The date information stored in argv is represented by the following order :
  227. argv[0,1,2,3] = [cmd, year, month, day] */
  228. for (index = 0; index < (len - 1); index ++)
  229. {
  230. arg[index] = atol(argv[index + 1]);
  231. }
  232. return set_date(arg[0], arg[1], arg[2]);
  233. }
  234. MSH_CMD_EXPORT_ALIAS(msh_rtc_set_date, rtc_set_date, e.g: rtc_set_date 2020 1 20);
  235. #endif
  236. #if defined (NU_RTC_SUPPORT_MSH_CMD)
  237. /* Support "rtc_det_time" command line in msh mode */
  238. static rt_err_t msh_rtc_set_time(int argc, char **argv)
  239. {
  240. rt_uint32_t index, len, arg[3];
  241. rt_memset(arg, 0, sizeof(arg));
  242. len = (argc >= 4) ? 4 : argc;
  243. /* The time information stored in argv is represented by the following order :
  244. argv[0,1,2,3] = [cmd, hour, minute, second] */
  245. for (index = 0; index < (len - 1); index ++)
  246. {
  247. arg[index] = atol(argv[index + 1]);
  248. }
  249. return set_time(arg[0], arg[1], arg[2]);
  250. }
  251. MSH_CMD_EXPORT_ALIAS(msh_rtc_set_time, rtc_set_time, e.g: rtc_set_time 18 30 00);
  252. #endif
  253. /* rtc interrupt entry */
  254. void RTC_IRQHandler(void)
  255. {
  256. rt_interrupt_enter();
  257. if (RTC_GET_TICK_INT_FLAG(RTC))
  258. {
  259. RTC_CLEAR_TICK_INT_FLAG(RTC);
  260. }
  261. #if defined(RT_USING_ALARM)
  262. if (RTC_GET_ALARM_INT_FLAG(RTC))
  263. {
  264. RTC_CLEAR_ALARM_INT_FLAG(RTC);
  265. /* Send an alarm event to notify rt-thread alarm service. */
  266. rt_alarm_update(&device_rtc, NULL);
  267. }
  268. #endif
  269. rt_interrupt_leave();
  270. }
  271. #endif /* BSP_USING_RTC */