drv_rtc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-03-15 picospuch Porting for maxiam32660
  9. */
  10. #include "drivers/dev_rtc.h"
  11. #include "board.h"
  12. #include <sys/time.h>
  13. #ifdef BSP_USING_ONCHIP_RTC
  14. #define DBG_LEVEL DBG_INFO
  15. #include <rtdbg.h>
  16. #define LOG_TAG "drv.rtc"
  17. static struct rt_device rtc;
  18. static sys_cfg_rtc_t sys_cfg;
  19. static time_t get_rtc_timestamp(void)
  20. {
  21. LOG_D("get rtc time.");
  22. return RTC_GetSecond();
  23. }
  24. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  25. {
  26. LOG_D("set rtc time.");
  27. if (RTC_Init(MXC_RTC, time_stamp, 0, &sys_cfg) != E_SUCCESS) {
  28. return -RT_ERROR;
  29. }
  30. if (RTC_EnableRTCE(MXC_RTC) != E_SUCCESS) {
  31. return -RT_ERROR;
  32. }
  33. return RT_EOK;
  34. }
  35. static void rt_rtc_init(void)
  36. {
  37. sys_cfg.tmr = MXC_TMR0;
  38. RTC_Init(MXC_RTC, 0, 0, &sys_cfg);
  39. }
  40. static rt_err_t rt_rtc_config(struct rt_device *dev)
  41. {
  42. if (RTC_EnableRTCE(MXC_RTC) != E_SUCCESS) {
  43. return -RT_ERROR;
  44. }
  45. return RT_EOK;
  46. }
  47. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  48. {
  49. rt_err_t result = RT_EOK;
  50. RT_ASSERT(dev != RT_NULL);
  51. switch (cmd)
  52. {
  53. case RT_DEVICE_CTRL_RTC_GET_TIME:
  54. *(rt_uint32_t *)args = get_rtc_timestamp();
  55. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  56. break;
  57. case RT_DEVICE_CTRL_RTC_SET_TIME:
  58. if (set_rtc_time_stamp(*(rt_uint32_t *)args))
  59. {
  60. result = -RT_ERROR;
  61. }
  62. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  63. break;
  64. }
  65. return result;
  66. }
  67. #ifdef RT_USING_DEVICE_OPS
  68. const static struct rt_device_ops rtc_ops =
  69. {
  70. RT_NULL,
  71. RT_NULL,
  72. RT_NULL,
  73. RT_NULL,
  74. RT_NULL,
  75. rt_rtc_control
  76. };
  77. #endif
  78. static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
  79. {
  80. RT_ASSERT(device != RT_NULL);
  81. rt_rtc_init();
  82. if (rt_rtc_config(device) != RT_EOK)
  83. {
  84. return -RT_ERROR;
  85. }
  86. #ifdef RT_USING_DEVICE_OPS
  87. device->ops = &rtc_ops;
  88. #else
  89. device->init = RT_NULL;
  90. device->open = RT_NULL;
  91. device->close = RT_NULL;
  92. device->read = RT_NULL;
  93. device->write = RT_NULL;
  94. device->control = rt_rtc_control;
  95. #endif
  96. device->type = RT_Device_Class_RTC;
  97. device->rx_indicate = RT_NULL;
  98. device->tx_complete = RT_NULL;
  99. device->user_data = RT_NULL;
  100. /* register a character device */
  101. return rt_device_register(device, name, flag);
  102. }
  103. int rt_hw_rtc_init(void)
  104. {
  105. rt_err_t result;
  106. result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  107. if (result != RT_EOK)
  108. {
  109. LOG_E("rtc register err code: %d", result);
  110. return result;
  111. }
  112. LOG_D("rtc init success");
  113. return RT_EOK;
  114. }
  115. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  116. #endif /* BSP_USING_ONCHIP_RTC */