Blame view

RIOT/tests/driver_ina220/main.c 2.68 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
  /*
   * 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 tests
   * @{
   *
   * @file
   * @brief       Test application for the INA220 sensor driver
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   *
   * @}
   */
  
  #ifndef TEST_INA220_I2C
  #error "TEST_INA220_I2C not defined"
  #endif
  #ifndef TEST_INA220_ADDR
  #error "TEST_INA220_ADDR not defined"
  #endif
  
  #include <stdio.h>
  
  #include "xtimer.h"
  #include "ina220.h"
  
  /* Use the following configuration:
   *
   *  - Continuous measurements, both shunt and bus voltage
   *  - +/- 320 mV Vshunt range
   *  - 32 V maximum bus voltage
   *  - 12 bit ADC resolution, no hardware averaging
   */
  #define CONFIG   (INA220_MODE_CONTINUOUS_SHUNT_BUS | INA220_RANGE_320MV | \
                    INA220_BRNG_32V_FSR | INA220_SADC_12BIT | INA220_BADC_12BIT)
  #define CALIBRATION (4096)
  #define SLEEP    (100 * 1000U)
  
  int main(void)
  {
      ina220_t dev;
      int16_t val;
  
      puts("INA220 sensor driver test application\n");
      printf("Initializing I2C_%i... ", TEST_INA220_I2C);
      if (i2c_init_master(TEST_INA220_I2C, I2C_SPEED_FAST) < 0) {
          return -1;
      }
  
      printf("Initializing INA220 sensor at I2C_%i, address 0x%02x... ",
          TEST_INA220_I2C, TEST_INA220_ADDR);
      if (ina220_init(&dev, TEST_INA220_I2C, TEST_INA220_ADDR) == 0) {
          puts("[OK]\n");
      } else {
          puts("[Failed]");
          return 1;
      }
      puts("Set configuration register");
      if (ina220_set_config(&dev, CONFIG) == 0) {
          puts("[OK]\n");
      } else {
          puts("[Failed]");
          return 1;
      }
  
      puts("Set calibration register");
      if (ina220_set_calibration(&dev, CALIBRATION) == 0) {
          puts("[OK]\n");
      } else {
          puts("[Failed]");
          return 1;
      }
  
      while (1) {
          /* Read shunt resistor voltage, in millivolts */
          ina220_read_shunt(&dev, &val);
          printf("shunt: %6d", val);
  
          /* Read VBUS voltage, in millivolts */
          ina220_read_bus(&dev, &val);
          /* The bus voltage is found in the topmost 13 bits of the bus voltage
           * register */
          val = (val >> INA220_BUS_VOLTAGE_SHIFT);
          printf("\tbus: %6d", val);
  
          /* Read current register, the scale depends on the value of the
           * calibration register */
          ina220_read_current(&dev, &val);
          printf("\tcurrent: %6d", val);
  
          /* Read power register, the scale depends on the value of the
           * calibration register */
          ina220_read_power(&dev, &val);
          printf("\tpower: %6d\n", val);
  
          xtimer_usleep(SLEEP);
      }
  
      return 0;
  }