rtc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * File :_rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-11-06 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "am_mcu_apollo.h"
  27. #define XT 1
  28. #define LFRC 2
  29. #define RTC_CLK_SRC XT
  30. //connect am drv to rt drv.
  31. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  32. {
  33. if (dev->rx_indicate != RT_NULL)
  34. {
  35. /* Open Interrupt */
  36. }
  37. return RT_EOK;
  38. }
  39. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  40. {
  41. return 0;
  42. }
  43. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  44. {
  45. time_t *time;
  46. struct tm time_temp;
  47. struct tm* time_new;
  48. am_hal_rtc_time_t hal_time;
  49. RT_ASSERT(dev != RT_NULL);
  50. rt_memset(&time_temp, 0, sizeof(struct tm));
  51. switch (cmd)
  52. {
  53. case RT_DEVICE_CTRL_RTC_GET_TIME:
  54. time = (time_t *)args;
  55. /* Get the current Time */
  56. am_hal_rtc_time_get(&hal_time);
  57. /* Years since 1900 : 0-99 range */
  58. time_temp.tm_year = hal_time.ui32Year + 2000 - 1900;
  59. /* Months *since* january 0-11 : RTC_Month_Date_Definitions 1 - 12 */
  60. time_temp.tm_mon = hal_time.ui32Month - 1;
  61. /* Day of the month 1-31 : 1-31 range */
  62. time_temp.tm_mday = hal_time.ui32DayOfMonth;
  63. /* Hours since midnight 0-23 : 0-23 range */
  64. time_temp.tm_hour = hal_time.ui32Hour;
  65. /* Minutes 0-59 : the 0-59 range */
  66. time_temp.tm_min = hal_time.ui32Minute;
  67. /* Seconds 0-59 : the 0-59 range */
  68. time_temp.tm_sec = hal_time.ui32Second;
  69. *time = mktime(&time_temp);
  70. break;
  71. case RT_DEVICE_CTRL_RTC_SET_TIME:
  72. time = (time_t *)args;
  73. time_new = localtime(time);
  74. hal_time.ui32Hour = time_new->tm_hour;
  75. hal_time.ui32Minute = time_new->tm_min;
  76. hal_time.ui32Second = time_new->tm_sec;
  77. hal_time.ui32Hundredths = 00;
  78. hal_time.ui32Weekday = time_new->tm_wday;
  79. hal_time.ui32DayOfMonth = time_new->tm_mday;
  80. hal_time.ui32Month = time_new->tm_mon + 1;
  81. hal_time.ui32Year = time_new->tm_year + 1900 - 2000;
  82. hal_time.ui32Century = 0;
  83. am_hal_rtc_time_set(&hal_time);
  84. break;
  85. }
  86. return RT_EOK;
  87. }
  88. int rt_hw_rtc_init(void)
  89. {
  90. static struct rt_device rtc;
  91. #if RTC_CLK_SRC == LFRC
  92. /* Enable the LFRC for the RTC */
  93. am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_LFRC);
  94. /* Select LFRC for RTC clock source */
  95. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  96. #endif
  97. #if RTC_CLK_SRC == XT
  98. /* Enable the XT for the RTC */
  99. //am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_LFRC);
  100. am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_XT);
  101. /* Select XT for RTC clock source */
  102. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT);
  103. #endif
  104. /* Enable the RTC */
  105. am_hal_rtc_osc_enable();
  106. /* register rtc device */
  107. rtc.type = RT_Device_Class_RTC;
  108. rtc.init = RT_NULL;
  109. rtc.open = rt_rtc_open;
  110. rtc.close = RT_NULL;
  111. rtc.read = rt_rtc_read;
  112. rtc.write = RT_NULL;
  113. rtc.control = rt_rtc_control;
  114. /* no private */
  115. rtc.user_data = RT_NULL;
  116. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  117. return 0;
  118. }
  119. #ifdef RT_USING_COMPONENTS_INIT
  120. INIT_BOARD_EXPORT(rt_hw_rtc_init);
  121. #endif
  122. /*@}*/