rtc.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-xx-xx user first version
  9. */
  10. #ifndef __RTC_H__
  11. #define __RTC_H__
  12. #include <rtthread.h>
  13. #include <stm32f4xx_hal.h>
  14. /* RTC备份寄存器标记地址 - 使用RTC_BKP_DR0 */
  15. #define RTC_BKP_FLAG RTC_BKP_DR0
  16. /* RTC已初始化标记值 */
  17. #define RTC_INIT_FLAG 0x3568
  18. /* 默认日期时间: 2024-01-01 00:00:00 */
  19. #define RTC_DEFAULT_YEAR 26 /* 2000 + 24 = 2024 */
  20. #define RTC_DEFAULT_MONTH 1
  21. #define RTC_DEFAULT_DATE 1
  22. #define RTC_DEFAULT_HOUR 0
  23. #define RTC_DEFAULT_MINUTE 0
  24. #define RTC_DEFAULT_SECOND 0
  25. /* RTC时间结构体 */
  26. struct rtc_time
  27. {
  28. rt_uint16_t year; /* 年份: 2000-2099 */
  29. rt_uint8_t month; /* 月份: 1-12 */
  30. rt_uint8_t date; /* 日期: 1-31 */
  31. rt_uint8_t hour; /* 小时: 0-23 */
  32. rt_uint8_t minute; /* 分钟: 0-59 */
  33. rt_uint8_t second; /* 秒钟: 0-59 */
  34. rt_uint8_t weekday; /* 星期: 0-6 */
  35. };
  36. struct TM {
  37. uint8_t tm_sec;
  38. uint8_t tm_min;
  39. uint8_t tm_hour;
  40. uint8_t tm_mday;
  41. uint8_t tm_mon;
  42. uint8_t tm_year;
  43. };
  44. /* RTC初始化函数 */
  45. int rt_hw_rtc_init(void);
  46. /* 获取RTC时间 */
  47. int rtc_get_time(struct rtc_time *time);
  48. /* 设置RTC时间 */
  49. int rtc_set_time(struct rtc_time *time);
  50. /* 将RTC时间转换为字符串 */
  51. void rtc_time_to_str(const struct rtc_time *time, char *buf, rt_size_t size);
  52. #endif /* __RTC_H__ */