Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.365 KB)

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