mbed_rtc_time.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "hal/rtc_api.h"
  17. #include "platform/mbed_critical.h"
  18. #include "platform/mbed_rtc_time.h"
  19. #include "platform/SingletonPtr.h"
  20. #include "platform/PlatformMutex.h"
  21. static SingletonPtr<PlatformMutex> _mutex;
  22. #if DEVICE_RTC
  23. static void (*_rtc_init)(void) = rtc_init;
  24. static int (*_rtc_isenabled)(void) = rtc_isenabled;
  25. static time_t (*_rtc_read)(void) = rtc_read;
  26. static void (*_rtc_write)(time_t t) = rtc_write;
  27. #elif DEVICE_LPTICKER
  28. #include "drivers/LowPowerTimer.h"
  29. static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer;
  30. static uint64_t _rtc_lp_base;
  31. static bool _rtc_enabled;
  32. static void _rtc_lpticker_init(void)
  33. {
  34. _rtc_lp_timer->start();
  35. _rtc_enabled = true;
  36. }
  37. static int _rtc_lpticker_isenabled(void)
  38. {
  39. return (_rtc_enabled == true);
  40. }
  41. static time_t _rtc_lpticker_read(void)
  42. {
  43. return (uint64_t)_rtc_lp_timer->read() + _rtc_lp_base;
  44. }
  45. static void _rtc_lpticker_write(time_t t)
  46. {
  47. _rtc_lp_base = t;
  48. }
  49. static void (*_rtc_init)(void) = _rtc_lpticker_init;
  50. static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled;
  51. static time_t (*_rtc_read)(void) = _rtc_lpticker_read;
  52. static void (*_rtc_write)(time_t t) = _rtc_lpticker_write;
  53. #else /* DEVICE_LPTICKER */
  54. static void (*_rtc_init)(void) = NULL;
  55. static int (*_rtc_isenabled)(void) = NULL;
  56. static time_t (*_rtc_read)(void) = NULL;
  57. static void (*_rtc_write)(time_t t) = NULL;
  58. #endif /* DEVICE_LPTICKER */
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. #if defined (__ICCARM__)
  63. time_t __time32(time_t *timer)
  64. #else
  65. time_t time(time_t *timer)
  66. #endif
  67. {
  68. _mutex->lock();
  69. if (_rtc_isenabled != NULL) {
  70. if (!(_rtc_isenabled())) {
  71. set_time(0);
  72. }
  73. }
  74. time_t t = (time_t) -1;
  75. if (_rtc_read != NULL) {
  76. t = _rtc_read();
  77. }
  78. if (timer != NULL) {
  79. *timer = t;
  80. }
  81. _mutex->unlock();
  82. return t;
  83. }
  84. void set_time(time_t t)
  85. {
  86. _mutex->lock();
  87. if (_rtc_init != NULL) {
  88. _rtc_init();
  89. }
  90. if (_rtc_write != NULL) {
  91. _rtc_write(t);
  92. }
  93. _mutex->unlock();
  94. }
  95. void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void))
  96. {
  97. _mutex->lock();
  98. _rtc_read = read_rtc;
  99. _rtc_write = write_rtc;
  100. _rtc_init = init_rtc;
  101. _rtc_isenabled = isenabled_rtc;
  102. _mutex->unlock();
  103. }
  104. #ifdef __cplusplus
  105. }
  106. #endif