Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.185 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 e545e620 Thomas Schöpping
21 ddf34c3d Thomas Schöpping
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_L3G4200D) && (AMIROLLD_CFG_L3G4200D == 1)) || defined(__DOXYGEN__)
22
23
#include <ut_alld_L3G4200D_v1.h>
24 e545e620 Thomas Schöpping
25 f3ac1c96 Thomas Schöpping
/******************************************************************************/
26
/* LOCAL DEFINITIONS                                                          */
27
/******************************************************************************/
28
29
/******************************************************************************/
30
/* EXPORTED VARIABLES                                                         */
31
/******************************************************************************/
32
33
/******************************************************************************/
34
/* LOCAL TYPES                                                                */
35
/******************************************************************************/
36
37
/******************************************************************************/
38
/* LOCAL VARIABLES                                                            */
39
/******************************************************************************/
40
41
/******************************************************************************/
42
/* LOCAL FUNCTIONS                                                            */
43
/******************************************************************************/
44
45
/******************************************************************************/
46
/* EXPORTED FUNCTIONS                                                         */
47
/******************************************************************************/
48 e545e620 Thomas Schöpping
49
/**
50
 * @brief   L3G4200D unit test function.
51
 *
52
 * @param[in] stream  Stream for input/output.
53
 * @param[in] ut      Unit test object.
54
 *
55
 * @return            Unit test result value.
56
 */
57
aos_utresult_t utAlldL3g4200dFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
58
{
59
  aosDbgCheck(ut->data != NULL && ((ut_l3g4200ddata_t*)(ut->data)) != NULL);
60
61
  // local variables
62
  aos_utresult_t result = {0, 0};
63
  uint32_t status;
64
  uint8_t data = 0;
65
  uint8_t write_data[5];
66
  uint8_t read_data[5];
67
  int16_t sdata[3];
68
  uint8_t status_reg;
69
  eventmask_t event_mask;
70
  bool success = false;
71
  uint8_t fifo = 0x5F;
72
  event_listener_t el;
73
74
  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
  status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_WHO_AM_I, &data, 1);
81
  if(status == APAL_STATUS_SUCCESS && data == L3G4200D_LLD_WHO_AM_I){
82
    aosUtPassed(stream, &result);
83
  } else {
84
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
85
  }
86
87
  chprintf(stream, "write register...\n");
88
  status = l3g4200d_lld_write_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 1);
89
  if (status == APAL_STATUS_SUCCESS) {
90
    aosUtPassed(stream, &result);
91
  } else {
92
    aosUtFailed(stream, &result);
93
  }
94
95
  chprintf(stream, "read register...\n");
96
  status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, &data, 1);
97
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
98
    aosUtPassed(stream, &result);
99
  } else {
100
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
101
  }
102
103
  chprintf(stream, "write multiple registers...\n");
104
  status = l3g4200d_lld_write_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 5);
105
  if (status == APAL_STATUS_SUCCESS) {
106
    aosUtPassed(stream, &result);
107
  } else {
108
    aosUtFailed(stream, &result);
109
  }
110
111
  chprintf(stream, "read multiple registers...\n");
112
  status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, read_data, 5);
113
  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
    aosUtPassed(stream, &result);
121
  } 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
    aosUtFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
126
  }
127
128
  chprintf(stream, "read config...\n");
129
  l3g4200d_lld_cfg_t cfg;
130
  status = l3g4200d_lld_read_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &cfg);
131
  if (status == APAL_STATUS_SUCCESS) {
132
    aosUtPassed(stream, &result);
133
  } else {
134
    aosUtFailed(stream, &result);
135
  }
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
  status = l3g4200d_lld_write_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, cfg);
143
  uint8_t reg1 = cfg.data[0];
144
  status |= l3g4200d_lld_read_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &cfg);
145
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
146
    aosUtPassed(stream, &result);
147
  } else {
148
    aosUtFailed(stream, &result);
149
  }
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
    status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
155
    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
    aosUtPassed(stream, &result);
160
  } else {
161
    aosUtFailed(stream, &result);
162
  }
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
    status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_X_AXIS, &cfg);
168
    chprintf(stream, "\t\tX = %6d\n", sdata[0]);
169
    aosThdSSleep(1);
170
  }
171
  if (status == APAL_STATUS_SUCCESS) {
172
    aosUtPassed(stream, &result);
173
  } else {
174
    aosUtFailed(stream, &result);
175
  }
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
    status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Y_AXIS, &cfg);
181
    chprintf(stream, "\t\tY = %6d\n", sdata[0]);
182
    aosThdSSleep(1);
183
  }
184
  if (status == APAL_STATUS_SUCCESS) {
185
    aosUtPassed(stream, &result);
186
  } else {
187
    aosUtFailed(stream, &result);
188
  }
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
    status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Z_AXIS, &cfg);
194
    chprintf(stream, "\t\tZ = %6d\n", sdata[0]);
195
    aosThdSSleep(1);
196
  }
197
  if (status == APAL_STATUS_SUCCESS) {
198
    aosUtPassed(stream, &result);
199
  } else {
200
    aosUtFailed(stream, &result);
201
  }
202
  aosThdMSleep(10);
203
204
  chprintf(stream, "read status register...\n");
205
  status = l3g4200d_lld_read_status_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &status_reg);
206
  if (status == APAL_STATUS_SUCCESS) {
207
    aosUtPassed(stream, &result);
208
  } else {
209
    aosUtFailed(stream, &result);
210
  }
211
212
  chprintf(stream, "read interrupt config...\n");
213
  l3g4200d_lld_int_cfg_t int_cfg;
214
  status = l3g4200d_lld_read_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &int_cfg);
215
  if (status == APAL_STATUS_SUCCESS) {
216
    aosUtPassed(stream, &result);
217
  } else {
218
    aosUtFailed(stream, &result);
219
  }
220
221
  chprintf(stream, "write interrupt config...\n");
222
  int_cfg.registers.int1_tsh_xh = 10;
223
  status = l3g4200d_lld_write_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, int_cfg);
224
  l3g4200d_lld_int_cfg_t int_cfg2;
225
  status |= l3g4200d_lld_read_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &int_cfg2);
226
  if (status == APAL_STATUS_SUCCESS && int_cfg.registers.int1_tsh_xh == 10) {
227
    aosUtPassed(stream, &result);
228
  } else {
229
    aosUtFailed(stream, &result);
230
  }
231
232
  chprintf(stream, "interrupt test: read fifo until empty...\n");
233
  chEvtRegister(((ut_l3g4200ddata_t*)(ut->data))->src, &el, 0);
234
  status = l3g4200d_lld_write_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,fifo);
235
  fifo = 0;
236
  status |= l3g4200d_lld_read_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo);
237
  status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
238
  chEvtGetAndClearFlags(&el);
239
  aosThdSSleep(1);
240
  chEvtGetAndClearFlags(&el);
241
  success = false;
242
  for (uint8_t i = 0; i < 200; i++) {
243
    status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
244
    event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
245
    status |= l3g4200d_lld_read_fifo_src_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo);
246
    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
    aosUtPassed(stream, &result);
254
  } else {
255
    aosUtFailed(stream, &result);
256
  }
257
258
  fifo = 0x4A;
259
  status |= l3g4200d_lld_write_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,fifo);
260
  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
  status |= l3g4200d_lld_write_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, cfg);
263
  chprintf(stream, "interrupt test: wait until wtm reached...\n");
264
  for (uint8_t i = 0; i < 200; i++) {
265
    status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg);
266
    event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
267
    status |= l3g4200d_lld_read_fifo_src_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo);
268
    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
    aosUtPassed(stream, &result);
276
  } else {
277
    aosUtFailed(stream, &result);
278
  }
279
280
  chEvtUnregister(((ut_l3g4200ddata_t*)(ut->data))->src, &el);
281
  aosThdMSleep(10);
282
283
  aosUtInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(L3G4200DDriver));
284
285
  return result;
286
}
287
288 ddf34c3d Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_L3G4200D) && (AMIROLLD_CFG_L3G4200D == 1) */