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