drv_rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * 2022-03-10 shelton first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <sys/time.h>
  13. #include "board.h"
  14. #ifdef BSP_USING_RTC
  15. #ifndef BKP_DR1
  16. #define BKP_DR1 RT_NULL
  17. #endif
  18. //#define DRV_DEBUG
  19. #define LOG_TAG "drv.rtc"
  20. #include <drv_log.h>
  21. #define BKUP_REG_DATA 0xA5A5
  22. static time_t get_rtc_timestamp(void)
  23. {
  24. return rtc_counter_get();
  25. }
  26. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  27. {
  28. /* set the rtc counter value */
  29. rtc_counter_set(time_stamp);
  30. /* wait until last write operation on rtc registers has finished */
  31. rtc_wait_config_finish();
  32. LOG_D("set rtc time.");
  33. bpr_data_write(BPR_DATA1, BKUP_REG_DATA);
  34. return RT_EOK;
  35. }
  36. static rt_err_t rt_rtc_config(void)
  37. {
  38. /* allow access to pattery powered domain */
  39. pwc_battery_powered_domain_access(TRUE);
  40. #ifdef BSP_RTC_USING_LICK
  41. crm_rtc_clock_select(CRM_RTC_CLOCK_LICK);
  42. #else
  43. crm_rtc_clock_select(CRM_RTC_CLOCK_LEXT);
  44. #endif /* BSP_RTC_USING_LICK */
  45. /* enable rtc */
  46. crm_rtc_clock_enable(TRUE);
  47. /* wait for rtc registers update finish */
  48. rtc_wait_update_finish();
  49. /* wait until last write operation on rtc registers has finished */
  50. rtc_wait_config_finish();
  51. if (bpr_data_read(BPR_DATA1) != BKUP_REG_DATA)
  52. {
  53. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  54. /* set rtc divider: set rtc period to 1sec */
  55. rtc_divider_set(32767);
  56. /* wait until last write operation on rtc registers has finished */
  57. rtc_wait_config_finish();
  58. }
  59. return RT_EOK;
  60. }
  61. static rt_err_t _rtc_init(void)
  62. {
  63. crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  64. crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
  65. #ifdef BSP_RTC_USING_LICK
  66. crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
  67. while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET);
  68. #else
  69. crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  70. pwc_battery_powered_domain_access(TRUE);
  71. crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);
  72. while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET);
  73. #endif /* BSP_RTC_USING_LICK */
  74. if (rt_rtc_config() != RT_EOK)
  75. {
  76. LOG_E("rtc init failed.");
  77. return -RT_ERROR;
  78. }
  79. return RT_EOK;
  80. }
  81. static rt_err_t _rtc_get_secs(void *args)
  82. {
  83. *(rt_uint32_t *)args = get_rtc_timestamp();
  84. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  85. return RT_EOK;
  86. }
  87. static rt_err_t _rtc_set_secs(void *args)
  88. {
  89. rt_err_t result = RT_EOK;
  90. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  91. {
  92. result = -RT_ERROR;
  93. }
  94. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  95. return result;
  96. }
  97. static const struct rt_rtc_ops _rtc_ops =
  98. {
  99. _rtc_init,
  100. _rtc_get_secs,
  101. _rtc_set_secs,
  102. RT_NULL,
  103. RT_NULL,
  104. RT_NULL,
  105. RT_NULL,
  106. };
  107. static rt_rtc_dev_t at32_rtc_dev;
  108. int rt_hw_rtc_init(void)
  109. {
  110. rt_err_t result;
  111. at32_rtc_dev.ops = &_rtc_ops;
  112. result = rt_hw_rtc_register(&at32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR,RT_NULL);
  113. if (result != RT_EOK)
  114. {
  115. LOG_E("rtc register err code: %d", result);
  116. return result;
  117. }
  118. LOG_D("rtc init success");
  119. return RT_EOK;
  120. }
  121. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  122. #endif /* BSP_USING_RTC */