rtc_core.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * COPYRIGHT (C) 2011-2021, Real-Thread Information Technology Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-11 iysheng first version.
  9. */
  10. #include <drivers/rtc_core.h>
  11. #define TRY_DO_RTC_FUNC(rt_rtc_dev, func_name, args) \
  12. rt_rtc_dev->ops->func_name ? rt_rtc_dev->ops->func_name(args) : -RT_EINVAL;
  13. /*
  14. * This function initializes rtc_core
  15. */
  16. static rt_err_t rt_rtc_core_init(struct rt_device *dev)
  17. {
  18. rt_rtc_dev_t *rtc_core;
  19. RT_ASSERT(dev != RT_NULL);
  20. rtc_core = (rt_rtc_dev_t *)dev;
  21. if (rtc_core->ops->init)
  22. {
  23. return (rtc_core->ops->init());
  24. }
  25. return (-RT_ENOSYS);
  26. }
  27. static rt_err_t rt_rtc_core_open(struct rt_device *dev, rt_uint16_t oflag)
  28. {
  29. return (RT_EOK);
  30. }
  31. static rt_err_t rt_rtc_core_close(struct rt_device *dev)
  32. {
  33. /* Add close member function in rt_rtc_ops when need,
  34. * then call that function here.
  35. * */
  36. return (RT_EOK);
  37. }
  38. static rt_err_t rt_rtc_core_control(struct rt_device *dev,
  39. int cmd,
  40. void *args)
  41. {
  42. rt_rtc_dev_t *rtc_core;
  43. rt_err_t ret = -RT_EINVAL;
  44. RT_ASSERT(dev != RT_NULL);
  45. rtc_core = (rt_rtc_dev_t *)dev;
  46. switch (cmd)
  47. {
  48. case RT_DEVICE_CTRL_RTC_GET_TIME:
  49. ret = TRY_DO_RTC_FUNC(rtc_core, get_secs, args);
  50. break;
  51. case RT_DEVICE_CTRL_RTC_SET_TIME:
  52. ret = TRY_DO_RTC_FUNC(rtc_core, set_secs, args);
  53. break;
  54. case RT_DEVICE_CTRL_RTC_GET_TIME_US:
  55. ret = TRY_DO_RTC_FUNC(rtc_core, get_usecs, args);
  56. break;
  57. case RT_DEVICE_CTRL_RTC_SET_TIME_US:
  58. ret = TRY_DO_RTC_FUNC(rtc_core, set_usecs, args);
  59. break;
  60. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  61. ret = TRY_DO_RTC_FUNC(rtc_core, get_alarm, args);
  62. break;
  63. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  64. ret = TRY_DO_RTC_FUNC(rtc_core, set_alarm, args);
  65. break;
  66. default:
  67. break;
  68. }
  69. return ret;
  70. }
  71. #ifdef RT_USING_DEVICE_OPS
  72. const static struct rt_device_ops rtc_core_ops =
  73. {
  74. rt_rtc_core_init,
  75. rt_rtc_core_open,
  76. rt_rtc_core_close,
  77. RT_NULL,
  78. RT_NULL,
  79. rt_rtc_core_control,
  80. };
  81. #endif
  82. rt_err_t rt_rtc_dev_register(rt_rtc_dev_t *rtc,
  83. const char *name,
  84. rt_uint32_t flag,
  85. void *data)
  86. {
  87. struct rt_device *device;
  88. RT_ASSERT(rtc != RT_NULL);
  89. device = &(rtc->parent);
  90. device->type = RT_Device_Class_RTC;
  91. device->rx_indicate = RT_NULL;
  92. device->tx_complete = RT_NULL;
  93. #ifdef RT_USING_DEVICE_OPS
  94. device->ops = &rtc_core_ops;
  95. #else
  96. device->init = rt_rtc_core_init;
  97. device->open = rt_rtc_core_open;
  98. device->close = rt_rtc_core_close;
  99. device->read = RT_NULL;
  100. device->write = RT_NULL;
  101. device->control = rt_rtc_core_control;
  102. #endif
  103. device->user_data = data;
  104. /* register a character device */
  105. return rt_device_register(device, name, flag);
  106. }