drv_rtc.c 3.8 KB

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