drv_rtc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * 2020-05-19 shelton first version
  9. */
  10. #include "board.h"
  11. #include <rtthread.h>
  12. #ifdef BSP_USING_RTC
  13. #ifndef BKP_DR1
  14. #define BKP_DR1 RT_NULL
  15. #endif
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.rtc"
  18. #include <drv_log.h>
  19. #define BKUP_REG_DATA 0xA5A5
  20. static struct rt_device rtc;
  21. static time_t get_rtc_timestamp(void)
  22. {
  23. #ifdef SOC_SERIES_AT32F415
  24. struct tm tm_new;
  25. ERTC_TimeType ERTC_TimeStruct;
  26. ERTC_DateType ERTC_DateStruct;
  27. ERTC_GetTimeValue(ERTC_Format_BIN, &ERTC_TimeStruct);
  28. ERTC_GetDateValue(ERTC_Format_BIN, &ERTC_DateStruct);
  29. tm_new.tm_sec = ERTC_TimeStruct.ERTC_Seconds;
  30. tm_new.tm_min = ERTC_TimeStruct.ERTC_Minutes;
  31. tm_new.tm_hour = ERTC_TimeStruct.ERTC_Hours;
  32. tm_new.tm_mday = ERTC_DateStruct.ERTC_Date;
  33. tm_new.tm_mon = ERTC_DateStruct.ERTC_Month - 1;
  34. tm_new.tm_year = ERTC_DateStruct.ERTC_Year + 100;
  35. LOG_D("get rtc time.");
  36. return mktime(&tm_new);
  37. #else
  38. return RTC_GetCounter();
  39. #endif
  40. }
  41. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  42. {
  43. #ifdef SOC_SERIES_AT32F415
  44. ERTC_TimeType ERTC_TimeStructure;
  45. ERTC_DateType ERTC_DateStructure;
  46. struct tm *p_tm;
  47. p_tm = localtime(&time_stamp);
  48. if (p_tm->tm_year < 100)
  49. {
  50. return -RT_ERROR;
  51. }
  52. ERTC_TimeStructure.ERTC_Seconds = p_tm->tm_sec ;
  53. ERTC_TimeStructure.ERTC_Minutes = p_tm->tm_min ;
  54. ERTC_TimeStructure.ERTC_Hours = p_tm->tm_hour;
  55. ERTC_DateStructure.ERTC_Date = p_tm->tm_mday;
  56. ERTC_DateStructure.ERTC_Month = p_tm->tm_mon + 1 ;
  57. ERTC_DateStructure.ERTC_Year = p_tm->tm_year - 100;
  58. ERTC_DateStructure.ERTC_WeekDay = p_tm->tm_wday + 1;
  59. if (ERTC_SetTimeValue(ERTC_Format_BIN, &ERTC_TimeStructure) != SUCCESS)
  60. {
  61. return -RT_ERROR;
  62. }
  63. if (ERTC_SetDateValue(ERTC_Format_BIN, &ERTC_DateStructure) != SUCCESS)
  64. {
  65. return -RT_ERROR;
  66. }
  67. #else
  68. /* Set the RTC counter value */
  69. RTC_SetCounter(time_stamp);
  70. /* Wait until last write operation on RTC registers has finished */
  71. RTC_WaitForLastTask();
  72. #endif /* SOC_SERIES_AT32F415 */
  73. LOG_D("set rtc time.");
  74. #ifdef SOC_SERIES_AT32F415
  75. ERTC_WriteBackupRegister(ERTC_BKP_DT0, BKUP_REG_DATA);
  76. #else
  77. BKP_WriteBackupReg(BKP_DT1, BKUP_REG_DATA);
  78. #endif
  79. return RT_EOK;
  80. }
  81. static void rt_rtc_init(void)
  82. {
  83. #if defined (SOC_SERIES_AT32F415)
  84. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_PWR, ENABLE);
  85. #else
  86. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_PWR | RCC_APB1PERIPH_BKP, ENABLE);
  87. #endif
  88. #ifdef BSP_RTC_USING_LSI
  89. RCC_LSICmd(ENABLE);
  90. while(RCC_GetFlagStatus(RCC_FLAG_LSISTBL) == RESET);
  91. #else
  92. PWR_BackupAccessCtrl(ENABLE);
  93. RCC_LSEConfig(RCC_LSE_ENABLE);
  94. while(RCC_GetFlagStatus(RCC_FLAG_LSESTBL) == RESET);
  95. #endif /* BSP_RTC_USING_LSI */
  96. }
  97. static rt_err_t rt_rtc_config(struct rt_device *dev)
  98. {
  99. #if defined (SOC_SERIES_AT32F415)
  100. ERTC_InitType ERTC_InitStructure;
  101. #endif
  102. /* Allow access to BKP Domain */
  103. PWR_BackupAccessCtrl(ENABLE);
  104. #ifdef SOC_SERIES_AT32F415
  105. #ifdef BSP_RTC_USING_LSI
  106. RCC_ERTCCLKConfig(RCC_ERTCCLKSelection_LSI);
  107. RCC_ERTCCLKCmd(ENABLE);
  108. #else
  109. RCC_ERTCCLKConfig(RCC_ERTCCLKSelection_LSE);
  110. RCC_ERTCCLKCmd(ENABLE);
  111. #endif /* BSP_RTC_USING_LSI */
  112. /* Wait for ERTC APB registers synchronisation */
  113. ERTC_WaitForSynchro();
  114. #else
  115. #ifdef BSP_RTC_USING_LSI
  116. RCC_RTCCLKConfig(RCC_RTCCLKSelection_LSI);
  117. RCC_RTCCLKCmd(ENABLE);
  118. #else
  119. RCC_RTCCLKConfig(RCC_RTCCLKSelection_LSE);
  120. RCC_RTCCLKCmd(ENABLE);
  121. #endif /* BSP_RTC_USING_LSI */
  122. /* Wait for RTC registers synchronization */
  123. RTC_WaitForSynchro();
  124. /* Wait until last write operation on RTC registers has finished */
  125. RTC_WaitForLastTask();
  126. #endif /* SOC_SERIES_AT32F415 */
  127. #ifdef SOC_SERIES_AT32F415
  128. if (ERTC_ReadBackupRegister(BKP_DT1)!= BKUP_REG_DATA)
  129. #else
  130. if (BKP_ReadBackupReg(BKP_DT1) != BKUP_REG_DATA)
  131. #endif
  132. {
  133. LOG_I("RTC hasn't been configured, please use <date> command to config.");
  134. #ifdef SOC_SERIES_AT32F415
  135. /* Configure the ERTC data register and ERTC prescaler */
  136. ERTC_InitStructure.ERTC_AsynchPrediv = 0x7F;
  137. ERTC_InitStructure.ERTC_SynchPrediv = 0xFF;
  138. ERTC_InitStructure.ERTC_HourFormat = ERTC_HourFormat_24;
  139. ERTC_Init(&ERTC_InitStructure);
  140. #else
  141. /* Set RTC prescaler: set RTC period to 1sec */
  142. RTC_SetDIV(32767);
  143. /* Wait until last write operation on RTC registers has finished */
  144. RTC_WaitForLastTask();
  145. #endif
  146. }
  147. return RT_EOK;
  148. }
  149. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  150. {
  151. rt_err_t result = RT_EOK;
  152. RT_ASSERT(dev != RT_NULL);
  153. switch (cmd)
  154. {
  155. case RT_DEVICE_CTRL_RTC_GET_TIME:
  156. *(rt_uint32_t *)args = get_rtc_timestamp();
  157. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  158. break;
  159. case RT_DEVICE_CTRL_RTC_SET_TIME:
  160. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  161. {
  162. result = -RT_ERROR;
  163. }
  164. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  165. break;
  166. }
  167. return result;
  168. }
  169. #ifdef RT_USING_DEVICE_OPS
  170. const static struct rt_device_ops rtc_ops =
  171. {
  172. RT_NULL,
  173. RT_NULL,
  174. RT_NULL,
  175. RT_NULL,
  176. RT_NULL,
  177. rt_rtc_control
  178. };
  179. #endif
  180. static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
  181. {
  182. RT_ASSERT(device != RT_NULL);
  183. rt_rtc_init();
  184. if (rt_rtc_config(device) != RT_EOK)
  185. {
  186. return -RT_ERROR;
  187. }
  188. #ifdef RT_USING_DEVICE_OPS
  189. device->ops = &rtc_ops;
  190. #else
  191. device->init = RT_NULL;
  192. device->open = RT_NULL;
  193. device->close = RT_NULL;
  194. device->read = RT_NULL;
  195. device->write = RT_NULL;
  196. device->control = rt_rtc_control;
  197. #endif
  198. device->type = RT_Device_Class_RTC;
  199. device->rx_indicate = RT_NULL;
  200. device->tx_complete = RT_NULL;
  201. device->user_data = RT_NULL;
  202. /* register a character device */
  203. return rt_device_register(device, name, flag);
  204. }
  205. int rt_hw_rtc_init(void)
  206. {
  207. rt_err_t result;
  208. result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  209. if (result != RT_EOK)
  210. {
  211. LOG_E("rtc register err code: %d", result);
  212. return result;
  213. }
  214. LOG_D("rtc init success");
  215. return RT_EOK;
  216. }
  217. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  218. #endif /* BSP_USING_RTC */