Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / L3G4200D_v1 / aos_test_L3G4200D.c @ 96621a83

History | View | Annotate | Download (11.35 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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 <amiroos.h>
20
#include <aos_test_L3G4200D.h>
21

    
22
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
23

    
24
/******************************************************************************/
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

    
48
/**
49
 * @brief   L3G4200D test function.
50
 *
51
 * @param[in] stream  Stream for input/output.
52
 * @param[in] test    Test object.
53
 *
54
 * @return            Test result value.
55
 */
56
aos_testresult_t aosTestL3g4200dFunc(BaseSequentialStream* stream, const aos_test_t* test)
57
{
58
  aosDbgCheck(test->data != NULL && ((aos_test_l3g4200ddata_t*)(test->data)) != NULL);
59

    
60
  // local variables
61
  aos_testresult_t result;
62
  int32_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
  aosTestResultInit(&result);
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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_WHO_AM_I, &data, 1);
81
  if(status == APAL_STATUS_SUCCESS && data == L3G4200D_LLD_WHO_AM_I){
82
    aosTestPassed(stream, &result);
83
  } else {
84
    aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
85
  }
86

    
87
  chprintf(stream, "write register...\n");
88
  status = l3g4200d_lld_write_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 1);
89
  if (status == APAL_STATUS_SUCCESS) {
90
    aosTestPassed(stream, &result);
91
  } else {
92
    aosTestFailed(stream, &result);
93
  }
94

    
95
  chprintf(stream, "read register...\n");
96
  status = l3g4200d_lld_read_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, &data, 1);
97
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
98
    aosTestPassed(stream, &result);
99
  } else {
100
    aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
101
  }
102

    
103
  chprintf(stream, "write multiple registers...\n");
104
  status = l3g4200d_lld_write_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 5);
105
  if (status == APAL_STATUS_SUCCESS) {
106
    aosTestPassed(stream, &result);
107
  } else {
108
    aosTestFailed(stream, &result);
109
  }
110

    
111
  chprintf(stream, "read multiple registers...\n");
112
  status = l3g4200d_lld_read_register(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(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
    aosTestFailedMsg(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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &cfg);
131
  if (status == APAL_STATUS_SUCCESS) {
132
    aosTestPassed(stream, &result);
133
  } else {
134
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, cfg);
143
  uint8_t reg1 = cfg.data[0];
144
  status |= l3g4200d_lld_read_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &cfg);
145
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
146
    aosTestPassed(stream, &result);
147
  } else {
148
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(stream, &result);
160
  } else {
161
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(stream, &result);
173
  } else {
174
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(stream, &result);
186
  } else {
187
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(stream, &result);
199
  } else {
200
    aosTestFailed(stream, &result);
201
  }
202
  aosThdMSleep(10);
203

    
204
  chprintf(stream, "read status register...\n");
205
  status = l3g4200d_lld_read_status_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &status_reg);
206
  if (status == APAL_STATUS_SUCCESS) {
207
    aosTestPassed(stream, &result);
208
  } else {
209
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &int_cfg);
215
  if (status == APAL_STATUS_SUCCESS) {
216
    aosTestPassed(stream, &result);
217
  } else {
218
    aosTestFailed(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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, int_cfg);
224
  l3g4200d_lld_int_cfg_t int_cfg2;
225
  status |= l3g4200d_lld_read_int_config(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, &int_cfg2);
226
  if (status == APAL_STATUS_SUCCESS && int_cfg.registers.int1_tsh_xh == 10) {
227
    aosTestPassed(stream, &result);
228
  } else {
229
    aosTestFailed(stream, &result);
230
  }
231

    
232
  chprintf(stream, "interrupt test: read fifo until empty...\n");
233
  chEvtRegister(((aos_test_l3g4200ddata_t*)(test->data))->src, &el, 0);
234
  status = l3g4200d_lld_write_fifo_ctrl_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,fifo);
235
  fifo = 0;
236
  status |= l3g4200d_lld_read_fifo_ctrl_register(((aos_test_l3g4200ddata_t*)(test->data))->l3gd,&fifo);
237
  status |= l3g4200d_lld_read_all_data(((aos_test_l3g4200ddata_t*)(test->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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, sdata, &cfg);
244
    event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
245
    status |= l3g4200d_lld_read_fifo_src_register(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(stream, &result);
254
  } else {
255
    aosTestFailed(stream, &result);
256
  }
257

    
258
  fifo = 0x4A;
259
  status |= l3g4200d_lld_write_fifo_ctrl_register(((aos_test_l3g4200ddata_t*)(test->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(((aos_test_l3g4200ddata_t*)(test->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(((aos_test_l3g4200ddata_t*)(test->data))->l3gd, sdata, &cfg);
266
    event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
267
    status |= l3g4200d_lld_read_fifo_src_register(((aos_test_l3g4200ddata_t*)(test->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
    aosTestPassed(stream, &result);
276
  } else {
277
    aosTestFailed(stream, &result);
278
  }
279

    
280
  chEvtUnregister(((aos_test_l3g4200ddata_t*)(test->data))->src, &el);
281
  aosThdMSleep(10);
282

    
283
  aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(L3G4200DDriver));
284

    
285
  return result;
286
}
287

    
288
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
289