rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-11-06 Haley the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "am_mcu_apollo.h"
  13. #include <sys/time.h>
  14. #define XT 1
  15. #define LFRC 2
  16. #define RTC_CLK_SRC XT
  17. //connect am drv to rt drv.
  18. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  19. {
  20. if (dev->rx_indicate != RT_NULL)
  21. {
  22. /* Open Interrupt */
  23. }
  24. return RT_EOK;
  25. }
  26. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  27. {
  28. return 0;
  29. }
  30. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  31. {
  32. time_t *time;
  33. struct tm time_temp;
  34. struct tm time_new;
  35. am_hal_rtc_time_t hal_time;
  36. RT_ASSERT(dev != RT_NULL);
  37. rt_memset(&time_temp, 0, sizeof(struct tm));
  38. switch (cmd)
  39. {
  40. case RT_DEVICE_CTRL_RTC_GET_TIME:
  41. time = (time_t *)args;
  42. /* Get the current Time */
  43. am_hal_rtc_time_get(&hal_time);
  44. /* Years since 1900 : 0-99 range */
  45. time_temp.tm_year = hal_time.ui32Year + 2000 - 1900;
  46. /* Months *since* january 0-11 : RTC_Month_Date_Definitions 1 - 12 */
  47. time_temp.tm_mon = hal_time.ui32Month - 1;
  48. /* Day of the month 1-31 : 1-31 range */
  49. time_temp.tm_mday = hal_time.ui32DayOfMonth;
  50. /* Hours since midnight 0-23 : 0-23 range */
  51. time_temp.tm_hour = hal_time.ui32Hour;
  52. /* Minutes 0-59 : the 0-59 range */
  53. time_temp.tm_min = hal_time.ui32Minute;
  54. /* Seconds 0-59 : the 0-59 range */
  55. time_temp.tm_sec = hal_time.ui32Second;
  56. *time = timegm(&time_temp);
  57. break;
  58. case RT_DEVICE_CTRL_RTC_SET_TIME:
  59. time = (time_t *)args;
  60. gmtime_r(time, &time_new);
  61. hal_time.ui32Hour = time_new.tm_hour;
  62. hal_time.ui32Minute = time_new.tm_min;
  63. hal_time.ui32Second = time_new.tm_sec;
  64. hal_time.ui32Hundredths = 00;
  65. hal_time.ui32Weekday = time_new.tm_wday;
  66. hal_time.ui32DayOfMonth = time_new.tm_mday;
  67. hal_time.ui32Month = time_new.tm_mon + 1;
  68. hal_time.ui32Year = time_new.tm_year + 1900 - 2000;
  69. hal_time.ui32Century = 0;
  70. am_hal_rtc_time_set(&hal_time);
  71. break;
  72. }
  73. return RT_EOK;
  74. }
  75. int rt_hw_rtc_init(void)
  76. {
  77. static struct rt_device rtc;
  78. #if RTC_CLK_SRC == LFRC
  79. /* Enable the LFRC for the RTC */
  80. am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_LFRC);
  81. /* Select LFRC for RTC clock source */
  82. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  83. #endif
  84. #if RTC_CLK_SRC == XT
  85. /* Enable the XT for the RTC */
  86. am_hal_clkgen_osc_start(AM_HAL_CLKGEN_OSC_XT);
  87. /* Select XT for RTC clock source */
  88. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_XT);
  89. #endif
  90. /* Enable the RTC */
  91. am_hal_rtc_osc_enable();
  92. /* register rtc device */
  93. rtc.type = RT_Device_Class_RTC;
  94. rtc.init = RT_NULL;
  95. rtc.open = rt_rtc_open;
  96. rtc.close = RT_NULL;
  97. rtc.read = rt_rtc_read;
  98. rtc.write = RT_NULL;
  99. rtc.control = rt_rtc_control;
  100. /* no private */
  101. rtc.user_data = RT_NULL;
  102. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  103. return 0;
  104. }
  105. #ifdef RT_USING_COMPONENTS_INIT
  106. INIT_BOARD_EXPORT(rt_hw_rtc_init);
  107. #endif
  108. /*@}*/