drv_rtc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * File : drv_rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2012, 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. * 2016/7/28 Urey the first version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <time.h>
  30. #include "board.h"
  31. #include "drv_clock.h"
  32. #include "drv_gpio.h"
  33. #include "drv_ost.h"
  34. #include "drv_rtc.h"
  35. static rt_base_t rtc32k_ref = 0;
  36. void rtc32k_enable(void)
  37. {
  38. if(++rtc32k_ref == 1)
  39. gpio_set_func(GPIO_PORT_B,GPIO_Pin_26,GPIO_FUNC_0);
  40. }
  41. void rtc32k_disable(void)
  42. {
  43. if(--rtc32k_ref == 0)
  44. gpio_set_func(GPIO_PORT_B,GPIO_Pin_26,GPIO_INPUT);
  45. }
  46. #if 0 /* not enable */
  47. static void jz_rtc_interrupt(int vector,void *param)
  48. {
  49. if (rtc_read_reg(RTC_RTCCR) & RTC_RTCCR_AF) /* rtc alarm interrupt */
  50. {
  51. rtc_clr_reg(RTC_RTCCR, RTC_RTCCR_AF);
  52. #ifdef AHB_MONITOR_PERIOD
  53. // Stop and log the data, and the 1st of the data is INVALID.
  54. MONITOR_LOG();
  55. // Start the monitor.
  56. MONITOR_START(MASTER_IPU, MEVENT_BUS_TRANS_CYCLE, MASTER_ALL, MEVENT_BUS_TRANS_CYCLE);
  57. // Config the RTC alarm for the next time
  58. rtc_write_reg(RTC_RTCSAR, rtc_read_reg(RTC_RTCSR) + AHB_MONITOR_PERIOD);
  59. #else
  60. printf("RTC alarm interrupt clean!\n");
  61. rtc_alarm_handler();
  62. #endif
  63. }
  64. else
  65. {
  66. rtc_clr_reg(RTC_RTCCR, RTC_RTCCR_1HZ);
  67. }
  68. }
  69. #endif
  70. void jz_rtc_init(void)
  71. {
  72. unsigned int tmpx , flag;
  73. tmpx = rtc_read_reg(RTC_BASE + RTC_HSPR);
  74. flag = rtc_read_reg(RTC_BASE + RTC_RTCGR) & RTC_RTCGR_NC1HZ_MASK;
  75. if((tmpx != COLD_BOOT_SIG) || (flag != RTCGR_DIV_1HZ))
  76. {
  77. rt_kprintf("rtc is not configured\n");
  78. rt_kprintf("please configure with set_date and set_time\n");
  79. rtc_write_reg(RTC_BASE + RTC_RTCGR, RTCGR_DIV_1HZ);
  80. rtc_write_reg(RTC_BASE + RTC_HSPR, COLD_BOOT_SIG);
  81. rtc_write_reg(RTC_BASE + RTC_RTCSR, 0x00000000);
  82. rtc_write_reg(RTC_BASE + RTC_RTCSR, 0);
  83. rtc_set_reg(RTC_BASE + RTC_RTCCR, RTC_RTCCR_RTCE);
  84. }
  85. else
  86. {
  87. rt_kprintf("skip init rtc...\n");
  88. }
  89. }
  90. void jz_rtc_deinit(void)
  91. {
  92. rtc_clr_reg(RTC_BASE + RTC_RTCCR, RTC_RTCCR_RTCE);
  93. }
  94. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  95. {
  96. if (dev->rx_indicate != RT_NULL)
  97. {
  98. /* Open Interrupt */
  99. }
  100. return RT_EOK;
  101. }
  102. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  103. {
  104. return 0;
  105. }
  106. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  107. {
  108. time_t *time;
  109. RT_ASSERT(dev != RT_NULL);
  110. switch (cmd)
  111. {
  112. case RT_DEVICE_CTRL_RTC_GET_TIME:
  113. {
  114. const struct tm* tm_now;
  115. struct tm tm_new;
  116. time_t timeNow;
  117. time = (time_t *)args;
  118. /* Get the current Time */
  119. timeNow = rtc_read_reg(RTC_BASE + RTC_RTCSR);
  120. tm_now = localtime(&timeNow);
  121. /* 0-99 range : Years since 1900 */
  122. rt_memcpy(&tm_new,tm_now,sizeof(struct tm));
  123. tm_new.tm_year = tm_now->tm_year + 2000 - 1900;
  124. *time = mktime(&tm_new);
  125. }
  126. break;
  127. case RT_DEVICE_CTRL_RTC_SET_TIME:
  128. {
  129. const struct tm* tm_now;
  130. struct tm tm_new;
  131. time_t timeNow;
  132. time = (time_t *)args;
  133. tm_now = localtime(time);
  134. rt_memcpy(&tm_new,tm_now,sizeof(struct tm));
  135. /* 0-99 range : Years since 1900 */
  136. tm_new.tm_year = tm_now->tm_year + 1900 - 2000;
  137. timeNow = mktime(&tm_new);
  138. /* upgrade the current Time */
  139. rtc_write_reg(RTC_BASE + RTC_RTCSR,timeNow);
  140. }
  141. break;
  142. }
  143. return RT_EOK;
  144. }
  145. int rt_hw_rtc_init(void)
  146. {
  147. static struct rt_device rtc;
  148. jz_rtc_init();
  149. rtc.type = RT_Device_Class_RTC;
  150. /* register rtc device */
  151. rtc.init = RT_NULL;
  152. rtc.open = rt_rtc_open;
  153. rtc.close = RT_NULL;
  154. rtc.read = rt_rtc_read;
  155. rtc.write = RT_NULL;
  156. rtc.control = rt_rtc_control;
  157. /* no private */
  158. rtc.user_data = RT_NULL;
  159. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  160. }
  161. INIT_DEVICE_EXPORT(rt_hw_rtc_init);