amiro-os / test / periphery-lld / L3G4200D_v1 / aos_test_L3G4200D.c @ 2347b2ff
History | View | Annotate | Download (11.35 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_L3G4200D.h> |
| 21 | e545e620 | Thomas Schöpping | |
| 22 | 4c72a54c | Thomas Schöpping | #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
| 23 | ddf34c3d | Thomas Schöpping | |
| 24 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
| 25 | /* LOCAL DEFINITIONS */
|
||
| 26 | /******************************************************************************/
|
||
| 27 | |||
| 28 | /******************************************************************************/
|
||
| 29 | /* EXPORTED VARIABLES */
|
||
| 30 | /******************************************************************************/
|
||
| 31 | |||
| 32 | /******************************************************************************/
|
||
| 33 | /* LOCAL TYPES */
|
||
| 34 | /******************************************************************************/
|
||
| 35 | |||
| 36 | /******************************************************************************/
|
||
| 37 | /* LOCAL VARIABLES */
|
||
| 38 | /******************************************************************************/
|
||
| 39 | |||
| 40 | /******************************************************************************/
|
||
| 41 | /* LOCAL FUNCTIONS */
|
||
| 42 | /******************************************************************************/
|
||
| 43 | |||
| 44 | /******************************************************************************/
|
||
| 45 | /* EXPORTED FUNCTIONS */
|
||
| 46 | /******************************************************************************/
|
||
| 47 | e545e620 | Thomas Schöpping | |
| 48 | /**
|
||
| 49 | 4c72a54c | Thomas Schöpping | * @brief L3G4200D test function.
|
| 50 | e545e620 | Thomas Schöpping | *
|
| 51 | * @param[in] stream Stream for input/output.
|
||
| 52 | 4c72a54c | Thomas Schöpping | * @param[in] test Test object.
|
| 53 | e545e620 | Thomas Schöpping | *
|
| 54 | 4c72a54c | Thomas Schöpping | * @return Test result value.
|
| 55 | e545e620 | Thomas Schöpping | */
|
| 56 | 4c72a54c | Thomas Schöpping | aos_testresult_t aosTestL3g4200dFunc(BaseSequentialStream* stream, const aos_test_t* test)
|
| 57 | e545e620 | Thomas Schöpping | {
|
| 58 | 4c72a54c | Thomas Schöpping | aosDbgCheck(test->data != NULL && ((aos_test_l3g4200ddata_t*)(test->data)) != NULL); |
| 59 | e545e620 | Thomas Schöpping | |
| 60 | // local variables
|
||
| 61 | ce12e797 | Thomas Schöpping | aos_testresult_t result; |
| 62 | b23ca7cc | Thomas Schöpping | int32_t status; |
| 63 | e545e620 | Thomas Schöpping | uint8_t data = 0;
|
| 64 | uint8_t write_data[5];
|
||
| 65 | uint8_t read_data[5];
|
||
| 66 | int16_t sdata[3];
|
||
| 67 | uint8_t status_reg; |
||
| 68 | eventmask_t event_mask; |
||
| 69 | bool success = false; |
||
| 70 | uint8_t fifo = 0x5F;
|
||
| 71 | event_listener_t el; |
||
| 72 | |||
| 73 | ce12e797 | Thomas Schöpping | aosTestResultInit(&result); |
| 74 | e545e620 | Thomas Schöpping | for (uint8_t dataIdx = 0; dataIdx < 4; dataIdx++) { |
| 75 | write_data[dataIdx] = (dataIdx+1)*11; |
||
| 76 | } |
||
| 77 | write_data[4] = 0; |
||
| 78 | |||
| 79 | chprintf(stream, "check identity...\n");
|
||
| 80 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_read_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_WHO_AM_I, &data, 1);
|
| 81 | e545e620 | Thomas Schöpping | if(status == APAL_STATUS_SUCCESS && data == L3G4200D_LLD_WHO_AM_I){
|
| 82 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 83 | e545e620 | Thomas Schöpping | } else {
|
| 84 | 4c72a54c | Thomas Schöpping | aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
|
| 85 | e545e620 | Thomas Schöpping | } |
| 86 | |||
| 87 | chprintf(stream, "write register...\n");
|
||
| 88 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_write_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 1);
|
| 89 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
| 90 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 91 | e545e620 | Thomas Schöpping | } else {
|
| 92 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 93 | e545e620 | Thomas Schöpping | } |
| 94 | |||
| 95 | chprintf(stream, "read register...\n");
|
||
| 96 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_read_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, &data, 1);
|
| 97 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS && data == write_data[0]) { |
| 98 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 99 | e545e620 | Thomas Schöpping | } else {
|
| 100 | 4c72a54c | Thomas Schöpping | aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
|
| 101 | e545e620 | Thomas Schöpping | } |
| 102 | |||
| 103 | chprintf(stream, "write multiple registers...\n");
|
||
| 104 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_write_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 5);
|
| 105 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
| 106 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 107 | e545e620 | Thomas Schöpping | } else {
|
| 108 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 109 | e545e620 | Thomas Schöpping | } |
| 110 | |||
| 111 | chprintf(stream, "read multiple registers...\n");
|
||
| 112 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_read_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, read_data, 5);
|
| 113 | e545e620 | Thomas Schöpping | uint8_t errors = 0;
|
| 114 | for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) { |
||
| 115 | if (read_data[dataIdx] != write_data[dataIdx]) {
|
||
| 116 | ++errors; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | if (status == APAL_STATUS_SUCCESS && errors == 0) { |
||
| 120 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 121 | e545e620 | Thomas Schöpping | } else {
|
| 122 | for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) { |
||
| 123 | chprintf(stream, "\t\tStatus: %d, CTRL_REG%d: %d, write_data: %d\n", status, dataIdx+1, read_data[dataIdx], write_data[dataIdx]); |
||
| 124 | } |
||
| 125 | 4c72a54c | Thomas Schöpping | aosTestFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
|
| 126 | e545e620 | Thomas Schöpping | } |
| 127 | |||
| 128 | chprintf(stream, "read config...\n");
|
||
| 129 | l3g4200d_lld_cfg_t cfg; |
||
| 130 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_read_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &cfg); |
| 131 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
| 132 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 133 | e545e620 | Thomas Schöpping | } else {
|
| 134 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 135 | e545e620 | Thomas Schöpping | } |
| 136 | |||
| 137 | chprintf(stream, "write config...\n");
|
||
| 138 | cfg.registers.ctrl_reg1 = L3G4200D_LLD_PD | L3G4200D_LLD_DR_100_HZ | L3G4200D_LLD_BW_12_5 | L3G4200D_LLD_ZEN | L3G4200D_LLD_YEN | L3G4200D_LLD_XEN; |
||
| 139 | //cfg.registers.ctrl_reg1 = L3G4200D_LLD_PD | L3G4200D_LLD_DR_800_HZ | L3G4200D_LLD_BW_20 | L3G4200D_LLD_ZEN | L3G4200D_LLD_YEN | L3G4200D_LLD_XEN;
|
||
| 140 | cfg.registers.ctrl_reg3 = 0x07;
|
||
| 141 | cfg.registers.ctrl_reg5 |= L3G4200D_LLD_FIFO_EN; |
||
| 142 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_write_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, cfg); |
| 143 | e545e620 | Thomas Schöpping | uint8_t reg1 = cfg.data[0];
|
| 144 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &cfg); |
| 145 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) { |
| 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 gyro data for five seconds...\n");
|
||
| 152 | status = APAL_STATUS_OK; |
||
| 153 | for (uint8_t i = 0; i < 5; ++i) { |
||
| 154 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_all_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, sdata, &cfg); |
| 155 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tX = %6d\tY = %6d\tZ = %6d\n", sdata[0], sdata[1], sdata[2]); |
| 156 | aosThdSSleep(1);
|
||
| 157 | } |
||
| 158 | if (status == APAL_STATUS_SUCCESS) {
|
||
| 159 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 160 | e545e620 | Thomas Schöpping | } else {
|
| 161 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 162 | e545e620 | Thomas Schöpping | } |
| 163 | |||
| 164 | chprintf(stream, "read X axis for five seconds...\n");
|
||
| 165 | status = APAL_STATUS_SUCCESS; |
||
| 166 | for (uint32_t i = 0; i <= 5; i++) { |
||
| 167 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &(sdata[0]), L3G4200D_LLD_X_AXIS, &cfg);
|
| 168 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tX = %6d\n", sdata[0]); |
| 169 | aosThdSSleep(1);
|
||
| 170 | } |
||
| 171 | if (status == APAL_STATUS_SUCCESS) {
|
||
| 172 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 173 | e545e620 | Thomas Schöpping | } else {
|
| 174 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 175 | e545e620 | Thomas Schöpping | } |
| 176 | |||
| 177 | chprintf(stream, "read Y axis for five seconds...\n");
|
||
| 178 | status = APAL_STATUS_SUCCESS; |
||
| 179 | for (uint32_t i = 0; i <= 5; i++) { |
||
| 180 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Y_AXIS, &cfg);
|
| 181 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tY = %6d\n", sdata[0]); |
| 182 | aosThdSSleep(1);
|
||
| 183 | } |
||
| 184 | if (status == APAL_STATUS_SUCCESS) {
|
||
| 185 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 186 | e545e620 | Thomas Schöpping | } else {
|
| 187 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 188 | e545e620 | Thomas Schöpping | } |
| 189 | |||
| 190 | chprintf(stream, "read Z axis for five seconds...\n");
|
||
| 191 | status = APAL_STATUS_SUCCESS; |
||
| 192 | for (uint32_t i = 0; i <= 5; i++) { |
||
| 193 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Z_AXIS, &cfg);
|
| 194 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tZ = %6d\n", sdata[0]); |
| 195 | aosThdSSleep(1);
|
||
| 196 | } |
||
| 197 | if (status == APAL_STATUS_SUCCESS) {
|
||
| 198 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 199 | e545e620 | Thomas Schöpping | } else {
|
| 200 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 201 | e545e620 | Thomas Schöpping | } |
| 202 | aosThdMSleep(10);
|
||
| 203 | |||
| 204 | chprintf(stream, "read status register...\n");
|
||
| 205 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_read_status_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &status_reg); |
| 206 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
| 207 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 208 | e545e620 | Thomas Schöpping | } else {
|
| 209 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 210 | e545e620 | Thomas Schöpping | } |
| 211 | |||
| 212 | chprintf(stream, "read interrupt config...\n");
|
||
| 213 | l3g4200d_lld_int_cfg_t int_cfg; |
||
| 214 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_read_int_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &int_cfg); |
| 215 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
| 216 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 217 | e545e620 | Thomas Schöpping | } else {
|
| 218 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 219 | e545e620 | Thomas Schöpping | } |
| 220 | |||
| 221 | chprintf(stream, "write interrupt config...\n");
|
||
| 222 | int_cfg.registers.int1_tsh_xh = 10;
|
||
| 223 | 4c72a54c | Thomas Schöpping | status = l3g4200d_lld_write_int_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, int_cfg); |
| 224 | e545e620 | Thomas Schöpping | l3g4200d_lld_int_cfg_t int_cfg2; |
| 225 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_int_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &int_cfg2); |
| 226 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS && int_cfg.registers.int1_tsh_xh == 10) { |
| 227 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 228 | e545e620 | Thomas Schöpping | } else {
|
| 229 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 230 | e545e620 | Thomas Schöpping | } |
| 231 | |||
| 232 | chprintf(stream, "interrupt test: read fifo until empty...\n");
|
||
| 233 | 4c72a54c | Thomas Schöpping | chEvtRegister(((aos_test_l3g4200ddata_t*)(test->data))->src, &el, 0);
|
| 234 | status = l3g4200d_lld_write_fifo_ctrl_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,fifo); |
||
| 235 | e545e620 | Thomas Schöpping | fifo = 0;
|
| 236 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_fifo_ctrl_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,&fifo); |
| 237 | status |= l3g4200d_lld_read_all_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, sdata, &cfg); |
||
| 238 | e545e620 | Thomas Schöpping | chEvtGetAndClearFlags(&el); |
| 239 | aosThdSSleep(1);
|
||
| 240 | chEvtGetAndClearFlags(&el); |
||
| 241 | success = false;
|
||
| 242 | for (uint8_t i = 0; i < 200; i++) { |
||
| 243 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_all_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, sdata, &cfg); |
| 244 | e545e620 | Thomas Schöpping | event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
|
| 245 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_fifo_src_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,&fifo); |
| 246 | e545e620 | Thomas Schöpping | if (event_mask != 0 && ((fifo & L3G4200D_LLD_EMPTY) || fifo == 0)) { |
| 247 | success = true;
|
||
| 248 | break;
|
||
| 249 | } |
||
| 250 | aosThdMSleep(1);
|
||
| 251 | } |
||
| 252 | if (status == APAL_STATUS_SUCCESS && success) {
|
||
| 253 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 254 | e545e620 | Thomas Schöpping | } else {
|
| 255 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 256 | e545e620 | Thomas Schöpping | } |
| 257 | |||
| 258 | fifo = 0x4A;
|
||
| 259 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_write_fifo_ctrl_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,fifo); |
| 260 | e545e620 | Thomas Schöpping | cfg.registers.ctrl_reg1 = L3G4200D_LLD_PD | L3G4200D_LLD_DR_800_HZ | L3G4200D_LLD_BW_20 | L3G4200D_LLD_ZEN | L3G4200D_LLD_YEN | L3G4200D_LLD_XEN; |
| 261 | cfg.registers.ctrl_reg3 = 0x04;
|
||
| 262 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_write_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, cfg); |
| 263 | e545e620 | Thomas Schöpping | chprintf(stream, "interrupt test: wait until wtm reached...\n");
|
| 264 | for (uint8_t i = 0; i < 200; i++) { |
||
| 265 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_all_data(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, sdata, &cfg); |
| 266 | e545e620 | Thomas Schöpping | event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
|
| 267 | 4c72a54c | Thomas Schöpping | status |= l3g4200d_lld_read_fifo_src_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,&fifo); |
| 268 | e545e620 | Thomas Schöpping | if (event_mask != 0 && (fifo & L3G4200D_LLD_WTM)) { |
| 269 | success = true;
|
||
| 270 | break;
|
||
| 271 | } |
||
| 272 | aosThdMSleep(10);
|
||
| 273 | } |
||
| 274 | if (status == APAL_STATUS_SUCCESS && success) {
|
||
| 275 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
| 276 | e545e620 | Thomas Schöpping | } else {
|
| 277 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
| 278 | e545e620 | Thomas Schöpping | } |
| 279 | |||
| 280 | 4c72a54c | Thomas Schöpping | chEvtUnregister(((aos_test_l3g4200ddata_t*)(test->data))->src, &el); |
| 281 | e545e620 | Thomas Schöpping | aosThdMSleep(10);
|
| 282 | |||
| 283 | 4c72a54c | Thomas Schöpping | aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(L3G4200DDriver)); |
| 284 | e545e620 | Thomas Schöpping | |
| 285 | return result;
|
||
| 286 | } |
||
| 287 | |||
| 288 | 4c72a54c | Thomas Schöpping | #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |