Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.779 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
#include <ut_alld_LIS331DLH_v1.h>
21

    
22
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_LIS331DLH) && (AMIROLLD_CFG_LIS331DLH == 1)) || 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 unit test function.
50
 *
51
 * @param[in] stream  Stream for input/output.
52
 * @param[in] ut      Unit test object.
53
 *
54
 * @return            Unit test result value.
55
 */
56
aos_utresult_t utAlldLis331dlhFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
57
{
58
  aosDbgCheck(ut->data != NULL && ((ut_lis331dlhdata_t*)(ut->data)) != NULL);
59

    
60
  // local variables
61
  aos_utresult_t result = {0, 0};
62
  uint32_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
  for (uint8_t dataIdx = 0; dataIdx < 4; dataIdx++) {
76
    write_data[dataIdx] = (dataIdx+1)*11;
77
  }
78
  write_data[4] = 0;
79

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
252
  return result;
253
}
254

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