drv_rtc.c 3.8 KB

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