drv_rtc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-01 wangyq the first version
  9. * 2019-11-01 wangyq update libraries
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <sys/time.h>
  15. #include <string.h>
  16. #include "board.h"
  17. #include "drv_rtc.h"
  18. #include <ald_cmu.h>
  19. #include <ald_rtc.h>
  20. #ifdef RT_USING_RTC
  21. static void __rtc_init(rtc_init_t *init)
  22. {
  23. assert_param(IS_RTC_HOUR_FORMAT(init->hour_format));
  24. assert_param(IS_RTC_OUTPUT_SEL(init->output));
  25. assert_param(IS_RTC_OUTPUT_POLARITY(init->output_polarity));
  26. ald_rtc_reset();
  27. RTC_UNLOCK();
  28. MODIFY_REG(RTC->CON, RTC_CON_HFM_MSK, init->hour_format << RTC_CON_HFM_POS);
  29. MODIFY_REG(RTC->CON, RTC_CON_EOS_MSK, init->output << RTC_CON_EOS_POSS);
  30. MODIFY_REG(RTC->CON, RTC_CON_POL_MSK, init->output_polarity << RTC_CON_POL_POS);
  31. MODIFY_REG(RTC->PSR, RTC_PSR_SPRS_MSK, init->synch_pre_div << RTC_PSR_SPRS_POSS);
  32. MODIFY_REG(RTC->PSR, RTC_PSR_APRS_MSK, init->asynch_pre_div << RTC_PSR_APRS_POSS);
  33. RTC_LOCK();
  34. return;
  35. }
  36. static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
  37. {
  38. rt_err_t result = RT_EOK;
  39. struct tm time_temp;
  40. struct tm *pNow;
  41. rtc_date_t date;
  42. rtc_time_t time;
  43. switch (cmd)
  44. {
  45. case RT_DEVICE_CTRL_RTC_GET_TIME:
  46. ald_rtc_get_date_time(&date, &time, RTC_FORMAT_DEC);
  47. time_temp.tm_sec = time.second;
  48. time_temp.tm_min = time.minute;
  49. time_temp.tm_hour = time.hour;
  50. time_temp.tm_mday = date.day;
  51. time_temp.tm_mon = date.month - 1;
  52. time_temp.tm_year = date.year - 1900 + 2000;
  53. *((time_t *)args) = timegm(&time_temp);
  54. break;
  55. case RT_DEVICE_CTRL_RTC_SET_TIME:
  56. rt_enter_critical();
  57. /* converts calendar time time into local time. */
  58. pNow = gmtime((const time_t *)args);
  59. /* copy the statically located variable */
  60. memcpy(&time_temp, pNow, sizeof(struct tm));
  61. /* unlock scheduler. */
  62. rt_exit_critical();
  63. time.hour = time_temp.tm_hour;
  64. time.minute = time_temp.tm_min;
  65. time.second = time_temp.tm_sec;
  66. date.year = time_temp.tm_year + 1900 - 2000;
  67. date.month = time_temp.tm_mon + 1;
  68. date.day = time_temp.tm_mday;
  69. ald_rtc_set_time(&time, RTC_FORMAT_DEC);
  70. ald_rtc_set_date(&date, RTC_FORMAT_DEC);
  71. /* start RTC */
  72. RTC_UNLOCK();
  73. SET_BIT(RTC->CON, RTC_CON_GO_MSK);
  74. RTC_LOCK();
  75. break;
  76. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  77. break;
  78. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  79. break;
  80. default:
  81. break;
  82. }
  83. return result;
  84. }
  85. #ifdef RT_USING_DEVICE_OPS
  86. const static struct rt_device_ops es32f0_rtc_ops =
  87. {
  88. RT_NULL,
  89. RT_NULL,
  90. RT_NULL,
  91. RT_NULL,
  92. RT_NULL,
  93. es32f0_rtc_control
  94. };
  95. #endif
  96. int rt_hw_rtc_init(void)
  97. {
  98. rt_err_t ret = RT_EOK;
  99. static struct rt_device rtc_dev;
  100. rtc_init_t rtc_initstruct;
  101. /* enable external 32.768kHz */
  102. CMU_LOSC_ENABLE();
  103. ald_cmu_losc_safe_config(ENABLE);
  104. /* set default time */
  105. RTC_UNLOCK();
  106. WRITE_REG(RTC->TIME, 0x134251);
  107. WRITE_REG(RTC->DATE, 0x1190401);
  108. RTC_LOCK();
  109. /* RTC function initialization */
  110. rtc_initstruct.hour_format = RTC_HOUR_FORMAT_24;
  111. rtc_initstruct.asynch_pre_div = 0;
  112. rtc_initstruct.synch_pre_div = 32767;
  113. rtc_initstruct.output = RTC_OUTPUT_DISABLE;
  114. __rtc_init(&rtc_initstruct);
  115. rtc_dev.type = RT_Device_Class_RTC;
  116. rtc_dev.rx_indicate = RT_NULL;
  117. rtc_dev.tx_complete = RT_NULL;
  118. #ifdef RT_USING_DEVICE_OPS
  119. rtc_dev.ops = &es32f0_rtc_ops;
  120. #else
  121. rtc_dev.init = RT_NULL;
  122. rtc_dev.open = RT_NULL;
  123. rtc_dev.close = RT_NULL;
  124. rtc_dev.read = RT_NULL;
  125. rtc_dev.write = RT_NULL;
  126. rtc_dev.control = es32f0_rtc_control;
  127. #endif
  128. rtc_dev.user_data = RTC;
  129. ret = rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  130. return ret;
  131. }
  132. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  133. #endif