Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.478 KB)

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

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
#include <amiroos.h>
20
#include <aos_test_INA219.h>
21

    
22
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
23

    
24
#include <math.h>
25

    
26
/******************************************************************************/
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
aos_testresult_t aosTestIna219Func(BaseSequentialStream* stream, const aos_test_t* test)
51
{
52
  aosDbgCheck(test->data != NULL && ((aos_test_ina219data_t*)(test->data))->inad != NULL);
53

    
54
  // local variables
55
  aos_testresult_t result;
56
  int32_t status;
57
  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
  aosTestResultInit(&result);
69

    
70
  chprintf(stream, "read registers...\n");
71
  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
  if (status == APAL_STATUS_SUCCESS) {
73
    aosTestPassed(stream, &result);
74
  } else {
75
    aosTestFailed(stream, &result);
76
  }
77

    
78
  chprintf(stream, "write registers...\n");
79
  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
  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
      aosTestPassed(stream, &result);
90
    } else {
91
      aosTestFailed(stream, &result);
92
    }
93
  } else {
94
    aosTestFailed(stream, &result);
95
  }
96

    
97
  chprintf(stream, "reset...\n");
98
  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
  if (status == APAL_STATUS_SUCCESS && reset_data == 0x399F) {
101
    aosTestPassed(stream, &result);
102
  } 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
  status = ina219_lld_read_config(((aos_test_ina219data_t*)test->data)->inad, &ina_config, ((aos_test_ina219data_t*)test->data)->timeout);
110
  if (status == APAL_STATUS_SUCCESS) {
111
    aosTestPassed(stream, &result);
112
  } else {
113
    aosTestFailed(stream, &result);
114
  }
115

    
116
  chprintf(stream, "write config...\n");
117
  ina_config.data = 0x7FFu;
118
  status = ina219_lld_write_config(((aos_test_ina219data_t*)test->data)->inad, ina_config, ((aos_test_ina219data_t*)test->data)->timeout);
119
  if (status == APAL_STATUS_SUCCESS) {
120
    aosTestPassed(stream, &result);
121
  } else {
122
    aosTestFailed(stream, &result);
123
  }
124

    
125

    
126
  chprintf(stream, "calibrate...\n");
127
  ina219_lld_calib_input_t calib_in;
128
  calib_in.shunt_resistance_0 = 0.1f;
129
  calib_in.max_expected_current_A = 0.075f;
130
  calib_in.current_lsb_uA = 10;
131
  calib_in.cfg_reg = ina_config;
132
  ina219_lld_calib_output_t calib_out;
133
  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
  ina219_lld_cfg_t test_config;
137
  ((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
  while (!busready || power == 0) {
141
    aosThdMSleep(20);
142
    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
  }
145
  if (status == APAL_STATUS_SUCCESS) {
146
    aosTestPassed(stream, &result);
147
  } else {
148
    aosTestFailed(stream, &result);
149
  }
150

    
151
  chprintf(stream, "read shunt voltage...\n");
152
  status = ina219_lld_read_shunt_voltage(((aos_test_ina219data_t*)test->data)->inad, &shunt, ((aos_test_ina219data_t*)test->data)->timeout);
153
  chprintf(stream, "\t\tshunt voltage: %fV\n", (float)shunt/1000000.f);
154
  if (status == APAL_STATUS_SUCCESS) {
155
    aosTestPassed(stream, &result);
156
  } else {
157
    aosTestFailed(stream, &result);
158
  }
159

    
160
  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
  chprintf(stream, "\t\tbus voltage: %fV\n", (float)bus/1000000.f);
163
  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
  } else {
166
    aosTestFailed(stream, &result);
167
  }
168

    
169
  chprintf(stream, "read power...\n");
170
  status = ina219_lld_read_power(((aos_test_ina219data_t*)test->data)->inad, &power, ((aos_test_ina219data_t*)test->data)->timeout);
171
  chprintf(stream, "\t\tpower: %fW\n", (float)(power)/1000000.f);
172
  if (status == APAL_STATUS_SUCCESS) {
173
    aosTestPassed(stream, &result);
174
  } else {
175
    aosTestFailed(stream, &result);
176
  }
177

    
178
  chprintf(stream, "read current...\n");
179
  status = ina219_lld_read_current(((aos_test_ina219data_t*)test->data)->inad, &usensor_data, ((aos_test_ina219data_t*)test->data)->timeout);
180
  chprintf(stream, "\t\tcurrent: %fA\n", (float)(usensor_data)/1000000.f);
181
  if (status == APAL_STATUS_SUCCESS) {
182
    aosTestPassed(stream, &result);
183
  } else {
184
    aosTestFailed(stream, &result);
185
  }
186

    
187
  aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(INA219Driver));
188

    
189
  return result;
190
}
191

    
192
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
193