amiro-os / unittests / periphery-lld / src / ut_alld_l3g4200d.c @ f3ac1c96
History | View | Annotate | Download (11.2 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 84f0ce9e | Thomas Schöpping | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 | e545e620 | Thomas Schöpping | |
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_l3g4200d.h> |
||
20 | |||
21 | #if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_L3G4200D)) || defined(__DOXYGEN__) |
||
22 | |||
23 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
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 | e545e620 | Thomas Schöpping | #include <aos_debug.h> |
47 | #include <chprintf.h> |
||
48 | #include <aos_thread.h> |
||
49 | #include <alld_l3g4200d.h> |
||
50 | |||
51 | /**
|
||
52 | * @brief L3G4200D unit test function.
|
||
53 | *
|
||
54 | * @param[in] stream Stream for input/output.
|
||
55 | * @param[in] ut Unit test object.
|
||
56 | *
|
||
57 | * @return Unit test result value.
|
||
58 | */
|
||
59 | aos_utresult_t utAlldL3g4200dFunc(BaseSequentialStream* stream, aos_unittest_t* ut) |
||
60 | { |
||
61 | aosDbgCheck(ut->data != NULL && ((ut_l3g4200ddata_t*)(ut->data)) != NULL); |
||
62 | |||
63 | // local variables
|
||
64 | aos_utresult_t result = {0, 0}; |
||
65 | uint32_t status; |
||
66 | uint8_t data = 0;
|
||
67 | uint8_t write_data[5];
|
||
68 | uint8_t read_data[5];
|
||
69 | int16_t sdata[3];
|
||
70 | uint8_t status_reg; |
||
71 | eventmask_t event_mask; |
||
72 | bool success = false; |
||
73 | uint8_t fifo = 0x5F;
|
||
74 | event_listener_t el; |
||
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 = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_WHO_AM_I, &data, 1);
|
||
83 | if(status == APAL_STATUS_SUCCESS && data == L3G4200D_LLD_WHO_AM_I){
|
||
84 | aosUtPassed(stream, &result); |
||
85 | } else {
|
||
86 | aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
|
||
87 | } |
||
88 | |||
89 | chprintf(stream, "write register...\n");
|
||
90 | status = l3g4200d_lld_write_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 1);
|
||
91 | if (status == APAL_STATUS_SUCCESS) {
|
||
92 | aosUtPassed(stream, &result); |
||
93 | } else {
|
||
94 | aosUtFailed(stream, &result); |
||
95 | } |
||
96 | |||
97 | chprintf(stream, "read register...\n");
|
||
98 | status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, &data, 1);
|
||
99 | if (status == APAL_STATUS_SUCCESS && data == write_data[0]) { |
||
100 | aosUtPassed(stream, &result); |
||
101 | } else {
|
||
102 | aosUtFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
|
||
103 | } |
||
104 | |||
105 | chprintf(stream, "write multiple registers...\n");
|
||
106 | status = l3g4200d_lld_write_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, write_data, 5);
|
||
107 | if (status == APAL_STATUS_SUCCESS) {
|
||
108 | aosUtPassed(stream, &result); |
||
109 | } else {
|
||
110 | aosUtFailed(stream, &result); |
||
111 | } |
||
112 | |||
113 | chprintf(stream, "read multiple registers...\n");
|
||
114 | status = l3g4200d_lld_read_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, read_data, 5);
|
||
115 | uint8_t errors = 0;
|
||
116 | for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) { |
||
117 | if (read_data[dataIdx] != write_data[dataIdx]) {
|
||
118 | ++errors; |
||
119 | } |
||
120 | } |
||
121 | if (status == APAL_STATUS_SUCCESS && errors == 0) { |
||
122 | aosUtPassed(stream, &result); |
||
123 | } else {
|
||
124 | for (uint8_t dataIdx = 0; dataIdx < 5; dataIdx++) { |
||
125 | chprintf(stream, "\t\tStatus: %d, CTRL_REG%d: %d, write_data: %d\n", status, dataIdx+1, read_data[dataIdx], write_data[dataIdx]); |
||
126 | } |
||
127 | aosUtFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
|
||
128 | } |
||
129 | |||
130 | chprintf(stream, "read config...\n");
|
||
131 | l3g4200d_lld_cfg_t cfg; |
||
132 | status = l3g4200d_lld_read_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &cfg); |
||
133 | if (status == APAL_STATUS_SUCCESS) {
|
||
134 | aosUtPassed(stream, &result); |
||
135 | } else {
|
||
136 | aosUtFailed(stream, &result); |
||
137 | } |
||
138 | |||
139 | chprintf(stream, "write config...\n");
|
||
140 | 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; |
||
141 | //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;
|
||
142 | cfg.registers.ctrl_reg3 = 0x07;
|
||
143 | cfg.registers.ctrl_reg5 |= L3G4200D_LLD_FIFO_EN; |
||
144 | status = l3g4200d_lld_write_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, cfg); |
||
145 | uint8_t reg1 = cfg.data[0];
|
||
146 | status |= l3g4200d_lld_read_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &cfg); |
||
147 | if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) { |
||
148 | aosUtPassed(stream, &result); |
||
149 | } else {
|
||
150 | aosUtFailed(stream, &result); |
||
151 | } |
||
152 | |||
153 | chprintf(stream, "read gyro data for five seconds...\n");
|
||
154 | status = APAL_STATUS_OK; |
||
155 | for (uint8_t i = 0; i < 5; ++i) { |
||
156 | status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg); |
||
157 | chprintf(stream, "\t\tX = %6d\tY = %6d\tZ = %6d\n", sdata[0], sdata[1], sdata[2]); |
||
158 | aosThdSSleep(1);
|
||
159 | } |
||
160 | if (status == APAL_STATUS_SUCCESS) {
|
||
161 | aosUtPassed(stream, &result); |
||
162 | } else {
|
||
163 | aosUtFailed(stream, &result); |
||
164 | } |
||
165 | |||
166 | chprintf(stream, "read X axis for five seconds...\n");
|
||
167 | status = APAL_STATUS_SUCCESS; |
||
168 | for (uint32_t i = 0; i <= 5; i++) { |
||
169 | status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_X_AXIS, &cfg);
|
||
170 | chprintf(stream, "\t\tX = %6d\n", sdata[0]); |
||
171 | aosThdSSleep(1);
|
||
172 | } |
||
173 | if (status == APAL_STATUS_SUCCESS) {
|
||
174 | aosUtPassed(stream, &result); |
||
175 | } else {
|
||
176 | aosUtFailed(stream, &result); |
||
177 | } |
||
178 | |||
179 | chprintf(stream, "read Y axis for five seconds...\n");
|
||
180 | status = APAL_STATUS_SUCCESS; |
||
181 | for (uint32_t i = 0; i <= 5; i++) { |
||
182 | status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Y_AXIS, &cfg);
|
||
183 | chprintf(stream, "\t\tY = %6d\n", sdata[0]); |
||
184 | aosThdSSleep(1);
|
||
185 | } |
||
186 | if (status == APAL_STATUS_SUCCESS) {
|
||
187 | aosUtPassed(stream, &result); |
||
188 | } else {
|
||
189 | aosUtFailed(stream, &result); |
||
190 | } |
||
191 | |||
192 | chprintf(stream, "read Z axis for five seconds...\n");
|
||
193 | status = APAL_STATUS_SUCCESS; |
||
194 | for (uint32_t i = 0; i <= 5; i++) { |
||
195 | status |= l3g4200d_lld_read_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &(sdata[0]), L3G4200D_LLD_Z_AXIS, &cfg);
|
||
196 | chprintf(stream, "\t\tZ = %6d\n", sdata[0]); |
||
197 | aosThdSSleep(1);
|
||
198 | } |
||
199 | if (status == APAL_STATUS_SUCCESS) {
|
||
200 | aosUtPassed(stream, &result); |
||
201 | } else {
|
||
202 | aosUtFailed(stream, &result); |
||
203 | } |
||
204 | aosThdMSleep(10);
|
||
205 | |||
206 | chprintf(stream, "read status register...\n");
|
||
207 | status = l3g4200d_lld_read_status_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &status_reg); |
||
208 | if (status == APAL_STATUS_SUCCESS) {
|
||
209 | aosUtPassed(stream, &result); |
||
210 | } else {
|
||
211 | aosUtFailed(stream, &result); |
||
212 | } |
||
213 | |||
214 | chprintf(stream, "read interrupt config...\n");
|
||
215 | l3g4200d_lld_int_cfg_t int_cfg; |
||
216 | status = l3g4200d_lld_read_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &int_cfg); |
||
217 | if (status == APAL_STATUS_SUCCESS) {
|
||
218 | aosUtPassed(stream, &result); |
||
219 | } else {
|
||
220 | aosUtFailed(stream, &result); |
||
221 | } |
||
222 | |||
223 | chprintf(stream, "write interrupt config...\n");
|
||
224 | int_cfg.registers.int1_tsh_xh = 10;
|
||
225 | status = l3g4200d_lld_write_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, int_cfg); |
||
226 | l3g4200d_lld_int_cfg_t int_cfg2; |
||
227 | status |= l3g4200d_lld_read_int_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, &int_cfg2); |
||
228 | if (status == APAL_STATUS_SUCCESS && int_cfg.registers.int1_tsh_xh == 10) { |
||
229 | aosUtPassed(stream, &result); |
||
230 | } else {
|
||
231 | aosUtFailed(stream, &result); |
||
232 | } |
||
233 | |||
234 | chprintf(stream, "interrupt test: read fifo until empty...\n");
|
||
235 | chEvtRegister(((ut_l3g4200ddata_t*)(ut->data))->src, &el, 0);
|
||
236 | status = l3g4200d_lld_write_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,fifo); |
||
237 | fifo = 0;
|
||
238 | status |= l3g4200d_lld_read_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo); |
||
239 | status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg); |
||
240 | chEvtGetAndClearFlags(&el); |
||
241 | aosThdSSleep(1);
|
||
242 | chEvtGetAndClearFlags(&el); |
||
243 | success = false;
|
||
244 | for (uint8_t i = 0; i < 200; i++) { |
||
245 | status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg); |
||
246 | event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
|
||
247 | status |= l3g4200d_lld_read_fifo_src_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo); |
||
248 | if (event_mask != 0 && ((fifo & L3G4200D_LLD_EMPTY) || fifo == 0)) { |
||
249 | success = true;
|
||
250 | break;
|
||
251 | } |
||
252 | aosThdMSleep(1);
|
||
253 | } |
||
254 | if (status == APAL_STATUS_SUCCESS && success) {
|
||
255 | aosUtPassed(stream, &result); |
||
256 | } else {
|
||
257 | aosUtFailed(stream, &result); |
||
258 | } |
||
259 | |||
260 | fifo = 0x4A;
|
||
261 | status |= l3g4200d_lld_write_fifo_ctrl_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,fifo); |
||
262 | 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; |
||
263 | cfg.registers.ctrl_reg3 = 0x04;
|
||
264 | status |= l3g4200d_lld_write_config(((ut_l3g4200ddata_t*)(ut->data))->l3gd, cfg); |
||
265 | chprintf(stream, "interrupt test: wait until wtm reached...\n");
|
||
266 | for (uint8_t i = 0; i < 200; i++) { |
||
267 | status |= l3g4200d_lld_read_all_data(((ut_l3g4200ddata_t*)(ut->data))->l3gd, sdata, &cfg); |
||
268 | event_mask = chEvtWaitAnyTimeout(~0, TIME_IMMEDIATE);
|
||
269 | status |= l3g4200d_lld_read_fifo_src_register(((ut_l3g4200ddata_t*)(ut->data))->l3gd,&fifo); |
||
270 | if (event_mask != 0 && (fifo & L3G4200D_LLD_WTM)) { |
||
271 | success = true;
|
||
272 | break;
|
||
273 | } |
||
274 | aosThdMSleep(10);
|
||
275 | } |
||
276 | if (status == APAL_STATUS_SUCCESS && success) {
|
||
277 | aosUtPassed(stream, &result); |
||
278 | } else {
|
||
279 | aosUtFailed(stream, &result); |
||
280 | } |
||
281 | |||
282 | chEvtUnregister(((ut_l3g4200ddata_t*)(ut->data))->src, &el); |
||
283 | aosThdMSleep(10);
|
||
284 | |||
285 | aosUtInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(L3G4200DDriver)); |
||
286 | |||
287 | return result;
|
||
288 | } |
||
289 | |||
290 | #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_L3G4200D) */ |
||
291 |