Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.995 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

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
#include <aos_debug.h>
24
#include <chprintf.h>
25
#include <aos_thread.h>
26
#include <amiroos.h>
27

    
28
#define INTERRUPT_EVENT_ID            1
29

    
30
aos_utresult_t utAlldMpr121Func(BaseSequentialStream* stream, aos_unittest_t* ut)
31
{
32
  aosDbgCheck(ut->data != NULL && ((ut_mpr121data_t*)ut->data)->mprd != NULL);
33

    
34
  // local variables
35
  aos_utresult_t result = {0, 0};
36
  uint32_t status;
37
  mpr121_lld_config_t cfg;
38
  uint8_t data, wdata, rdata;
39
  uint16_t edata[4];
40
  uint8_t tstate = 0;
41
  event_listener_t event_listener;
42
  aos_timestamp_t tstart, tcurrent, tend;
43

    
44
  chprintf(stream, "soft reset...\n");
45
  status = mpr121_lld_soft_reset(((ut_mpr121data_t*)ut->data)->mprd, ((ut_mpr121data_t*)ut->data)->timeout);
46
  status |= mpr121_lld_read_config(((ut_mpr121data_t*)ut->data)->mprd, &cfg, ((ut_mpr121data_t*)ut->data)->timeout);
47
  if (status == APAL_STATUS_SUCCESS && cfg.registers.ele_config == 0) {
48
    aosUtPassed(stream, &result);
49
  } else {
50
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
51
  }
52

    
53
  chprintf(stream, "read register...\n");
54
  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);
55
  chprintf(stream, "\t\tdata: 0x%02X\n", data);
56
  if (status == APAL_STATUS_SUCCESS && data == (MPR121_LLD_CDT_0_5 | MPR121_LLD_ESI_16)) {
57
    aosUtPassed(stream, &result);
58
  } else {
59
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
60
  }
61

    
62
  chprintf(stream, "write register...\n");
63
  wdata = MPR121_LLD_CDT_32 | MPR121_LLD_SFI_10 | MPR121_LLD_ESI_16;
64
  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);
65
  aosThdMSleep(500);
66
  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);
67
  chprintf(stream, "\t\tdata: 0x%02X\n", rdata);
68
  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);
69
  if (status == APAL_STATUS_SUCCESS && rdata == wdata) {
70
    aosUtPassed(stream, &result);
71
  } else {
72
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
73
  }
74

    
75
  chprintf(stream, "read config...\n");
76
  status = mpr121_lld_read_config(((ut_mpr121data_t*)ut->data)->mprd, &cfg, ((ut_mpr121data_t*)ut->data)->timeout);
77
  if (status == APAL_STATUS_SUCCESS && cfg.registers.config_1 == 0x10 && cfg.registers.config_2 == 0x24) {
78
    aosUtPassed(stream, &result);
79
  } else {
80
    chprintf(stream, "\t\tconfig 1: 0x%02X\n", cfg.registers.config_1);
81
    chprintf(stream, "\t\tconfig 2: 0x%02X\n", cfg.registers.config_2);
82
    mpr121_lld_read_register(((ut_mpr121data_t*)ut->data)->mprd, MPR121_LLD_REGISTER_CONFIG_1, 0, 1, &rdata, ((ut_mpr121data_t*)ut->data)->timeout);
83
    chprintf(stream, "\t\tconfig 1: 0x%02X\n", rdata);
84
    mpr121_lld_read_register(((ut_mpr121data_t*)ut->data)->mprd, MPR121_LLD_REGISTER_CONFIG_2, 0, 1, &rdata, ((ut_mpr121data_t*)ut->data)->timeout);
85
    chprintf(stream, "\t\tconfig 2: 0x%02X\n", rdata);
86
    for (uint8_t valueIdx = 0; valueIdx < 8; valueIdx++) {
87
      chprintf(stream, "\t\tdata %d: 0x%02X\n", valueIdx, cfg.values[valueIdx]);
88
    }
89
    chprintf(stream, "ele_config: 0x%02X\n", cfg.registers.ele_config);
90
    chprintf(stream, "config_1: 0x%02X\n", cfg.registers.config_1);
91
    chprintf(stream, "config_2: 0x%02X\n", cfg.registers.config_2);
92
    chprintf(stream, "target_level: 0x%02X\n", cfg.registers.target_level);
93
    chprintf(stream, "low_side_limit: 0x%02X\n", cfg.registers.low_side_limit);
94
    chprintf(stream, "up_side_limit: 0x%02X\n", cfg.registers.up_side_limit);
95
    chprintf(stream, "auto_config_2: 0x%02X\n", cfg.registers.auto_config_2);
96
    chprintf(stream, "auto_config_1: 0x%02X\n", cfg.registers.auto_config_1);
97
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
98
  }
99

    
100
  chprintf(stream, "write config...\n");
101
  cfg.registers.ele_config = MPR121_LLD_CL_ON_ALL | MPR121_LLD_ELEPROX_0 | 4;
102
  cfg.registers.config_1 = MPR121_LLD_FFI_18 | 16;
103
  cfg.registers.config_2 = MPR121_LLD_CDT_1 | MPR121_LLD_SFI_10 | MPR121_LLD_ESI_32;
104
  cfg.registers.up_side_limit = 0x96u;
105
  cfg.registers.low_side_limit = 0x58u;
106
  cfg.registers.target_level = 0x68u;
107
  cfg.registers.auto_config_1 = MPR121_LLD_FFI_18 | MPR121_LLD_RETRY_2 | MPR121_LLD_BVA_ON_ALL | MPR121_LLD_AC_RECONF_EN;
108
  status = mpr121_lld_write_config(((ut_mpr121data_t*)ut->data)->mprd, cfg, ((ut_mpr121data_t*)ut->data)->timeout);
109
  aosThdMSleep(500);
110
  status |= mpr121_lld_read_config(((ut_mpr121data_t*)ut->data)->mprd, &cfg, ((ut_mpr121data_t*)ut->data)->timeout);
111
  if (status == APAL_STATUS_SUCCESS && cfg.registers.up_side_limit == 0x96u) {
112
    aosUtPassed(stream, &result);
113
  } else {
114
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
115
  }
116

    
117
  chprintf(stream, "read filtered data for ten seconds...\n");
118
  chprintf(stream, "(values should change if you touch the sensors now)\n");
119
  for (uint8_t i = 0; i < 10; i++) {
120
    status = mpr121_lld_read_filtered_data(((ut_mpr121data_t*)ut->data)->mprd, 0, 4, edata, ((ut_mpr121data_t*)ut->data)->timeout);
121
    chprintf(stream, "\t\t0x%04X | 0x%04X | 0x%04X | 0x%04X\n", edata[0], edata[1], edata[2], edata[3]);
122
    aosThdSSleep(1);
123
  }
124
  if (status == APAL_STATUS_SUCCESS) {
125
    aosUtPassed(stream, &result);
126
  } else {
127
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
128
  }
129

    
130
  chprintf(stream, "test interrupts...\n");
131
  status = AOS_OK;
132
  chEvtRegister(((ut_mpr121data_t*)ut->data)->evtsource, &event_listener, INTERRUPT_EVENT_ID);
133
  aosSysGetUptime(&tstart);
134
  tend = tstart + (30 * MICROSECONDS_PER_SECOND);
135
  do {
136
    aosSysGetUptime(&tcurrent);
137
    const aos_timestamp_t ttimeout = MICROSECONDS_PER_SECOND - ((tcurrent - tstart) % MICROSECONDS_PER_SECOND);
138
    const eventmask_t emask = chEvtWaitOneTimeout(EVENT_MASK(INTERRUPT_EVENT_ID), chTimeUS2I(ttimeout));
139
    const eventflags_t eflags = chEvtGetAndClearFlags(&event_listener);
140
    if (emask == EVENT_MASK(INTERRUPT_EVENT_ID) && eflags == ((ut_mpr121data_t*)ut->data)->evtflags) {
141
      // interrupt detected
142
      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);
143
      chprintf(stream, "\t\tinterrupt detected\n");
144
      chprintf(stream, "\t\ttstate: %X %X %X %X\n", tstate & 1, (tstate >> 1) & 1, (tstate >> 2) & 1, (tstate >> 3) & 1);
145
    }
146
    else if (emask == 0) {
147
      // timeout
148
      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);
149
      chprintf(stream, "\t\ttstate: %X %X %X %X\n", tstate & 1, (tstate >> 1) & 1, (tstate >> 2) & 1, (tstate >> 3) & 1);
150
    }
151
    aosSysGetUptime(&tcurrent);
152
  } while (tcurrent < tend);
153
  chEvtUnregister(((ut_mpr121data_t*)ut->data)->evtsource, &event_listener);
154
  if (status == APAL_STATUS_SUCCESS) {
155
    aosUtPassed(stream, &result);
156
  } else {
157
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
158
  }
159

    
160
  aosUtInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(MPR121Driver));
161

    
162
  return result;
163
}
164

    
165
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_MPR121) */