Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / INA219_v1 / aos_test_INA219.c @ 8d4d058e

History | View | Annotate | Download (8.456 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 84f0ce9e Thomas Schöpping
Copyright (C) 2016..2019  Thomas Schöpping et al.
4 e545e620 Thomas Schöpping

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19 ddf34c3d Thomas Schöpping
#include <amiroos.h>
20 4c72a54c Thomas Schöpping
#include <aos_test_INA219.h>
21 e545e620 Thomas Schöpping
22 4c72a54c Thomas Schöpping
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
23 e545e620 Thomas Schöpping
24
#include <math.h>
25
26 f3ac1c96 Thomas Schöpping
/******************************************************************************/
27
/* LOCAL DEFINITIONS                                                          */
28
/******************************************************************************/
29
30
/******************************************************************************/
31
/* EXPORTED VARIABLES                                                         */
32
/******************************************************************************/
33
34
/******************************************************************************/
35
/* LOCAL TYPES                                                                */
36
/******************************************************************************/
37
38
/******************************************************************************/
39
/* LOCAL VARIABLES                                                            */
40
/******************************************************************************/
41
42
/******************************************************************************/
43
/* LOCAL FUNCTIONS                                                            */
44
/******************************************************************************/
45
46
/******************************************************************************/
47
/* EXPORTED FUNCTIONS                                                         */
48
/******************************************************************************/
49
50 4c72a54c Thomas Schöpping
aos_testresult_t aosTestIna219Func(BaseSequentialStream* stream, const aos_test_t* test)
51 e545e620 Thomas Schöpping
{
52 4c72a54c Thomas Schöpping
  aosDbgCheck(test->data != NULL && ((aos_test_ina219data_t*)(test->data))->inad != NULL);
53 e545e620 Thomas Schöpping
54
  // local variables
55 4c72a54c Thomas Schöpping
  aos_testresult_t result = {0, 0};
56
  int32_t status;
57 e545e620 Thomas Schöpping
  uint16_t data[6];
58
  uint16_t write_data = 0x1011;
59
  uint16_t new_data[6];
60
  uint16_t reset_data;
61
  uint16_t test_calib = 0;
62
  int16_t usensor_data = 0;
63
  uint16_t busready = 0;
64
  uint32_t power = 0;
65
  int32_t shunt;
66
  uint32_t bus;
67
68
  chprintf(stream, "read registers...\n");
69 4c72a54c Thomas Schöpping
  status = ina219_lld_read_register(((aos_test_ina219data_t*)test->data)->inad, INA219_LLD_REGISTER_CONFIGURATION, data, 6, ((aos_test_ina219data_t*)test->data)->timeout);
70 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
71 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
72 e545e620 Thomas Schöpping
  } else {
73 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
74 e545e620 Thomas Schöpping
  }
75
76
  chprintf(stream, "write registers...\n");
77 4c72a54c Thomas Schöpping
  status = ina219_lld_write_register(((aos_test_ina219data_t*)test->data)->inad, INA219_LLD_REGISTER_CONFIGURATION, &write_data, 1, ((aos_test_ina219data_t*)test->data)->timeout);
78
  status |= ina219_lld_read_register(((aos_test_ina219data_t*)test->data)->inad, INA219_LLD_REGISTER_CONFIGURATION, new_data, 6, ((aos_test_ina219data_t*)test->data)->timeout);
79 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && new_data[0] == write_data) {
80
    uint8_t errors = 0;
81
    for (uint8_t dataIdx = 1; dataIdx < 6; dataIdx++) {
82
      if (new_data[dataIdx] != data[dataIdx]) {
83
        ++errors;
84
      }
85
    }
86
    if (errors == 0) {
87 4c72a54c Thomas Schöpping
      aosTestPassed(stream, &result);
88 e545e620 Thomas Schöpping
    } else {
89 4c72a54c Thomas Schöpping
      aosTestFailed(stream, &result);
90 e545e620 Thomas Schöpping
    }
91
  } else {
92 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
93 e545e620 Thomas Schöpping
  }
94
95
  chprintf(stream, "reset...\n");
96 4c72a54c Thomas Schöpping
  status = ina219_lld_reset(((aos_test_ina219data_t*)test->data)->inad, ((aos_test_ina219data_t*)test->data)->timeout);
97
  status |= ina219_lld_read_register(((aos_test_ina219data_t*)test->data)->inad, INA219_LLD_REGISTER_CONFIGURATION, &reset_data, 1, ((aos_test_ina219data_t*)test->data)->timeout);
98 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && reset_data == 0x399F) {
99 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
100 e545e620 Thomas Schöpping
  } else {
101
    chprintf(stream, "\tfailed\n");
102
    ++result.failed;
103
  }
104
105
  chprintf(stream, "read config...\n");
106
  ina219_lld_cfg_t ina_config;
107 4c72a54c Thomas Schöpping
  status = ina219_lld_read_config(((aos_test_ina219data_t*)test->data)->inad, &ina_config, ((aos_test_ina219data_t*)test->data)->timeout);
108 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
109 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
110 e545e620 Thomas Schöpping
  } else {
111 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
112 e545e620 Thomas Schöpping
  }
113
114
  chprintf(stream, "write config...\n");
115
  ina_config.data = 0x7FFu;
116 4c72a54c Thomas Schöpping
  status = ina219_lld_write_config(((aos_test_ina219data_t*)test->data)->inad, ina_config, ((aos_test_ina219data_t*)test->data)->timeout);
117 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
118 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
119 e545e620 Thomas Schöpping
  } else {
120 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
121 e545e620 Thomas Schöpping
  }
122
123
124
  chprintf(stream, "calibrate...\n");
125
  ina219_lld_calib_input_t calib_in;
126 4c72a54c Thomas Schöpping
  calib_in.shunt_resistance_0 = 0.1f;
127
  calib_in.max_expected_current_A = 0.075f;
128 e545e620 Thomas Schöpping
  calib_in.current_lsb_uA = 10;
129
  calib_in.cfg_reg = ina_config;
130
  ina219_lld_calib_output_t calib_out;
131 4c72a54c Thomas Schöpping
  status = ina219_lld_calibration(((aos_test_ina219data_t*)test->data)->inad, &calib_in, &calib_out);
132
  status |= ina219_lld_write_calibration(((aos_test_ina219data_t*)test->data)->inad, calib_out.calibration & 0xFFFEu, ((aos_test_ina219data_t*)test->data)->timeout);
133
  status |= ina219_lld_write_calibration(((aos_test_ina219data_t*)test->data)->inad, 0xA000, ((aos_test_ina219data_t*)test->data)->timeout);
134 e545e620 Thomas Schöpping
  ina219_lld_cfg_t test_config;
135 4c72a54c Thomas Schöpping
  ((aos_test_ina219data_t*)test->data)->inad->current_lsb_uA = 0xA;
136
  status |= ina219_lld_read_config(((aos_test_ina219data_t*)test->data)->inad, &test_config, ((aos_test_ina219data_t*)test->data)->timeout);
137
  status |= ina219_lld_read_calibration(((aos_test_ina219data_t*)test->data)->inad, &test_calib, ((aos_test_ina219data_t*)test->data)->timeout);
138 e545e620 Thomas Schöpping
  while (!busready || power == 0) {
139
    aosThdMSleep(20);
140 4c72a54c Thomas Schöpping
    status |= ina219_lld_bus_conversion_ready(((aos_test_ina219data_t*)test->data)->inad, &busready, ((aos_test_ina219data_t*)test->data)->timeout);
141
    status |= ina219_lld_read_power(((aos_test_ina219data_t*)test->data)->inad, &power, ((aos_test_ina219data_t*)test->data)->timeout);
142 e545e620 Thomas Schöpping
  }
143
  if (status == APAL_STATUS_SUCCESS) {
144 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
145 e545e620 Thomas Schöpping
  } else {
146 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
147 e545e620 Thomas Schöpping
  }
148
149
  chprintf(stream, "read shunt voltage...\n");
150 4c72a54c Thomas Schöpping
  status = ina219_lld_read_shunt_voltage(((aos_test_ina219data_t*)test->data)->inad, &shunt, ((aos_test_ina219data_t*)test->data)->timeout);
151 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tshunt voltage: %fV\n", (float)shunt/1000000.f);
152
  if (status == APAL_STATUS_SUCCESS) {
153 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
154 e545e620 Thomas Schöpping
  } else {
155 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
156 e545e620 Thomas Schöpping
  }
157
158 4c72a54c Thomas Schöpping
  chprintf(stream, "read bus voltage (%u%% tolerance)...\n", (uint8_t)(((aos_test_ina219data_t*)test->data)->tolerance * 100.f + 0.5f));
159
  status = ina219_lld_read_bus_voltage(((aos_test_ina219data_t*)test->data)->inad, &bus, ((aos_test_ina219data_t*)test->data)->timeout);
160 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tbus voltage: %fV\n", (float)bus/1000000.f);
161 4c72a54c Thomas Schöpping
  if ((status == APAL_STATUS_SUCCESS) && (fabs(((float)bus/1000000.f) - ((aos_test_ina219data_t*)test->data)->v_expected) < ((aos_test_ina219data_t*)test->data)->v_expected * ((aos_test_ina219data_t*)test->data)->tolerance)) {
162
    aosTestPassed(stream, &result);
163 e545e620 Thomas Schöpping
  } else {
164 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
165 e545e620 Thomas Schöpping
  }
166
167
  chprintf(stream, "read power...\n");
168 4c72a54c Thomas Schöpping
  status = ina219_lld_read_power(((aos_test_ina219data_t*)test->data)->inad, &power, ((aos_test_ina219data_t*)test->data)->timeout);
169 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tpower: %fW\n", (float)(power)/1000000.f);
170
  if (status == APAL_STATUS_SUCCESS) {
171 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
172 e545e620 Thomas Schöpping
  } else {
173 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
174 e545e620 Thomas Schöpping
  }
175
176
  chprintf(stream, "read current...\n");
177 4c72a54c Thomas Schöpping
  status = ina219_lld_read_current(((aos_test_ina219data_t*)test->data)->inad, &usensor_data, ((aos_test_ina219data_t*)test->data)->timeout);
178 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tcurrent: %fA\n", (float)(usensor_data)/1000000.f);
179
  if (status == APAL_STATUS_SUCCESS) {
180 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
181 e545e620 Thomas Schöpping
  } else {
182 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
183 e545e620 Thomas Schöpping
  }
184
185 4c72a54c Thomas Schöpping
  aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(INA219Driver));
186 e545e620 Thomas Schöpping
187
  return result;
188
}
189
190 4c72a54c Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */