Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_L3G4200D_v1.c @ a193bcf1

History | View | Annotate | Download (11.2 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 2a4dbf93 Thomas Schöpping
#include <ut_alld_L3G4200D_v1.h>
21 e545e620 Thomas Schöpping
22 ddf34c3d Thomas Schöpping
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_L3G4200D) && (AMIROLLD_CFG_L3G4200D == 1)) || defined(__DOXYGEN__)
23
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
 * @brief   L3G4200D unit test function.
50
 *
51
 * @param[in] stream  Stream for input/output.
52
 * @param[in] ut      Unit test object.
53
 *
54
 * @return            Unit test result value.
55
 */
56
aos_utresult_t utAlldL3g4200dFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
57
{
58
  aosDbgCheck(ut->data != NULL && ((ut_l3g4200ddata_t*)(ut->data)) != NULL);
59
60
  // local variables
61
  aos_utresult_t result = {0, 0};
62
  uint32_t status;
63
  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
  for (uint8_t dataIdx = 0; dataIdx < 4; dataIdx++) {
74
    write_data[dataIdx] = (dataIdx+1)*11;
75
  }
76
  write_data[4] = 0;
77
78
  chprintf(stream, "check identity...\n");
79
  status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_WHO_AM_I, &data, 1);
80
  if(status == APAL_STATUS_SUCCESS && data == L3G4200D_LLD_WHO_AM_I){
81
    aosUtPassed(stream, &result);
82
  } else {
83
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
84
  }
85
86
  chprintf(stream, "write register...\n");
87
  status = l3g4200d_lld_write_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 1);
88
  if (status == APAL_STATUS_SUCCESS) {
89
    aosUtPassed(stream, &result);
90
  } else {
91
    aosUtFailed(stream, &result);
92
  }
93
94
  chprintf(stream, "read register...\n");
95
  status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, &data, 1);
96
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
97
    aosUtPassed(stream, &result);
98
  } else {
99
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
100
  }
101
102
  chprintf(stream, "write multiple registers...\n");
103
  status = l3g4200d_lld_write_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 5);
104
  if (status == APAL_STATUS_SUCCESS) {
105
    aosUtPassed(stream, &result);
106
  } else {
107
    aosUtFailed(stream, &result);
108
  }
109
110
  chprintf(stream, "read multiple registers...\n");
111
  status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, read_data, 5);
112
  uint8_t errors = 0;
113
  for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
114
    if (read_data[dataIdx] != write_data[dataIdx]) {
115
      ++errors;
116
    }
117
  }
118
  if (status == APAL_STATUS_SUCCESS && errors == 0) {
119
    aosUtPassed(stream, &result);
120
  } else {
121
    for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
122
      chprintf(stream, "\t\tStatus: %d, CTRL_REG%d: %d, write_data: %d\n", status, dataIdx+1, read_data[dataIdx], write_data[dataIdx]);
123
    }
124
    aosUtFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
125
  }
126
127
  chprintf(stream, "read config...\n");
128
  l3g4200d_lld_cfg_t cfg;
129
  status = l3g4200d_lld_read_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &cfg);
130
  if (status == APAL_STATUS_SUCCESS) {
131
    aosUtPassed(stream, &result);
132
  } else {
133
    aosUtFailed(stream, &result);
134
  }
135
136
  chprintf(stream, "write config...\n");
137
  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;
138
  //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;
139
  cfg.registers.ctrl_reg3 = 0x07;
140
  cfg.registers.ctrl_reg5 |= L3G4200D_LLD_FIFO_EN;
141
  status = l3g4200d_lld_write_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, cfg);
142
  uint8_t reg1 = cfg.data[0];
143
  status |= l3g4200d_lld_read_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &cfg);
144
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
145
    aosUtPassed(stream, &result);
146
  } else {
147
    aosUtFailed(stream, &result);
148
  }
149
150
  chprintf(stream, "read gyro data for five seconds...\n");
151
  status = APAL_STATUS_OK;
152
  for (uint8_t i = 0; i < 5; ++i) {
153
    status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
154
    chprintf(stream, "\t\tX = %6d\tY = %6d\tZ = %6d\n", sdata[0], sdata[1], sdata[2]);
155
    aosThdSSleep(1);
156
  }
157
  if (status == APAL_STATUS_SUCCESS) {
158
    aosUtPassed(stream, &result);
159
  } else {
160
    aosUtFailed(stream, &result);
161
  }
162
163
  chprintf(stream, "read X axis for five seconds...\n");
164
  status = APAL_STATUS_SUCCESS;
165
  for (uint32_t i = 0; i <= 5; i++) {
166
    status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_X_AXIS, &cfg);
167
    chprintf(stream, "\t\tX = %6d\n", sdata[0]);
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 Y axis for five seconds...\n");
177
  status = APAL_STATUS_SUCCESS;
178
  for (uint32_t i = 0; i <= 5; i++) {
179
    status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Y_AXIS, &cfg);
180
    chprintf(stream, "\t\tY = %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 Z axis for five seconds...\n");
190
  status = APAL_STATUS_SUCCESS;
191
  for (uint32_t i = 0; i <= 5; i++) {
192
    status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Z_AXIS, &cfg);
193
    chprintf(stream, "\t\tZ = %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
  aosThdMSleep(10);
202
203
  chprintf(stream, "read status register...\n");
204
  status = l3g4200d_lld_read_status_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &status_reg);
205
  if (status == APAL_STATUS_SUCCESS) {
206
    aosUtPassed(stream, &result);
207
  } else {
208
    aosUtFailed(stream, &result);
209
  }
210
211
  chprintf(stream, "read interrupt config...\n");
212
  l3g4200d_lld_int_cfg_t int_cfg;
213
  status = l3g4200d_lld_read_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &int_cfg);
214
  if (status == APAL_STATUS_SUCCESS) {
215
    aosUtPassed(stream, &result);
216
  } else {
217
    aosUtFailed(stream, &result);
218
  }
219
220
  chprintf(stream, "write interrupt config...\n");
221
  int_cfg.registers.int1_tsh_xh = 10;
222
  status = l3g4200d_lld_write_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, int_cfg);
223
  l3g4200d_lld_int_cfg_t int_cfg2;
224
  status |= l3g4200d_lld_read_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &int_cfg2);
225
  if (status == APAL_STATUS_SUCCESS && int_cfg.registers.int1_tsh_xh == 10) {
226
    aosUtPassed(stream, &result);
227
  } else {
228
    aosUtFailed(stream, &result);
229
  }
230
231
  chprintf(stream, "interrupt test: read fifo until empty...\n");
232
  chEvtRegister(((ut_l3g4200ddata_t*)(ut->data))->src, &el, 0);
233
  status = l3g4200d_lld_write_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,fifo);
234
  fifo = 0;
235
  status |= l3g4200d_lld_read_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo);
236
  status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
237
  chEvtGetAndClearFlags(&el);
238
  aosThdSSleep(1);
239
  chEvtGetAndClearFlags(&el);
240
  success = false;
241
  for (uint8_t i = 0; i < 200; i++) {
242
    status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
243
    event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
244
    status |= l3g4200d_lld_read_fifo_src_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo);
245
    if (event_mask != 0 && ((fifo & L3G4200D_LLD_EMPTY) || fifo == 0)) {
246
      success = true;
247
      break;
248
    }
249
    aosThdMSleep(1);
250
  }
251
  if (status == APAL_STATUS_SUCCESS && success) {
252
    aosUtPassed(stream, &result);
253
  } else {
254
    aosUtFailed(stream, &result);
255
  }
256
257
  fifo = 0x4A;
258
  status |= l3g4200d_lld_write_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,fifo);
259
  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;
260
  cfg.registers.ctrl_reg3 = 0x04;
261
  status |= l3g4200d_lld_write_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, cfg);
262
  chprintf(stream, "interrupt test: wait until wtm reached...\n");
263
  for (uint8_t i = 0; i < 200; i++) {
264
    status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
265
    event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
266
    status |= l3g4200d_lld_read_fifo_src_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo);
267
    if (event_mask != 0 && (fifo & L3G4200D_LLD_WTM)) {
268
      success = true;
269
      break;
270
    }
271
    aosThdMSleep(10);
272
  }
273
  if (status == APAL_STATUS_SUCCESS && success) {
274
    aosUtPassed(stream, &result);
275
  } else {
276
    aosUtFailed(stream, &result);
277
  }
278
279
  chEvtUnregister(((ut_l3g4200ddata_t*)(ut->data))->src, &el);
280
  aosThdMSleep(10);
281
282
  aosUtInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(L3G4200DDriver));
283
284
  return result;
285
}
286
287 ddf34c3d Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_L3G4200D) && (AMIROLLD_CFG_L3G4200D == 1) */