Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_lis331dlh.c @ 8a93b752

History | View | Annotate | Download (9.699 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 <ut_alld_lis331dlh.h>
20

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

    
23
/******************************************************************************/
24
/* LOCAL DEFINITIONS                                                          */
25
/******************************************************************************/
26

    
27
/******************************************************************************/
28
/* EXPORTED VARIABLES                                                         */
29
/******************************************************************************/
30

    
31
/******************************************************************************/
32
/* LOCAL TYPES                                                                */
33
/******************************************************************************/
34

    
35
/******************************************************************************/
36
/* LOCAL VARIABLES                                                            */
37
/******************************************************************************/
38

    
39
/******************************************************************************/
40
/* LOCAL FUNCTIONS                                                            */
41
/******************************************************************************/
42

    
43
/******************************************************************************/
44
/* EXPORTED FUNCTIONS                                                         */
45
/******************************************************************************/
46

    
47
/**
48
 * @brief   LIS331DLH unit test function.
49
 *
50
 * @param[in] stream  Stream for input/output.
51
 * @param[in] ut      Unit test object.
52
 *
53
 * @return            Unit test result value.
54
 */
55
aos_utresult_t utAlldLis331dlhFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
56
{
57
  aosDbgCheck(ut->data != NULL && ((ut_lis331dlhdata_t*)(ut->data)) != NULL);
58

    
59
  // local variables
60
  aos_utresult_t result = {0, 0};
61
  uint32_t status;
62
  uint8_t data = 0;
63
  uint8_t write_data[5];
64
  uint8_t read_data[5];
65
  uint8_t errors = 0;
66
  uint8_t reg1;
67
  int16_t sdata[3];
68
  uint8_t status_reg;
69
  lis331dlh_lld_int_cfg_t intcfg;
70
  event_listener_t el;
71
  eventmask_t event_mask;
72
  uint8_t success = 0;
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 = lis331dlh_lld_read_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_WHO_AM_I, &data, 1);
81
  if (status == APAL_STATUS_SUCCESS &&
82
      data == LIS331DLH_LLD_WHO_AM_I) {
83
    aosUtPassed(stream, &result);
84
  } else {
85
    chprintf(stream, "\tfailed\n");
86
    aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
87
    ++result.failed;
88
  }
89

    
90
  chprintf(stream, "write register...\n");
91
  status = lis331dlh_lld_write_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 1);
92
  if (status == APAL_STATUS_SUCCESS) {
93
    aosUtPassed(stream, &result);
94
  } else {
95
    aosUtFailed(stream, &result);
96
  }
97

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

    
108
  chprintf(stream, "write multiple registers...\n");
109
  status = lis331dlh_lld_write_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 5);
110
  if (status == APAL_STATUS_SUCCESS) {
111
    aosUtPassed(stream, &result);
112
  } else {
113
    aosUtFailed(stream, &result);
114
  }
115

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

    
134
  chprintf(stream, "read config...\n");
135
  lis331dlh_lld_cfg_t cfg;
136
  status = lis331dlh_lld_read_config(((ut_lis331dlhdata_t*)(ut->data))->lisd, &cfg);
137
  if (status == APAL_STATUS_SUCCESS) {
138
    aosUtPassed(stream, &result);
139
  } else {
140
    aosUtFailed(stream, &result);
141
  }
142

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

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

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

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

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

    
211
  chprintf(stream, "read status register...\n");
212
  status = lis331dlh_lld_read_status_register(((ut_lis331dlhdata_t*)(ut->data))->lisd, &status_reg);
213
  if (status == APAL_STATUS_SUCCESS) {
214
    aosUtPassed(stream, &result);
215
  } else {
216
    aosUtFailed(stream, &result);
217
  }
218

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

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

    
251
  return result;
252
}
253

    
254
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_LIS331DLH) */
255