1
0

drv_rtc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * 2021-08-31 AisinoChip first add to bsp
  9. */
  10. #include "board.h"
  11. #include <sys/time.h>
  12. #include <rtdevice.h>
  13. #ifdef BSP_USING_RTC
  14. static RTC_ConfigTypeDef RTC_Handle;
  15. rt_inline rt_uint8_t dec2hex(rt_uint8_t dec)
  16. {
  17. return ((dec / 10) << 4) + (dec % 10);
  18. }
  19. rt_inline rt_uint8_t hex2dec(rt_uint8_t hex)
  20. {
  21. return ((hex / 16) * 10) + (hex % 16);
  22. }
  23. static time_t get_rtc_timestamp(void)
  24. {
  25. RTC_TimeTypeDef RTC_TimeStruct = {0};
  26. RTC_DateTypeDef RTC_DateStruct = {0};
  27. struct tm tm_new = {0};
  28. HAL_RTC_GetTime(&RTC_TimeStruct);
  29. HAL_RTC_GetDate(&RTC_DateStruct);
  30. tm_new.tm_sec = hex2dec(RTC_TimeStruct.u8_Seconds);
  31. tm_new.tm_min = hex2dec(RTC_TimeStruct.u8_Minutes);
  32. tm_new.tm_hour = hex2dec(RTC_TimeStruct.u8_Hours);
  33. tm_new.tm_mday = hex2dec(RTC_DateStruct.u8_Date);
  34. tm_new.tm_mon = hex2dec(RTC_DateStruct.u8_Month) - 1;
  35. tm_new.tm_year = hex2dec(RTC_DateStruct.u8_Year) + 100;
  36. return timegm(&tm_new);
  37. }
  38. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  39. {
  40. RTC_TimeTypeDef RTC_TimeStruct = {0};
  41. RTC_DateTypeDef RTC_DateStruct = {0};
  42. struct tm now;
  43. gmtime_r(&time_stamp, &now);
  44. if (now.tm_year < 100)
  45. {
  46. return -RT_ERROR;
  47. }
  48. RTC_TimeStruct.u8_Seconds = dec2hex(now.tm_sec);
  49. RTC_TimeStruct.u8_Minutes = dec2hex(now.tm_min);
  50. RTC_TimeStruct.u8_Hours = dec2hex(now.tm_hour);
  51. RTC_DateStruct.u8_Date = dec2hex(now.tm_mday);
  52. RTC_DateStruct.u8_Month = dec2hex(now.tm_mon + 1);
  53. RTC_DateStruct.u8_Year = dec2hex(now.tm_year - 100);
  54. RTC_DateStruct.u8_WeekDay = dec2hex(now.tm_wday) + 1;
  55. HAL_RTC_SetTime(&RTC_TimeStruct);
  56. HAL_RTC_SetDate(&RTC_DateStruct);
  57. return RT_EOK;
  58. }
  59. static rt_err_t _rtc_init(void)
  60. {
  61. RTC_Handle.u32_ClockSource = RTC_CLOCK_XTL;
  62. RTC_Handle.u32_Compensation = COMPENSATION_INCREASE;
  63. RTC_Handle.u32_CompensationValue = 0x05;
  64. HAL_RTC_Config(&RTC_Handle);
  65. return RT_EOK;
  66. }
  67. static rt_err_t _rtc_get_secs(void *args)
  68. {
  69. *(rt_uint32_t *)args = get_rtc_timestamp();
  70. return RT_EOK;
  71. }
  72. static rt_err_t _rtc_set_secs(void *args)
  73. {
  74. rt_err_t result = RT_EOK;
  75. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  76. {
  77. result = -RT_ERROR;
  78. }
  79. return result;
  80. }
  81. static const struct rt_rtc_ops acm32_rtc_ops =
  82. {
  83. _rtc_init,
  84. _rtc_get_secs,
  85. _rtc_set_secs,
  86. RT_NULL,
  87. RT_NULL,
  88. RT_NULL,
  89. RT_NULL,
  90. };
  91. static rt_rtc_dev_t acm32_rtc_dev;
  92. static int rt_hw_rtc_init(void)
  93. {
  94. acm32_rtc_dev.ops = &acm32_rtc_ops;
  95. return rt_hw_rtc_register(&acm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  96. }
  97. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  98. #endif /* BSP_USING_RTC */