drv_rtc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-05 sundm75 first version
  9. */
  10. /* Includes ------------------------------------------------------------------*/
  11. #include "board.h"
  12. #include "drv_rtc.h"
  13. #include <rtdevice.h>
  14. #include <sys/time.h>
  15. #include "../libraries/ls1c_regs.h"
  16. #include "../libraries/ls1c_rtc.h"
  17. //#define RT_RTC_DEBUG
  18. #if defined(RT_USING_RTC)
  19. #ifdef RT_RTC_DEBUG
  20. #define rtc_debug(format,args...) rt_kprintf(format, ##args)
  21. #else
  22. #define rtc_debug(format,args...)
  23. #endif
  24. static struct rt_device rtc;
  25. RTC_TypeDef * RTC_Handler;
  26. static time_t get_timestamp(void)
  27. {
  28. struct tm tm_new = {0};
  29. RTC_TimeTypeDef rtcDate;
  30. RTC_GetTime(RTC_Handler, &rtcDate);
  31. tm_new.tm_sec = rtcDate.Seconds;
  32. tm_new.tm_min = rtcDate.Minutes;
  33. tm_new.tm_hour = rtcDate.Hours;
  34. tm_new.tm_mday = rtcDate.Date;
  35. tm_new.tm_mon = rtcDate.Month- 1;
  36. tm_new.tm_year = rtcDate.Year + 2000 - 1900;
  37. return timegm(&tm_new);
  38. }
  39. static int set_timestamp(time_t timestamp)
  40. {
  41. struct tm now;
  42. RTC_TimeTypeDef rtcDate;
  43. gmtime_r(&timestamp, &now);
  44. rtcDate.Seconds= now.tm_sec ;
  45. rtcDate.Minutes= now.tm_min ;
  46. rtcDate.Hours= now.tm_hour;
  47. rtcDate.Date= now.tm_mday;
  48. rtcDate.Month= now.tm_mon + 1;
  49. rtcDate.Year= now.tm_year + 1900 - 2000;
  50. RTC_SetTime(RTC_Handler, &rtcDate);
  51. rt_kprintf("\r\nrtcDate is %d.%d.%d - %d:%d:%d",rtcDate.Year, rtcDate.Month, rtcDate.Date, rtcDate.Hours, rtcDate.Minutes, rtcDate.Seconds);
  52. return RT_EOK;
  53. }
  54. rt_uint8_t RTC_Init(void)
  55. {
  56. RTC_Handler = RTC;
  57. return 0;
  58. }
  59. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  60. {
  61. if (dev->rx_indicate != RT_NULL)
  62. {
  63. /* Open Interrupt */
  64. }
  65. return RT_EOK;
  66. }
  67. static rt_size_t rt_rtc_read(
  68. rt_device_t dev,
  69. rt_off_t pos,
  70. void* buffer,
  71. rt_size_t size)
  72. {
  73. return 0;
  74. }
  75. /**
  76. * This function configure RTC device.
  77. *
  78. * @param dev, pointer to device descriptor.
  79. * @param cmd, RTC control command.
  80. *
  81. * @return the error code.
  82. */
  83. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  84. {
  85. rt_err_t result;
  86. RT_ASSERT(dev != RT_NULL);
  87. switch (cmd)
  88. {
  89. case RT_DEVICE_CTRL_RTC_GET_TIME:
  90. *(rt_uint32_t *)args = get_timestamp();
  91. rtc_debug("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  92. break;
  93. case RT_DEVICE_CTRL_RTC_SET_TIME:
  94. {
  95. result = set_timestamp(*(rt_uint32_t *)args);
  96. rtc_debug("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  97. }
  98. break;
  99. }
  100. return result;
  101. }
  102. /**
  103. * This function register RTC device.
  104. *
  105. * @param device, pointer to device descriptor.
  106. * @param name, device name.
  107. * @param flag, configuration flags.
  108. *
  109. * @return the error code.
  110. */
  111. rt_err_t rt_hw_rtc_register(
  112. rt_device_t device,
  113. const char *name,
  114. rt_uint32_t flag)
  115. {
  116. RT_ASSERT(device != RT_NULL);
  117. device->type = RT_Device_Class_RTC;
  118. device->rx_indicate = RT_NULL;
  119. device->tx_complete = RT_NULL;
  120. device->init = RT_NULL;
  121. device->open = rt_rtc_open;
  122. device->close = RT_NULL;
  123. device->read = rt_rtc_read;
  124. device->write = RT_NULL;
  125. device->control = rt_rtc_control;
  126. device->user_data = RT_NULL; /* no private */
  127. /* register a character device */
  128. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  129. }
  130. /**
  131. * This function initialize RTC module related hardware and register RTC device to kernel.
  132. *
  133. * @param none.
  134. *
  135. * @return the error code.
  136. */
  137. int rt_hw_rtc_init(void)
  138. {
  139. RTC_Init();
  140. /* register rtc device */
  141. rt_hw_rtc_register(&rtc, RT_RTC_NAME, 0);
  142. return RT_EOK;
  143. }
  144. INIT_BOARD_EXPORT(rt_hw_rtc_init);
  145. #endif