Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_mpr121.c @ f3ac1c96

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