1
0

drv_rtc.c 3.1 KB

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