drv_rtc.c 4.0 KB

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