rtc_core.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_ALARM:
  55. ret = TRY_DO_RTC_FUNC(rtc_core, get_alarm, args);
  56. break;
  57. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  58. ret = TRY_DO_RTC_FUNC(rtc_core, set_alarm, args);
  59. break;
  60. default:
  61. break;
  62. }
  63. return ret;
  64. }
  65. #ifdef RT_USING_DEVICE_OPS
  66. const static struct rt_device_ops rtc_core_ops =
  67. {
  68. rt_rtc_core_init,
  69. rt_rtc_core_open,
  70. rt_rtc_core_close,
  71. RT_NULL,
  72. RT_NULL,
  73. rt_rtc_core_control,
  74. };
  75. #endif
  76. rt_err_t rt_rtc_dev_register(rt_rtc_dev_t *rtc,
  77. const char *name,
  78. rt_uint32_t flag,
  79. void *data)
  80. {
  81. struct rt_device *device;
  82. RT_ASSERT(rtc != RT_NULL);
  83. device = &(rtc->parent);
  84. device->type = RT_Device_Class_RTC;
  85. device->rx_indicate = RT_NULL;
  86. device->tx_complete = RT_NULL;
  87. #ifdef RT_USING_DEVICE_OPS
  88. device->ops = &rtc_core_ops;
  89. #else
  90. device->init = rt_rtc_core_init;
  91. device->open = rt_rtc_core_open;
  92. device->close = rt_rtc_core_close;
  93. device->read = RT_NULL;
  94. device->write = RT_NULL;
  95. device->control = rt_rtc_core_control;
  96. #endif
  97. device->user_data = data;
  98. /* register a character device */
  99. return rt_device_register(device, name, flag);
  100. }