amiro-os / test / periphery-lld / LIS331DLH_v1 / aos_test_LIS331DLH.c @ f5363588
History | View | Annotate | Download (9.866 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 | ddf34c3d | Thomas Schöpping | #include <amiroos.h> |
20 | 4c72a54c | Thomas Schöpping | #include <aos_test_LIS331DLH.h> |
21 | e545e620 | Thomas Schöpping | |
22 | 4c72a54c | Thomas Schöpping | #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
23 | ddf34c3d | Thomas Schöpping | |
24 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
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 | e545e620 | Thomas Schöpping | |
48 | /**
|
||
49 | 4c72a54c | Thomas Schöpping | * @brief LIS331DLH test function.
|
50 | e545e620 | Thomas Schöpping | *
|
51 | * @param[in] stream Stream for input/output.
|
||
52 | 4c72a54c | Thomas Schöpping | * @param[in] test Test object.
|
53 | e545e620 | Thomas Schöpping | *
|
54 | 4c72a54c | Thomas Schöpping | * @return Test result value.
|
55 | e545e620 | Thomas Schöpping | */
|
56 | 4c72a54c | Thomas Schöpping | aos_testresult_t aosTestLis331dlhFunc(BaseSequentialStream* stream, const aos_test_t* test)
|
57 | e545e620 | Thomas Schöpping | { |
58 | 4c72a54c | Thomas Schöpping | aosDbgCheck(test->data != NULL && ((aos_test_lis331dlhdata_t*)(test->data)) != NULL); |
59 | e545e620 | Thomas Schöpping | |
60 | // local variables
|
||
61 | ce12e797 | Thomas Schöpping | aos_testresult_t result; |
62 | 4c72a54c | Thomas Schöpping | int32_t status; |
63 | e545e620 | Thomas Schöpping | 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 | ce12e797 | Thomas Schöpping | aosTestResultInit(&result); |
76 | e545e620 | Thomas Schöpping | 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 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_WHO_AM_I, &data, 1);
|
83 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS &&
|
84 | data == LIS331DLH_LLD_WHO_AM_I) { |
||
85 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
86 | e545e620 | Thomas Schöpping | } else {
|
87 | chprintf(stream, "\tfailed\n");
|
||
88 | 4c72a54c | Thomas Schöpping | aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
|
89 | e545e620 | Thomas Schöpping | ++result.failed; |
90 | } |
||
91 | |||
92 | chprintf(stream, "write register...\n");
|
||
93 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_write_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 1);
|
94 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
95 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
96 | e545e620 | Thomas Schöpping | } else {
|
97 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
98 | e545e620 | Thomas Schöpping | } |
99 | |||
100 | chprintf(stream, "read register...\n");
|
||
101 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, &data, 1);
|
102 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS && data == write_data[0]) { |
103 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
104 | e545e620 | Thomas Schöpping | } else {
|
105 | chprintf(stream, "\tfailed\n");
|
||
106 | 4c72a54c | Thomas Schöpping | aosTestFailedMsg(stream, &result, "0x%08X, data: %d\n", status, data);
|
107 | e545e620 | Thomas Schöpping | ++result.failed; |
108 | } |
||
109 | |||
110 | chprintf(stream, "write multiple registers...\n");
|
||
111 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_write_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, write_data, 5);
|
112 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
113 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
114 | e545e620 | Thomas Schöpping | } else {
|
115 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
116 | e545e620 | Thomas Schöpping | } |
117 | |||
118 | chprintf(stream, "read multiple registers...\n");
|
||
119 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_read_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, read_data, 5);
|
120 | e545e620 | Thomas Schöpping | 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 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
128 | e545e620 | Thomas Schöpping | } 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 | 4c72a54c | Thomas Schöpping | aosTestFailedMsg(stream, &result, "0x%08X, errors: %d\n", status, errors);
|
134 | e545e620 | Thomas Schöpping | } |
135 | |||
136 | chprintf(stream, "read config...\n");
|
||
137 | lis331dlh_lld_cfg_t cfg; |
||
138 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_read_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg); |
139 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
140 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
141 | e545e620 | Thomas Schöpping | } else {
|
142 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
143 | e545e620 | Thomas Schöpping | } |
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 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_write_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg); |
153 | e545e620 | Thomas Schöpping | reg1 = cfg.data[0];
|
154 | 4c72a54c | Thomas Schöpping | lis331dlh_lld_read_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &cfg); |
155 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS && cfg.data[0] == reg1) { |
156 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
157 | e545e620 | Thomas Schöpping | } else {
|
158 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
159 | e545e620 | Thomas Schöpping | } |
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 | 4c72a54c | Thomas Schöpping | status |= lis331dlh_lld_read_all_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, sdata, &cfg); |
165 | e545e620 | Thomas Schöpping | 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 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
170 | e545e620 | Thomas Schöpping | } else {
|
171 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
172 | e545e620 | Thomas Schöpping | } |
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 | 4c72a54c | Thomas Schöpping | status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_X_AXIS, &cfg);
|
178 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tX = %6d\n", sdata[0]); |
179 | aosThdSSleep(1);
|
||
180 | } |
||
181 | if (status == APAL_STATUS_SUCCESS) {
|
||
182 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
183 | e545e620 | Thomas Schöpping | } else {
|
184 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
185 | e545e620 | Thomas Schöpping | } |
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 | 4c72a54c | Thomas Schöpping | status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Y_AXIS, &cfg);
|
191 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tY = %6d\n", sdata[0]); |
192 | aosThdSSleep(1);
|
||
193 | } |
||
194 | if (status == APAL_STATUS_SUCCESS) {
|
||
195 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
196 | e545e620 | Thomas Schöpping | } else {
|
197 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
198 | e545e620 | Thomas Schöpping | } |
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 | 4c72a54c | Thomas Schöpping | status |= lis331dlh_lld_read_data(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &(sdata[0]), LIS331DLH_LLD_Z_AXIS, &cfg);
|
204 | e545e620 | Thomas Schöpping | chprintf(stream, "\t\tZ = %6d\n", sdata[0]); |
205 | aosThdSSleep(1);
|
||
206 | } |
||
207 | if (status == APAL_STATUS_SUCCESS) {
|
||
208 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
209 | e545e620 | Thomas Schöpping | } else {
|
210 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
211 | e545e620 | Thomas Schöpping | } |
212 | |||
213 | chprintf(stream, "read status register...\n");
|
||
214 | 4c72a54c | Thomas Schöpping | status = lis331dlh_lld_read_status_register(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &status_reg); |
215 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS) {
|
216 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
217 | e545e620 | Thomas Schöpping | } else {
|
218 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
219 | e545e620 | Thomas Schöpping | } |
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 | 4c72a54c | Thomas Schöpping | lis331dlh_lld_write_int_config(((aos_test_lis331dlhdata_t*)(test->data))->lisd, &intcfg, LIS331DLH_LLD_INT1); |
226 | e545e620 | Thomas Schöpping | aosThdSSleep(1);
|
227 | 4c72a54c | Thomas Schöpping | chEvtRegister(((aos_test_lis331dlhdata_t*)(test->data))->src, &el, 3);
|
228 | e545e620 | Thomas Schöpping | 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 | 4c72a54c | Thomas Schöpping | 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 | e545e620 | Thomas Schöpping | eventflags_t flags = chEvtGetAndClearFlags(&el); |
235 | 4c72a54c | Thomas Schöpping | if (event_mask == EVENT_MASK(3) && (flags & ((aos_test_lis331dlhdata_t*)(test->data))->evtflags)) { |
236 | e545e620 | Thomas Schöpping | ++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 | 1e5f7648 | Thomas Schöpping | event_mask = chEvtWaitOneTimeout(EVENT_MASK(3), TIME_IMMEDIATE);
|
244 | 4c72a54c | Thomas Schöpping | chEvtUnregister(((aos_test_lis331dlhdata_t*)(test->data))->src, &el); |
245 | e545e620 | Thomas Schöpping | if (status == APAL_STATUS_SUCCESS && success > 0) { |
246 | 4c72a54c | Thomas Schöpping | aosTestPassed(stream, &result); |
247 | e545e620 | Thomas Schöpping | } else {
|
248 | 4c72a54c | Thomas Schöpping | aosTestFailed(stream, &result); |
249 | e545e620 | Thomas Schöpping | } |
250 | |||
251 | 4c72a54c | Thomas Schöpping | aosTestInfoMsg(stream, "driver object memory footprint: %u bytes\n", sizeof(LIS331DLHDriver)); |
252 | e545e620 | Thomas Schöpping | |
253 | return result;
|
||
254 | } |
||
255 | |||
256 | 4c72a54c | Thomas Schöpping | #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |