drv_rtc.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-03-13 wcx1024979076 the first version
  9. */
  10. #include <rtdevice.h>
  11. #include "board.h"
  12. #include "drv_rtc.h"
  13. #ifdef BSP_USING_RTC
  14. #define DBG_TAG "DRV.RTC"
  15. #define DBG_LVL DBG_WARNING
  16. #include <rtdbg.h>
  17. static struct rt_device rtc;
  18. static rt_uint32_t rtc_time;
  19. static rt_err_t _rtc_open(rt_device_t dev, rt_uint16_t oflag)
  20. {
  21. if (dev->rx_indicate != RT_NULL)
  22. {
  23. /* Open Interrupt */
  24. }
  25. return RT_EOK;
  26. }
  27. static rt_ssize_t _rtc_read(
  28. rt_device_t dev,
  29. rt_off_t pos,
  30. void* buffer,
  31. rt_size_t size)
  32. {
  33. return 0;
  34. }
  35. static rt_err_t _rtc_control(rt_device_t dev, int cmd, void *args)
  36. {
  37. RT_ASSERT(dev != RT_NULL);
  38. struct bflb_device_s* bflb_rtc = bflb_device_get_by_name("rtc");
  39. switch (cmd)
  40. {
  41. case RT_DEVICE_CTRL_RTC_GET_TIME:
  42. *(time_t *)args = rtc_time + BFLB_RTC_TIME2SEC(bflb_rtc_get_time(bflb_rtc));
  43. break;
  44. case RT_DEVICE_CTRL_RTC_SET_TIME:
  45. rtc_time = *(time_t *)args;
  46. bflb_rtc_set_time(bflb_rtc, 0);
  47. break;
  48. }
  49. return RT_EOK;
  50. }
  51. int rt_hw_rtc_init(void)
  52. {
  53. int result = RT_EOK;
  54. struct bflb_device_s* bflb_rtc = bflb_device_get_by_name("rtc");
  55. bflb_rtc_set_time(bflb_rtc, 0);
  56. /* register rtc device */
  57. rtc.type = RT_Device_Class_RTC;
  58. rtc.rx_indicate = RT_NULL;
  59. rtc.tx_complete = RT_NULL;
  60. rtc.init = RT_NULL;
  61. rtc.open = _rtc_open;
  62. rtc.close = RT_NULL;
  63. rtc.read = _rtc_read;
  64. rtc.write = RT_NULL;
  65. rtc.control = _rtc_control;
  66. rtc.user_data = RT_NULL; /* no private */
  67. result = rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  68. if(result != RT_EOK)
  69. {
  70. LOG_E("rtc device register fail.");
  71. }
  72. return result;
  73. }
  74. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  75. #endif /* BSP_USING_RTC */