Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / MPR121_v1 / aos_test_MPR121.c @ 98949060

History | View | Annotate | Download (9.633 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_MPR121.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 e545e620 Thomas Schöpping
28
#define INTERRUPT_EVENT_ID            1
29
30 f3ac1c96 Thomas Schöpping
/******************************************************************************/
31
/* EXPORTED VARIABLES                                                         */
32
/******************************************************************************/
33
34
/******************************************************************************/
35
/* LOCAL TYPES                                                                */
36
/******************************************************************************/
37
38
/******************************************************************************/
39
/* LOCAL VARIABLES                                                            */
40
/******************************************************************************/
41
42
/******************************************************************************/
43
/* LOCAL FUNCTIONS                                                            */
44
/******************************************************************************/
45
46
/******************************************************************************/
47
/* EXPORTED FUNCTIONS                                                         */
48
/******************************************************************************/
49
50 4c72a54c Thomas Schöpping
aos_testresult_t aosTestMpr121Func(BaseSequentialStream* stream, const aos_test_t* test)
51 e545e620 Thomas Schöpping
{
52 4c72a54c Thomas Schöpping
  aosDbgCheck(test->data != NULL && ((aos_test_mpr121data_t*)test->data)->mprd != NULL);
53 e545e620 Thomas Schöpping
54
  // local variables
55 ce12e797 Thomas Schöpping
  aos_testresult_t result;
56 b23ca7cc Thomas Schöpping
  int32_t status;
57 e545e620 Thomas Schöpping
  mpr121_lld_config_t cfg;
58 4c72a54c Thomas Schöpping
  uint8_t data = 0;
59
  uint8_t wdata = 0;
60
  uint8_t rdata = 0;
61
  uint16_t edata[4] = {0};
62 e545e620 Thomas Schöpping
  uint8_t tstate = 0;
63
  event_listener_t event_listener;
64
  aos_timestamp_t tstart, tcurrent, tend;
65
66 ce12e797 Thomas Schöpping
  aosTestResultInit(&result);
67
68 e545e620 Thomas Schöpping
  chprintf(stream, "soft reset...\n");
69 4c72a54c Thomas Schöpping
  status = mpr121_lld_soft_reset(((aos_test_mpr121data_t*)test->data)->mprd, ((aos_test_mpr121data_t*)test->data)->timeout);
70
  status |= mpr121_lld_read_config(((aos_test_mpr121data_t*)test->data)->mprd, &cfg, ((aos_test_mpr121data_t*)test->data)->timeout);
71 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && cfg.registers.ele_config == 0) {
72 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
73 e545e620 Thomas Schöpping
  } else {
74 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
75 e545e620 Thomas Schöpping
  }
76
77
  chprintf(stream, "read register...\n");
78 4c72a54c Thomas Schöpping
  status = mpr121_lld_read_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_CONFIG_2, 0, 1, &data, ((aos_test_mpr121data_t*)test->data)->timeout);
79 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tdata: 0x%02X\n", data);
80
  if (status == APAL_STATUS_SUCCESS && data == (MPR121_LLD_CDT_0_5 | MPR121_LLD_ESI_16)) {
81 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
82 e545e620 Thomas Schöpping
  } else {
83 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
84 e545e620 Thomas Schöpping
  }
85
86
  chprintf(stream, "write register...\n");
87
  wdata = MPR121_LLD_CDT_32 | MPR121_LLD_SFI_10 | MPR121_LLD_ESI_16;
88 4c72a54c Thomas Schöpping
  status = mpr121_lld_write_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_CONFIG_2, 0, 1, &wdata, ((aos_test_mpr121data_t*)test->data)->timeout);
89 e545e620 Thomas Schöpping
  aosThdMSleep(500);
90 4c72a54c Thomas Schöpping
  status |= mpr121_lld_read_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_CONFIG_2, 0, 1, &rdata, ((aos_test_mpr121data_t*)test->data)->timeout);
91 e545e620 Thomas Schöpping
  chprintf(stream, "\t\tdata: 0x%02X\n", rdata);
92 4c72a54c Thomas Schöpping
  status |= mpr121_lld_write_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_CONFIG_2, 0, 1, &data, ((aos_test_mpr121data_t*)test->data)->timeout);
93 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && rdata == wdata) {
94 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
95 e545e620 Thomas Schöpping
  } else {
96 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
97 e545e620 Thomas Schöpping
  }
98
99
  chprintf(stream, "read config...\n");
100 4c72a54c Thomas Schöpping
  status = mpr121_lld_read_config(((aos_test_mpr121data_t*)test->data)->mprd, &cfg, ((aos_test_mpr121data_t*)test->data)->timeout);
101 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && cfg.registers.config_1 == 0x10 && cfg.registers.config_2 == 0x24) {
102 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
103 e545e620 Thomas Schöpping
  } else {
104
    chprintf(stream, "\t\tconfig 1: 0x%02X\n", cfg.registers.config_1);
105
    chprintf(stream, "\t\tconfig 2: 0x%02X\n", cfg.registers.config_2);
106 4c72a54c Thomas Schöpping
    mpr121_lld_read_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_CONFIG_1, 0, 1, &rdata, ((aos_test_mpr121data_t*)test->data)->timeout);
107 e545e620 Thomas Schöpping
    chprintf(stream, "\t\tconfig 1: 0x%02X\n", rdata);
108 4c72a54c Thomas Schöpping
    mpr121_lld_read_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_CONFIG_2, 0, 1, &rdata, ((aos_test_mpr121data_t*)test->data)->timeout);
109 e545e620 Thomas Schöpping
    chprintf(stream, "\t\tconfig 2: 0x%02X\n", rdata);
110
    for (uint8_t valueIdx = 0; valueIdx < 8; valueIdx++) {
111
      chprintf(stream, "\t\tdata %d: 0x%02X\n", valueIdx, cfg.values[valueIdx]);
112
    }
113
    chprintf(stream, "ele_config: 0x%02X\n", cfg.registers.ele_config);
114
    chprintf(stream, "config_1: 0x%02X\n", cfg.registers.config_1);
115
    chprintf(stream, "config_2: 0x%02X\n", cfg.registers.config_2);
116
    chprintf(stream, "target_level: 0x%02X\n", cfg.registers.target_level);
117
    chprintf(stream, "low_side_limit: 0x%02X\n", cfg.registers.low_side_limit);
118
    chprintf(stream, "up_side_limit: 0x%02X\n", cfg.registers.up_side_limit);
119
    chprintf(stream, "auto_config_2: 0x%02X\n", cfg.registers.auto_config_2);
120
    chprintf(stream, "auto_config_1: 0x%02X\n", cfg.registers.auto_config_1);
121 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
122 e545e620 Thomas Schöpping
  }
123
124
  chprintf(stream, "write config...\n");
125
  cfg.registers.ele_config = MPR121_LLD_CL_ON_ALL | MPR121_LLD_ELEPROX_0 | 4;
126
  cfg.registers.config_1 = MPR121_LLD_FFI_18 | 16;
127
  cfg.registers.config_2 = MPR121_LLD_CDT_1 | MPR121_LLD_SFI_10 | MPR121_LLD_ESI_32;
128
  cfg.registers.up_side_limit = 0x96u;
129
  cfg.registers.low_side_limit = 0x58u;
130
  cfg.registers.target_level = 0x68u;
131
  cfg.registers.auto_config_1 = MPR121_LLD_FFI_18 | MPR121_LLD_RETRY_2 | MPR121_LLD_BVA_ON_ALL | MPR121_LLD_AC_RECONF_EN;
132 4c72a54c Thomas Schöpping
  status = mpr121_lld_write_config(((aos_test_mpr121data_t*)test->data)->mprd, cfg, ((aos_test_mpr121data_t*)test->data)->timeout);
133 e545e620 Thomas Schöpping
  aosThdMSleep(500);
134 4c72a54c Thomas Schöpping
  status |= mpr121_lld_read_config(((aos_test_mpr121data_t*)test->data)->mprd, &cfg, ((aos_test_mpr121data_t*)test->data)->timeout);
135 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS && cfg.registers.up_side_limit == 0x96u) {
136 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
137 e545e620 Thomas Schöpping
  } else {
138 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
139 e545e620 Thomas Schöpping
  }
140
141
  chprintf(stream, "read filtered data for ten seconds...\n");
142
  chprintf(stream, "(values should change if you touch the sensors now)\n");
143
  for (uint8_t i = 0; i < 10; i++) {
144 4c72a54c Thomas Schöpping
    status = mpr121_lld_read_filtered_data(((aos_test_mpr121data_t*)test->data)->mprd, 0, 4, edata, ((aos_test_mpr121data_t*)test->data)->timeout);
145 e545e620 Thomas Schöpping
    chprintf(stream, "\t\t0x%04X | 0x%04X | 0x%04X | 0x%04X\n", edata[0], edata[1], edata[2], edata[3]);
146
    aosThdSSleep(1);
147
  }
148
  if (status == APAL_STATUS_SUCCESS) {
149 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
150 e545e620 Thomas Schöpping
  } else {
151 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
152 e545e620 Thomas Schöpping
  }
153
154
  chprintf(stream, "test interrupts...\n");
155
  status = AOS_OK;
156 4c72a54c Thomas Schöpping
  chEvtRegister(((aos_test_mpr121data_t*)test->data)->evtsource, &event_listener, INTERRUPT_EVENT_ID);
157 e545e620 Thomas Schöpping
  aosSysGetUptime(&tstart);
158
  tend = tstart + (30 * MICROSECONDS_PER_SECOND);
159
  do {
160
    aosSysGetUptime(&tcurrent);
161
    const aos_timestamp_t ttimeout = MICROSECONDS_PER_SECOND - ((tcurrent - tstart) % MICROSECONDS_PER_SECOND);
162 1e5f7648 Thomas Schöpping
    const eventmask_t emask = chEvtWaitOneTimeout(EVENT_MASK(INTERRUPT_EVENT_ID), chTimeUS2I(ttimeout));
163 e545e620 Thomas Schöpping
    const eventflags_t eflags = chEvtGetAndClearFlags(&event_listener);
164 4c72a54c Thomas Schöpping
    if (emask == EVENT_MASK(INTERRUPT_EVENT_ID) && eflags == ((aos_test_mpr121data_t*)test->data)->evtflags) {
165 e545e620 Thomas Schöpping
      // interrupt detected
166 4c72a54c Thomas Schöpping
      status |= mpr121_lld_read_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_TOUCH_STATUS, 0, 1, &tstate, ((aos_test_mpr121data_t*)test->data)->timeout);
167 e545e620 Thomas Schöpping
      chprintf(stream, "\t\tinterrupt detected\n");
168
      chprintf(stream, "\t\ttstate: %X %X %X %X\n", tstate & 1, (tstate >> 1) & 1, (tstate >> 2) & 1, (tstate >> 3) & 1);
169
    }
170
    else if (emask == 0) {
171
      // timeout
172 4c72a54c Thomas Schöpping
      status |= mpr121_lld_read_register(((aos_test_mpr121data_t*)test->data)->mprd, MPR121_LLD_REGISTER_TOUCH_STATUS, 0, 1, &tstate, ((aos_test_mpr121data_t*)test->data)->timeout);
173 e545e620 Thomas Schöpping
      chprintf(stream, "\t\ttstate: %X %X %X %X\n", tstate & 1, (tstate >> 1) & 1, (tstate >> 2) & 1, (tstate >> 3) & 1);
174
    }
175
    aosSysGetUptime(&tcurrent);
176
  } while (tcurrent < tend);
177 4c72a54c Thomas Schöpping
  chEvtUnregister(((aos_test_mpr121data_t*)test->data)->evtsource, &event_listener);
178 e545e620 Thomas Schöpping
  if (status == APAL_STATUS_SUCCESS) {
179 4c72a54c Thomas Schöpping
    aosTestPassed(stream, &result);
180 e545e620 Thomas Schöpping
  } else {
181 4c72a54c Thomas Schöpping
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
182 e545e620 Thomas Schöpping
  }
183
184 4c72a54c Thomas Schöpping
  aosTestInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(MPR121Driver));
185 e545e620 Thomas Schöpping
186
  return result;
187
}
188
189 4c72a54c Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */