1. 概述

RTC (Real-Time Clock)实时时钟可以提供精确的实时时间,它可以用于产生年、月、日、时、分、秒等信息。
TG6210的 RTC设备为操作系统的时间系统提供了基础服务。面对越来越多的 IoT 场景,RTC 已经成为产品的标配,甚至在诸如 SSL 的安全传输过程中,RTC 已经成为不可或缺的部分。

2. API参考

2.1 RTC初始化

  • 函数原型:
    int hosal_rtc_init(hosal_rtc_dev_t *rtc);
  • 功能描述:初始化RTC
  • 函数参数:
    • rtc:需要初始化的RTC句柄 ```c typedef struct { uint8_t format; /*< time formart DEC or BCD / } hosal_rtc_config_t;
  1. ```c
  2. typedef struct {
  3. uint8_t port; /**< rtc port */
  4. hosal_rtc_config_t config; /**< rtc config */
  5. void *priv; /**< priv data */
  6. } hosal_rtc_dev_t;
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      2.2 设置时间

  • 函数原型:
    int hosal_rtc_set_time(hosal_rtc_dev_t *rtc, const hosal_rtc_time_t *time);

  • 功能描述:设置时间。
  • 函数参数:
    • rtc:RTC的句柄
    • time:需要设置时间的句柄 ```c typedef struct { uint8_t sec; /< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 */ uint8_t min; /< DEC format:value range from 0 to 59, BCD format:value range from 0x00 to 0x59 / uint8_t hr; /**< DEC format:value range from 0 to 23, BCD format:value range from 0x00 to 0x23 / uint8_t date; /< DEC format:value range from 1 to 31, BCD format:value range from 0x01 to 0x31 */ uint8_t month; /< DEC format:value range from 1 to 12, BCD format:value range from 0x01 to 0x12 / uint16_t year; /**< DEC format:value range from 0 to 9999, BCD format:value range from 0x0000 to 0x9999 / } hosal_rtc_time_t;
  1. - 返回值:
  2. - 成功返回0
  3. - 错误返回-1
  4. <a name="bMUff"></a>
  5. ## 2.3 获取当前时间
  6. - 函数原型:<br />`int hosal_rtc_get_time(hosal_rtc_dev_t *rtc, hosal_rtc_time_t *time);`
  7. - 功能描述:获取时间。
  8. - 函数参数:
  9. - rtcRTC的句柄
  10. - time:存储时间的句柄
  11. - 返回值:
  12. - 成功返回0
  13. - 错误返回-1
  14. <a name="GSBej"></a>
  15. ## 2.4 设置RTC Counter值
  16. - 函数原型:<br />`int hosal_rtc_set_count(hosal_rtc_dev_t *rtc, uint64_t *time_stamp);`
  17. - 功能描述:设置counter
  18. - 函数参数:
  19. - rtcRTC的句柄
  20. - time_stamp:需要设置counter的句柄
  21. - 返回值:
  22. - 成功返回0
  23. - 错误返回-1
  24. <a name="zqNFf"></a>
  25. ## 2.5 获取RTC Counter值
  26. - 函数原型:<br />`int hosal_rtc_get_count(hosal_rtc_dev_t *rtc, uint64_t *time_stamp);`
  27. - 功能描述:获取counter
  28. - 函数参数:
  29. - rtcRTC的句柄
  30. - time_stamp:存储counter的句柄
  31. - 返回值:
  32. - 成功返回0
  33. - 错误返回-1
  34. <a name="ZCGap"></a>
  35. ## 2.6 反初始化RTC
  36. - 函数原型:<br />`int hosal_rtc_finalize(hosal_rtc_dev_t *rtc);`
  37. - 功能描述:deinit RTC
  38. - 函数参数:
  39. - rtcRTC的句柄
  40. - 返回值:
  41. - 成功返回0
  42. - 错误返回-1
  43. <a name="r8Yi5"></a>
  44. # 3. Demo示例
  45. ```c
  46. int ret = -1
  47. hosal_rtc_dev_t rtc;
  48. rtc.port = 0;
  49. rtc.config.format =HOSAL_RTC_FORMAT_BCD;
  • 通过 hosal_rtc_dev_t 定义一个rtc设备,并设置port,格式设置为BCD格式。
  ret = hosal_rtc_init(&rtc);
  if (ret != 0) {
      blog_error("init rtc error\r\n");
  }
  • 调用 hosal_rtc_init 初始化rtc 。
  int ret = -1;
  hosal_rtc_time_t time1;
  time1.sec   = 0x59;
  time1.min   = 0x59;
  time1.hr    = 0x23;
  time1.data  = 0x31;
  time1.month = 0x12;
  time1.year  = 0x37;
  ret = hosal_rtc_set_time(&rtc, &time1);
  if (ret != 0) {
      blog_error("set rtc time error\r\n");
  }
  • 通过 hosal_rtc_set_time 来设置rtc时间,此时按照BCD格式设置。
  hosal_rtc_time_t time2;
  memset(&time, 0, sizeof(hosal_rtc_time_t));
  ret = hosal_rtc_get_time(&rtc, &time2);
  if(ret != 0) {
      blog_error("get rtc time error\r\n");
  }
  blog_info(" get time sec     = %x\r\n", time2.sec);
  blog_info(" get time min     = %x\r\n", time2.min);
  blog_info(" get time hr      = %x\r\n", time2.hr);
  blog_info(" get time weekday = %x\r\n", time2.weekday);
  blog_info(" get time data    = %x\r\n", time2.data);
  blog_info(" get time month   = %x\r\n", time2.month);
  blog_info(" get time year    = %x\r\n", time2.year);
  • 通过 hosal_rtc_get_time 接口来获取rtc时间,注意获取到的时间也是BCD格式的。
  hosal_rtc_finalize(&rtc);
  • 通过 hosal_rtc_finalize 结束此rtc的使用。