drv_rtc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * 2021-04-25 Lyons first version
  9. */
  10. #include <rtconfig.h>
  11. #ifdef BSP_USING_ONCHIP_RTC
  12. #include "board.h"
  13. #define DBG_TAG "drv.rtc"
  14. #define DBG_LVL DBG_WARNING
  15. #include <rtdbg.h>
  16. #define _DEVICE_NAME "rtc"
  17. _internal_rw struct rt_device _s_rtc_device;
  18. static time_t _get_rtc_timestamp(void)
  19. {
  20. snvs_hp_rtc_datetime_t rtcDate;
  21. SNVS_Type *snvs = (SNVS_Type*)g_snvs_vbase;
  22. struct tm tm_new;
  23. SNVS_HP_RTC_GetDatetime(snvs, &rtcDate);
  24. tm_new.tm_sec = rtcDate.second;
  25. tm_new.tm_min = rtcDate.minute;
  26. tm_new.tm_hour = rtcDate.hour;
  27. tm_new.tm_mday = rtcDate.day;
  28. tm_new.tm_mon = rtcDate.month - 1;
  29. tm_new.tm_year = rtcDate.year - 1900;
  30. return mktime(&tm_new);
  31. }
  32. static rt_err_t _set_rtc_time_stamp(time_t time_stamp)
  33. {
  34. snvs_hp_rtc_datetime_t rtcDate;
  35. SNVS_Type *snvs = (SNVS_Type*)g_snvs_vbase;
  36. struct tm *p_tm;
  37. p_tm = localtime(&time_stamp);
  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. if (kStatus_Success != SNVS_HP_RTC_SetDatetime(snvs, &rtcDate))
  45. {
  46. return -RT_ERROR;
  47. }
  48. return RT_EOK;
  49. }
  50. static void _rtc_init(void)
  51. {
  52. snvs_hp_rtc_config_t snvsRtcConfig;
  53. SNVS_Type *snvs = (SNVS_Type*)g_snvs_vbase;
  54. SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
  55. SNVS_HP_RTC_Init(snvs, &snvsRtcConfig);
  56. SNVS_HP_RTC_StartTimer(snvs);
  57. }
  58. static rt_err_t _rtc_config(struct rt_device *dev)
  59. {
  60. return RT_EOK;
  61. }
  62. static rt_err_t _rtc_ops_control( rt_device_t dev, int cmd, void *args )
  63. {
  64. rt_err_t result;
  65. RT_ASSERT(RT_NULL != dev);
  66. result = RT_EOK;
  67. switch (cmd)
  68. {
  69. case RT_DEVICE_CTRL_RTC_GET_TIME:
  70. *(time_t *)args = _get_rtc_timestamp();
  71. LOG_D("RTC: get rtc_time %x", *(time_t *)args);
  72. break;
  73. case RT_DEVICE_CTRL_RTC_SET_TIME:
  74. if (_set_rtc_time_stamp(*(time_t *)args))
  75. {
  76. result = -RT_ERROR;
  77. }
  78. LOG_D("RTC: set rtc_time %x", *(time_t *)args);
  79. break;
  80. }
  81. return result;
  82. }
  83. #ifdef RT_USING_DEVICE_OPS
  84. _internal_ro struct rt_device_ops _k_rtc_ops =
  85. {
  86. RT_NULL, /* init */
  87. RT_NULL, /* open */
  88. RT_NULL, /* close */
  89. RT_NULL, /* read */
  90. RT_NULL, /* write */
  91. _rtc_ops_control, /* control */
  92. };
  93. #endif
  94. static rt_err_t _rt_rtc_register( rt_device_t device, const char *name, rt_uint32_t flag )
  95. {
  96. RT_ASSERT(RT_NULL != device);
  97. _rtc_init();
  98. if (RT_EOK != _rtc_config(device))
  99. {
  100. return -RT_ERROR;
  101. }
  102. device->type = RT_Device_Class_RTC;
  103. #ifdef RT_USING_DEVICE_OPS
  104. device->ops = &_k_rtc_ops;
  105. #else
  106. device->init = RT_NULL;
  107. device->open = RT_NULL;
  108. device->close = RT_NULL;
  109. device->read = RT_NULL;
  110. device->write = RT_NULL;
  111. device->control = _rtc_ops_control;
  112. #endif
  113. device->user_data = RT_NULL;
  114. device->rx_indicate = RT_NULL;
  115. device->tx_complete = RT_NULL;
  116. /* register a character device */
  117. return rt_device_register(device, name, flag);
  118. }
  119. int rt_hw_rtc_init(void)
  120. {
  121. rt_err_t result;
  122. result = _rt_rtc_register(&_s_rtc_device, _DEVICE_NAME, RT_DEVICE_FLAG_RDWR);
  123. if (RT_EOK != result)
  124. {
  125. LOG_E("rtc register err code: %d", result);
  126. return result;
  127. }
  128. LOG_D("rtc init success.");
  129. return RT_EOK;
  130. }
  131. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  132. #endif /* BSP_USING_ONCHIP_RTC */