Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_lis331dlh.c @ f3ac1c96

History | View | Annotate | Download (9.794 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_lis331dlh.h>
20
21
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_LIS331DLH)) || defined(__DOXYGEN__)
22
23 f3ac1c96 Thomas Schöpping
/******************************************************************************/
24
/* LOCAL DEFINITIONS                                                          */
25
/******************************************************************************/
26
27
/******************************************************************************/
28
/* EXPORTED VARIABLES                                                         */
29
/******************************************************************************/
30
31
/******************************************************************************/
32
/* LOCAL TYPES                                                                */
33
/******************************************************************************/
34
35
/******************************************************************************/
36
/* LOCAL VARIABLES                                                            */
37
/******************************************************************************/
38
39
/******************************************************************************/
40
/* LOCAL FUNCTIONS                                                            */
41
/******************************************************************************/
42
43
/******************************************************************************/
44
/* EXPORTED FUNCTIONS                                                         */
45
/******************************************************************************/
46 e545e620 Thomas Schöpping
#include <aos_debug.h>
47
#include <chprintf.h>
48
#include <aos_thread.h>
49
#include <alld_lis331dlh.h>
50
51
/**
52
 * @brief   LIS331DLH unit test function.
53
 *
54
 * @param[in] stream  Stream for input/output.
55
 * @param[in] ut      Unit test object.
56
 *
57
 * @return            Unit test result value.
58
 */
59
aos_utresult_t utAlldLis331dlhFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
60
{
61
  aosDbgCheck(ut->data != NULL && ((ut_lis331dlhdata_t*)(ut->data)) != NULL);
62
63
  // local variables
64
  aos_utresult_t result = {0, 0};
65
  uint32_t status;
66
  uint8_t data = 0;
67
  uint8_t write_data[5];
68
  uint8_t read_data[5];
69
  uint8_t errors = 0;
70
  uint8_t reg1;
71
  int16_t sdata[3];
72
  uint8_t status_reg;
73
  lis331dlh_lld_int_cfg_t intcfg;
74
  event_listener_t el;
75
  eventmask_t event_mask;
76
  uint8_t success = 0;
77
78
  for (uint8_t dataIdx = 0; dataIdx < 4; dataIdx++) {
79
    write_data[dataIdx] = (dataIdx+1)*11;
80
  }
81
  write_data[4] = 0;
82
83
  chprintf(stream, "check identity...\n");
84
  status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_WHO_AM_I, &data, 1);
85
  if (status == APAL_STATUS_SUCCESS &&
86
      data == LIS331DLH_LLD_WHO_AM_I) {
87
    aosUtPassed(stream, &result);
88
  } else {
89
    chprintf(stream, "\tfailed\n");
90
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
91
    ++result.failed;
92
  }
93
94
  chprintf(stream, "write register...\n");
95
  status = lis331dlh_lld_write_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 1);
96
  if (status == APAL_STATUS_SUCCESS) {
97
    aosUtPassed(stream, &result);
98
  } else {
99
    aosUtFailed(stream, &result);
100
  }
101
102
  chprintf(stream, "read register...\n");
103
  status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, &data, 1);
104
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
105
    aosUtPassed(stream, &result);
106
  } else {
107
    chprintf(stream, "\tfailed\n");
108
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
109
    ++result.failed;
110
  }
111
112
  chprintf(stream, "write multiple registers...\n");
113
  status = lis331dlh_lld_write_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 5);
114
  if (status == APAL_STATUS_SUCCESS) {
115
    aosUtPassed(stream, &result);
116
  } else {
117
    aosUtFailed(stream, &result);
118
  }
119
120
  chprintf(stream, "read multiple registers...\n");
121
  status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, read_data, 5);
122
  for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
123
    if (read_data[dataIdx] != write_data[dataIdx]) {
124
      ++errors;
125
    }
126
  }
127
  if (status == APAL_STATUS_SUCCESS &&
128
      errors == 0) {
129
    aosUtPassed(stream, &result);
130
  } else {
131
    chprintf(stream, "\tfailed\n");
132
    for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
133
      chprintf(stream, "\t\tStatus: %d, CTRL_REG%d: %d, write_data: %d\n", status, dataIdx+1, read_data[dataIdx], write_data[dataIdx]);
134
    }
135
    aosUtFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
136
  }
137
138
  chprintf(stream, "read config...\n");
139
  lis331dlh_lld_cfg_t cfg;
140
  status = lis331dlh_lld_read_config(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
141
  if (status == APAL_STATUS_SUCCESS) {
142
    aosUtPassed(stream, &result);
143
  } else {
144
    aosUtFailed(stream, &result);
145
  }
146
147
  chprintf(stream, "write config...\n");
148
  cfg.registers.ctrl_reg1 = LIS331DLH_LLD_PM_ODR |
149
      LIS331DLH_LLD_DR_1000HZ_780LP |
150
      LIS331DLH_LLD_X_AXIS_ENABLE |
151
      LIS331DLH_LLD_Y_AXIS_ENABLE |
152
      LIS331DLH_LLD_Z_AXIS_ENABLE;
153
  cfg.registers.ctrl_reg3 = 0x00;
154
  status = lis331dlh_lld_write_config(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
155
  reg1 = cfg.data[0];
156
  lis331dlh_lld_read_config(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
157
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
158
    aosUtPassed(stream, &result);
159
  } else {
160
    aosUtFailed(stream, &result);
161
  }
162
163
  chprintf(stream, "read acceleration for five seconds...\n");
164
  status = APAL_STATUS_SUCCESS;
165
  for (uint8_t i = 0; i < 5; ++i) {
166
    status |= lis331dlh_lld_read_all_data(((ut_lis331dlhdata_t*)(ut->data))->lisd, sdata, &cfg);
167
    chprintf(stream, "\t\tX = %6d\tY = %6d\tZ = %6d\n", sdata[0], sdata[1], sdata[2]);
168
    aosThdSSleep(1);
169
  }
170
  if (status == APAL_STATUS_SUCCESS) {
171
    aosUtPassed(stream, &result);
172
  } else {
173
    aosUtFailed(stream, &result);
174
  }
175
176
  chprintf(stream, "read X acceleration for five seconds...\n");
177
  status = APAL_STATUS_SUCCESS;
178
  for (uint8_t i = 0; i < 5; ++i) {
179
    status |= lis331dlh_lld_read_data(((ut_lis331dlhdata_t*)(ut->data))->lisd, &(sdata[0]), LIS331DLH_LLD_X_AXIS, &cfg);
180
    chprintf(stream, "\t\tX = %6d\n", sdata[0]);
181
    aosThdSSleep(1);
182
  }
183
  if (status == APAL_STATUS_SUCCESS) {
184
    aosUtPassed(stream, &result);
185
  } else {
186
    aosUtFailed(stream, &result);
187
  }
188
189
  chprintf(stream, "read Y acceleration for five seconds...\n");
190
  status = APAL_STATUS_SUCCESS;
191
  for (uint8_t i = 0; i < 5; ++i) {
192
    status |= lis331dlh_lld_read_data(((ut_lis331dlhdata_t*)(ut->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Y_AXIS, &cfg);
193
    chprintf(stream, "\t\tY = %6d\n", sdata[0]);
194
    aosThdSSleep(1);
195
  }
196
  if (status == APAL_STATUS_SUCCESS) {
197
    aosUtPassed(stream, &result);
198
  } else {
199
    aosUtFailed(stream, &result);
200
  }
201
202
  chprintf(stream, "read Z acceleration for five seconds...\n");
203
  status = APAL_STATUS_SUCCESS;
204
  for (uint8_t i = 0; i < 5; ++i) {
205
    status |= lis331dlh_lld_read_data(((ut_lis331dlhdata_t*)(ut->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Z_AXIS, &cfg);
206
    chprintf(stream, "\t\tZ = %6d\n", sdata[0]);
207
    aosThdSSleep(1);
208
  }
209
  if (status == APAL_STATUS_SUCCESS) {
210
    aosUtPassed(stream, &result);
211
  } else {
212
    aosUtFailed(stream, &result);
213
  }
214
215
  chprintf(stream, "read status register...\n");
216
  status = lis331dlh_lld_read_status_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, &status_reg);
217
  if (status == APAL_STATUS_SUCCESS) {
218
    aosUtPassed(stream, &result);
219
  } else {
220
    aosUtFailed(stream, &result);
221
  }
222
223
  chprintf(stream, "interrupt...\n");
224
  intcfg.cfg_reg = LIS331DLH_LLD_INT_CFG_X_HIGH_ENABLE | LIS331DLH_LLD_INT_CFG_Y_HIGH_ENABLE | LIS331DLH_LLD_INT_CFG_Z_HIGH_ENABLE;
225
  intcfg.threshold = 10;
226
  intcfg.duration = 1;
227
  lis331dlh_lld_write_int_config(((ut_lis331dlhdata_t*)(ut->data))->lisd, &intcfg, LIS331DLH_LLD_INT1);
228
  aosThdSSleep(1);
229
  chEvtRegister(((ut_lis331dlhdata_t*)(ut->data))->src, &el, 3);
230
  chprintf(stream, "\t\tmove the AMiRo now to generate interrupts\n");
231
  aosThdSSleep(1);
232
  for (uint8_t i = 0; i < 10; i++) {
233
    event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
234
    status |= lis331dlh_lld_read_all_data(((ut_lis331dlhdata_t*)(ut->data))->lisd, sdata, &cfg);
235
    status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_INT1_SOURCE, &data, 1);
236
    eventflags_t flags = chEvtGetAndClearFlags(&el);
237
    if (event_mask == EVENT_MASK(3) && (flags & ((ut_lis331dlhdata_t*)(ut->data))->evtflags)) {
238
      ++success;
239
      chprintf(stream, "\t\tX = %6d INTERRUPT\n", sdata[0]);
240
    } else {
241
      chprintf(stream, "\t\tX = %6d\n", sdata[0]);
242
    }
243
    aosThdSSleep(1);
244
  }
245 1e5f7648 Thomas Schöpping
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
246 e545e620 Thomas Schöpping
  chEvtUnregister(((ut_lis331dlhdata_t*)(ut->data))->src, &el);
247
  if (status == APAL_STATUS_SUCCESS && success > 0) {
248
    aosUtPassed(stream, &result);
249
  } else {
250
    aosUtFailed(stream, &result);
251
  }
252
253
  aosUtInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(LIS331DLHDriver));
254
255
  return result;
256
}
257
258
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_LIS331DLH) */