drv_rtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * 2021-04-12 Wayne 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. #include <drv_sys.h>
  18. /* Private define ---------------------------------------------------------------*/
  19. /* convert the real year and month value to the format of struct tm. */
  20. #define CONV_TO_TM_YEAR(year) ((year) - 1900)
  21. #define CONV_TO_TM_MON(mon) ((mon) - 1)
  22. /* convert the tm_year and tm_mon from struct tm to the real value. */
  23. #define CONV_FROM_TM_YEAR(tm_year) ((tm_year) + 1900)
  24. #define CONV_FROM_TM_MON(tm_mon) ((tm_mon) + 1)
  25. /* rtc date upper bound reaches the year of 2099. */
  26. #define RTC_TM_UPPER_BOUND \
  27. { .tm_year = CONV_TO_TM_YEAR(2038), \
  28. .tm_mon = CONV_TO_TM_MON(1), \
  29. .tm_mday = 19, \
  30. .tm_hour = 3, \
  31. .tm_min = 14, \
  32. .tm_sec = 07, \
  33. }
  34. /* rtc date lower bound reaches the year of 2000. */
  35. #define RTC_TM_LOWER_BOUND \
  36. { .tm_year = CONV_TO_TM_YEAR(2000), \
  37. .tm_mon = CONV_TO_TM_MON(1), \
  38. .tm_mday = 1, \
  39. .tm_hour = 0, \
  40. .tm_min = 0, \
  41. .tm_sec = 0, \
  42. }
  43. /* Private typedef --------------------------------------------------------------*/
  44. /* Private functions ------------------------------------------------------------*/
  45. static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args);
  46. #if defined (NU_RTC_SUPPORT_IO_RW)
  47. static rt_size_t nu_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  48. static rt_size_t nu_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  49. #endif
  50. static rt_err_t nu_rtc_is_date_valid(const time_t t);
  51. static rt_err_t nu_rtc_init(void);
  52. #if defined(RT_USING_ALARM)
  53. static void nu_rtc_alarm_reset(void);
  54. static void nu_rtc_isr(int vector, void *param);
  55. #endif
  56. /* Public functions -------------------------------------------------------------*/
  57. #if defined (NU_RTC_SUPPORT_MSH_CMD)
  58. extern rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
  59. extern rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
  60. #endif
  61. /* Private variables ------------------------------------------------------------*/
  62. static struct rt_device device_rtc;
  63. static rt_err_t nu_rtc_init(void)
  64. {
  65. S_RTC_TIME_DATA_T sInitTime = {0};
  66. nu_sys_ipclk_enable(RTCCKEN);
  67. /* Time Setting */
  68. sInitTime.u32Year = 2015;
  69. sInitTime.u32cMonth = 5;
  70. sInitTime.u32cDay = 25;
  71. sInitTime.u32cHour = 13;
  72. sInitTime.u32cMinute = 30;
  73. sInitTime.u32cSecond = 0;
  74. sInitTime.u32cDayOfWeek = RTC_TUESDAY;
  75. sInitTime.u8cClockDisplay = RTC_CLOCK_24;
  76. /* hw rtc initialise */
  77. if (RTC_Init() != E_RTC_SUCCESS)
  78. rt_kprintf("[%s] failure!!\n", __func__);
  79. /* Initialization the RTC timer */
  80. if (RTC_Open(&sInitTime) != E_RTC_SUCCESS)
  81. rt_kprintf("Open Fail!!\n");
  82. /* Do RTC Calibration */
  83. RTC_Ioctl(0, RTC_IOC_SET_FREQUENCY, 0, 0);
  84. RTC_DisableInt(RTC_TICK_INT);
  85. RTC_DisableInt(RTC_ALARM_INT);
  86. #if defined(RT_USING_ALARM)
  87. nu_rtc_alarm_reset();
  88. rt_hw_interrupt_install(IRQ_RTC, nu_rtc_isr, &device_rtc, "rtc");
  89. rt_hw_interrupt_umask(IRQ_RTC);
  90. #endif
  91. return RT_EOK;
  92. }
  93. #if defined(RT_USING_ALARM)
  94. /* Reset alarm settings to avoid the unwanted values remain in rtc registers. */
  95. static void nu_rtc_alarm_reset(void)
  96. {
  97. S_RTC_TIME_DATA_T alarm = {0};
  98. /* Reset alarm time and calendar. */
  99. alarm.u32Year = RTC_YEAR2000;
  100. alarm.u32cMonth = 1;
  101. alarm.u32cDay = 1;
  102. alarm.u8cClockDisplay = RTC_CLOCK_24;
  103. RTC_Write(RTC_ALARM_TIME, &alarm);
  104. /* Clear alarm flag for safe */
  105. RTC_CLEAR_ALARM_INT_FLAG();
  106. }
  107. #endif
  108. /* rtc device driver initialise. */
  109. int rt_hw_rtc_init(void)
  110. {
  111. rt_err_t ret;
  112. nu_rtc_init();
  113. /* register rtc device IO operations */
  114. device_rtc.type = RT_Device_Class_RTC;
  115. device_rtc.init = NULL;
  116. device_rtc.open = NULL;
  117. device_rtc.close = NULL;
  118. device_rtc.control = nu_rtc_control;
  119. #if defined (NU_RTC_SUPPORT_IO_RW)
  120. device_rtc.read = nu_rtc_read;
  121. device_rtc.write = nu_rtc_write;
  122. #else
  123. device_rtc.read = NULL;
  124. device_rtc.write = NULL;
  125. #endif
  126. device_rtc.user_data = RT_NULL;
  127. device_rtc.rx_indicate = RT_NULL;
  128. device_rtc.tx_complete = RT_NULL;
  129. ret = rt_device_register(&device_rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  130. return (int)ret;
  131. }
  132. INIT_BOARD_EXPORT(rt_hw_rtc_init);
  133. #if defined (NU_RTC_SUPPORT_IO_RW)
  134. /* Register rt-thread device.read() entry. */
  135. static rt_size_t nu_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  136. {
  137. (void) pos;
  138. nu_rtc_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, buffer);
  139. return size;
  140. }
  141. #endif
  142. #if defined (NU_RTC_SUPPORT_IO_RW)
  143. /* Register rt-thread device.write() entry. */
  144. static rt_size_t nu_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  145. {
  146. (void) pos;
  147. nu_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
  148. return size;
  149. }
  150. #endif
  151. static rt_err_t nu_rtc_is_date_valid(const time_t t)
  152. {
  153. static struct tm tm_upper = RTC_TM_UPPER_BOUND;
  154. static struct tm tm_lower = RTC_TM_LOWER_BOUND;
  155. static time_t t_upper, t_lower;
  156. static rt_bool_t initialised = RT_FALSE;
  157. if (!initialised)
  158. {
  159. t_upper = timegm((struct tm *)&tm_upper);
  160. t_lower = timegm((struct tm *)&tm_lower);
  161. initialised = RT_TRUE;
  162. }
  163. /* check the date is supported by rtc. */
  164. if ((t > t_upper) || (t < t_lower))
  165. return -(RT_EINVAL);
  166. return RT_EOK;
  167. }
  168. /* Register rt-thread device.control() entry. */
  169. static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
  170. {
  171. struct tm tm_out, tm_in;
  172. time_t *time;
  173. S_RTC_TIME_DATA_T hw_time = {0};
  174. #if defined(RT_USING_ALARM)
  175. struct rt_rtc_wkalarm *wkalarm;
  176. S_RTC_TIME_DATA_T hw_alarm = {0};
  177. #endif
  178. if ((dev == NULL) || (args == NULL))
  179. return -(RT_EINVAL);
  180. switch (cmd)
  181. {
  182. case RT_DEVICE_CTRL_RTC_GET_TIME:
  183. time = (time_t *)args;
  184. if (RTC_Read(RTC_CURRENT_TIME, &hw_time) != E_RTC_SUCCESS)
  185. return -(RT_ERROR);
  186. tm_out.tm_year = CONV_TO_TM_YEAR(hw_time.u32Year);
  187. tm_out.tm_mon = CONV_TO_TM_MON(hw_time.u32cMonth);
  188. tm_out.tm_mday = hw_time.u32cDay;
  189. tm_out.tm_hour = hw_time.u32cHour;
  190. tm_out.tm_min = hw_time.u32cMinute;
  191. tm_out.tm_sec = hw_time.u32cSecond;
  192. tm_out.tm_wday = hw_time.u32cDayOfWeek;
  193. *time = timegm(&tm_out);
  194. break;
  195. case RT_DEVICE_CTRL_RTC_SET_TIME:
  196. time = (time_t *) args;
  197. if (nu_rtc_is_date_valid(*time) != RT_EOK)
  198. return -(RT_ERROR);
  199. gmtime_r(time, &tm_in);
  200. hw_time.u32Year = CONV_FROM_TM_YEAR(tm_in.tm_year);
  201. hw_time.u32cMonth = CONV_FROM_TM_MON(tm_in.tm_mon);
  202. hw_time.u32cDay = tm_in.tm_mday;
  203. hw_time.u32cHour = tm_in.tm_hour;
  204. hw_time.u32cMinute = tm_in.tm_min;
  205. hw_time.u32cSecond = tm_in.tm_sec;
  206. hw_time.u32cDayOfWeek = tm_in.tm_wday;
  207. hw_time.u8cClockDisplay = RTC_CLOCK_24;
  208. hw_time.u8cAmPm = 0;
  209. if (RTC_Write(RTC_CURRENT_TIME, &hw_time) != E_RTC_SUCCESS)
  210. return -(RT_ERROR);
  211. break;
  212. #if defined(RT_USING_ALARM)
  213. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  214. wkalarm = (struct rt_rtc_wkalarm *) args;
  215. if (RTC_Read(RTC_ALARM_TIME, &hw_alarm) != E_RTC_SUCCESS)
  216. return -(RT_ERROR);
  217. wkalarm->tm_hour = hw_alarm.u32cHour;
  218. wkalarm->tm_min = hw_alarm.u32cMinute;
  219. wkalarm->tm_sec = hw_alarm.u32cSecond;
  220. break;
  221. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  222. wkalarm = (struct rt_rtc_wkalarm *) args;
  223. /* Readback current ALARM time from RTC register for avoiding wrong parameter when next RTC_Write. */
  224. if (RTC_Read(RTC_CURRENT_TIME, &hw_alarm) != E_RTC_SUCCESS)
  225. return -(RT_ERROR);
  226. hw_alarm.u32AlarmMaskHour = 0;
  227. hw_alarm.u32AlarmMaskMinute = 0;
  228. hw_alarm.u32AlarmMaskSecond = 0;
  229. hw_alarm.u32cHour = wkalarm->tm_hour;
  230. hw_alarm.u32cMinute = wkalarm->tm_min;
  231. hw_alarm.u32cSecond = wkalarm->tm_sec;
  232. if (RTC_Write(RTC_ALARM_TIME, &hw_alarm) != E_RTC_SUCCESS)
  233. return -(RT_ERROR);
  234. break;
  235. default:
  236. return -(RT_EINVAL);
  237. #endif
  238. }
  239. return RT_EOK;
  240. }
  241. #if defined (NU_RTC_SUPPORT_MSH_CMD)
  242. /* Support "rtc_det_date" command line in msh mode */
  243. static rt_err_t msh_rtc_set_date(int argc, char **argv)
  244. {
  245. rt_uint32_t index, len, arg[3];
  246. rt_memset(arg, 0, sizeof(arg));
  247. len = (argc >= 4) ? 4 : argc;
  248. /* The date information stored in argv is represented by the following order :
  249. argv[0,1,2,3] = [cmd, year, month, day] */
  250. for (index = 0; index < (len - 1); index ++)
  251. {
  252. arg[index] = atol(argv[index + 1]);
  253. }
  254. return set_date(arg[0], arg[1], arg[2]);
  255. }
  256. MSH_CMD_EXPORT_ALIAS(msh_rtc_set_date, rtc_set_date, e.g: rtc_set_date 2020 1 20);
  257. #endif
  258. #if defined (NU_RTC_SUPPORT_MSH_CMD)
  259. /* Support "rtc_det_time" command line in msh mode */
  260. static rt_err_t msh_rtc_set_time(int argc, char **argv)
  261. {
  262. rt_uint32_t index, len, arg[3];
  263. rt_memset(arg, 0, sizeof(arg));
  264. len = (argc >= 4) ? 4 : argc;
  265. /* The time information stored in argv is represented by the following order :
  266. argv[0,1,2,3] = [cmd, hour, minute, second] */
  267. for (index = 0; index < (len - 1); index ++)
  268. {
  269. arg[index] = atol(argv[index + 1]);
  270. }
  271. return set_time(arg[0], arg[1], arg[2]);
  272. }
  273. MSH_CMD_EXPORT_ALIAS(msh_rtc_set_time, rtc_set_time, e.g: rtc_set_time 18 30 00);
  274. #endif
  275. #if defined(RT_USING_ALARM)
  276. /* rtc interrupt entry */
  277. static void nu_rtc_isr(int vector, void *param)
  278. {
  279. if (RTC_GET_TICK_INT_FLAG())
  280. {
  281. RTC_CLEAR_TICK_INT_FLAG();
  282. }
  283. if (RTC_GET_ALARM_INT_FLAG())
  284. {
  285. RTC_CLEAR_ALARM_INT_FLAG();
  286. /* Send an alarm event to notify rt-thread alarm service. */
  287. rt_alarm_update(&device_rtc, (rt_uint32_t)NULL);
  288. }
  289. }
  290. #endif
  291. #endif /* BSP_USING_RTC */