drv_hp_rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * File : drv_hp_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_hp_rtc.h"
  15. #include <time.h>
  16. #include "fsl_common.h"
  17. #include "fsl_snvs_hp.h"
  18. #include "fsl_snvs_lp.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. /* Çý¶¯½Ó¿Ú */
  51. static rt_err_t rt1052_hp_rtc_init(rt_device_t dev)
  52. {
  53. snvs_hp_rtc_config_t snvsRtcConfig;
  54. SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
  55. SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
  56. SNVS_HP_RTC_StartTimer(SNVS);
  57. return RT_EOK;
  58. }
  59. static rt_err_t rt1052_hp_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  60. {
  61. return RT_EOK;
  62. }
  63. static rt_err_t rt1052_hp_rtc_close(rt_device_t dev)
  64. {
  65. return RT_EOK;
  66. }
  67. static rt_size_t rt1052_hp_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  68. {
  69. return 0;
  70. }
  71. static rt_size_t rt1052_hp_rtc_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  72. {
  73. return 0;
  74. }
  75. static rt_err_t rt1052_hp_rtc_control(rt_device_t dev, int cmd, void *args)
  76. {
  77. RT_ASSERT(dev != RT_NULL);
  78. switch(cmd)
  79. {
  80. case RT_DEVICE_CTRL_RTC_GET_TIME:
  81. {
  82. *(uint32_t *)args = get_timestamp();
  83. }
  84. break;
  85. case RT_DEVICE_CTRL_RTC_SET_TIME:
  86. {
  87. set_timestamp(*(time_t *)args);
  88. }
  89. break;
  90. /* ÎÞЧ²ÎÊý */
  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 = rt1052_hp_rtc_init,
  100. .open = rt1052_hp_rtc_open,
  101. .close = rt1052_hp_rtc_close,
  102. .read = rt1052_hp_rtc_read,
  103. .write = rt1052_hp_rtc_write,
  104. .control = rt1052_hp_rtc_control,
  105. };
  106. int rt_hw_hp_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_hp_rtc_init);
  118. #endif /*RT_USING_RTC */