drv_rtc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-09-08 Chenyingchun first version
  9. */
  10. #include "board.h"
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <sys/time.h>
  14. #include <nrfx_rtc.h>
  15. #include <nrfx_clock.h>
  16. #ifdef BSP_USING_ONCHIP_RTC
  17. #define LOG_TAG "drv.rtc"
  18. #define DBG_LVL DBG_LOG
  19. #include <rtdbg.h>
  20. /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
  21. #define RTC_TIME_INIT(year, month, day, hour, minute, second) \
  22. {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
  23. #ifndef ONCHIP_RTC_TIME_DEFAULT
  24. #define ONCHIP_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
  25. #endif
  26. #ifndef RTC_INSTANCE_ID
  27. #define RTC_INSTANCE_ID (2)
  28. #endif
  29. #define TICK_FREQUENCE_HZ (RT_TICK_PER_SECOND) // RTC tick frequence, in HZ
  30. static struct rt_device rtc;
  31. static time_t init_time;
  32. static uint32_t tick = 0;
  33. static void rtc_callback(nrfx_rtc_int_type_t int_type)
  34. {
  35. static uint32_t count = 0;
  36. if (int_type == NRFX_RTC_INT_TICK)
  37. {
  38. count++;
  39. if((count % TICK_FREQUENCE_HZ) == 0)
  40. {
  41. tick++;
  42. }
  43. }
  44. }
  45. static rt_err_t rt_rtc_config(struct rt_device *dev)
  46. {
  47. #define SYSTICK_CLOCK_HZ (32768UL)
  48. #define RTC_PRESCALER ((uint32_t) (NRFX_ROUNDED_DIV(SYSTICK_CLOCK_HZ, TICK_FREQUENCE_HZ) - 1))
  49. const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(RTC_INSTANCE_ID);
  50. nrf_clock_lf_src_set(NRF_CLOCK, (nrf_clock_lfclk_t)NRFX_CLOCK_CONFIG_LF_SRC);
  51. nrfx_clock_lfclk_start();
  52. //Initialize RTC instance
  53. nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
  54. config.prescaler = RTC_PRESCALER;
  55. nrfx_rtc_init(&rtc_instance, &config, rtc_callback);
  56. nrfx_rtc_tick_enable(&rtc_instance, true);
  57. //Power on RTC instance
  58. nrfx_rtc_enable(&rtc_instance);
  59. return RT_EOK;
  60. }
  61. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  62. {
  63. time_t *time;
  64. RT_ASSERT(dev != RT_NULL);
  65. switch (cmd)
  66. {
  67. case RT_DEVICE_CTRL_RTC_GET_TIME:
  68. {
  69. time = (time_t *) args;
  70. *time = init_time + tick;
  71. break;
  72. }
  73. case RT_DEVICE_CTRL_RTC_SET_TIME:
  74. {
  75. time = (time_t *) args;
  76. init_time = *time - tick;
  77. break;
  78. }
  79. }
  80. return RT_EOK;
  81. }
  82. #ifdef RT_USING_DEVICE_OPS
  83. const static struct rt_device_ops rtc_ops =
  84. {
  85. RT_NULL,
  86. RT_NULL,
  87. RT_NULL,
  88. RT_NULL,
  89. RT_NULL,
  90. rt_rtc_control
  91. };
  92. #endif
  93. static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
  94. {
  95. struct tm time_new = ONCHIP_RTC_TIME_DEFAULT;
  96. RT_ASSERT(device != RT_NULL);
  97. init_time = timegm(&time_new);
  98. if (rt_rtc_config(device) != RT_EOK)
  99. {
  100. return -RT_ERROR;
  101. }
  102. #ifdef RT_USING_DEVICE_OPS
  103. device->ops = &rtc_ops;
  104. #else
  105. device->init = RT_NULL;
  106. device->open = RT_NULL;
  107. device->close = RT_NULL;
  108. device->read = RT_NULL;
  109. device->write = RT_NULL;
  110. device->control = rt_rtc_control;
  111. #endif
  112. device->type = RT_Device_Class_RTC;
  113. device->rx_indicate = RT_NULL;
  114. device->tx_complete = RT_NULL;
  115. device->user_data = RT_NULL;
  116. /* register a character device */
  117. rt_device_register(device, name, flag);
  118. return RT_EOK;
  119. }
  120. int rt_hw_rtc_init(void)
  121. {
  122. rt_err_t result;
  123. result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  124. if (result != RT_EOK)
  125. {
  126. LOG_E("rtc register err code: %d", result);
  127. return result;
  128. }
  129. LOG_D("rtc init success");
  130. return RT_EOK;
  131. }
  132. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  133. #endif /* BSP_USING_ONCHIP_RTC */