Blame view

RIOT/cpu/kinetis_common/periph/rtc.c 1.78 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
  /*
   * Copyright (C) 2015 Eistec AB
   *
   * 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.
   */
  
  /**
   * @ingroup     cpu_kinetis_common_rtt
   *
   * @{
   *
   * @file
   * @brief       RTC interface wrapper for use with RTT modules.
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   *
   * @}
   */
  
  #include <stdint.h>
  #include <time.h>
  #include "cpu.h"
  #include "periph/rtc.h"
  #include "periph/rtt.h"
  #include "periph_conf.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  #if RTC_NUMOF
  
  
  typedef struct {
      rtc_alarm_cb_t cb;        /**< callback called from RTC interrupt */
  } rtc_state_t;
  
  static rtc_state_t rtc_callback;
  
  /**
   * @brief Wrapper function to call RTC callback from RTT interrupt
   *
   * @param[inout] arg    argument passed from the RTT interrupt
   */
  static void rtc_cb(void* arg);
  
  void rtc_init(void)
  {
      rtt_init();
  }
  
  int rtc_set_time(struct tm *time)
  {
      time_t t = mktime(time);
  
      rtt_set_counter((uint32_t)t);
  
      return 0;
  }
  
  int rtc_get_time(struct tm *time)
  {
      time_t t = (time_t)rtt_get_counter();
  
      gmtime_r(&t, time);
  
      return 0;
  }
  
  int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
  {
      time_t t = mktime(time);
  
      rtc_callback.cb = cb;
  
      rtt_set_alarm((uint32_t)t, rtc_cb, arg);
  
      return 0;
  }
  
  int rtc_get_alarm(struct tm *time)
  {
      time_t t = (time_t)rtt_get_alarm();
  
      gmtime_r(&t, time);
  
      return 0;
  }
  
  void rtc_clear_alarm(void)
  {
      rtt_clear_alarm();
      rtc_callback.cb = NULL;
  }
  
  void rtc_poweron(void)
  {
      rtt_poweron();
  }
  
  void rtc_poweroff(void)
  {
      rtt_poweroff();
  }
  
  static void rtc_cb(void* arg)
  {
      if (rtc_callback.cb != NULL) {
          rtc_callback.cb(arg);
      }
  }
  
  #endif /* RTC_NUMOF */