Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.78 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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

    
21
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_LIS331DLH) && (AMIROLLD_CFG_LIS331DLH == 1)) || defined(__DOXYGEN__)
22

    
23
#include <ut_alld_LIS331DLH_v1.h>
24

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

    
49
/**
50
 * @brief   LIS331DLH 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 utAlldLis331dlhFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
58
{
59
  aosDbgCheck(ut->data != NULL && ((ut_lis331dlhdata_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
  uint8_t errors = 0;
68
  uint8_t reg1;
69
  int16_t sdata[3];
70
  uint8_t status_reg;
71
  lis331dlh_lld_int_cfg_t intcfg;
72
  event_listener_t el;
73
  eventmask_t event_mask;
74
  uint8_t success = 0;
75

    
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(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_WHO_AM_I, &data, 1);
83
  if (status == APAL_STATUS_SUCCESS &&
84
      data == LIS331DLH_LLD_WHO_AM_I) {
85
    aosUtPassed(stream, &result);
86
  } else {
87
    chprintf(stream, "\tfailed\n");
88
    aosUtFailedMsg(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(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 1);
94
  if (status == APAL_STATUS_SUCCESS) {
95
    aosUtPassed(stream, &result);
96
  } else {
97
    aosUtFailed(stream, &result);
98
  }
99

    
100
  chprintf(stream, "read register...\n");
101
  status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, &data, 1);
102
  if (status == APAL_STATUS_SUCCESS && data == write_data[0]) {
103
    aosUtPassed(stream, &result);
104
  } else {
105
    chprintf(stream, "\tfailed\n");
106
    aosUtFailedMsg(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(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 5);
112
  if (status == APAL_STATUS_SUCCESS) {
113
    aosUtPassed(stream, &result);
114
  } else {
115
    aosUtFailed(stream, &result);
116
  }
117

    
118
  chprintf(stream, "read multiple registers...\n");
119
  status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->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
    aosUtPassed(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
    aosUtFailedMsg(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(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
139
  if (status == APAL_STATUS_SUCCESS) {
140
    aosUtPassed(stream, &result);
141
  } else {
142
    aosUtFailed(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(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
153
  reg1 = cfg.data[0];
154
  lis331dlh_lld_read_config(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
155
  if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) {
156
    aosUtPassed(stream, &result);
157
  } else {
158
    aosUtFailed(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(((ut_lis331dlhdata_t*)(ut->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
    aosUtPassed(stream, &result);
170
  } else {
171
    aosUtFailed(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(((ut_lis331dlhdata_t*)(ut->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
    aosUtPassed(stream, &result);
183
  } else {
184
    aosUtFailed(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(((ut_lis331dlhdata_t*)(ut->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
    aosUtPassed(stream, &result);
196
  } else {
197
    aosUtFailed(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(((ut_lis331dlhdata_t*)(ut->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
    aosUtPassed(stream, &result);
209
  } else {
210
    aosUtFailed(stream, &result);
211
  }
212

    
213
  chprintf(stream, "read status register...\n");
214
  status = lis331dlh_lld_read_status_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, &status_reg);
215
  if (status == APAL_STATUS_SUCCESS) {
216
    aosUtPassed(stream, &result);
217
  } else {
218
    aosUtFailed(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(((ut_lis331dlhdata_t*)(ut->data))->lisd, &intcfg, LIS331DLH_LLD_INT1);
226
  aosThdSSleep(1);
227
  chEvtRegister(((ut_lis331dlhdata_t*)(ut->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(((ut_lis331dlhdata_t*)(ut->data))->lisd, sdata, &cfg);
233
    status = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_INT1_SOURCE, &data, 1);
234
    eventflags_t flags = chEvtGetAndClearFlags(&el);
235
    if (event_mask == EVENT_MASK(3) && (flags & ((ut_lis331dlhdata_t*)(ut->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(((ut_lis331dlhdata_t*)(ut->data))->src, &el);
245
  if (status == APAL_STATUS_SUCCESS && success > 0) {
246
    aosUtPassed(stream, &result);
247
  } else {
248
    aosUtFailed(stream, &result);
249
  }
250

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

    
253
  return result;
254
}
255

    
256
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_LIS331DLH) && (AMIROLLD_CFG_LIS331DLH == 1) */
257