drv_rtc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2021-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-19 HPMicro First version
  9. * 2023-05-08 HPMicro Adapt RT-Thread V5.0.0
  10. */
  11. #include "board.h"
  12. #include "drv_rtc.h"
  13. #include "hpm_rtc_drv.h"
  14. #include "hpm_bpor_drv.h"
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <rtdbg.h>
  18. #ifdef RT_USING_RTC
  19. /*******************************************************************************************
  20. *
  21. * Prototypes
  22. *
  23. ******************************************************************************************/
  24. static rt_err_t hpm_rtc_init(rt_device_t dev);
  25. static rt_err_t hpm_rtc_open(rt_device_t dev, rt_uint16_t oflag);
  26. static rt_err_t hpm_rtc_close(rt_device_t dev);
  27. static rt_ssize_t hpm_rtc_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size);
  28. static rt_ssize_t hpm_rtc_write(rt_device_t dev, rt_off_t pos, const void *buf, rt_size_t size);
  29. static rt_err_t hpm_rtc_control(rt_device_t dev, int cmd, void *args);
  30. static time_t hpm_rtc_get_timestamp(void);
  31. static int hpm_rtc_set_timestamp(time_t timestamp);
  32. /*******************************************************************************************
  33. *
  34. * Variables
  35. *
  36. ******************************************************************************************/
  37. #ifdef RT_USING_DEVICE_OPS
  38. const struct rt_device_ops hpm_rtc_ops = {
  39. .init = hpm_rtc_init,
  40. .open = hpm_rtc_open,
  41. .close = hpm_rtc_close,
  42. .read = hpm_rtc_read,
  43. .write = hpm_rtc_write,
  44. .control = hpm_rtc_control,
  45. };
  46. #endif
  47. static struct rt_device hpm_rtc= {
  48. .type = RT_Device_Class_RTC,
  49. #ifdef RT_USING_DEVICE_OPS
  50. .ops = &hpm_rtc_ops,
  51. #else
  52. .init = hpm_rtc_init,
  53. .open = hpm_rtc_open,
  54. .close = hpm_rtc_close,
  55. .read = hpm_rtc_read,
  56. .write = hpm_rtc_write,
  57. .control = hpm_rtc_control,
  58. #endif
  59. };
  60. /*******************************************************************************************
  61. *
  62. * Codes
  63. *
  64. ******************************************************************************************/
  65. static rt_err_t hpm_rtc_init(rt_device_t dev)
  66. {
  67. /* Enable Power retention mode for the battery domain */
  68. bpor_enable_reg_value_retention(HPM_BPOR);
  69. return RT_EOK;
  70. }
  71. static rt_err_t hpm_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  72. {
  73. return RT_EOK;
  74. }
  75. static rt_err_t hpm_rtc_close(rt_device_t dev)
  76. {
  77. return RT_EOK;
  78. }
  79. static rt_ssize_t hpm_rtc_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size)
  80. {
  81. return 0;
  82. }
  83. static rt_ssize_t hpm_rtc_write(rt_device_t dev, rt_off_t pos, const void *buf, rt_size_t size)
  84. {
  85. return 0;
  86. }
  87. static rt_err_t hpm_rtc_control(rt_device_t dev, int cmd, void *args)
  88. {
  89. RT_ASSERT(dev != RT_NULL);
  90. rt_err_t err = RT_EOK;
  91. switch(cmd) {
  92. case RT_DEVICE_CTRL_RTC_GET_TIME:
  93. *(uint32_t *)args = hpm_rtc_get_timestamp();
  94. break;
  95. case RT_DEVICE_CTRL_RTC_SET_TIME:
  96. hpm_rtc_set_timestamp(*(time_t *)args);
  97. break;
  98. default:
  99. err = RT_EINVAL;
  100. break;
  101. }
  102. return err;
  103. }
  104. static time_t hpm_rtc_get_timestamp(void)
  105. {
  106. time_t time = rtc_get_time(HPM_RTC);
  107. return time;
  108. }
  109. static int hpm_rtc_set_timestamp(time_t timestamp)
  110. {
  111. (void)rtc_config_time(HPM_RTC, timestamp);
  112. return RT_EOK;
  113. }
  114. int rt_hw_rtc_init(void)
  115. {
  116. rt_err_t err = RT_EOK;
  117. err = rt_device_register(&hpm_rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  118. if (err != RT_EOK) {
  119. LOG_E("rt device %s failed, status=%d\n", "rtc", err);
  120. return err;
  121. }
  122. rt_device_open(&hpm_rtc, RT_DEVICE_FLAG_RDWR);
  123. return RT_EOK;
  124. }
  125. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  126. #endif /* RT_USING_RTC */