Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_AT42QT1050_v1.c @ 916f8d28

History | View | Annotate | Download (18.244 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 <amiroos.h>
20
#include <ut_alld_AT42QT1050_v1.h>
21

    
22
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_AT42QT1050) && (AMIROLLD_CFG_AT42QT1050 == 1)) || defined(__DOXYGEN__)
23
/******************************************************************************/
24
/* LOCAL DEFINITIONS                                                          */
25
/******************************************************************************/
26

    
27
#define INTERRUPT_EVENT_ID            1
28

    
29
/******************************************************************************/
30
/* EXPORTED VARIABLES                                                         */
31
/******************************************************************************/
32

    
33
/******************************************************************************/
34
/* LOCAL TYPES                                                                */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* LOCAL VARIABLES                                                            */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL FUNCTIONS                                                            */
43
/******************************************************************************/
44

    
45
void print_settings(apalExitStatus_t* status, BaseSequentialStream* stream, aos_unittest_t* ut) {
46
    chprintf(stream, "settings...\n");
47

    
48
    uint8_t test8;
49
    at42qt1050_lld_register_t txbuf;
50
    *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_LOWPOWERMODE, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
51
    chprintf(stream, "\tmeasurement inverval %d ms\n", test8*8);
52

    
53
    *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_MAXONDURATION, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
54
    chprintf(stream, "\tMax on duration %d ms\n", test8*160);
55

    
56
    *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_FINFOUTMAXCALGUARD, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
57
    chprintf(stream, "\tFast In %d, Fast Out, %d MaxCal %d\n\tGuard channel ",
58
             test8 & AT42QT1050_LLD_FINFOUTMAXCALGUARD_FI,     //enter fast mode whenever an unfiltered signal value is detected
59
             test8 & AT42QT1050_LLD_FINFOUTMAXCALGUARD_FO,     //DI of 4 (global setting for all keys)
60
             test8 & AT42QT1050_LLD_FINFOUTMAXCALGUARD_MAXCAL);//recalibrate ALL KEYS after a Max On Duration timeout vs. individually
61

    
62
    //guard channel (which gets priority filtering)
63
    if((test8 & AT42QT1050_LLD_FINFOUTMAXCALGUARD_GUARD) > AT42QT1050_LLD_NUM_KEYS-1)
64
        chprintf(stream, "off");
65
    else
66
        chprintf(stream, " %d", test8 & AT42QT1050_LLD_FINFOUTMAXCALGUARD_GUARD);
67

    
68
    chprintf(stream, "\n\n\tkey\tgroup\tintegr.\tdelay\tthresh.\tpulse\tscale\n");
69
     for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
70
         chprintf(stream, "\t%d\t", key);
71
         txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_INTEGRATOR_AKS_0, key);
72
         *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
73
         if(!((at42qt1050_lld_detectionintegratoraksreg_t)test8).aks)
74
             chprintf(stream, "none\t");
75
         else
76
             chprintf(stream, "%d\t", ((at42qt1050_lld_detectionintegratoraksreg_t)test8).aks);
77

    
78
         if(!((at42qt1050_lld_detectionintegratoraksreg_t)test8).detection_integrator)
79
             chprintf(stream, "off", key);
80
         else
81
             chprintf(stream, "%d", ((at42qt1050_lld_detectionintegratoraksreg_t)test8).detection_integrator);
82

    
83
         txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_CHARGESHAREDELAY_0, key);
84
         *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
85
         chprintf(stream, "\t+%d", test8);
86

    
87
         txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0, key);
88
         *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
89
         chprintf(stream, "\t%d", test8);
90

    
91
         txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_PULSE_SCALE_0, key);
92
         *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &test8, ((ut_at42qt1050data_t*)ut->data)->timeout);
93
         chprintf(stream, "\t%d\t%d\n", key, ((at42qt1050_lld_pulsescalereg_t)test8).pulse, ((at42qt1050_lld_pulsescalereg_t)test8).scale);
94
    }
95

    
96
}
97

    
98
void show_live(const uint8_t first_key, apalExitStatus_t* status,
99
               BaseSequentialStream* stream, aos_unittest_t* ut) {
100

    
101
    apalDbgAssert(first_key<5);
102

    
103
    event_listener_t event_listener;
104
    aos_timestamp_t tstart, tcurrent, tend;
105

    
106
    uint8_t keyStatus, detectionStatus;
107
    uint16_t signal, reference;
108
    uint8_t threshold[AT42QT1050_LLD_NUM_KEYS];
109

    
110
    chprintf(stream, "key, count, signal, ref, [threshold], \033[31mtouch\n\033[0m");
111

    
112
    //get thresholds
113
    for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
114
        const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0, key);
115
        *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &threshold[key], ((ut_at42qt1050data_t*)ut->data)->timeout);
116
    }
117
    chEvtRegister(((ut_at42qt1050data_t*)ut->data)->evtsource, &event_listener, INTERRUPT_EVENT_ID);
118
    for(uint16_t i=0;; i++) {
119
        aosSysGetUptime(&tstart);
120
        //wait 1 second for a touch event
121
        tend = tstart + (MICROSECONDS_PER_SECOND);
122

    
123
        for (uint8_t key = first_key; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
124

    
125
            //highlight touched key
126
            *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_KEYSTATUS, &keyStatus, ((ut_at42qt1050data_t*)ut->data)->timeout);
127
            if((key == 0 && keyStatus&AT42QT1050_LLD_KEYSTATUS_KEY0)
128
                    || (key == 1 && keyStatus&AT42QT1050_LLD_KEYSTATUS_KEY1)
129
                    || (key == 2 && keyStatus&AT42QT1050_LLD_KEYSTATUS_KEY2)
130
                    || (key == 3 && keyStatus&AT42QT1050_LLD_KEYSTATUS_KEY3)
131
                    || (key == 4 && keyStatus&AT42QT1050_LLD_KEYSTATUS_KEY4))
132
                chprintf(stream, "\033[31m"); //red
133
            else
134
                chprintf(stream, "\033[0m"); //black
135

    
136
            *status |= at42qt1050_lld_read_keyssignal(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, key, &signal, ((ut_at42qt1050data_t*)ut->data)->timeout);
137
            *status |= at42qt1050_lld_read_referencedata(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, key, &reference, ((ut_at42qt1050data_t*)ut->data)->timeout);
138

    
139
            const uint16_t dist = (signal<reference?0:signal-reference);
140
            chprintf(stream, "\033[Kkey %d, %d\t0x%04X 0x%04X [", key, dist, reference, signal);
141
            uint16_t stars=0;
142
            for(; stars < dist; stars++) {
143
                chprintf(stream, "0");
144
                if(stars > 40) {           //max_stars = 40
145
                    chprintf(stream, "+"); //more than max_stars
146
                    break;
147
                }
148
            }
149
            for(; stars < threshold[key]; stars++)
150
                chprintf(stream, " ");
151
            if(stars == threshold[key])
152
                chprintf(stream, "]");
153
            chprintf(stream, "\n\033[0m");
154
        }
155

    
156
        aosSysGetUptime(&tcurrent);
157
//        const aos_timestamp_t ttimeout = MICROSECONDS_PER_SECOND - ((tcurrent - tstart) % MICROSECONDS_PER_SECOND);
158
        const eventmask_t emask = chEvtWaitOneTimeout(EVENT_MASK(INTERRUPT_EVENT_ID), chTimeUS2I(tend));//ttimeout));
159
        const eventflags_t eflags = chEvtGetAndClearFlags(&event_listener);
160
        if (emask == EVENT_MASK(INTERRUPT_EVENT_ID) && eflags == ((ut_at42qt1050data_t*)ut->data)->evtflags) {
161
            // interrupt detected
162
            chprintf(stream, "interrupt ");
163
        } // else: timeout
164

    
165
        *status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_DETECTIONSTATUS, &detectionStatus, ((ut_at42qt1050data_t*)ut->data)->timeout);
166
        if(detectionStatus & AT42QT1050_LLD_DETECTIONSTATUS_TOUCH)
167
            chprintf(stream, "touch ");
168
        if(detectionStatus & AT42QT1050_LLD_DETECTIONSTATUS_OVERFLOW)
169
            chprintf(stream, "overflow ");
170
        if(detectionStatus & AT42QT1050_LLD_DETECTIONSTATUS_CALIBRATE)
171
            chprintf(stream, "calibrate");
172
        chprintf(stream, "\033[K");
173

    
174
        if(i>=30)
175
            break;
176

    
177
        chprintf(stream, "\033[%dF", 5-first_key); //cursor up
178
    }
179
    chEvtUnregister(((ut_at42qt1050data_t*)ut->data)->evtsource, &event_listener);
180
}
181

    
182
/******************************************************************************/
183
/* EXPORTED FUNCTIONS                                                         */
184
/******************************************************************************/
185

    
186
aos_utresult_t utAlldAt42qt1050Func(BaseSequentialStream* stream, aos_unittest_t* ut)
187
{
188
    aosDbgCheck(ut->data != NULL && ((ut_at42qt1050data_t*)ut->data)->at42qt1050d != NULL);
189

    
190
    // local variables
191
    aos_utresult_t result = {0, 0};
192
    apalExitStatus_t status;
193
    uint8_t  test_8;
194
    bool error;
195

    
196
    chprintf(stream, "read register...\n");
197
    error = false;
198
    status = at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_CHIPID, &test_8, ((ut_at42qt1050data_t*)ut->data)->timeout);
199
    chprintf(stream, "\t\tchip ID: 0x%02X\n", test_8);
200
    if (test_8 != AT42QT1050_LLD_CHIPID)
201
        error = true;
202
    status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_FIRMWAREVERSION, &test_8, ((ut_at42qt1050data_t*)ut->data)->timeout);
203
    chprintf(stream, "\t\tfirmware version: %u.%u (0x%02X)\n", ((at42qt1050_lld_firmwarereg_t)test_8).major, ((at42qt1050_lld_firmwarereg_t)test_8).minor, test_8);
204
    if (status == APAL_STATUS_SUCCESS && !error) {
205
        aosUtPassed(stream, &result);
206
    } else {
207
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
208
    }
209

    
210
    chprintf(stream, "write and readback threshold data...\n");
211
    status = APAL_STATUS_OK;
212
    const uint8_t threshold_test = 0x40;
213
    error = false;
214
    for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
215
        const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0, key);
216
        status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, threshold_test , ((ut_at42qt1050data_t*)ut->data)->timeout);
217
        status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &test_8, ((ut_at42qt1050data_t*)ut->data)->timeout);
218
        if(test_8 != threshold_test)
219
            error = true;
220
    }
221
    if (status == APAL_STATUS_SUCCESS  &&  !error) {
222
        aosUtPassedMsg(stream, &result, "Set thresholds successfull to 0x%04X\n", threshold_test);
223
    } else {
224
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
225
    }
226

    
227
    chprintf(stream, "guarding...\nincrease charge_delay0\ndisable multitouch\n");
228

    
229
    //channel 0 is to big to be charged in the default cycle
230
    status = at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_CHARGESHAREDELAY_0, 128 , ((ut_at42qt1050data_t*)ut->data)->timeout);
231

    
232
    //set all channel to group 1 -> only 1 simultaneous touch in group = single touch
233
    at42qt1050_lld_detectionintegratoraksreg_t detectionintegrator;
234
    for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
235
        const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_INTEGRATOR_AKS_0, key);
236
        detectionintegrator.aks = 1;                  //on touch per group-id simultaneous
237
        detectionintegrator.detection_integrator = 4; //4 times > threshold => touchevent
238
        status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, 0x11 , ((ut_at42qt1050data_t*)ut->data)->timeout);
239
    }
240
    if (status == APAL_STATUS_SUCCESS) {
241
        aosUtPassed(stream, &result);
242
    } else {
243
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
244
    }
245

    
246
    chprintf(stream, "reset device...\n");
247
    status = at42qt1050_lld_reset(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, ((ut_at42qt1050data_t*)ut->data)->timeout, true);
248
    if (status == APAL_STATUS_SUCCESS) {
249
        aosUtPassed(stream, &result);
250
    } else {
251
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
252
    }
253

    
254
    chprintf(stream, "read threshold data again...\n");
255
    status = APAL_STATUS_OK;
256
    const uint8_t threshold_default = 20;
257
    error = false;
258
    for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
259
        const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0, key);
260
        status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, &test_8, ((ut_at42qt1050data_t*)ut->data)->timeout);
261
        if(test_8 != threshold_default)
262
            error = true;
263
    }
264

    
265
    if (status == APAL_STATUS_SUCCESS && !error) {
266
        aosUtPassedMsg(stream, &result, "threshold 0x%04X = default\n", threshold_default);
267
    } else {
268
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
269
    }
270

    
271
    chprintf(stream, "demo of default configuration:\n");
272
    status = APAL_STATUS_OK;
273
    show_live(0, &status, stream, ut);
274
    if (status == APAL_STATUS_SUCCESS) {
275
        aosUtPassed(stream, &result);
276
    } else {
277
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
278
    }
279

    
280
    status = APAL_STATUS_OK;
281
    chprintf(stream, "write configuration...\npuls + scale + threshold 16\n");
282
    at42qt1050_lld_pulsescalereg_t pulse_scale;
283
    //values stored as exponent of 2
284
    pulse_scale.pulse = 0; // accumulate #pulses -> increase resolution & time to acquire
285
    pulse_scale.scale = 0; // scale = average factor n: NewAvg = (NewData/n) + [OldAvg*(n-1/n)] -> decrease noise
286
    status  = at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_0, pulse_scale.raw , ((ut_at42qt1050data_t*)ut->data)->timeout);
287
    status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_3, 0x00u , ((ut_at42qt1050data_t*)ut->data)->timeout);
288
    pulse_scale.pulse = 4;
289
    status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_1, pulse_scale.raw , ((ut_at42qt1050data_t*)ut->data)->timeout);
290
    status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_2, pulse_scale.raw , ((ut_at42qt1050data_t*)ut->data)->timeout);
291
    status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_4, pulse_scale.raw , ((ut_at42qt1050data_t*)ut->data)->timeout);
292

    
293
    for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
294
        const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0, key);
295
        status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, txbuf, 16, ((ut_at42qt1050data_t*)ut->data)->timeout);
296
    }
297
    chprintf(stream, "disable guard key\n");
298
    status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_INTEGRATOR_AKS_0, 0 , ((ut_at42qt1050data_t*)ut->data)->timeout); //disable key
299
    status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_FINFOUTMAXCALGUARD, 8 , ((ut_at42qt1050data_t*)ut->data)->timeout); //disable feature
300

    
301
    chprintf(stream, "calibrate...\n");
302
    status = at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_RESET_CALIBRATE, AT42QT1050_LLD_RESETCALIBRATE_CALIBRATE, ((ut_at42qt1050data_t*)ut->data)->timeout);
303

    
304

    
305

    
306
    //wait for calibration to complete
307
    error = true;
308
    for(uint8_t i=0; i<0xFF; i++) {
309
        usleep(10);
310
        status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_DETECTIONSTATUS, &test_8, ((ut_at42qt1050data_t*)ut->data)->timeout);
311
        if(!(test_8 & AT42QT1050_LLD_DETECTIONSTATUS_CALIBRATE)) {
312
            error = false;
313
            break;
314
        }
315
    }
316
    if (status == APAL_STATUS_SUCCESS) {
317
        aosUtPassed(stream, &result);
318
    } else {
319
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
320
    }
321
    chprintf(stream, "demo of custom configuration:\n");
322
    status = APAL_STATUS_OK;
323
    show_live(1, &status, stream, ut);
324
    if (status == APAL_STATUS_SUCCESS) {
325
        aosUtPassed(stream, &result);
326
    } else {
327
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
328
    }
329

    
330
    status = APAL_STATUS_OK;
331
    print_settings(&status, stream, ut);
332
    if (status == APAL_STATUS_SUCCESS) {
333
        aosUtPassed(stream, &result);
334
    } else {
335
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
336
    }
337

    
338
    chprintf(stream, "shutdown touch\n");
339
    test_8 = 0; //Power down
340
    status = at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_LOWPOWERMODE, &test_8, ((ut_at42qt1050data_t*)ut->data)->timeout);
341
    if (status == APAL_STATUS_SUCCESS) {
342
        aosUtPassed(stream, &result);
343
    } else {
344
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
345
    }
346

    
347
    aosUtInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(AT42QT1050Driver));
348

    
349
    return result;
350
}
351

    
352
#endif