rtc.c 3.4 KB

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