Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / LIS331DLH_v1 / aos_test_LIS331DLH.c @ 3cee92c7

History | View | Annotate | Download (9.846 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_LIS331DLH.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   LIS331DLH 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 aosTestLis331dlhFunc(BaseSequentialStream* stream, const aos_test_t* test)
57 e545e620 Thomas Schöpping
{
58 4c72a54c Thomas Schöpping
  aosDbgCheck(test->data != NULL && ((aos_test_lis331dlhdata_t*)(test->data)) != NULL);
59 e545e620 Thomas Schöpping
60
  // local variables
61 4c72a54c Thomas Schöpping
  aos_testresult_t result = {0, 0};
62
  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
  uint8_t errors = 0;
67
  uint8_t reg1;
68
  int16_t sdata[3];
69
  uint8_t status_reg;
70
  lis331dlh_lld_int_cfg_t intcfg;
71
  event_listener_t el;
72
  eventmask_t event_mask;
73
  uint8_t success = 0;
74
75
  for (uint8_t dataIdx = 0; dataIdx < 4; dataIdx++) {
76
    write_data[dataIdx] = (dataIdx+1)*11;
77
  }
78
  write_data[4] = 0;
79
80
  chprintf(stream, "check identity...\n");
81 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_WHO_AM_I, &data, 1);
82 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS &&
83
      data == LIS331DLH_LLD_WHO_AM_I) {
84 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
85 e545e620 Thomas Schöpping
  } else {
86
    chprintf(stream, "\tfailed\n");
87 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
88 e545e620 Thomas Schöpping
    ++result.failed;
89
  }
90
91
  chprintf(stream, "write register...\n");
92 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_write_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 1);
93 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
94 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
95 e545e620 Thomas Schöpping
  } else {
96 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
97 e545e620 Thomas Schöpping
  }
98
99
  chprintf(stream, "read register...\n");
100 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, &data, 1);
101 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
102 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
103 e545e620 Thomas Schöpping
  } else {
104
    chprintf(stream, "\tfailed\n");
105 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
106 e545e620 Thomas Schöpping
    ++result.failed;
107
  }
108
109
  chprintf(stream, "write multiple registers...\n");
110 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_write_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 5);
111 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
112 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
113 e545e620 Thomas Schöpping
  } else {
114 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
115 e545e620 Thomas Schöpping
  }
116
117
  chprintf(stream, "read multiple registers...\n");
118 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, read_data, 5);
119 e545e620 Thomas Schöpping
  for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
120
    if (read_data[dataIdx] != write_data[dataIdx]) {
121
      ++errors;
122
    }
123
  }
124
  if (status == APAL_STATUS_SUCCESS &&
125
      errors == 0) {
126 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
127 e545e620 Thomas Schöpping
  } else {
128
    chprintf(stream, "\tfailed\n");
129
    for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
130
      chprintf(stream, "\t\tStatus: %d, CTRL_REG%d: %d, write_data: %d\n", status, dataIdx+1, read_data[dataIdx], write_data[dataIdx]);
131
    }
132 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
133 e545e620 Thomas Schöpping
  }
134
135
  chprintf(stream, "read config...\n");
136
  lis331dlh_lld_cfg_t cfg;
137 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_read_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg);
138 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
139 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
140 e545e620 Thomas Schöpping
  } else {
141 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
142 e545e620 Thomas Schöpping
  }
143
144
  chprintf(stream, "write config...\n");
145
  cfg.registers.ctrl_reg1 = LIS331DLH_LLD_PM_ODR |
146
      LIS331DLH_LLD_DR_1000HZ_780LP |
147
      LIS331DLH_LLD_X_AXIS_ENABLE |
148
      LIS331DLH_LLD_Y_AXIS_ENABLE |
149
      LIS331DLH_LLD_Z_AXIS_ENABLE;
150
  cfg.registers.ctrl_reg3 = 0x00;
151 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_write_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg);
152 e545e620 Thomas Schöpping
  reg1 = cfg.data[0];
153 4c72a54c Thomas Schöpping
  lis331dlh_lld_read_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg);
154 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
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
  chprintf(stream, "read acceleration for five seconds...\n");
161
  status = APAL_STATUS_SUCCESS;
162
  for (uint8_t i = 0; i < 5; ++i) {
163 4c72a54c Thomas Schöpping
    status |= lis331dlh_lld_read_all_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, sdata, &cfg);
164 e545e620 Thomas Schöpping
    chprintf(stream, "\t\tX = %6d\tY = %6d\tZ = %6d\n", sdata[0], sdata[1], sdata[2]);
165
    aosThdSSleep(1);
166
  }
167
  if (status == APAL_STATUS_SUCCESS) {
168 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
169 e545e620 Thomas Schöpping
  } else {
170 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
171 e545e620 Thomas Schöpping
  }
172
173
  chprintf(stream, "read X acceleration for five seconds...\n");
174
  status = APAL_STATUS_SUCCESS;
175
  for (uint8_t i = 0; i < 5; ++i) {
176 4c72a54c Thomas Schöpping
    status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_X_AXIS, &cfg);
177 e545e620 Thomas Schöpping
    chprintf(stream, "\t\tX = %6d\n", sdata[0]);
178
    aosThdSSleep(1);
179
  }
180
  if (status == APAL_STATUS_SUCCESS) {
181 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
182 e545e620 Thomas Schöpping
  } else {
183 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
184 e545e620 Thomas Schöpping
  }
185
186
  chprintf(stream, "read Y acceleration for five seconds...\n");
187
  status = APAL_STATUS_SUCCESS;
188
  for (uint8_t i = 0; i < 5; ++i) {
189 4c72a54c Thomas Schöpping
    status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Y_AXIS, &cfg);
190 e545e620 Thomas Schöpping
    chprintf(stream, "\t\tY = %6d\n", sdata[0]);
191
    aosThdSSleep(1);
192
  }
193
  if (status == APAL_STATUS_SUCCESS) {
194 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
195 e545e620 Thomas Schöpping
  } else {
196 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
197 e545e620 Thomas Schöpping
  }
198
199
  chprintf(stream, "read Z acceleration for five seconds...\n");
200
  status = APAL_STATUS_SUCCESS;
201
  for (uint8_t i = 0; i < 5; ++i) {
202 4c72a54c Thomas Schöpping
    status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Z_AXIS, &cfg);
203 e545e620 Thomas Schöpping
    chprintf(stream, "\t\tZ = %6d\n", sdata[0]);
204
    aosThdSSleep(1);
205
  }
206
  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 status register...\n");
213 4c72a54c Thomas Schöpping
  status = lis331dlh_lld_read_status_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &status_reg);
214 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
215 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
216 e545e620 Thomas Schöpping
  } else {
217 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
218 e545e620 Thomas Schöpping
  }
219
220
  chprintf(stream, "interrupt...\n");
221
  intcfg.cfg_reg = LIS331DLH_LLD_INT_CFG_X_HIGH_ENABLE | LIS331DLH_LLD_INT_CFG_Y_HIGH_ENABLE | LIS331DLH_LLD_INT_CFG_Z_HIGH_ENABLE;
222
  intcfg.threshold = 10;
223
  intcfg.duration = 1;
224 4c72a54c Thomas Schöpping
  lis331dlh_lld_write_int_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &intcfg, LIS331DLH_LLD_INT1);
225 e545e620 Thomas Schöpping
  aosThdSSleep(1);
226 4c72a54c Thomas Schöpping
  chEvtRegister(((aos_test_lis331dlhdata_t*)(test->data))->src, &el, 3);
227 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tmove the AMiRo now to generate interrupts\n");
228
  aosThdSSleep(1);
229
  for (uint8_t i = 0; i < 10; i++) {
230
    event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
231 4c72a54c Thomas Schöpping
    status |= lis331dlh_lld_read_all_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, sdata, &cfg);
232
    status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_INT1_SOURCE, &data, 1);
233 e545e620 Thomas Schöpping
    eventflags_t flags = chEvtGetAndClearFlags(&el);
234 4c72a54c Thomas Schöpping
    if (event_mask == EVENT_MASK(3) && (flags & ((aos_test_lis331dlhdata_t*)(test->data))->evtflags)) {
235 e545e620 Thomas Schöpping
      ++success;
236
      chprintf(stream, "\t\tX = %6d INTERRUPT\n", sdata[0]);
237
    } else {
238
      chprintf(stream, "\t\tX = %6d\n", sdata[0]);
239
    }
240
    aosThdSSleep(1);
241
  }
242 1e5f7648 Thomas Schöpping
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
243 4c72a54c Thomas Schöpping
  chEvtUnregister(((aos_test_lis331dlhdata_t*)(test->data))->src, &el);
244 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && success > 0) {
245 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
246 e545e620 Thomas Schöpping
  } else {
247 4c72a54c Thomas Schöpping
    aosTestFailed(stream, &result);
248 e545e620 Thomas Schöpping
  }
249
250 4c72a54c Thomas Schöpping
  aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(LIS331DLHDriver));
251 e545e620 Thomas Schöpping
252
  return result;
253
}
254
255 4c72a54c Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */