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. #include <sys/time.h>
  28. #define XT 1
  29. #define LFRC 2
  30. #define RTC_CLK_SRC XT
  31. //connect am drv to rt drv.
  32. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  33. {
  34. if (dev->rx_indicate != RT_NULL)
  35. {
  36. /* Open Interrupt */
  37. }
  38. return RT_EOK;
  39. }
  40. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  41. {
  42. return 0;
  43. }
  44. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  45. {
  46. time_t *time;
  47. struct tm time_temp;
  48. struct tm* time_new;
  49. am_hal_rtc_time_t hal_time;
  50. RT_ASSERT(dev != RT_NULL);
  51. rt_memset(&time_temp, 0, sizeof(struct tm));
  52. switch (cmd)
  53. {
  54. case RT_DEVICE_CTRL_RTC_GET_TIME:
  55. time = (time_t *)args;
  56. /* Get the current Time */
  57. am_hal_rtc_time_get(&hal_time);
  58. /* Years since 1900 : 0-99 range */
  59. time_temp.tm_year = hal_time.ui32Year + 2000 - 1900;
  60. /* Months *since* january 0-11 : RTC_Month_Date_Definitions 1 - 12 */
  61. time_temp.tm_mon = hal_time.ui32Month - 1;
  62. /* Day of the month 1-31 : 1-31 range */
  63. time_temp.tm_mday = hal_time.ui32DayOfMonth;
  64. /* Hours since midnight 0-23 : 0-23 range */
  65. time_temp.tm_hour = hal_time.ui32Hour;
  66. /* Minutes 0-59 : the 0-59 range */
  67. time_temp.tm_min = hal_time.ui32Minute;
  68. /* Seconds 0-59 : the 0-59 range */
  69. time_temp.tm_sec = hal_time.ui32Second;
  70. *time = timegm(&time_temp);
  71. break;
  72. case RT_DEVICE_CTRL_RTC_SET_TIME:
  73. time = (time_t *)args;
  74. time_new = gmtime(time);
  75. hal_time.ui32Hour = time_new->tm_hour;
  76. hal_time.ui32Minute = time_new->tm_min;
  77. hal_time.ui32Second = time_new->tm_sec;
  78. hal_time.ui32Hundredths = 00;
  79. hal_time.ui32Weekday = time_new->tm_wday;
  80. hal_time.ui32DayOfMonth = time_new->tm_mday;
  81. hal_time.ui32Month = time_new->tm_mon + 1;
  82. hal_time.ui32Year = time_new->tm_year + 1900 - 2000;
  83. hal_time.ui32Century = 0;
  84. am_hal_rtc_time_set(&hal_time);
  85. break;
  86. }
  87. return RT_EOK;
  88. }
  89. int rt_hw_rtc_init(void)
  90. {
  91. static struct rt_device rtc;
  92. #if RTC_CLK_SRC == LFRC
  93. /* Enable the LFRC for the RTC */
  94. am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_LFRC);
  95. /* Select LFRC for RTC clock source */
  96. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  97. #endif
  98. #if RTC_CLK_SRC == XT
  99. /* Enable the XT for the RTC */
  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. /*@}*/