Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.478 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  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 ce12e797 Thomas Schöpping
  aos_testresult_t result;
56 4c72a54c Thomas Schöpping
  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 ce12e797 Thomas Schöpping
  aosTestResultInit(&result);
69
70 e545e620 Thomas Schöpping
  chprintf(stream, "read registers...\n");
71 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);
72 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
73 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
74 e545e620 Thomas Schöpping
  } else {
75 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
76 e545e620 Thomas Schöpping
  }
77
78
  chprintf(stream, "write registers...\n");
79 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);
80
  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);
81 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && new_data[0] == write_data) {
82
    uint8_t errors = 0;
83
    for (uint8_t dataIdx = 1; dataIdx < 6; dataIdx++) {
84
      if (new_data[dataIdx] != data[dataIdx]) {
85
        ++errors;
86
      }
87
    }
88
    if (errors == 0) {
89 4c72a54c Thomas Schöpping
      aosTestPassed(stream, &result);
90 e545e620 Thomas Schöpping
    } else {
91 4c72a54c Thomas Schöpping
      aosTestFailed(stream, &result);
92 e545e620 Thomas Schöpping
    }
93
  } else {
94 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
95 e545e620 Thomas Schöpping
  }
96
97
  chprintf(stream, "reset...\n");
98 4c72a54c Thomas Schöpping
  status = ina219_lld_reset(((aos_test_ina219data_t*)test->data)->inad, ((aos_test_ina219data_t*)test->data)->timeout);
99
  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);
100 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && reset_data == 0x399F) {
101 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
102 e545e620 Thomas Schöpping
  } else {
103
    chprintf(stream, "\tfailed\n");
104
    ++result.failed;
105
  }
106
107
  chprintf(stream, "read config...\n");
108
  ina219_lld_cfg_t ina_config;
109 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);
110 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
111 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
112 e545e620 Thomas Schöpping
  } else {
113 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
114 e545e620 Thomas Schöpping
  }
115
116
  chprintf(stream, "write config...\n");
117
  ina_config.data = 0x7FFu;
118 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);
119 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
120 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
121 e545e620 Thomas Schöpping
  } else {
122 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
123 e545e620 Thomas Schöpping
  }
124
125
126
  chprintf(stream, "calibrate...\n");
127
  ina219_lld_calib_input_t calib_in;
128 4c72a54c Thomas Schöpping
  calib_in.shunt_resistance_0 = 0.1f;
129
  calib_in.max_expected_current_A = 0.075f;
130 e545e620 Thomas Schöpping
  calib_in.current_lsb_uA = 10;
131
  calib_in.cfg_reg = ina_config;
132
  ina219_lld_calib_output_t calib_out;
133 4c72a54c Thomas Schöpping
  status = ina219_lld_calibration(((aos_test_ina219data_t*)test->data)->inad, &calib_in, &calib_out);
134
  status |= ina219_lld_write_calibration(((aos_test_ina219data_t*)test->data)->inad, calib_out.calibration & 0xFFFEu, ((aos_test_ina219data_t*)test->data)->timeout);
135
  status |= ina219_lld_write_calibration(((aos_test_ina219data_t*)test->data)->inad, 0xA000, ((aos_test_ina219data_t*)test->data)->timeout);
136 e545e620 Thomas Schöpping
  ina219_lld_cfg_t test_config;
137 4c72a54c Thomas Schöpping
  ((aos_test_ina219data_t*)test->data)->inad->current_lsb_uA = 0xA;
138
  status |= ina219_lld_read_config(((aos_test_ina219data_t*)test->data)->inad, &test_config, ((aos_test_ina219data_t*)test->data)->timeout);
139
  status |= ina219_lld_read_calibration(((aos_test_ina219data_t*)test->data)->inad, &test_calib, ((aos_test_ina219data_t*)test->data)->timeout);
140 e545e620 Thomas Schöpping
  while (!busready || power == 0) {
141
    aosThdMSleep(20);
142 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);
143
    status |= ina219_lld_read_power(((aos_test_ina219data_t*)test->data)->inad, &power, ((aos_test_ina219data_t*)test->data)->timeout);
144 e545e620 Thomas Schöpping
  }
145
  if (status == APAL_STATUS_SUCCESS) {
146 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
147 e545e620 Thomas Schöpping
  } else {
148 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
149 e545e620 Thomas Schöpping
  }
150
151
  chprintf(stream, "read shunt voltage...\n");
152 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);
153 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tshunt voltage: %fV\n", (float)shunt/1000000.f);
154
  if (status == APAL_STATUS_SUCCESS) {
155 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
156 e545e620 Thomas Schöpping
  } else {
157 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
158 e545e620 Thomas Schöpping
  }
159
160 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));
161
  status = ina219_lld_read_bus_voltage(((aos_test_ina219data_t*)test->data)->inad, &bus, ((aos_test_ina219data_t*)test->data)->timeout);
162 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tbus voltage: %fV\n", (float)bus/1000000.f);
163 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)) {
164
    aosTestPassed(stream, &result);
165 e545e620 Thomas Schöpping
  } else {
166 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
167 e545e620 Thomas Schöpping
  }
168
169
  chprintf(stream, "read power...\n");
170 4c72a54c Thomas Schöpping
  status = ina219_lld_read_power(((aos_test_ina219data_t*)test->data)->inad, &power, ((aos_test_ina219data_t*)test->data)->timeout);
171 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tpower: %fW\n", (float)(power)/1000000.f);
172
  if (status == APAL_STATUS_SUCCESS) {
173 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
174 e545e620 Thomas Schöpping
  } else {
175 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
176 e545e620 Thomas Schöpping
  }
177
178
  chprintf(stream, "read current...\n");
179 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);
180 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tcurrent: %fA\n", (float)(usensor_data)/1000000.f);
181
  if (status == APAL_STATUS_SUCCESS) {
182 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
183 e545e620 Thomas Schöpping
  } else {
184 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
185 e545e620 Thomas Schöpping
  }
186
187 4c72a54c Thomas Schöpping
  aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(INA219Driver));
188 e545e620 Thomas Schöpping
189
  return result;
190
}
191
192 4c72a54c Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */