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