Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.14 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_at42qt1050.h>
20

    
21
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_AT42QT1050)) || defined(__DOXYGEN__)
22

    
23
/******************************************************************************/
24
/* LOCAL DEFINITIONS                                                          */
25
/******************************************************************************/
26
#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
/******************************************************************************/
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
aos_utresult_t utAlldAt42qt1050Func(BaseSequentialStream* stream, aos_unittest_t* ut)
54
{
55
  aosDbgCheck(ut->data != NULL && ((ut_at42qt1050data_t*)ut->data)->at42qt1050d != NULL);
56

    
57
  // local variables
58
  aos_utresult_t result = {0, 0};
59
  uint32_t status;
60
  uint8_t buffer8[4] = {0, 0};
61
  uint16_t buffer16[5];
62
  event_listener_t event_listener;
63
  aos_timestamp_t tstart, tcurrent, tend;
64

    
65
  chprintf(stream, "read register...\n");
66
  status = at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_CHIPID, &buffer8[0], ((ut_at42qt1050data_t*)ut->data)->timeout);
67
  status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_FIRMWAREVERSION, &buffer8[1], ((ut_at42qt1050data_t*)ut->data)->timeout);
68
  chprintf(stream, "\t\tchip ID: 0x%02X\n", buffer8[0]);
69
  chprintf(stream, "\t\tfirmware version: %u.%u (0x%02X)\n", ((at42qt1050_lld_firmwarereg_t)buffer8[1]).major, ((at42qt1050_lld_firmwarereg_t)buffer8[1]).minor, buffer8[1]);
70
  if (status == APAL_STATUS_SUCCESS && buffer8[0] == AT42QT1050_LLD_CHIPID) {
71
    aosUtPassed(stream, &result);
72
  } else {
73
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
74
  }
75

    
76
  chprintf(stream, "write register...\n");
77
  buffer8[1] = (((uint8_t)(at42qt1050_lld_samples2pulse(4096) + 0.5f)) << 4) | ((uint8_t)(at42qt1050_lld_scaling2scale(1024) + 0.5f));
78
  status = at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_1, &buffer8[0], ((ut_at42qt1050data_t*)ut->data)->timeout);
79
  status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_1, buffer8[1] , ((ut_at42qt1050data_t*)ut->data)->timeout);
80
  status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_1, &buffer8[2], ((ut_at42qt1050data_t*)ut->data)->timeout);
81
  status |= at42qt1050_lld_write_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_1, buffer8[0] , ((ut_at42qt1050data_t*)ut->data)->timeout);
82
  status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_PULSE_SCALE_1, &buffer8[3], ((ut_at42qt1050data_t*)ut->data)->timeout);
83
  if (status == APAL_STATUS_SUCCESS &&
84
      buffer8[3] == buffer8[0] &&
85
      buffer8[2] == buffer8[1] &&
86
      at42qt1050_lld_pulse2samples(((at42qt1050_lld_pulsescalereg_t)buffer8[2]).pulse) == 4096 &&
87
      at42qt1050_lld_scale2scaling(((at42qt1050_lld_pulsescalereg_t)buffer8[2]).scale) == 1024) {
88
    aosUtPassed(stream, &result);
89
  } else {
90
    aosUtFailedMsg(stream, &result, "0x%08X [0x%02X 0x%02X 0x%02X 0x%02X]\n", status, buffer8[0], buffer8[1], buffer8[2], buffer8[3]);
91
  }
92

    
93
  chprintf(stream, "read reference data...\n");
94
  status = APAL_STATUS_OK;
95
  for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
96
    status |= at42qt1050_lld_read_referencedata(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, key, &buffer16[key], ((ut_at42qt1050data_t*)ut->data)->timeout);
97
    chprintf(stream, "\t\tkey %u: 0x%04X\n", key, buffer16[key]);
98
  }
99
  if (status == APAL_STATUS_SUCCESS) {
100
    aosUtPassed(stream, &result);
101
  } else {
102
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
103
  }
104

    
105
  chprintf(stream, "read key signal for ten seconds...\n");
106
  status = APAL_STATUS_OK;
107
  chprintf(stream, "\t\t key 0  key 1  key 2  key 3  key 4\n");
108
  for (uint8_t s = 0; s < 10; ++s) {
109
    aosThdSSleep(1);
110
    for (uint8_t key = 0; key < AT42QT1050_LLD_NUM_KEYS; ++key) {
111
      status |= at42qt1050_lld_read_keyssignal(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, key, &buffer16[key], ((ut_at42qt1050data_t*)ut->data)->timeout);
112
    }
113
    chprintf(stream, "\t\t0x%04X 0x%04X 0x%04X 0x%04X 0x%04X\n", buffer16[0], buffer16[1], buffer16[2], buffer16[3], buffer16[4]);
114
  }
115
  if (status == APAL_STATUS_SUCCESS) {
116
    aosUtPassed(stream, &result);
117
  } else {
118
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
119
  }
120

    
121
  chprintf(stream, "test interrupts...\n");
122
  status = AOS_OK;
123
  chEvtRegister(((ut_at42qt1050data_t*)ut->data)->evtsource, &event_listener, INTERRUPT_EVENT_ID);
124
  aosSysGetUptime(&tstart);
125
  tend = tstart + (30 * MICROSECONDS_PER_SECOND);
126
  do {
127
    aosSysGetUptime(&tcurrent);
128
    const aos_timestamp_t ttimeout = MICROSECONDS_PER_SECOND - ((tcurrent - tstart) % MICROSECONDS_PER_SECOND);
129
    const eventmask_t emask = chEvtWaitOneTimeout(EVENT_MASK(INTERRUPT_EVENT_ID), chTimeUS2I(ttimeout));
130
    const eventflags_t eflags = chEvtGetAndClearFlags(&event_listener);
131
    if (emask == EVENT_MASK(INTERRUPT_EVENT_ID) && eflags == ((ut_at42qt1050data_t*)ut->data)->evtflags) {
132
      // interrupt detected
133
      chprintf(stream, "\t\tinterrupt detected\n");
134
    } // else: timeout
135
    status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_KEYSTATUS, &buffer8[0], ((ut_at42qt1050data_t*)ut->data)->timeout);
136
    chprintf(stream, "\t\tkey status: %u %u %u %u %u\n",
137
             (((buffer8[0] & AT42QT1050_LLD_KEYSTATUS_KEY0) == 0) ? 0 : 1),
138
             (((buffer8[0] & AT42QT1050_LLD_KEYSTATUS_KEY1) == 0) ? 0 : 1),
139
             (((buffer8[0] & AT42QT1050_LLD_KEYSTATUS_KEY2) == 0) ? 0 : 1),
140
             (((buffer8[0] & AT42QT1050_LLD_KEYSTATUS_KEY3) == 0) ? 0 : 1),
141
             (((buffer8[0] & AT42QT1050_LLD_KEYSTATUS_KEY4) == 0) ? 0 : 1));
142
    aosSysGetUptime(&tcurrent);
143
  } while (tcurrent < tend);
144
  chEvtUnregister(((ut_at42qt1050data_t*)ut->data)->evtsource, &event_listener);
145
  if (status == APAL_STATUS_SUCCESS) {
146
    aosUtPassed(stream, &result);
147
  } else {
148
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
149
  }
150

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

    
153
  return result;
154
}
155

    
156
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_AT42QT1050) */