Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.05 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

    
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
/******************************************************************************/
46
/* EXPORTED FUNCTIONS                                                         */
47
/******************************************************************************/
48

    
49
aos_utresult_t utAlldAt42qt1050Func(BaseSequentialStream* stream, aos_unittest_t* ut)
50
{
51
  aosDbgCheck(ut->data != NULL && ((ut_at42qt1050data_t*)ut->data)->at42qt1050d != NULL);
52

    
53
  // local variables
54
  aos_utresult_t result = {0, 0};
55
  uint32_t status;
56
  uint8_t buffer8[4] = {0, 0};
57
  uint16_t buffer16[5];
58
  event_listener_t event_listener;
59
  aos_timestamp_t tstart, tcurrent, tend;
60

    
61
  chprintf(stream, "read register...\n");
62
  status = at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_CHIPID, &buffer8[0], ((ut_at42qt1050data_t*)ut->data)->timeout);
63
  status |= at42qt1050_lld_read_reg(((ut_at42qt1050data_t*)ut->data)->at42qt1050d, AT42QT1050_LLD_REG_FIRMWAREVERSION, &buffer8[1], ((ut_at42qt1050data_t*)ut->data)->timeout);
64
  chprintf(stream, "\t\tchip ID: 0x%02X\n", buffer8[0]);
65
  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]);
66
  if (status == APAL_STATUS_SUCCESS && buffer8[0] == AT42QT1050_LLD_CHIPID) {
67
    aosUtPassed(stream, &result);
68
  } else {
69
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
70
  }
71

    
72
  chprintf(stream, "write register...\n");
73
  buffer8[1] = (((uint8_t)(at42qt1050_lld_samples2pulse(4096) + 0.5f)) << 4) | ((uint8_t)(at42qt1050_lld_scaling2scale(1024) + 0.5f));
74
  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);
75
  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);
76
  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);
77
  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);
78
  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);
79
  if (status == APAL_STATUS_SUCCESS &&
80
      buffer8[3] == buffer8[0] &&
81
      buffer8[2] == buffer8[1] &&
82
      at42qt1050_lld_pulse2samples(((at42qt1050_lld_pulsescalereg_t)buffer8[2]).pulse) == 4096 &&
83
      at42qt1050_lld_scale2scaling(((at42qt1050_lld_pulsescalereg_t)buffer8[2]).scale) == 1024) {
84
    aosUtPassed(stream, &result);
85
  } else {
86
    aosUtFailedMsg(stream, &result, "0x%08X [0x%02X 0x%02X 0x%02X 0x%02X]\n", status, buffer8[0], buffer8[1], buffer8[2], buffer8[3]);
87
  }
88

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

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

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

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

    
149
  return result;
150
}
151

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