drv_rtc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2019-03-22 wangyq the first version
  21. * 2019-11-01 wangyq update libraries
  22. * 2021-04-20 liuhy the second version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include <sys/time.h>
  28. #include <string.h>
  29. #include "board.h"
  30. #include "drv_rtc.h"
  31. #ifdef RT_USING_RTC
  32. static void __rtc_init(rtc_init_t *init)
  33. {
  34. assert_param(IS_RTC_HOUR_FORMAT(init->hour_format));
  35. assert_param(IS_RTC_OUTPUT_SEL(init->output));
  36. assert_param(IS_RTC_OUTPUT_POLARITY(init->output_polarity));
  37. ald_rtc_reset();
  38. RTC_UNLOCK();
  39. MODIFY_REG(RTC->CON, RTC_CON_HFM_MSK, init->hour_format << RTC_CON_HFM_POS);
  40. MODIFY_REG(RTC->CON, RTC_CON_EOS_MSK, init->output << RTC_CON_EOS_POSS);
  41. MODIFY_REG(RTC->CON, RTC_CON_POL_MSK, init->output_polarity << RTC_CON_POL_POS);
  42. MODIFY_REG(RTC->PSR, RTC_PSR_SPRS_MSK, init->synch_pre_div << RTC_PSR_SPRS_POSS);
  43. MODIFY_REG(RTC->PSR, RTC_PSR_APRS_MSK, init->asynch_pre_div << RTC_PSR_APRS_POSS);
  44. RTC_LOCK();
  45. return;
  46. }
  47. static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
  48. {
  49. rt_err_t result = RT_EOK;
  50. struct tm time_temp;
  51. rtc_date_t date;
  52. rtc_time_t time;
  53. switch (cmd)
  54. {
  55. case RT_DEVICE_CTRL_RTC_GET_TIME:
  56. ald_rtc_get_date_time(&date, &time, RTC_FORMAT_DEC);
  57. time_temp.tm_sec = time.second;
  58. time_temp.tm_min = time.minute;
  59. time_temp.tm_hour = time.hour;
  60. time_temp.tm_mday = date.day;
  61. time_temp.tm_mon = date.month - 1;
  62. time_temp.tm_year = date.year - 1900 + 2000;
  63. *((time_t *)args) = timegm(&time_temp);
  64. break;
  65. case RT_DEVICE_CTRL_RTC_SET_TIME:
  66. gmtime_r((const time_t *)args, &time_temp);
  67. time.hour = time_temp.tm_hour;
  68. time.minute = time_temp.tm_min;
  69. time.second = time_temp.tm_sec;
  70. date.year = time_temp.tm_year + 1900 - 2000;
  71. date.month = time_temp.tm_mon + 1;
  72. date.day = time_temp.tm_mday;
  73. ald_rtc_set_time(&time, RTC_FORMAT_DEC);
  74. ald_rtc_set_date(&date, RTC_FORMAT_DEC);
  75. /* start RTC */
  76. RTC_UNLOCK();
  77. SET_BIT(RTC->CON, RTC_CON_GO_MSK);
  78. RTC_LOCK();
  79. break;
  80. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  81. break;
  82. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  83. break;
  84. default:
  85. break;
  86. }
  87. return result;
  88. }
  89. #ifdef RT_USING_DEVICE_OPS
  90. const static struct rt_device_ops es32f0_rtc_ops =
  91. {
  92. RT_NULL,
  93. RT_NULL,
  94. RT_NULL,
  95. RT_NULL,
  96. RT_NULL,
  97. es32f0_rtc_control
  98. };
  99. #endif
  100. int rt_hw_rtc_init(void)
  101. {
  102. rt_err_t ret = RT_EOK;
  103. static struct rt_device rtc_dev;
  104. rtc_init_t rtc_initstruct;
  105. /* enable clk */
  106. ald_rtc_source_select(ES_RTC_CLK_SOURCE);
  107. if(ES_RTC_CLK_SOURCE == ES_C_RTC_SOURCE_LOSC)
  108. {
  109. CMU_LOSC_ENABLE();
  110. ald_cmu_losc_safe_config(ENABLE);
  111. }
  112. /* set default time */
  113. RTC_UNLOCK();
  114. WRITE_REG(RTC->TIME, 0x134251);
  115. WRITE_REG(RTC->DATE, 0x1190401);
  116. RTC_LOCK();
  117. /* RTC function initialization */
  118. rtc_initstruct.hour_format = RTC_HOUR_FORMAT_24;
  119. rtc_initstruct.asynch_pre_div = 0;
  120. rtc_initstruct.synch_pre_div = 32767;
  121. rtc_initstruct.output = RTC_OUTPUT_DISABLE;
  122. rtc_initstruct.output_polarity = RTC_OUTPUT_POLARITY_HIGH;
  123. __rtc_init(&rtc_initstruct);
  124. rtc_dev.type = RT_Device_Class_RTC;
  125. rtc_dev.rx_indicate = RT_NULL;
  126. rtc_dev.tx_complete = RT_NULL;
  127. #ifdef RT_USING_DEVICE_OPS
  128. rtc_dev.ops = &es32f0_rtc_ops;
  129. #else
  130. rtc_dev.init = RT_NULL;
  131. rtc_dev.open = RT_NULL;
  132. rtc_dev.close = RT_NULL;
  133. rtc_dev.read = RT_NULL;
  134. rtc_dev.write = RT_NULL;
  135. rtc_dev.control = es32f0_rtc_control;
  136. #endif
  137. rtc_dev.user_data = RTC;
  138. ret = rt_device_register(&rtc_dev, ES_DEVICE_NAME_RTC, RT_DEVICE_FLAG_RDWR);
  139. return ret;
  140. }
  141. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  142. #endif