Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / bq27500_v1 / aos_test_bq27500.c @ 23ec8223

History | View | Annotate | Download (27.073 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 96621a83 Thomas Schöpping
Copyright (C) 2016..2020  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_bq27500.h>
21 e545e620 Thomas Schöpping
22 4c72a54c Thomas Schöpping
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
23 e545e620 Thomas Schöpping
24
#include <string.h>
25 f3ac1c96 Thomas Schöpping
26
/******************************************************************************/
27
/* LOCAL DEFINITIONS                                                          */
28
/******************************************************************************/
29 e545e620 Thomas Schöpping
30 57a5ea60 Marc Rothmann
// change saved unseal keys to test bruteforcing
31 4c72a54c Thomas Schöpping
#if defined(BQ27500_TEST_BRUTEFORCE)
32 57a5ea60 Marc Rothmann
33 7de0cc90 Thomas Schöpping
#if defined(BQ27500_LLD_DEFAULT_UNSEAL_KEY0)
34 57a5ea60 Marc Rothmann
#undef BQ27500_LLD_DEFAULT_UNSEAL_KEY0
35
#define BQ27500_LLD_DEFAULT_UNSEAL_KEY0 0x1234
36 7de0cc90 Thomas Schöpping
#endif /* defined(BQ27500_LLD_DEFAULT_UNSEAL_KEY0) */
37 57a5ea60 Marc Rothmann
38 7de0cc90 Thomas Schöpping
#if defined(BQ27500_LLD_DEFAULT_UNSEAL_KEY1)
39 57a5ea60 Marc Rothmann
#undef BQ27500_LLD_DEFAULT_UNSEAL_KEY1
40
#define BQ27500_LLD_DEFAULT_UNSEAL_KEY1 0x5678
41 7de0cc90 Thomas Schöpping
#endif /* defined(BQ27500_LLD_DEFAULT_UNSEAL_KEY1) */
42 57a5ea60 Marc Rothmann
43 4c72a54c Thomas Schöpping
#endif /* defined(BQ27500_TEST_BRUTEFORCE) */
44 57a5ea60 Marc Rothmann
45 f3ac1c96 Thomas Schöpping
/******************************************************************************/
46
/* EXPORTED VARIABLES                                                         */
47
/******************************************************************************/
48
49
/******************************************************************************/
50
/* LOCAL TYPES                                                                */
51
/******************************************************************************/
52
53
/******************************************************************************/
54
/* LOCAL VARIABLES                                                            */
55
/******************************************************************************/
56
57
/******************************************************************************/
58
/* LOCAL FUNCTIONS                                                            */
59
/******************************************************************************/
60
61 57a5ea60 Marc Rothmann
bq27500_lld_control_status_t _try_unseal(BQ27500Driver* driver, uint16_t key0, uint16_t key1, apalTime_t timeout) {
62
  uint16_t dst;
63
  bq27500_lld_control_status_t ctrl;
64
  bq27500_lld_send_ctnl_data(driver, key1, timeout);
65
  aosThdUSleep(1);
66
  bq27500_lld_send_ctnl_data(driver, key0, timeout);
67
  aosThdUSleep(1);
68
  bq27500_lld_sub_command_call(driver, BQ27500_LLD_SUB_CMD_CONTROL_STATUS, timeout);
69
  aosThdUSleep(1);
70
  bq27500_lld_std_command(driver, BQ27500_LLD_STD_CMD_Control, &dst, timeout);
71
  bq27500_lld_sub_command_read(driver, &ctrl.value, timeout);
72
  return ctrl;
73
}
74
75
uint8_t _bruteforce_sealed_key_bitflips(BaseSequentialStream* stream, BQ27500Driver* driver, uint16_t key0, uint16_t key1, apalTime_t timeout) {
76
  bq27500_lld_control_status_t ctrl;
77
  uint16_t k0;
78
  uint16_t k1 = key1;
79
  for (uint8_t i = 0; i < 16; i++) {
80
    k0 = key0 ^ (1 << i); // flip bit i
81
    ctrl = _try_unseal(driver, k0, k1, timeout);
82
    if (ctrl.content.ss == 0x0) {
83
      chprintf(stream, "\t\tSUCCESS!\n");
84
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", k0, k1);
85
      return 1;
86
    }
87
  }
88
  k0 = key0;
89
  for (uint8_t i = 0; i < 16; i++) {
90
    k1 = key1 ^ (1 << i); // flip bit i
91
    ctrl = _try_unseal(driver, k0, k1, timeout);
92
    if (ctrl.content.ss == 0x0) {
93
      chprintf(stream, "\t\tSUCCESS!\n");
94
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", k0, k1);
95
      return 1;
96
    }
97
  }
98
  return 0;
99
}
100
101
void _bruteforce_sealed_key(BaseSequentialStream* stream, BQ27500Driver* driver, apalTime_t timeout) {
102
  chprintf(stream, "start bruteforcing sealed keys...\n");
103
  bq27500_lld_control_status_t ctrl;
104
  uint16_t key0_reversed = 0x7236;
105
  uint16_t key1_reversed = 0x1404;
106
  uint16_t key0 = BQ27500_LLD_DEFAULT_UNSEAL_KEY0;
107
  uint16_t key1 = BQ27500_LLD_DEFAULT_UNSEAL_KEY1;
108
  uint16_t k0 = key0;
109
  uint16_t k1 = key1;
110
111
  // testing default keys in different orders
112
  chprintf(stream, "\ttry reversed byte order and different key order...\n");
113
  // default unseal keys
114
  ctrl = _try_unseal(driver, k0, k1, timeout);
115
  if (ctrl.content.ss == 0x0) {
116
    chprintf(stream, "\t\tSUCCESS!\n");
117
    chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", k0, k1);
118
    return;
119
  }
120
  // default unseal keys in reversed order
121
  ctrl = _try_unseal(driver, k1, k0, timeout);
122
  if (ctrl.content.ss == 0x0) {
123
    chprintf(stream, "\t\tSUCCESS!\n");
124
    chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", k0, k1);
125
    return;
126
  }
127
  // byte reversed keys
128
  k0 = key0_reversed;
129
  k1 = key1_reversed;
130
  ctrl = _try_unseal(driver, k0, k1, timeout);
131
  if (ctrl.content.ss == 0x0) {
132
    chprintf(stream, "\t\tSUCCESS!\n");
133
    chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", k0, k1);
134
    return;
135
  }
136
  // byte reversed keys in reversed order
137
  ctrl = _try_unseal(driver, k1, k0, timeout);
138
  if (ctrl.content.ss == 0x0) {
139
    chprintf(stream, "\t\tSUCCESS!\n");
140
    chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", k0, k1);
141
    return;
142
  }
143
  chprintf(stream, "\t\tfailed\n");
144
145
146
  // testing single bit flips of the default keys in different orders
147
  chprintf(stream, "\ttry single bit flips of default keys...\n");
148
  // default unseal keys
149
  uint8_t result = 0;
150
  result = _bruteforce_sealed_key_bitflips(stream, driver, key0, key1, timeout);
151
  if (result == 1) {
152
    return;
153
  }
154
  // default unseal keys in reversed order
155
  result = _bruteforce_sealed_key_bitflips(stream, driver, key1, key0, timeout);
156
  if (result == 1) {
157
    return;
158
  }
159
  // byte reversed keys
160
  result = _bruteforce_sealed_key_bitflips(stream, driver, key0_reversed, key1_reversed, timeout);
161
  if (result == 1) {
162
    return;
163
  }
164
  // byte reversed keys in reversed order
165
  result = _bruteforce_sealed_key_bitflips(stream, driver, key1_reversed, key0_reversed, timeout);
166
  if (result == 1) {
167
    return;
168
  }
169
  chprintf(stream, "\t\tfailed\n");
170
171
172
  // bruteforcing one of the keys, assuming only one of them was changed
173
  chprintf(stream, "\ttry bruteforcing a single key...\n");
174
  // default unseal key0
175
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
176
    ctrl = _try_unseal(driver, key0, i, timeout);
177
    if (ctrl.content.ss == 0x0) {
178
      chprintf(stream, "\t\tSUCCESS!\n");
179
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", key0, i);
180
      return;
181
    }
182
  }
183
  chprintf(stream, "\t\tkey failed. 1/8\n");
184
  // reversed unseal key0
185
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
186
    ctrl = _try_unseal(driver, key0_reversed, i, timeout);
187
    if (ctrl.content.ss == 0x0) {
188
      chprintf(stream, "\t\tSUCCESS!\n");
189
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", key0_reversed, i);
190
      return;
191
    }
192
  }
193
  chprintf(stream, "\t\tkey failed. 2/8\n");
194
  // default unseal key0 in reversed order
195
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
196
    ctrl = _try_unseal(driver, i, key0, timeout);
197
    if (ctrl.content.ss == 0x0) {
198
      chprintf(stream, "\t\tSUCCESS!\n");
199
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", i, key0);
200
      return;
201
    }
202
  }
203
  chprintf(stream, "\t\tkey failed. 3/8\n");
204
  // reversed unseal key0 in reversed order
205
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
206
    ctrl = _try_unseal(driver, i, key0_reversed, timeout);
207
    if (ctrl.content.ss == 0x0) {
208
      chprintf(stream, "\t\tSUCCESS!\n");
209
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", i, key0_reversed);
210
      return;
211
    }
212
  }
213
  chprintf(stream, "\t\tkey failed. 4/8\n");
214
  // default unseal key1
215
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
216
    ctrl = _try_unseal(driver, i, key1, timeout);
217
    if (ctrl.content.ss == 0x0) {
218
      chprintf(stream, "\t\tSUCCESS!\n");
219
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", i, key1);
220
      return;
221
    }
222
  }
223
  chprintf(stream, "\t\tkey failed. 5/8\n");
224
  // reversed unseal key1
225
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
226
    ctrl = _try_unseal(driver, i, key1_reversed, timeout);
227
    if (ctrl.content.ss == 0x0) {
228
      chprintf(stream, "\t\tSUCCESS!\n");
229
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", i, key1_reversed);
230
      return;
231
    }
232
  }
233
  chprintf(stream, "\t\tkey failed. 6/8\n");
234
  // default unseal key1 in reversed order
235
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
236
    ctrl = _try_unseal(driver, key1, i, timeout);
237
    if (ctrl.content.ss == 0x0) {
238
      chprintf(stream, "\t\tSUCCESS!\n");
239
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", key1, i);
240
      return;
241
    }
242
  }
243
  chprintf(stream, "\t\tkey failed. 7/8\n");
244
  // reversed unseal key1 in reversed order
245
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
246
    ctrl = _try_unseal(driver, key1_reversed, i, timeout);
247
    if (ctrl.content.ss == 0x0) {
248
      chprintf(stream, "\t\tSUCCESS!\n");
249
      chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", key1_reversed, i);
250
      return;
251
    }
252
  }
253
  chprintf(stream, "\t\tkey failed. 8/8\n");
254
  chprintf(stream, "\t\tfailed\n");
255
256
257
  // full bruteforce
258
  chprintf(stream, "\tbruteforcing both keys...\t");
259
  for (uint32_t i = 0; i <= 0xFFFF; i++) {
260
    chprintf(stream, "\t\ti: %u\n", i);
261
    for (uint32_t j = 0; j <= 0xFFFF; j++) {
262
      ctrl = _try_unseal(driver, i, j, timeout);
263
      if (ctrl.content.ss == 0x0) {
264
        chprintf(stream, "\t\tSUCCESS!\n");
265
        chprintf(stream, "\t\tkey0: 0x%X, key1: 0x%X\n", i, j);
266
        return;
267
      }
268
    }
269
  }
270
271
  chprintf(stream, "\t\tfailed, no keys could be found");
272
}
273
274 f3ac1c96 Thomas Schöpping
/******************************************************************************/
275
/* EXPORTED FUNCTIONS                                                         */
276
/******************************************************************************/
277
278 4c72a54c Thomas Schöpping
aos_testresult_t aosTestBq27500Func(BaseSequentialStream* stream, const aos_test_t* test)
279 e545e620 Thomas Schöpping
{
280 4c72a54c Thomas Schöpping
  aosDbgCheck(test->data != NULL && ((aos_test_bq27500data_t*)(test->data))->driver != NULL);
281 e545e620 Thomas Schöpping
282
  // local variables
283 ce12e797 Thomas Schöpping
  aos_testresult_t result;
284 b23ca7cc Thomas Schöpping
  int32_t status;
285 e545e620 Thomas Schöpping
  bq27500_lld_batlow_t bl;
286
  bq27500_lld_batgood_t bg;
287
  uint16_t dst;
288
  bq27500_lld_flags_t flags;
289
  uint16_t subdata = 0;
290
  uint8_t original_length;
291
  char original_name[8+1];
292
  uint8_t val = 0x00;
293
  uint8_t block[32];
294
  char new_name[] = "test";
295
  uint8_t new_lenght;
296
  char name[8+1] = {'\0'};
297
  uint8_t sum = 0;
298
  bool success;
299
  bool success2;
300
301 ce12e797 Thomas Schöpping
  aosTestResultInit(&result);
302
303 e545e620 Thomas Schöpping
  chprintf(stream, "read battery low gpio...\n");
304 4c72a54c Thomas Schöpping
  status = bq27500_lld_read_batlow(((aos_test_bq27500data_t*)test->data)->driver, &bl);
305 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tbattery low: 0x%X\n", bl);
306
  if (status == APAL_STATUS_SUCCESS) {
307 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
308 e545e620 Thomas Schöpping
  } else {
309 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
310 e545e620 Thomas Schöpping
  }
311
312
  chprintf(stream, "read battery good gpio...\n");
313 4c72a54c Thomas Schöpping
  status = bq27500_lld_read_batgood(((aos_test_bq27500data_t*)test->data)->driver, &bg);
314 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tbattery good: 0x%X\n", bg);
315
  if (status == APAL_STATUS_SUCCESS) {
316 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
317 e545e620 Thomas Schöpping
  } else {
318 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
319 e545e620 Thomas Schöpping
  }
320
321
  chprintf(stream, "std command FLAGS...\n");
322 4c72a54c Thomas Schöpping
  status = bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Flags, &flags.value, ((aos_test_bq27500data_t*)test->data)->timeout);
323 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tflags: 0x%04X\n", flags.value);
324
  chprintf(stream, "\t\tbattery detected: 0x%X\n", flags.content.bat_det);
325
  chprintf(stream, "\t\tbattery fully charged: 0x%X\n", flags.content.fc);
326
  if (status == APAL_STATUS_SUCCESS) {
327 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
328 e545e620 Thomas Schöpping
  } else {
329 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
330 e545e620 Thomas Schöpping
  }
331
332
  chprintf(stream, "std command CTNL...\n");
333 4c72a54c Thomas Schöpping
  status = bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Control, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
334 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
335 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
336 e545e620 Thomas Schöpping
  } else {
337 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
338 e545e620 Thomas Schöpping
  }
339
340
  chprintf(stream, "sub command: CTRL Status...\n");
341 57a5ea60 Marc Rothmann
  aosThdUSleep(1);
342 4c72a54c Thomas Schöpping
  status = bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_CONTROL_STATUS, ((aos_test_bq27500data_t*)test->data)->timeout);
343 57a5ea60 Marc Rothmann
  aosThdUSleep(1);
344 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Control, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
345 57a5ea60 Marc Rothmann
  aosThdUSleep(1);
346 e545e620 Thomas Schöpping
  bq27500_lld_control_status_t ctrl;
347 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_read(((aos_test_bq27500data_t*)test->data)->driver, &ctrl.value, ((aos_test_bq27500data_t*)test->data)->timeout);
348 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tdst: 0x%X\n", ctrl.value);
349
  chprintf(stream, "\t\tsleep: 0x%X\n", ctrl.content.sleep);
350
  chprintf(stream, "\t\thibernate: 0x%X\n", ctrl.content.hibernate);
351
  if (status == APAL_STATUS_SUCCESS) {
352 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
353 e545e620 Thomas Schöpping
  } else {
354 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
355 e545e620 Thomas Schöpping
  }
356
357
  chprintf(stream, "sub command: firmware version...\n");
358 4c72a54c Thomas Schöpping
  status = bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_FW_VERSION, ((aos_test_bq27500data_t*)test->data)->timeout);
359 e545e620 Thomas Schöpping
  aosThdMSleep(1);
360
  bq27500_lld_version_t version;
361 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_read(((aos_test_bq27500data_t*)test->data)->driver, &version.value, ((aos_test_bq27500data_t*)test->data)->timeout);
362 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tfirmware version: %X%X-%X%X\n", version.content.major_high, version.content.major_low, version.content.minor_high, version.content.minor_low);
363
  if (status == APAL_STATUS_SUCCESS) {
364 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
365 e545e620 Thomas Schöpping
  } else {
366 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
367 e545e620 Thomas Schöpping
  }
368
369
  chprintf(stream, "sub command: hardware version...\n");
370 4c72a54c Thomas Schöpping
  status = bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_HW_VERSION, ((aos_test_bq27500data_t*)test->data)->timeout);
371 e545e620 Thomas Schöpping
  aosThdMSleep(1);
372 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_read(((aos_test_bq27500data_t*)test->data)->driver, &version.value, ((aos_test_bq27500data_t*)test->data)->timeout);
373 e545e620 Thomas Schöpping
  chprintf(stream, "\t\thardware version: %X%X-%X%X\n", version.content.major_high, version.content.major_low, version.content.minor_high, version.content.minor_low);
374
  if (status == APAL_STATUS_SUCCESS) {
375 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
376 e545e620 Thomas Schöpping
  } else {
377 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
378 e545e620 Thomas Schöpping
  }
379
380
  chprintf(stream, "ext command: device name length...\n");
381 4c72a54c Thomas Schöpping
  status = bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DeviceNameLength, BQ27500_LLD_EXT_CMD_READ, &original_length, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
382 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\tdevice name length: %d\n", original_length);
383 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && original_length <= 8) {
384 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
385 e545e620 Thomas Schöpping
  } else {
386 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
387 e545e620 Thomas Schöpping
    original_length = 8;
388
  }
389
390
  chprintf(stream, "ext command: device name (read)...\n");
391 4c72a54c Thomas Schöpping
  status = bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DeviceName, BQ27500_LLD_EXT_CMD_READ, (uint8_t*)original_name, original_length, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
392 e545e620 Thomas Schöpping
  original_name[original_length] = '\0';
393
  chprintf(stream, "\t\tdevice name: %s\n", original_name);
394
  if (status == APAL_STATUS_SUCCESS) {
395 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
396 e545e620 Thomas Schöpping
  } else {
397 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
398 e545e620 Thomas Schöpping
  }
399
400
  chprintf(stream, "battery info std commands...\n");
401 4c72a54c Thomas Schöpping
  status = bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Temperature, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
402 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\ttemperature: %fK (%fC)\n", (float)dst/10.0f, (float)dst/10.0f-273.5f);
403 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_FullAvailableCapacity, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
404 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\tfull available capacity: %umAh\n", dst);
405 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_FullChargeCapacity, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
406 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\tfull charge capacity: %umAh\n", dst);
407 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_RemainingCapacity, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
408 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\tremaining capacity: %umAh\n", dst);
409 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Voltage, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
410 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\tvoltage: %umV\n", dst);
411 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_AverageCurrent, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
412 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\taverage current: %dmA\n", (int8_t)dst);
413 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_AveragePower, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
414 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\taverage power: %dmW\n", (int8_t)dst);
415 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_StateOfCharge, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
416 a193bcf1 Thomas Schöpping
  chprintf(stream, "\t\tstate of charge: %u%%\n", dst);
417 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
418 e545e620 Thomas Schöpping
  if (dst != (uint16_t)~0) {
419 a193bcf1 Thomas Schöpping
    chprintf(stream, "\t\ttime to full: %umin\n", dst);
420 e545e620 Thomas Schöpping
  } else {
421 a193bcf1 Thomas Schöpping
    chprintf(stream, "\t\ttime to full: (not charging)\n", dst);
422 e545e620 Thomas Schöpping
  }
423 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_TimeToEmpty, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
424 e545e620 Thomas Schöpping
  if (dst != (uint16_t)~0) {
425 a193bcf1 Thomas Schöpping
    chprintf(stream, "\t\ttime to empty: %umin\n", dst);
426 e545e620 Thomas Schöpping
  } else {
427 a193bcf1 Thomas Schöpping
    chprintf(stream, "\t\ttime to empty: (not discharging)\n", dst);
428 e545e620 Thomas Schöpping
  }
429
  if (status == APAL_STATUS_SUCCESS) {
430 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
431 e545e620 Thomas Schöpping
  } else {
432 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
433 e545e620 Thomas Schöpping
  }
434
435
  chprintf(stream, "check sealed state...\n");
436 4c72a54c Thomas Schöpping
  status = bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Control, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
437
  status |= bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_CONTROL_STATUS, ((aos_test_bq27500data_t*)test->data)->timeout);
438 e545e620 Thomas Schöpping
  aosThdMSleep(1);
439 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_read(((aos_test_bq27500data_t*)test->data)->driver, &ctrl.value, ((aos_test_bq27500data_t*)test->data)->timeout);
440 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tsealed: 0x%X\n", ctrl.content.ss);
441
  chprintf(stream, "\t\tfull access sealed: 0x%X\n", ctrl.content.fas);
442
  if (status == APAL_STATUS_SUCCESS) {
443 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
444 e545e620 Thomas Schöpping
  } else {
445 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
446 e545e620 Thomas Schöpping
  }
447
448
  chprintf(stream, "unseale...\n");
449 4c72a54c Thomas Schöpping
  status = bq27500_lld_send_ctnl_data(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_DEFAULT_UNSEAL_KEY1, ((aos_test_bq27500data_t*)test->data)->timeout);
450 57a5ea60 Marc Rothmann
  aosThdMSleep(1);
451 4c72a54c Thomas Schöpping
  status |= bq27500_lld_send_ctnl_data(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_DEFAULT_UNSEAL_KEY0, ((aos_test_bq27500data_t*)test->data)->timeout);
452 57a5ea60 Marc Rothmann
  aosThdMSleep(1);
453 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_CONTROL_STATUS, ((aos_test_bq27500data_t*)test->data)->timeout);
454 57a5ea60 Marc Rothmann
  aosThdMSleep(1);
455 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Control, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
456
  status |= bq27500_lld_sub_command_read(((aos_test_bq27500data_t*)test->data)->driver, &ctrl.value, ((aos_test_bq27500data_t*)test->data)->timeout);
457 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && ctrl.content.ss == 0x0) {
458 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
459 e545e620 Thomas Schöpping
  } else {
460 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
461
    _bruteforce_sealed_key(stream, ((aos_test_bq27500data_t*)test->data)->driver, ((aos_test_bq27500data_t*)test->data)->timeout);
462 e545e620 Thomas Schöpping
  }
463
464
  chprintf(stream, "read device name from data flash...\n");
465 4c72a54c Thomas Schöpping
  status = bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockDataControl, BQ27500_LLD_EXT_CMD_WRITE, &val, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
466 e545e620 Thomas Schöpping
  aosThdMSleep(50);
467
  val = 0x30;
468 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DataFlashClass, BQ27500_LLD_EXT_CMD_WRITE, &val, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
469 e545e620 Thomas Schöpping
  aosThdMSleep(50);
470
  val = 0;
471 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DataFlashBlock, BQ27500_LLD_EXT_CMD_WRITE, &val, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
472 e545e620 Thomas Schöpping
  aosThdMSleep(50);
473 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockData, BQ27500_LLD_EXT_CMD_READ, (uint8_t*)block, original_length, 13, ((aos_test_bq27500data_t*)test->data)->timeout);
474 e545e620 Thomas Schöpping
  block[original_length] = '\0';
475
  chprintf(stream, "\t\tdevice name: %s\n", block);
476
  if (status == APAL_STATUS_SUCCESS && (strcmp((char*)block, (char*)original_name) == 0)) {
477 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
478 e545e620 Thomas Schöpping
  } else {
479
    chprintf(stream, "\t\tread data: ");
480
    for (uint8_t blockIdx = 0; blockIdx < original_length; blockIdx++) {
481
      chprintf(stream, "0x%02X\n", block[blockIdx]);
482
    }
483 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
484 e545e620 Thomas Schöpping
  }
485
486
  chprintf(stream, "change device name in data flash to \"test\"...\n");
487 4c72a54c Thomas Schöpping
  status = bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockData, BQ27500_LLD_EXT_CMD_WRITE, (uint8_t*)new_name, 5, 13, ((aos_test_bq27500data_t*)test->data)->timeout);
488 e545e620 Thomas Schöpping
  aosThdMSleep(50);
489 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockData, BQ27500_LLD_EXT_CMD_READ, (uint8_t*)block, 32, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
490 e545e620 Thomas Schöpping
  // compute blockdata checksum
491
  status |= bq27500_lld_compute_blockdata_checksum(block, &sum);
492
  // write checksum to BlockDataChecksum, triggering the write of BlackData
493 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockDataCheckSum, BQ27500_LLD_EXT_CMD_WRITE, &sum, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
494 e545e620 Thomas Schöpping
  aosThdMSleep(50);
495 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DeviceNameLength, BQ27500_LLD_EXT_CMD_READ, &new_lenght, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
496 e545e620 Thomas Schöpping
  aosThdMSleep(50);
497
  // read out device name, to see if changing it was successfull
498 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DeviceName, BQ27500_LLD_EXT_CMD_READ, (uint8_t*)name, new_lenght, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
499 e545e620 Thomas Schöpping
  name[new_lenght] = '\0';
500
  chprintf(stream, "\t\tdevice name: %s\n", name);
501
  success = (strcmp(name, new_name) == 0);
502
503
  // change device name back to original name
504
  val = 0x00;
505 4c72a54c Thomas Schöpping
  status = bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockDataControl, BQ27500_LLD_EXT_CMD_WRITE, &val, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
506 e545e620 Thomas Schöpping
  aosThdMSleep(50);
507
  val = 0x30;
508 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DataFlashClass, BQ27500_LLD_EXT_CMD_WRITE, &val, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
509 e545e620 Thomas Schöpping
  aosThdMSleep(50);
510
  val = 0;
511 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DataFlashBlock, BQ27500_LLD_EXT_CMD_WRITE, &val, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
512 e545e620 Thomas Schöpping
  aosThdMSleep(50);
513 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockData, BQ27500_LLD_EXT_CMD_WRITE, (uint8_t*)original_name, 7, 13, ((aos_test_bq27500data_t*)test->data)->timeout);
514 e545e620 Thomas Schöpping
  aosThdMSleep(50);
515 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockData, BQ27500_LLD_EXT_CMD_READ, (uint8_t*)block, 32, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
516 e545e620 Thomas Schöpping
  // compute blockdata checksum
517
  sum = 0;
518
  status |= bq27500_lld_compute_blockdata_checksum(block, &sum);
519
  // write checksum to BlockDataChecksum, triggering the write of BlackData
520 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_BlockDataCheckSum, BQ27500_LLD_EXT_CMD_WRITE, &sum, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
521 e545e620 Thomas Schöpping
  aosThdMSleep(1000);
522 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DeviceNameLength, BQ27500_LLD_EXT_CMD_READ, &original_length, 1, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
523 e545e620 Thomas Schöpping
  aosThdMSleep(50);
524
  // read device name, to see if changing it back to the original name was successfull
525 4c72a54c Thomas Schöpping
  status |= bq27500_lld_ext_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_EXT_CMD_DeviceName, BQ27500_LLD_EXT_CMD_READ, (uint8_t*)name, original_length, 0, ((aos_test_bq27500data_t*)test->data)->timeout);
526 e545e620 Thomas Schöpping
  success2 = (strcmp(name, original_name) == 0);
527
  name[original_length] = '\0';
528
  chprintf(stream, "\t\tchanged back to name: %s, original_name: %s\n", name, original_name);
529
  if (status == APAL_STATUS_OK && ((success && success2) || (ctrl.content.sleep == 0))) {
530 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
531 e545e620 Thomas Schöpping
  } else {
532 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X, changing: 0x%X - changing back: 0x%X\n", status, success, success2);
533 e545e620 Thomas Schöpping
  }
534
535
  chprintf(stream, "seal...\n");
536 4c72a54c Thomas Schöpping
  status = bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Control, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
537 e545e620 Thomas Schöpping
  aosThdMSleep(50);
538 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_SEALED, ((aos_test_bq27500data_t*)test->data)->timeout);
539 e545e620 Thomas Schöpping
  aosThdMSleep(50);
540 4c72a54c Thomas Schöpping
  status |= bq27500_lld_std_command(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_STD_CMD_Control, &dst, ((aos_test_bq27500data_t*)test->data)->timeout);
541 e545e620 Thomas Schöpping
  aosThdMSleep(50);
542 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_call(((aos_test_bq27500data_t*)test->data)->driver, BQ27500_LLD_SUB_CMD_CONTROL_STATUS, ((aos_test_bq27500data_t*)test->data)->timeout);
543 e545e620 Thomas Schöpping
  aosThdMSleep(50);
544 4c72a54c Thomas Schöpping
  status |= bq27500_lld_sub_command_read(((aos_test_bq27500data_t*)test->data)->driver, &ctrl.value, ((aos_test_bq27500data_t*)test->data)->timeout);
545 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && ctrl.content.ss == 0x1) {
546 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
547 e545e620 Thomas Schöpping
  } else {
548
    chprintf(stream, "\tfailed (0x%X)\n", status);
549 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X, ctrl 0x%X\n", status, subdata);
550 e545e620 Thomas Schöpping
    ++result.failed;
551
  }
552
553 4c72a54c Thomas Schöpping
  aosTestInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(BQ27500Driver));
554 e545e620 Thomas Schöpping
555
  return result;
556
}
557
558 4c72a54c Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */