drv_rtc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * File : drv_rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, 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. * 2017-10-25 ZYH first implementation
  23. */
  24. #include "drv_rtc.h"
  25. #include <board.h>
  26. #include <rtdevice.h>
  27. #include <string.h>
  28. #include <time.h>
  29. RTC_HandleTypeDef hrtc;
  30. /* RTC init function */
  31. void MX_RTC_Init(void)
  32. {
  33. RTC_TimeTypeDef sTime;
  34. RTC_DateTypeDef sDate;
  35. /**Initialize RTC Only
  36. */
  37. hrtc.Instance = RTC;
  38. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  39. hrtc.Init.AsynchPrediv = 127;
  40. hrtc.Init.SynchPrediv = 255;
  41. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  42. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  43. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  44. RT_ASSERT(HAL_RTC_Init(&hrtc) == HAL_OK);
  45. /**Initialize RTC and set the Time and Date
  46. */
  47. if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2)
  48. {
  49. sTime.Hours = 22;
  50. sTime.Minutes = 28;
  51. sTime.Seconds = 0;
  52. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  53. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  54. RT_ASSERT(HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK);
  55. sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
  56. sDate.Month = RTC_MONTH_OCTOBER;
  57. sDate.Date = 26;
  58. sDate.Year = 17;
  59. RT_ASSERT(HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK);
  60. }
  61. HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
  62. }
  63. void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle)
  64. {
  65. if (rtcHandle->Instance == RTC)
  66. {
  67. /* USER CODE BEGIN RTC_MspInit 0 */
  68. /* USER CODE END RTC_MspInit 0 */
  69. /* RTC clock enable */
  70. __HAL_RCC_RTC_ENABLE();
  71. /* USER CODE BEGIN RTC_MspInit 1 */
  72. /* USER CODE END RTC_MspInit 1 */
  73. }
  74. }
  75. void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
  76. {
  77. if (rtcHandle->Instance == RTC)
  78. {
  79. /* USER CODE BEGIN RTC_MspDeInit 0 */
  80. /* USER CODE END RTC_MspDeInit 0 */
  81. /* Peripheral clock disable */
  82. __HAL_RCC_RTC_DISABLE();
  83. /* USER CODE BEGIN RTC_MspDeInit 1 */
  84. /* USER CODE END RTC_MspDeInit 1 */
  85. }
  86. }
  87. static rt_err_t stm32_rtc_control(struct rt_device *dev,
  88. int cmd,
  89. void *args)
  90. {
  91. struct tm *tm_now;
  92. struct tm now;
  93. RTC_TimeTypeDef sTime;
  94. RTC_DateTypeDef sDate;
  95. rt_enter_critical();
  96. /* converts calendar time time into local time. */
  97. tm_now = localtime((const time_t *) args);
  98. /* copy the statically located variable */
  99. memcpy(&now, tm_now, sizeof(struct tm));
  100. /* unlock scheduler. */
  101. rt_exit_critical();
  102. switch (cmd)
  103. {
  104. case RT_DEVICE_CTRL_RTC_GET_TIME:
  105. HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
  106. HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
  107. now.tm_hour = sTime.Hours;
  108. now.tm_min = sTime.Minutes;
  109. now.tm_sec = sTime.Seconds;
  110. now.tm_year = sDate.Year + 100;
  111. now.tm_mon = sDate.Month - 1;
  112. now.tm_mday = sDate.Date;
  113. *((time_t *)args) = mktime(&now);
  114. break;
  115. case RT_DEVICE_CTRL_RTC_SET_TIME:
  116. sTime.Hours = now.tm_hour;
  117. sTime.Minutes = now.tm_min;
  118. sTime.Seconds = now.tm_sec;
  119. sDate.Year = now.tm_year - 100;
  120. sDate.Month = now.tm_mon + 1;
  121. sDate.Date = now.tm_mday;
  122. HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
  123. HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
  124. break;
  125. }
  126. return RT_EOK;
  127. }
  128. static rt_err_t stm32_rtc_init(struct rt_device *dev)
  129. {
  130. return RT_EOK;
  131. }
  132. static rt_err_t stm32_rtc_open(struct rt_device *dev, rt_uint16_t oflag)
  133. {
  134. return RT_EOK;
  135. }
  136. static rt_err_t stm32_rtc_close(struct rt_device *dev)
  137. {
  138. return RT_EOK;
  139. }
  140. static rt_size_t stm32_rtc_read(struct rt_device *dev,
  141. rt_off_t pos,
  142. void *buffer,
  143. rt_size_t size)
  144. {
  145. stm32_rtc_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, buffer);
  146. return size;
  147. }
  148. static rt_size_t stm32_rtc_write(struct rt_device *dev,
  149. rt_off_t pos,
  150. const void *buffer,
  151. rt_size_t size)
  152. {
  153. stm32_rtc_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, (void *)buffer);
  154. return size;
  155. }
  156. struct rt_device rtc_device;
  157. int rt_hw_rtc_init(void)
  158. {
  159. MX_RTC_Init();
  160. rtc_device.type = RT_Device_Class_RTC;
  161. rtc_device.rx_indicate = RT_NULL;
  162. rtc_device.tx_complete = RT_NULL;
  163. rtc_device.init = stm32_rtc_init;
  164. rtc_device.open = stm32_rtc_open;
  165. rtc_device.close = stm32_rtc_close;
  166. rtc_device.read = stm32_rtc_read;
  167. rtc_device.write = stm32_rtc_write;
  168. rtc_device.control = stm32_rtc_control;
  169. rtc_device.user_data = RT_NULL;
  170. /* register a character device */
  171. return rt_device_register(&rtc_device, "rtc", RT_DEVICE_FLAG_DEACTIVATE);
  172. }
  173. INIT_BOARD_EXPORT(rt_hw_rtc_init);