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 |
|
---|
18 | #include <time.h>
|
---|
19 | #include "platform/mbed_critical.h"
|
---|
20 | #include "platform/mbed_rtc_time.h"
|
---|
21 | #include "hal/us_ticker_api.h"
|
---|
22 | //#include "platform/SingletonPtr.h"
|
---|
23 | //#include "platform/PlatformMutex.h"
|
---|
24 | #include "platform/mbed_critical.h"
|
---|
25 |
|
---|
26 | //static SingletonPtr<PlatformMutex> _mutex;
|
---|
27 |
|
---|
28 | #if DEVICE_RTC
|
---|
29 | static void (*_rtc_init)(void) = rtc_init;
|
---|
30 | static int (*_rtc_isenabled)(void) = rtc_isenabled;
|
---|
31 | static time_t (*_rtc_read)(void) = rtc_read;
|
---|
32 | static void (*_rtc_write)(time_t t) = rtc_write;
|
---|
33 | #else
|
---|
34 | static void (*_rtc_init)(void) = NULL;
|
---|
35 | static int (*_rtc_isenabled)(void) = NULL;
|
---|
36 | static time_t (*_rtc_read)(void) = NULL;
|
---|
37 | static void (*_rtc_write)(time_t t) = NULL;
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #ifdef __cplusplus
|
---|
41 | extern "C" {
|
---|
42 | #endif
|
---|
43 | #if defined (__ICCARM__)
|
---|
44 | time_t __time32(time_t *timer)
|
---|
45 | #else
|
---|
46 | time_t get_time(time_t *timer)
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | {
|
---|
50 | //_mutex->lock();
|
---|
51 | if (_rtc_isenabled != NULL) {
|
---|
52 | if (!(_rtc_isenabled())) {
|
---|
53 | set_time(0);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | time_t t = 0;
|
---|
58 | if (_rtc_read != NULL) {
|
---|
59 | t = _rtc_read();
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (timer != NULL) {
|
---|
63 | *timer = t;
|
---|
64 | }
|
---|
65 | //_mutex->unlock();
|
---|
66 | return t;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void set_time(time_t t) {
|
---|
70 | //_mutex->lock();
|
---|
71 | if (_rtc_init != NULL) {
|
---|
72 | _rtc_init();
|
---|
73 | }
|
---|
74 | if (_rtc_write != NULL) {
|
---|
75 | _rtc_write(t);
|
---|
76 | }
|
---|
77 | //_mutex->unlock();
|
---|
78 | }
|
---|
79 |
|
---|
80 | clock_t get_clock() {
|
---|
81 | //_mutex->lock();
|
---|
82 | clock_t t = us_ticker_read();
|
---|
83 | t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
|
---|
84 | //_mutex->unlock();
|
---|
85 | return t;
|
---|
86 | }
|
---|
87 |
|
---|
88 | void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
|
---|
89 | //_mutex->lock();
|
---|
90 | core_util_critical_section_enter();
|
---|
91 | _rtc_read = read_rtc;
|
---|
92 | _rtc_write = write_rtc;
|
---|
93 | _rtc_init = init_rtc;
|
---|
94 | _rtc_isenabled = isenabled_rtc;
|
---|
95 | //_mutex->unlock();
|
---|
96 | core_util_critical_section_exit();
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 | #ifdef __cplusplus
|
---|
102 | }
|
---|
103 | #endif
|
---|