drv_rtc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * File : drv_rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2018-03-15 Liuguang the first version.
  13. */
  14. #include "drv_rtc.h"
  15. #include "fsl_common.h"
  16. #include "fsl_snvs_hp.h"
  17. #include "fsl_snvs_lp.h"
  18. #include <time.h>
  19. #ifdef RT_USING_RTC
  20. #if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
  21. #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
  22. #endif
  23. static time_t get_timestamp(void)
  24. {
  25. struct tm tm_new = {0};
  26. snvs_hp_rtc_datetime_t rtcDate;
  27. SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
  28. tm_new.tm_sec = rtcDate.second;
  29. tm_new.tm_min = rtcDate.minute;
  30. tm_new.tm_hour = rtcDate.hour;
  31. tm_new.tm_mday = rtcDate.day;
  32. tm_new.tm_mon = rtcDate.month - 1;
  33. tm_new.tm_year = rtcDate.year - 1900;
  34. return mktime(&tm_new);
  35. }
  36. static int set_timestamp(time_t timestamp)
  37. {
  38. struct tm *p_tm;
  39. snvs_hp_rtc_datetime_t rtcDate;
  40. p_tm = localtime(&timestamp);
  41. rtcDate.second = p_tm->tm_sec ;
  42. rtcDate.minute = p_tm->tm_min ;
  43. rtcDate.hour = p_tm->tm_hour;
  44. rtcDate.day = p_tm->tm_mday;
  45. rtcDate.month = p_tm->tm_mon + 1;
  46. rtcDate.year = p_tm->tm_year + 1900;
  47. SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate);
  48. return RT_EOK;
  49. }
  50. static rt_err_t rt1052_hp_rtc_init(rt_device_t dev)
  51. {
  52. snvs_hp_rtc_config_t snvsRtcConfig;
  53. SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
  54. SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
  55. SNVS_HP_RTC_StartTimer(SNVS);
  56. return RT_EOK;
  57. }
  58. static rt_err_t rt1052_hp_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  59. {
  60. return RT_EOK;
  61. }
  62. static rt_err_t rt1052_hp_rtc_close(rt_device_t dev)
  63. {
  64. return RT_EOK;
  65. }
  66. static rt_size_t rt1052_hp_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  67. {
  68. return 0;
  69. }
  70. static rt_size_t rt1052_hp_rtc_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  71. {
  72. return 0;
  73. }
  74. static rt_err_t rt1052_hp_rtc_control(rt_device_t dev, int cmd, void *args)
  75. {
  76. RT_ASSERT(dev != RT_NULL);
  77. switch(cmd)
  78. {
  79. case RT_DEVICE_CTRL_RTC_GET_TIME:
  80. {
  81. *(uint32_t *)args = get_timestamp();
  82. }
  83. break;
  84. case RT_DEVICE_CTRL_RTC_SET_TIME:
  85. {
  86. set_timestamp(*(time_t *)args);
  87. }
  88. break;
  89. default:
  90. return RT_EINVAL;
  91. }
  92. return RT_EOK;
  93. }
  94. static struct rt_device device =
  95. {
  96. .type = RT_Device_Class_RTC,
  97. .init = rt1052_hp_rtc_init,
  98. .open = rt1052_hp_rtc_open,
  99. .close = rt1052_hp_rtc_close,
  100. .read = rt1052_hp_rtc_read,
  101. .write = rt1052_hp_rtc_write,
  102. .control = rt1052_hp_rtc_control,
  103. };
  104. int rt_hw_hp_rtc_init(void)
  105. {
  106. rt_err_t ret = RT_EOK;
  107. ret = rt_device_register(&device, "rtc", RT_DEVICE_FLAG_RDWR);
  108. if(ret != RT_EOK)
  109. {
  110. return ret;
  111. }
  112. rt_device_open(&device, RT_DEVICE_OFLAG_RDWR);
  113. return RT_EOK;
  114. }
  115. INIT_DEVICE_EXPORT(rt_hw_hp_rtc_init);
  116. #endif /*RT_USING_RTC */