Blame view

RIOT/cpu/native/periph/rtc.c 3.71 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  /*
   * Copyright (C) 2013, 2014 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * Native CPU periph/rtc.h implementation
   *
   * The implementation uses POSIX system calls to emulate a real-time
   * clock based on the system clock.
   *
   * @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
   *
   * @ingroup _native_cpu
   * @defgroup _native_rtc
   * @file
   */
  
  #include <time.h>
  #include <stdlib.h>
  #include <string.h>
  #include <err.h>
  
  #include "periph/rtc.h"
  #include "cpu.h"
  
  #include "native_internal.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  static int _native_rtc_initialized = 0;
  static int _native_rtc_powered = 0;
  static struct tm _native_rtc_alarm;
  static rtc_alarm_cb_t _native_rtc_alarm_callback;
  static void *_native_rtc_alarm_argument;
  
  void rtc_init(void)
  {
      DEBUG("rtc_init\n");
  
      memset(&_native_rtc_alarm, 0, sizeof(_native_rtc_alarm));
      _native_rtc_alarm_callback = NULL;
      _native_rtc_alarm_argument = NULL;
  
      _native_rtc_initialized = 1;
      printf("Native RTC initialized.\n");
  
      rtc_poweron();
  }
  
  void rtc_poweron(void)
  {
      DEBUG("rtc_poweron\n");
  
      if (!_native_rtc_initialized) {
          warnx("rtc_poweron: not initialized");
          return;
      }
  
      _native_rtc_powered = 1;
  }
  
  void rtc_poweroff(void)
  {
      DEBUG("rtc_poweroff()\n");
  
      if (!_native_rtc_initialized) {
          warnx("rtc_poweroff: not initialized");
      }
      if (!_native_rtc_powered) {
          warnx("rtc_poweroff: not powered on");
      }
  
      _native_rtc_powered = 0;
  }
  
  /* TODO: implement time setting using a delta */
  int rtc_set_time(struct tm *ttime)
  {
      (void) ttime;
  
      DEBUG("rtc_set_time()\n");
  
      if (!_native_rtc_initialized) {
          warnx("rtc_set_time: not initialized");
          return -1;
      }
      if (!_native_rtc_powered) {
          warnx("rtc_set_time: not powered on");
          return -1;
      }
  
      warnx("rtc_set_time: not implemented");
  
      return -1;
  }
  
  int rtc_get_time(struct tm *ttime)
  {
      time_t t;
  
      if (!_native_rtc_initialized) {
          warnx("rtc_get_time: not initialized");
          return -1;
      }
      if (!_native_rtc_powered) {
          warnx("rtc_get_time: not powered on");
          return -1;
      }
  
      _native_syscall_enter();
      t = time(NULL);
  
      if (localtime_r(&t, ttime) == NULL) {
          err(EXIT_FAILURE, "rtc_get_time: localtime_r");
      }
      _native_syscall_leave();
  
      return 0;
  }
  
  /* TODO: implement alarm scheduling */
  int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
  {
      (void) time;
      (void) cb;
      (void) arg;
  
      if (!_native_rtc_initialized) {
          warnx("rtc_set_alarm: not initialized");
          return -1;
      }
      if (!_native_rtc_powered) {
          warnx("rtc_set_alarm: not powered on");
          return -1;
      }
  
      memcpy(&_native_rtc_alarm, time, sizeof(_native_rtc_alarm));
  
      warnx("rtc_set_alarm: not implemented");
  
      return -1;
  }
  
  int rtc_get_alarm(struct tm *time)
  {
      (void) time;
  
      if (!_native_rtc_initialized) {
          warnx("rtc_get_alarm: not initialized");
          return -1;
      }
      if (!_native_rtc_powered) {
          warnx("rtc_get_alarm: not powered on");
          return -1;
      }
  
      memcpy(time, &_native_rtc_alarm, sizeof(_native_rtc_alarm));
  
      return 0;
  }
  
  /* TODO: implement alarm unscheduling once rtc_set_alarm is
   * implemented */
  void rtc_clear_alarm(void)
  {
      DEBUG("rtc_clear_alarm()\n");
  
      if (!_native_rtc_initialized) {
          warnx("rtc_clear_alarm: not initialized");
      }
      if (!_native_rtc_powered) {
          warnx("rtc_clear_alarm: not powered on");
      }
  
      memset(&_native_rtc_alarm, 0, sizeof(_native_rtc_alarm));
  }