drv_rtc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-03-04 stevetong459 first version
  9. */
  10. #include "board.h"
  11. #include <sys/time.h>
  12. #ifdef BSP_USING_ONCHIP_RTC
  13. #define LOG_TAG "drv.rtc"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #ifndef LSI_VALUE
  17. #define LSI_VALUE ((uint32_t)40000)
  18. #endif
  19. #ifndef LSE_VALUE
  20. #define LSE_VALUE ((uint32_t)32768)
  21. #endif
  22. #define DRV_RTC_TIME_OUT 0xFFF
  23. static rt_rtc_dev_t apm32_rtc_dev;
  24. /**
  25. * @brief This function will initialize the rtc on chip.
  26. *
  27. * @return RT_EOK indicates successful initialize, other value indicates failed;
  28. */
  29. static rt_err_t _rtc_init(void)
  30. {
  31. volatile rt_uint32_t counter = 0;
  32. /* Enable RTC Clock */
  33. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU | RCM_APB1_PERIPH_BAKR);
  34. PMU_EnableBackupAccess();
  35. /* Config RTC clock */
  36. #ifdef BSP_RTC_USING_LSI
  37. RCM_EnableLSI();
  38. while (!RCM_ReadStatusFlag(RCM_FLAG_LSIRDY))
  39. {
  40. if (++counter > DRV_RTC_TIME_OUT)
  41. {
  42. return RT_ETIMEOUT;
  43. }
  44. }
  45. RCM_ConfigRTCCLK(RCM_RTCCLK_LSI);
  46. #else
  47. RCM_ConfigLSE(RCM_LSE_OPEN);
  48. while (!RCM_ReadStatusFlag(RCM_FLAG_LSERDY))
  49. {
  50. if (++counter > DRV_RTC_TIME_OUT)
  51. {
  52. return RT_ETIMEOUT;
  53. }
  54. }
  55. RCM_ConfigRTCCLK(RCM_RTCCLK_LSE);
  56. #endif
  57. RCM_EnableRTCCLK();
  58. RTC_WaitForSynchor();
  59. counter = 0;
  60. while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
  61. {
  62. if (++counter > DRV_RTC_TIME_OUT)
  63. {
  64. return RT_ETIMEOUT;
  65. }
  66. }
  67. RTC_EnableConfigMode();
  68. RTC_ClearStatusFlag(RTC_FLAG_OVR | RTC_FLAG_ALR | RTC_FLAG_SEC);
  69. #ifdef BSP_RTC_USING_LSI
  70. RTC_ConfigPrescaler(LSI_VALUE - 1);
  71. #else
  72. RTC_ConfigPrescaler(LSE_VALUE - 1);
  73. #endif
  74. return RT_EOK;
  75. }
  76. /**
  77. * @brief This function will initialize the rtc on chip.
  78. *
  79. * @return RT_EOK indicates successful initialize, other value indicates failed;
  80. */
  81. static rt_err_t _rtc_get_secs(void *args)
  82. {
  83. volatile rt_uint32_t counter = 0;
  84. while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
  85. {
  86. if (++counter > DRV_RTC_TIME_OUT)
  87. {
  88. return RT_ETIMEOUT;
  89. }
  90. }
  91. *(rt_uint32_t *) args = RTC_ReadCounter();
  92. return RT_EOK;
  93. }
  94. static rt_err_t _rtc_set_secs(void *args)
  95. {
  96. volatile rt_uint32_t counter = 0;
  97. while (!RTC_ReadStatusFlag(RTC_FLAG_OC))
  98. {
  99. if (++counter > DRV_RTC_TIME_OUT)
  100. {
  101. return RT_ETIMEOUT;
  102. }
  103. }
  104. RTC_ConfigCounter(*(rt_uint32_t *)args);
  105. return RT_EOK;
  106. }
  107. static const struct rt_rtc_ops _rtc_ops =
  108. {
  109. _rtc_init,
  110. _rtc_get_secs,
  111. _rtc_set_secs,
  112. RT_NULL,
  113. RT_NULL,
  114. RT_NULL,
  115. RT_NULL,
  116. };
  117. /**
  118. * @brief RTC initialization function.
  119. *
  120. * @return RT_EOK indicates successful initialization, other value indicates failed;
  121. */
  122. static int rt_hw_rtc_init(void)
  123. {
  124. rt_err_t result = RT_EOK;
  125. apm32_rtc_dev.ops = &_rtc_ops;
  126. if (rt_hw_rtc_register(&apm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  127. {
  128. LOG_E("rtc init failed");
  129. result = RT_ERROR;
  130. }
  131. else
  132. {
  133. LOG_D("rtc init success");
  134. }
  135. return result;
  136. }
  137. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  138. #endif /* BSP_USING_ONCHIP_RTC */