drv_rtc.c 3.7 KB

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