Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / LIS331DLH_v1 / aos_test_LIS331DLH.c @ 7387fb42

History | View | Annotate | Download (9.866 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_LIS331DLH.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   LIS331DLH 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 aosTestLis331dlhFunc(BaseSequentialStream* stream, const aos_test_t* test)
57
{
58
  aosDbgCheck(test->data != NULL && ((aos_test_lis331dlhdata_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
  uint8_t errors = 0;
67
  uint8_t reg1;
68
  int16_t sdata[3];
69
  uint8_t status_reg;
70
  lis331dlh_lld_int_cfg_t intcfg;
71
  event_listener_t el;
72
  eventmask_t event_mask;
73
  uint8_t success = 0;
74

    
75
  aosTestResultInit(&result);
76
  for (uint8_t dataIdx = 0; dataIdx < 4; dataIdx++) {
77
    write_data[dataIdx] = (dataIdx+1)*11;
78
  }
79
  write_data[4] = 0;
80

    
81
  chprintf(stream, "check identity...\n");
82
  status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_WHO_AM_I, &data, 1);
83
  if (status == APAL_STATUS_SUCCESS &&
84
      data == LIS331DLH_LLD_WHO_AM_I) {
85
    aosTestPassed(stream, &result);
86
  } else {
87
    chprintf(stream, "\tfailed\n");
88
    aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
89
    ++result.failed;
90
  }
91

    
92
  chprintf(stream, "write register...\n");
93
  status = lis331dlh_lld_write_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 1);
94
  if (status == APAL_STATUS_SUCCESS) {
95
    aosTestPassed(stream, &result);
96
  } else {
97
    aosTestFailed(stream, &result);
98
  }
99

    
100
  chprintf(stream, "read register...\n");
101
  status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, &data, 1);
102
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
103
    aosTestPassed(stream, &result);
104
  } else {
105
    chprintf(stream, "\tfailed\n");
106
    aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
107
    ++result.failed;
108
  }
109

    
110
  chprintf(stream, "write multiple registers...\n");
111
  status = lis331dlh_lld_write_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 5);
112
  if (status == APAL_STATUS_SUCCESS) {
113
    aosTestPassed(stream, &result);
114
  } else {
115
    aosTestFailed(stream, &result);
116
  }
117

    
118
  chprintf(stream, "read multiple registers...\n");
119
  status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, read_data, 5);
120
  for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
121
    if (read_data[dataIdx] != write_data[dataIdx]) {
122
      ++errors;
123
    }
124
  }
125
  if (status == APAL_STATUS_SUCCESS &&
126
      errors == 0) {
127
    aosTestPassed(stream, &result);
128
  } else {
129
    chprintf(stream, "\tfailed\n");
130
    for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) {
131
      chprintf(stream, "\t\tStatus: %d, CTRL_REG%d: %d, write_data: %d\n", status, dataIdx+1, read_data[dataIdx], write_data[dataIdx]);
132
    }
133
    aosTestFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
134
  }
135

    
136
  chprintf(stream, "read config...\n");
137
  lis331dlh_lld_cfg_t cfg;
138
  status = lis331dlh_lld_read_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg);
139
  if (status == APAL_STATUS_SUCCESS) {
140
    aosTestPassed(stream, &result);
141
  } else {
142
    aosTestFailed(stream, &result);
143
  }
144

    
145
  chprintf(stream, "write config...\n");
146
  cfg.registers.ctrl_reg1 = LIS331DLH_LLD_PM_ODR |
147
      LIS331DLH_LLD_DR_1000HZ_780LP |
148
      LIS331DLH_LLD_X_AXIS_ENABLE |
149
      LIS331DLH_LLD_Y_AXIS_ENABLE |
150
      LIS331DLH_LLD_Z_AXIS_ENABLE;
151
  cfg.registers.ctrl_reg3 = 0x00;
152
  status = lis331dlh_lld_write_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg);
153
  reg1 = cfg.data[0];
154
  lis331dlh_lld_read_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg);
155
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
156
    aosTestPassed(stream, &result);
157
  } else {
158
    aosTestFailed(stream, &result);
159
  }
160

    
161
  chprintf(stream, "read acceleration for five seconds...\n");
162
  status = APAL_STATUS_SUCCESS;
163
  for (uint8_t i = 0; i < 5; ++i) {
164
    status |= lis331dlh_lld_read_all_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, sdata, &cfg);
165
    chprintf(stream, "\t\tX = %6d\tY = %6d\tZ = %6d\n", sdata[0], sdata[1], sdata[2]);
166
    aosThdSSleep(1);
167
  }
168
  if (status == APAL_STATUS_SUCCESS) {
169
    aosTestPassed(stream, &result);
170
  } else {
171
    aosTestFailed(stream, &result);
172
  }
173

    
174
  chprintf(stream, "read X acceleration for five seconds...\n");
175
  status = APAL_STATUS_SUCCESS;
176
  for (uint8_t i = 0; i < 5; ++i) {
177
    status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_X_AXIS, &cfg);
178
    chprintf(stream, "\t\tX = %6d\n", sdata[0]);
179
    aosThdSSleep(1);
180
  }
181
  if (status == APAL_STATUS_SUCCESS) {
182
    aosTestPassed(stream, &result);
183
  } else {
184
    aosTestFailed(stream, &result);
185
  }
186

    
187
  chprintf(stream, "read Y acceleration for five seconds...\n");
188
  status = APAL_STATUS_SUCCESS;
189
  for (uint8_t i = 0; i < 5; ++i) {
190
    status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Y_AXIS, &cfg);
191
    chprintf(stream, "\t\tY = %6d\n", sdata[0]);
192
    aosThdSSleep(1);
193
  }
194
  if (status == APAL_STATUS_SUCCESS) {
195
    aosTestPassed(stream, &result);
196
  } else {
197
    aosTestFailed(stream, &result);
198
  }
199

    
200
  chprintf(stream, "read Z acceleration for five seconds...\n");
201
  status = APAL_STATUS_SUCCESS;
202
  for (uint8_t i = 0; i < 5; ++i) {
203
    status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Z_AXIS, &cfg);
204
    chprintf(stream, "\t\tZ = %6d\n", sdata[0]);
205
    aosThdSSleep(1);
206
  }
207
  if (status == APAL_STATUS_SUCCESS) {
208
    aosTestPassed(stream, &result);
209
  } else {
210
    aosTestFailed(stream, &result);
211
  }
212

    
213
  chprintf(stream, "read status register...\n");
214
  status = lis331dlh_lld_read_status_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &status_reg);
215
  if (status == APAL_STATUS_SUCCESS) {
216
    aosTestPassed(stream, &result);
217
  } else {
218
    aosTestFailed(stream, &result);
219
  }
220

    
221
  chprintf(stream, "interrupt...\n");
222
  intcfg.cfg_reg = LIS331DLH_LLD_INT_CFG_X_HIGH_ENABLE | LIS331DLH_LLD_INT_CFG_Y_HIGH_ENABLE | LIS331DLH_LLD_INT_CFG_Z_HIGH_ENABLE;
223
  intcfg.threshold = 10;
224
  intcfg.duration = 1;
225
  lis331dlh_lld_write_int_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &intcfg, LIS331DLH_LLD_INT1);
226
  aosThdSSleep(1);
227
  chEvtRegister(((aos_test_lis331dlhdata_t*)(test->data))->src, &el, 3);
228
  chprintf(stream, "\t\tmove the AMiRo now to generate interrupts\n");
229
  aosThdSSleep(1);
230
  for (uint8_t i = 0; i < 10; i++) {
231
    event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
232
    status |= lis331dlh_lld_read_all_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, sdata, &cfg);
233
    status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_INT1_SOURCE, &data, 1);
234
    eventflags_t flags = chEvtGetAndClearFlags(&el);
235
    if (event_mask == EVENT_MASK(3) && (flags & ((aos_test_lis331dlhdata_t*)(test->data))->evtflags)) {
236
      ++success;
237
      chprintf(stream, "\t\tX = %6d INTERRUPT\n", sdata[0]);
238
    } else {
239
      chprintf(stream, "\t\tX = %6d\n", sdata[0]);
240
    }
241
    aosThdSSleep(1);
242
  }
243
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
244
  chEvtUnregister(((aos_test_lis331dlhdata_t*)(test->data))->src, &el);
245
  if (status == APAL_STATUS_SUCCESS && success > 0) {
246
    aosTestPassed(stream, &result);
247
  } else {
248
    aosTestFailed(stream, &result);
249
  }
250

    
251
  aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(LIS331DLHDriver));
252

    
253
  return result;
254
}
255

    
256
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */