Modified for BG96
Fork of mbed-dev by
platform/mbed_rtc_time.cpp@188:60408c49b6d4, 2018-09-20 (annotated)
- Committer:
- WaleedElmughrabi
- Date:
- Thu Sep 20 16:11:23 2018 +0000
- Revision:
- 188:60408c49b6d4
- Parent:
- 187:0387e8f68319
Fork modified for BG96 error
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "hal/rtc_api.h" |
<> | 149:156823d33999 | 17 | |
<> | 160:d5399cc887bb | 18 | #include "platform/mbed_critical.h" |
<> | 160:d5399cc887bb | 19 | #include "platform/mbed_rtc_time.h" |
<> | 149:156823d33999 | 20 | #include "platform/SingletonPtr.h" |
<> | 149:156823d33999 | 21 | #include "platform/PlatformMutex.h" |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | static SingletonPtr<PlatformMutex> _mutex; |
<> | 149:156823d33999 | 24 | |
<> | 149:156823d33999 | 25 | #if DEVICE_RTC |
Anna Bridge |
186:707f6e361f3e | 26 | |
<> | 149:156823d33999 | 27 | static void (*_rtc_init)(void) = rtc_init; |
<> | 149:156823d33999 | 28 | static int (*_rtc_isenabled)(void) = rtc_isenabled; |
<> | 149:156823d33999 | 29 | static time_t (*_rtc_read)(void) = rtc_read; |
<> | 149:156823d33999 | 30 | static void (*_rtc_write)(time_t t) = rtc_write; |
Anna Bridge |
186:707f6e361f3e | 31 | |
Anna Bridge |
186:707f6e361f3e | 32 | #elif DEVICE_LPTICKER |
Anna Bridge |
186:707f6e361f3e | 33 | |
Anna Bridge |
186:707f6e361f3e | 34 | #include "drivers/LowPowerTimer.h" |
Anna Bridge |
186:707f6e361f3e | 35 | |
Anna Bridge |
186:707f6e361f3e | 36 | static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer; |
Anna Bridge |
186:707f6e361f3e | 37 | static uint64_t _rtc_lp_base; |
Anna Bridge |
186:707f6e361f3e | 38 | static bool _rtc_enabled; |
Anna Bridge |
186:707f6e361f3e | 39 | |
Anna Bridge |
186:707f6e361f3e | 40 | static void _rtc_lpticker_init(void) |
Anna Bridge |
186:707f6e361f3e | 41 | { |
Anna Bridge |
186:707f6e361f3e | 42 | _rtc_lp_timer->start(); |
Anna Bridge |
186:707f6e361f3e | 43 | _rtc_enabled = true; |
Anna Bridge |
186:707f6e361f3e | 44 | } |
Anna Bridge |
186:707f6e361f3e | 45 | |
Anna Bridge |
186:707f6e361f3e | 46 | static int _rtc_lpticker_isenabled(void) |
Anna Bridge |
186:707f6e361f3e | 47 | { |
Anna Bridge |
186:707f6e361f3e | 48 | return (_rtc_enabled == true); |
Anna Bridge |
186:707f6e361f3e | 49 | } |
Anna Bridge |
186:707f6e361f3e | 50 | |
Anna Bridge |
186:707f6e361f3e | 51 | static time_t _rtc_lpticker_read(void) |
Anna Bridge |
186:707f6e361f3e | 52 | { |
Anna Bridge |
186:707f6e361f3e | 53 | return (uint64_t)_rtc_lp_timer->read() + _rtc_lp_base; |
Anna Bridge |
186:707f6e361f3e | 54 | } |
Anna Bridge |
186:707f6e361f3e | 55 | |
Anna Bridge |
186:707f6e361f3e | 56 | static void _rtc_lpticker_write(time_t t) |
Anna Bridge |
186:707f6e361f3e | 57 | { |
Anna Bridge |
186:707f6e361f3e | 58 | _rtc_lp_base = t; |
Anna Bridge |
186:707f6e361f3e | 59 | } |
Anna Bridge |
186:707f6e361f3e | 60 | |
Anna Bridge |
186:707f6e361f3e | 61 | static void (*_rtc_init)(void) = _rtc_lpticker_init; |
Anna Bridge |
186:707f6e361f3e | 62 | static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled; |
Anna Bridge |
186:707f6e361f3e | 63 | static time_t (*_rtc_read)(void) = _rtc_lpticker_read; |
Anna Bridge |
186:707f6e361f3e | 64 | static void (*_rtc_write)(time_t t) = _rtc_lpticker_write; |
Anna Bridge |
186:707f6e361f3e | 65 | |
Anna Bridge |
186:707f6e361f3e | 66 | #else /* DEVICE_LPTICKER */ |
Anna Bridge |
186:707f6e361f3e | 67 | |
<> | 149:156823d33999 | 68 | static void (*_rtc_init)(void) = NULL; |
<> | 149:156823d33999 | 69 | static int (*_rtc_isenabled)(void) = NULL; |
<> | 149:156823d33999 | 70 | static time_t (*_rtc_read)(void) = NULL; |
<> | 149:156823d33999 | 71 | static void (*_rtc_write)(time_t t) = NULL; |
Anna Bridge |
186:707f6e361f3e | 72 | #endif /* DEVICE_LPTICKER */ |
<> | 149:156823d33999 | 73 | |
<> | 149:156823d33999 | 74 | #ifdef __cplusplus |
<> | 149:156823d33999 | 75 | extern "C" { |
<> | 149:156823d33999 | 76 | #endif |
<> | 149:156823d33999 | 77 | #if defined (__ICCARM__) |
<> | 149:156823d33999 | 78 | time_t __time32(time_t *timer) |
<> | 149:156823d33999 | 79 | #else |
<> | 149:156823d33999 | 80 | time_t time(time_t *timer) |
<> | 149:156823d33999 | 81 | #endif |
<> | 149:156823d33999 | 82 | |
<> | 149:156823d33999 | 83 | { |
<> | 149:156823d33999 | 84 | _mutex->lock(); |
<> | 149:156823d33999 | 85 | if (_rtc_isenabled != NULL) { |
<> | 149:156823d33999 | 86 | if (!(_rtc_isenabled())) { |
<> | 149:156823d33999 | 87 | set_time(0); |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | } |
AnnaBridge | 187:0387e8f68319 | 90 | |
AnnaBridge | 187:0387e8f68319 | 91 | time_t t = (time_t) -1; |
<> | 149:156823d33999 | 92 | if (_rtc_read != NULL) { |
<> | 149:156823d33999 | 93 | t = _rtc_read(); |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | |
<> | 149:156823d33999 | 96 | if (timer != NULL) { |
<> | 149:156823d33999 | 97 | *timer = t; |
<> | 149:156823d33999 | 98 | } |
<> | 149:156823d33999 | 99 | _mutex->unlock(); |
<> | 149:156823d33999 | 100 | return t; |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | |
AnnaBridge | 187:0387e8f68319 | 103 | void set_time(time_t t) |
AnnaBridge | 187:0387e8f68319 | 104 | { |
<> | 149:156823d33999 | 105 | _mutex->lock(); |
<> | 149:156823d33999 | 106 | if (_rtc_init != NULL) { |
<> | 149:156823d33999 | 107 | _rtc_init(); |
<> | 149:156823d33999 | 108 | } |
<> | 149:156823d33999 | 109 | if (_rtc_write != NULL) { |
<> | 149:156823d33999 | 110 | _rtc_write(t); |
<> | 149:156823d33999 | 111 | } |
<> | 149:156823d33999 | 112 | _mutex->unlock(); |
<> | 149:156823d33999 | 113 | } |
<> | 149:156823d33999 | 114 | |
AnnaBridge | 187:0387e8f68319 | 115 | void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) |
AnnaBridge | 187:0387e8f68319 | 116 | { |
<> | 149:156823d33999 | 117 | _mutex->lock(); |
<> | 149:156823d33999 | 118 | _rtc_read = read_rtc; |
<> | 149:156823d33999 | 119 | _rtc_write = write_rtc; |
<> | 149:156823d33999 | 120 | _rtc_init = init_rtc; |
<> | 149:156823d33999 | 121 | _rtc_isenabled = isenabled_rtc; |
<> | 149:156823d33999 | 122 | _mutex->unlock(); |
<> | 149:156823d33999 | 123 | } |
<> | 149:156823d33999 | 124 | |
<> | 149:156823d33999 | 125 | |
<> | 149:156823d33999 | 126 | |
<> | 149:156823d33999 | 127 | #ifdef __cplusplus |
<> | 149:156823d33999 | 128 | } |
<> | 149:156823d33999 | 129 | #endif |