Blame view

RIOT/tests/driver_lis3dh/main.c 3.4 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
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
  /*
   * Copyright (C) 2015 Eistec AB
   *               2017 Freie Universität Berlin
   *
   * 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 application for the LIS3DH accelerometer driver
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "xtimer.h"
  #include "lis3dh.h"
  #include "lis3dh_params.h"
  
  
  #define SLEEP       (100 * 1000U)
  
  #define WATERMARK_LEVEL 16
  
  static volatile int int1_count = 0;
  
  static void test_int1(void *arg)
  {
      volatile int *int1_count_ptr = arg;
      ++(*int1_count_ptr);
  }
  
  int main(void)
  {
      lis3dh_t dev;
      lis3dh_data_t acc_data;
  
      puts("LIS3DH accelerometer driver test application\n");
  
      puts("Initializing LIS3DH sensor... ");
      if (lis3dh_init(&dev, &lis3dh_params[0]) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Set ODR... ");
      if (lis3dh_set_odr(&dev, lis3dh_params[0].odr) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Set scale... ");
      if (lis3dh_set_scale(&dev, lis3dh_params[0].scale) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Set axes XYZ... ");
      if (lis3dh_set_axes(&dev, LIS3DH_AXES_XYZ) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Enable streaming FIFO mode... ");
      if (lis3dh_set_fifo(&dev, LIS3DH_FIFO_MODE_STREAM, WATERMARK_LEVEL) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Enable temperature reading... ");
      if (lis3dh_set_aux_adc(&dev, 1, 1) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Set INT1 watermark function... ");
      if (lis3dh_set_int1(&dev, LIS3DH_CTRL_REG3_I1_WTM_MASK) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("Set INT1 callback");
      if (gpio_init_int(lis3dh_params[0].int1, GPIO_IN, GPIO_RISING,
                        test_int1, (void*)&int1_count) == 0) {
          puts("[OK]");
      }
      else {
          puts("[Failed]\n");
          return 1;
      }
  
      puts("LIS3DH init done.\n");
  
      while (1) {
          int fifo_level;
  
          fifo_level = lis3dh_get_fifo_level(&dev);
          printf("int1_count = %d\n", int1_count);
          printf("Reading %d measurements\n", fifo_level);
          while (fifo_level > 0) {
              int16_t temperature;
              int int1;
              if (lis3dh_read_xyz(&dev, &acc_data) != 0) {
                  puts("Reading acceleration data... ");
                  puts("[Failed]\n");
              }
              if (lis3dh_read_aux_adc3(&dev, &temperature) != 0) {
                  puts("Reading temperature data... ");
                  puts("[Failed]\n");
                  return 1;
              }
              int1 = gpio_read(lis3dh_params[0].int1);
              printf("X: %6d Y: %6d Z: %6d Temp: %6d, INT1: %08x\n",
                     acc_data.acc_x, acc_data.acc_y, acc_data.acc_z, temperature, int1);
              --fifo_level;
          }
  
          xtimer_usleep(SLEEP);
      }
  
      return 0;
  }