Blame view

RIOT/tests/periph_rtc/main.c 2.31 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2015 Lari Lehtomäki
   *
   * 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 tests
   * @{
   *
   * @file
   * @brief       Test for low-level Real Time clock drivers
   *
   * This test will initialize the real-time timer and trigger an alarm printing
   * 'Hello' every 10 seconds
   *
   * @author      Lari Lehtomäki <lari@lehtomaki.fi>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <time.h>
  
  #include "periph_conf.h"
  #include "periph/rtc.h"
  #include "xtimer.h"
  
  #define PERIOD              (2U)
  #define REPEAT              (4U)
  
  #define TM_YEAR_OFFSET      (1900)
  
  static unsigned cnt = 0;
  
  static void print_time(const char *label, const struct tm *time)
  {
      printf("%s  %04d-%02d-%02d %02d:%02d:%02d\n", label,
              time->tm_year + TM_YEAR_OFFSET,
              time->tm_mon + 1,
              time->tm_mday,
              time->tm_hour,
              time->tm_min,
              time->tm_sec);
  }
  
  static void inc_secs(struct tm *time, unsigned val)
  {
      time->tm_sec += val;
      if (time->tm_sec >= 60) {
          time->tm_sec -= 60;
      }
  }
  
  static void cb(void *arg)
  {
      (void)arg;
  
      puts("Alarm!");
  
      if (++cnt < REPEAT) {
          struct tm time;
          rtc_get_alarm(&time);
          inc_secs(&time, PERIOD);
          rtc_set_alarm(&time, cb, NULL);
      }
  }
  
  int main(void)
  {
      struct tm time = {
          .tm_year = 2011 - TM_YEAR_OFFSET,   /* years are counted from 1900 */
          .tm_mon  = 11,                      /* 0 = January, 11 = December */
          .tm_mday = 13,
          .tm_hour = 14,
          .tm_min  = 15,
          .tm_sec  = 15
      };
  
      puts("\nRIOT RTC low-level driver test");
      printf("This test will display 'Alarm!' every %u seconds for %u times\n",
              PERIOD, REPEAT);
  
      /* initialize RTC */
      rtc_init();
  
      /* set RTC */
      print_time("  Setting clock to ", &time);
      rtc_set_time(&time);
  
      /* read RTC to confirm value */
      rtc_get_time(&time);
      print_time("Clock value is now ", &time);
  
      /* set initial alarm */
      inc_secs(&time, PERIOD);
      print_time("  Setting alarm to ", &time);
      rtc_set_alarm(&time, cb, NULL);
  
      /* verify alarm */
      rtc_get_alarm(&time);
      print_time("   Alarm is set to ", &time);
      puts("");
  
      return 0;
  }