drv_rtc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-03-15 Liuguang the first version.
  9. * 2019-07-19 Magicoe The first version for LPC55S6x
  10. */
  11. #include "drv_rtc.h"
  12. #include "fsl_common.h"
  13. #include "fsl_rtc.h"
  14. #include <time.h>
  15. #ifdef RT_USING_RTC
  16. #if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
  17. #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
  18. #endif
  19. static time_t get_timestamp(void)
  20. {
  21. struct tm tm_new = {0};
  22. rtc_datetime_t rtcDate;
  23. /* Get date time */
  24. RTC_GetDatetime(RTC, &rtcDate);
  25. tm_new.tm_sec = rtcDate.second;
  26. tm_new.tm_min = rtcDate.minute;
  27. tm_new.tm_hour = rtcDate.hour;
  28. tm_new.tm_mday = rtcDate.day;
  29. tm_new.tm_mon = rtcDate.month - 1;
  30. tm_new.tm_year = rtcDate.year - 1900;
  31. return mktime(&tm_new);
  32. }
  33. static int set_timestamp(time_t timestamp)
  34. {
  35. struct tm *p_tm;
  36. rtc_datetime_t rtcDate;
  37. p_tm = localtime(&timestamp);
  38. rtcDate.second = p_tm->tm_sec ;
  39. rtcDate.minute = p_tm->tm_min ;
  40. rtcDate.hour = p_tm->tm_hour;
  41. rtcDate.day = p_tm->tm_mday;
  42. rtcDate.month = p_tm->tm_mon + 1;
  43. rtcDate.year = p_tm->tm_year + 1900;
  44. /* RTC time counter has to be stopped before setting the date & time in the TSR register */
  45. RTC_StopTimer(RTC);
  46. /* Set RTC time to default */
  47. RTC_SetDatetime(RTC, &rtcDate);
  48. /* Start the RTC time counter */
  49. RTC_StartTimer(RTC);
  50. return RT_EOK;
  51. }
  52. static rt_err_t lpc_rtc_init(rt_device_t dev)
  53. {
  54. /* Init RTC */
  55. RTC_Init(RTC);
  56. /* Start the RTC time counter */
  57. RTC_StartTimer(RTC);
  58. return RT_EOK;
  59. }
  60. static rt_err_t lpc_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  61. {
  62. return RT_EOK;
  63. }
  64. static rt_err_t lpc_rtc_close(rt_device_t dev)
  65. {
  66. return RT_EOK;
  67. }
  68. static rt_size_t lpc_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  69. {
  70. return 0;
  71. }
  72. static rt_size_t lpc_rtc_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  73. {
  74. return 0;
  75. }
  76. static rt_err_t lpc_rtc_control(rt_device_t dev, int cmd, void *args)
  77. {
  78. RT_ASSERT(dev != RT_NULL);
  79. switch(cmd)
  80. {
  81. case RT_DEVICE_CTRL_RTC_GET_TIME:
  82. {
  83. *(uint32_t *)args = get_timestamp();
  84. }
  85. break;
  86. case RT_DEVICE_CTRL_RTC_SET_TIME:
  87. {
  88. set_timestamp(*(time_t *)args);
  89. }
  90. break;
  91. default:
  92. return RT_EINVAL;
  93. }
  94. return RT_EOK;
  95. }
  96. static struct rt_device device =
  97. {
  98. .type = RT_Device_Class_RTC,
  99. .init = lpc_rtc_init,
  100. .open = lpc_rtc_open,
  101. .close = lpc_rtc_close,
  102. .read = lpc_rtc_read,
  103. .write = lpc_rtc_write,
  104. .control = lpc_rtc_control,
  105. };
  106. int rt_hw_rtc_init(void)
  107. {
  108. rt_err_t ret = RT_EOK;
  109. ret = rt_device_register(&device, "rtc", RT_DEVICE_FLAG_RDWR);
  110. if(ret != RT_EOK)
  111. {
  112. return ret;
  113. }
  114. rt_device_open(&device, RT_DEVICE_OFLAG_RDWR);
  115. return RT_EOK;
  116. }
  117. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  118. #endif /*RT_USING_RTC */