drv_rtc.c 3.9 KB

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