drv_rtc.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include "rtc.h"
  18. #ifdef BSP_USING_ONCHIP_RTC
  19. #define HW_RTC_TIME_INIT
  20. static struct rt_device rtc_dev;
  21. static rt_err_t rt_hw_rtc_control(rt_device_t dev, int cmd, void *args)
  22. {
  23. time_t *time;
  24. struct tm *time_new;
  25. struct tm time_s;
  26. RT_ASSERT(dev != RT_NULL);
  27. switch (cmd)
  28. {
  29. /* read time */
  30. case RT_DEVICE_CTRL_RTC_GET_TIME:
  31. time = (time_t *)args;
  32. rtc_timer_get(&time_s.tm_year,
  33. &time_s.tm_mon,
  34. &time_s.tm_mday,
  35. &time_s.tm_hour,
  36. &time_s.tm_min,
  37. &time_s.tm_sec);
  38. time_s.tm_year -= 1900;
  39. time_s.tm_mon -= 1;
  40. *time = mktime(&time_s);
  41. break;
  42. /* set time */
  43. case RT_DEVICE_CTRL_RTC_SET_TIME:
  44. time = (time_t *)args;
  45. time_new = gmtime(time);
  46. rtc_init();
  47. rtc_timer_set(time_new->tm_year + 1900,
  48. time_new->tm_mon + 1,
  49. time_new->tm_mday,
  50. time_new->tm_hour,
  51. time_new->tm_min,
  52. time_new->tm_sec);
  53. break;
  54. default:
  55. break;
  56. }
  57. return RT_EOK;
  58. }
  59. int rt_hw_rtc_init(void)
  60. {
  61. #if defined(HW_RTC_TIME_INIT)
  62. rtc_init();
  63. rtc_timer_set(2021, 3, 15, 17, 40, 00);
  64. #endif
  65. /* register rtc device */
  66. rtc_dev.type = RT_Device_Class_RTC;
  67. rtc_dev.init = RT_NULL;
  68. rtc_dev.open = RT_NULL;
  69. rtc_dev.close = RT_NULL;
  70. rtc_dev.read = RT_NULL;
  71. rtc_dev.write = RT_NULL;
  72. rtc_dev.control = rt_hw_rtc_control;
  73. rtc_dev.user_data = RT_NULL;
  74. rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  75. return RT_EOK;
  76. }
  77. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  78. #endif