drv_rtc.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * 2022-02-22 airm2m first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include <sys/time.h>
  14. #ifdef BSP_USING_ONCHIP_RTC
  15. #ifndef RTC_BKP_DR1
  16. #define RTC_BKP_DR1 RT_NULL
  17. #endif
  18. //#define DRV_DEBUG
  19. #define LOG_TAG "drv.rtc"
  20. #include <drv_log.h>
  21. static rt_err_t air105_rtc_init(void)
  22. {
  23. RTC_GlobalInit();
  24. return RT_EOK;
  25. }
  26. static rt_err_t air105_rtc_get_secs(void *args)
  27. {
  28. *(rt_uint32_t *) args = RTC_GetUTC();
  29. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  30. return RT_EOK;
  31. }
  32. static rt_err_t air105_rtc_set_secs(void *args)
  33. {
  34. rt_err_t result = RT_EOK;
  35. rt_uint32_t stamp = *(rt_uint32_t *)args;
  36. RTC_SetStamp(stamp);
  37. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  38. return result;
  39. }
  40. static rt_err_t air105_rtc_get_timeval(void *args)
  41. {
  42. struct timeval *tv = (struct timeval *) args;
  43. tv->tv_sec = RTC_GetUTC();
  44. return RT_EOK;
  45. }
  46. static const struct rt_rtc_ops air105_rtc_ops =
  47. {
  48. air105_rtc_init,
  49. air105_rtc_get_secs,
  50. air105_rtc_set_secs,
  51. RT_NULL,
  52. RT_NULL,
  53. air105_rtc_get_timeval,
  54. RT_NULL,
  55. };
  56. static rt_rtc_dev_t air105_rtc_dev;
  57. static int rt_hw_rtc_init(void)
  58. {
  59. rt_err_t result;
  60. air105_rtc_dev.ops = &air105_rtc_ops;
  61. result = rt_hw_rtc_register(&air105_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  62. if (result != RT_EOK)
  63. {
  64. LOG_E("rtc register err code: %d", result);
  65. return result;
  66. }
  67. LOG_D("rtc init success");
  68. return RT_EOK;
  69. }
  70. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  71. #endif /* BSP_USING_ONCHIP_RTC */