drv_rtc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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-11-17 JasonHu first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <rtdbg.h>
  16. #include <sunxi_hal_rtc.h>
  17. #ifdef RT_USING_RTC
  18. static rt_err_t rtc_init(struct rt_device *dev)
  19. {
  20. if (hal_rtc_init() != 0)
  21. {
  22. LOG_E("init rtc hal failed!");
  23. return -RT_ERROR;
  24. }
  25. return RT_EOK;
  26. }
  27. static rt_err_t rtc_control(rt_device_t dev, int cmd, void *args)
  28. {
  29. rt_err_t result = RT_EOK;
  30. struct tm time_temp;
  31. struct tm *time_now;
  32. struct rtc_time hal_rtc_time;
  33. switch (cmd)
  34. {
  35. case RT_DEVICE_CTRL_RTC_GET_TIME:
  36. if (hal_rtc_gettime(&hal_rtc_time) != 0)
  37. {
  38. LOG_E("rtc gettime failed!\n");
  39. return -RT_ERROR;
  40. }
  41. time_temp.tm_sec = hal_rtc_time.tm_sec;
  42. time_temp.tm_min = hal_rtc_time.tm_min;
  43. time_temp.tm_hour = hal_rtc_time.tm_hour;
  44. time_temp.tm_mday = hal_rtc_time.tm_mday;
  45. time_temp.tm_mon = hal_rtc_time.tm_mon;
  46. time_temp.tm_year = hal_rtc_time.tm_year;
  47. *((time_t *)args) = mktime(&time_temp);
  48. break;
  49. case RT_DEVICE_CTRL_RTC_SET_TIME:
  50. rt_enter_critical();
  51. /* converts calendar time time into local time. */
  52. time_now = localtime((const time_t *)args);
  53. /* copy the statically located variable */
  54. memcpy(&time_temp, time_now, sizeof(struct tm));
  55. /* unlock scheduler. */
  56. rt_exit_critical();
  57. hal_rtc_time.tm_sec = time_temp.tm_sec;
  58. hal_rtc_time.tm_min = time_temp.tm_min;
  59. hal_rtc_time.tm_hour = time_temp.tm_hour;
  60. hal_rtc_time.tm_mday = time_temp.tm_mday;
  61. hal_rtc_time.tm_mon = time_temp.tm_mon;
  62. hal_rtc_time.tm_year = time_temp.tm_year;
  63. if (hal_rtc_settime(&hal_rtc_time) != 0)
  64. {
  65. LOG_E("rtc settime failed!\n");
  66. return -RT_ERROR;
  67. }
  68. break;
  69. default:
  70. return -RT_EINVAL;
  71. }
  72. return result;
  73. }
  74. #ifdef RT_USING_DEVICE_OPS
  75. const static struct rt_device_ops rt_hw_rtc_ops =
  76. {
  77. rtc_init,
  78. RT_NULL,
  79. RT_NULL,
  80. RT_NULL,
  81. RT_NULL,
  82. rtc_control
  83. };
  84. #endif
  85. int rt_hw_rtc_init(void)
  86. {
  87. rt_err_t ret = RT_EOK;
  88. static struct rt_device rtc_dev;
  89. rtc_dev.type = RT_Device_Class_RTC;
  90. rtc_dev.rx_indicate = RT_NULL;
  91. rtc_dev.tx_complete = RT_NULL;
  92. #ifdef RT_USING_DEVICE_OPS
  93. rtc_dev.ops = &rt_hw_rtc_ops;
  94. #else
  95. rtc_dev.init = rtc_init;
  96. rtc_dev.open = RT_NULL;
  97. rtc_dev.close = RT_NULL;
  98. rtc_dev.read = RT_NULL;
  99. rtc_dev.write = RT_NULL;
  100. rtc_dev.control = rtc_control;
  101. #endif
  102. rtc_dev.user_data = RT_NULL;
  103. #ifdef BSP_USING_RTC
  104. ret = rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  105. #endif
  106. return ret;
  107. }
  108. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  109. #endif