Production Test Program (PTP) for the LPC4088 Experiment Base Board

Dependencies:   EALib I2S LM75B SDFileSystem mbed

Committer:
embeddedartists
Date:
Wed Oct 01 11:16:38 2014 +0000
Revision:
9:eb6086159020
Parent:
7:48375cb50f3a
Updated used libraries

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 1:47680ec5d783 1 /*
embeddedartists 1:47680ec5d783 2 * Copyright 2013 Embedded Artists AB
embeddedartists 1:47680ec5d783 3 *
embeddedartists 1:47680ec5d783 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 1:47680ec5d783 5 * you may not use this file except in compliance with the License.
embeddedartists 1:47680ec5d783 6 * You may obtain a copy of the License at
embeddedartists 1:47680ec5d783 7 *
embeddedartists 1:47680ec5d783 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 1:47680ec5d783 9 *
embeddedartists 1:47680ec5d783 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 1:47680ec5d783 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 1:47680ec5d783 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 1:47680ec5d783 13 * See the License for the specific language governing permissions and
embeddedartists 1:47680ec5d783 14 * limitations under the License.
embeddedartists 1:47680ec5d783 15 */
embeddedartists 1:47680ec5d783 16
embeddedartists 1:47680ec5d783 17 /******************************************************************************
embeddedartists 1:47680ec5d783 18 * Includes
embeddedartists 1:47680ec5d783 19 *****************************************************************************/
embeddedartists 1:47680ec5d783 20
embeddedartists 1:47680ec5d783 21 #include "mbed.h"
embeddedartists 1:47680ec5d783 22 #include "mbed_debug.h"
embeddedartists 1:47680ec5d783 23
embeddedartists 1:47680ec5d783 24 #include "AR1021I2C.h"
embeddedartists 1:47680ec5d783 25
embeddedartists 1:47680ec5d783 26 /******************************************************************************
embeddedartists 1:47680ec5d783 27 * Defines and typedefs
embeddedartists 1:47680ec5d783 28 *****************************************************************************/
embeddedartists 1:47680ec5d783 29
embeddedartists 1:47680ec5d783 30 #define AR1021_REG_TOUCH_THRESHOLD (0x02)
embeddedartists 1:47680ec5d783 31 #define AR1021_REG_SENS_FILTER (0x03)
embeddedartists 1:47680ec5d783 32 #define AR1021_REG_SAMPLING_FAST (0x04)
embeddedartists 1:47680ec5d783 33 #define AR1021_REG_SAMPLING_SLOW (0x05)
embeddedartists 1:47680ec5d783 34 #define AR1021_REG_ACC_FILTER_FAST (0x06)
embeddedartists 1:47680ec5d783 35 #define AR1021_REG_ACC_FILTER_SLOW (0x07)
embeddedartists 1:47680ec5d783 36 #define AR1021_REG_SPEED_THRESHOLD (0x08)
embeddedartists 1:47680ec5d783 37 #define AR1021_REG_SLEEP_DELAY (0x0A)
embeddedartists 1:47680ec5d783 38 #define AR1021_REG_PEN_UP_DELAY (0x0B)
embeddedartists 1:47680ec5d783 39 #define AR1021_REG_TOUCH_MODE (0x0C)
embeddedartists 1:47680ec5d783 40 #define AR1021_REG_TOUCH_OPTIONS (0x0D)
embeddedartists 1:47680ec5d783 41 #define AR1021_REG_CALIB_INSETS (0x0E)
embeddedartists 1:47680ec5d783 42 #define AR1021_REG_PEN_STATE_REPORT_DELAY (0x0F)
embeddedartists 1:47680ec5d783 43 #define AR1021_REG_TOUCH_REPORT_DELAY (0x11)
embeddedartists 1:47680ec5d783 44
embeddedartists 1:47680ec5d783 45
embeddedartists 1:47680ec5d783 46 #define AR1021_CMD_GET_VERSION (0x10)
embeddedartists 1:47680ec5d783 47 #define AR1021_CMD_ENABLE_TOUCH (0x12)
embeddedartists 1:47680ec5d783 48 #define AR1021_CMD_DISABLE_TOUCH (0x13)
embeddedartists 1:47680ec5d783 49 #define AR1021_CMD_CALIBRATE_MODE (0x14)
embeddedartists 1:47680ec5d783 50 #define AR1021_CMD_REGISTER_READ (0x20)
embeddedartists 1:47680ec5d783 51 #define AR1021_CMD_REGISTER_WRITE (0x21)
embeddedartists 1:47680ec5d783 52 #define AR1021_CMD_REGISTER_START_ADDR_REQUEST (0x22)
embeddedartists 1:47680ec5d783 53 #define AR1021_CMD_REGISTER_WRITE_TO_EEPROM (0x23)
embeddedartists 1:47680ec5d783 54 #define AR1021_CMD_EEPROM_READ (0x28)
embeddedartists 1:47680ec5d783 55 #define AR1021_CMD_EEPROM_WRITE (0x29)
embeddedartists 1:47680ec5d783 56 #define AR1021_CMD_EEPROM_WRITE_TO_REGISTERS (0x2B)
embeddedartists 1:47680ec5d783 57
embeddedartists 1:47680ec5d783 58 #define AR1021_RESP_STAT_OK (0x00)
embeddedartists 1:47680ec5d783 59 #define AR1021_RESP_STAT_CMD_UNREC (0x01)
embeddedartists 1:47680ec5d783 60 #define AR1021_RESP_STAT_HDR_UNREC (0x03)
embeddedartists 1:47680ec5d783 61 #define AR1021_RESP_STAT_TIMEOUT (0x04)
embeddedartists 1:47680ec5d783 62 #define AR1021_RESP_STAT_CANCEL_CALIB (0xFC)
embeddedartists 1:47680ec5d783 63
embeddedartists 1:47680ec5d783 64
embeddedartists 1:47680ec5d783 65 #define AR1021_ERR_NO_HDR (-1000)
embeddedartists 1:47680ec5d783 66 #define AR1021_ERR_INV_LEN (-1001)
embeddedartists 1:47680ec5d783 67 #define AR1021_ERR_INV_RESP (-1002)
embeddedartists 1:47680ec5d783 68 #define AR1021_ERR_INV_RESPLEN (-1003)
embeddedartists 1:47680ec5d783 69 #define AR1021_ERR_TIMEOUT (-1004)
embeddedartists 1:47680ec5d783 70
embeddedartists 1:47680ec5d783 71 // bit 7 is always 1 and bit 0 defines pen up or down
embeddedartists 1:47680ec5d783 72 #define AR1021_PEN_MASK (0x81)
embeddedartists 1:47680ec5d783 73
embeddedartists 1:47680ec5d783 74 #define AR1021_NUM_CALIB_POINTS (4)
embeddedartists 1:47680ec5d783 75
embeddedartists 1:47680ec5d783 76 #define AR1021_ADDR (0x4D << 1)
embeddedartists 1:47680ec5d783 77
embeddedartists 1:47680ec5d783 78 #define AR1021_TIMEOUT 1000 //how many ms to wait for responce
embeddedartists 1:47680ec5d783 79 #define AR1021_RETRY 5 //how many times to retry sending command
embeddedartists 1:47680ec5d783 80
embeddedartists 7:48375cb50f3a 81 #define AR1021_MIN(__a, __b) (((__a)<(__b))?(__a):(__b))
embeddedartists 7:48375cb50f3a 82
embeddedartists 1:47680ec5d783 83
embeddedartists 1:47680ec5d783 84 AR1021I2C::AR1021I2C(PinName sda, PinName scl, PinName siq) :
embeddedartists 1:47680ec5d783 85 _i2c(sda, scl), _siq(siq), _siqIrq(siq)
embeddedartists 1:47680ec5d783 86 {
embeddedartists 1:47680ec5d783 87 _i2c.frequency(200000);
embeddedartists 1:47680ec5d783 88
embeddedartists 1:47680ec5d783 89 // default calibration inset is 25 -> (25/2 = 12.5%)
embeddedartists 1:47680ec5d783 90 _inset = 25;
embeddedartists 1:47680ec5d783 91
embeddedartists 1:47680ec5d783 92 // make sure _calibPoint has an invalid value to begin with
embeddedartists 1:47680ec5d783 93 // correct value is set in calibrateStart()
embeddedartists 1:47680ec5d783 94 _calibPoint = AR1021_NUM_CALIB_POINTS+1;
embeddedartists 1:47680ec5d783 95
embeddedartists 1:47680ec5d783 96 _x = 0;
embeddedartists 1:47680ec5d783 97 _y = 0;
embeddedartists 1:47680ec5d783 98 _pen = 0;
embeddedartists 1:47680ec5d783 99
embeddedartists 1:47680ec5d783 100 _initialized = false;
embeddedartists 1:47680ec5d783 101 }
embeddedartists 1:47680ec5d783 102
embeddedartists 1:47680ec5d783 103
embeddedartists 1:47680ec5d783 104 bool AR1021I2C::read(touchCoordinate_t &coord) {
embeddedartists 1:47680ec5d783 105
embeddedartists 1:47680ec5d783 106 if (!_initialized) return false;
embeddedartists 1:47680ec5d783 107
embeddedartists 1:47680ec5d783 108 coord.x = _x * _width/4095;
embeddedartists 1:47680ec5d783 109 coord.y = _y * _height/4095;
embeddedartists 1:47680ec5d783 110 coord.z = _pen;
embeddedartists 1:47680ec5d783 111
embeddedartists 1:47680ec5d783 112 return true;
embeddedartists 1:47680ec5d783 113 }
embeddedartists 1:47680ec5d783 114
embeddedartists 1:47680ec5d783 115
embeddedartists 1:47680ec5d783 116 bool AR1021I2C::init(uint16_t width, uint16_t height) {
embeddedartists 1:47680ec5d783 117 int result = 0;
embeddedartists 1:47680ec5d783 118 bool ok = false;
embeddedartists 1:47680ec5d783 119 int attempts = 0;
embeddedartists 1:47680ec5d783 120
embeddedartists 1:47680ec5d783 121 _width = width;
embeddedartists 1:47680ec5d783 122 _height = height;
embeddedartists 1:47680ec5d783 123
embeddedartists 1:47680ec5d783 124 while (1) {
embeddedartists 1:47680ec5d783 125
embeddedartists 1:47680ec5d783 126 do {
embeddedartists 1:47680ec5d783 127 // disable touch
embeddedartists 1:47680ec5d783 128 result = cmd(AR1021_CMD_DISABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 1:47680ec5d783 129 if (result != 0) {
embeddedartists 1:47680ec5d783 130 debug("disable touch failed (%d)\n", result);
embeddedartists 1:47680ec5d783 131 break;
embeddedartists 1:47680ec5d783 132 }
embeddedartists 1:47680ec5d783 133
embeddedartists 1:47680ec5d783 134 char regOffset = 0;
embeddedartists 1:47680ec5d783 135 int regOffLen = 1;
embeddedartists 1:47680ec5d783 136 result = cmd(AR1021_CMD_REGISTER_START_ADDR_REQUEST, NULL, 0,
embeddedartists 1:47680ec5d783 137 &regOffset, &regOffLen);
embeddedartists 1:47680ec5d783 138 if (result != 0) {
embeddedartists 1:47680ec5d783 139 debug("register offset request failed (%d)\n", result);
embeddedartists 1:47680ec5d783 140 break;
embeddedartists 1:47680ec5d783 141 }
embeddedartists 1:47680ec5d783 142
embeddedartists 1:47680ec5d783 143 // enable calibrated coordinates
embeddedartists 1:47680ec5d783 144 // high, low address, len, value
embeddedartists 1:47680ec5d783 145 char toptions[4] = {0x00, AR1021_REG_TOUCH_OPTIONS+regOffset, 0x01, 0x01};
embeddedartists 1:47680ec5d783 146 result = cmd(AR1021_CMD_REGISTER_WRITE, toptions, 4, NULL, 0);
embeddedartists 1:47680ec5d783 147 if (result != 0) {
embeddedartists 1:47680ec5d783 148 debug("register write request failed (%d)\n", result);
embeddedartists 1:47680ec5d783 149 break;
embeddedartists 1:47680ec5d783 150 }
embeddedartists 1:47680ec5d783 151
embeddedartists 1:47680ec5d783 152 // save registers to eeprom
embeddedartists 1:47680ec5d783 153 result = cmd(AR1021_CMD_REGISTER_WRITE_TO_EEPROM, NULL, 0, NULL, 0);
embeddedartists 1:47680ec5d783 154 if (result != 0) {
embeddedartists 1:47680ec5d783 155 debug("register write to eeprom failed (%d)\n", result);
embeddedartists 1:47680ec5d783 156 break;
embeddedartists 1:47680ec5d783 157 }
embeddedartists 1:47680ec5d783 158
embeddedartists 1:47680ec5d783 159 // enable touch
embeddedartists 1:47680ec5d783 160 result = cmd(AR1021_CMD_ENABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 1:47680ec5d783 161 if (result != 0) {
embeddedartists 1:47680ec5d783 162 debug("enable touch failed (%d)\n", result);
embeddedartists 1:47680ec5d783 163 break;
embeddedartists 1:47680ec5d783 164 }
embeddedartists 1:47680ec5d783 165
embeddedartists 1:47680ec5d783 166 _siqIrq.rise(this, &AR1021I2C::readTouchIrq);
embeddedartists 1:47680ec5d783 167
embeddedartists 1:47680ec5d783 168 _initialized = true;
embeddedartists 1:47680ec5d783 169 ok = true;
embeddedartists 1:47680ec5d783 170
embeddedartists 1:47680ec5d783 171 } while(0);
embeddedartists 1:47680ec5d783 172
embeddedartists 1:47680ec5d783 173 if (ok) break;
embeddedartists 1:47680ec5d783 174
embeddedartists 1:47680ec5d783 175 // try to run the initialize sequence at most 2 times
embeddedartists 1:47680ec5d783 176 if(++attempts >= 2) break;
embeddedartists 1:47680ec5d783 177 }
embeddedartists 1:47680ec5d783 178
embeddedartists 1:47680ec5d783 179
embeddedartists 1:47680ec5d783 180 return ok;
embeddedartists 1:47680ec5d783 181 }
embeddedartists 1:47680ec5d783 182
embeddedartists 1:47680ec5d783 183 bool AR1021I2C::calibrateStart() {
embeddedartists 1:47680ec5d783 184 bool ok = false;
embeddedartists 1:47680ec5d783 185 int result = 0;
embeddedartists 1:47680ec5d783 186 int attempts = 0;
embeddedartists 1:47680ec5d783 187
embeddedartists 1:47680ec5d783 188 if (!_initialized) return false;
embeddedartists 1:47680ec5d783 189
embeddedartists 1:47680ec5d783 190 _siqIrq.rise(NULL);
embeddedartists 1:47680ec5d783 191
embeddedartists 1:47680ec5d783 192 while(1) {
embeddedartists 1:47680ec5d783 193
embeddedartists 1:47680ec5d783 194 do {
embeddedartists 1:47680ec5d783 195 // disable touch
embeddedartists 1:47680ec5d783 196 result = cmd(AR1021_CMD_DISABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 1:47680ec5d783 197 if (result != 0) {
embeddedartists 1:47680ec5d783 198 debug("disable touch failed (%d)\n", result);
embeddedartists 1:47680ec5d783 199 break;
embeddedartists 1:47680ec5d783 200 }
embeddedartists 1:47680ec5d783 201
embeddedartists 1:47680ec5d783 202 char regOffset = 0;
embeddedartists 1:47680ec5d783 203 int regOffLen = 1;
embeddedartists 1:47680ec5d783 204 result = cmd(AR1021_CMD_REGISTER_START_ADDR_REQUEST, NULL, 0,
embeddedartists 1:47680ec5d783 205 &regOffset, &regOffLen);
embeddedartists 1:47680ec5d783 206 if (result != 0) {
embeddedartists 1:47680ec5d783 207 debug("register offset request failed (%d)\n", result);
embeddedartists 1:47680ec5d783 208 break;
embeddedartists 1:47680ec5d783 209 }
embeddedartists 1:47680ec5d783 210
embeddedartists 1:47680ec5d783 211 // set insets
embeddedartists 1:47680ec5d783 212 // enable calibrated coordinates
embeddedartists 1:47680ec5d783 213 // high, low address, len, value
embeddedartists 1:47680ec5d783 214 char insets[4] = {0x00, AR1021_REG_CALIB_INSETS+regOffset, 0x01, _inset};
embeddedartists 1:47680ec5d783 215 result = cmd(AR1021_CMD_REGISTER_WRITE, insets, 4, NULL, 0);
embeddedartists 1:47680ec5d783 216 if (result != 0) {
embeddedartists 1:47680ec5d783 217 debug("register write request failed (%d)\n", result);
embeddedartists 1:47680ec5d783 218 break;
embeddedartists 1:47680ec5d783 219 }
embeddedartists 1:47680ec5d783 220
embeddedartists 1:47680ec5d783 221 // calibration mode
embeddedartists 1:47680ec5d783 222 char calibType = 4;
embeddedartists 1:47680ec5d783 223 result = cmd(AR1021_CMD_CALIBRATE_MODE, &calibType, 1, NULL, 0, false);
embeddedartists 1:47680ec5d783 224 if (result != 0) {
embeddedartists 1:47680ec5d783 225 debug("calibration mode failed (%d)\n", result);
embeddedartists 1:47680ec5d783 226 break;
embeddedartists 1:47680ec5d783 227 }
embeddedartists 1:47680ec5d783 228
embeddedartists 1:47680ec5d783 229 _calibPoint = 0;
embeddedartists 1:47680ec5d783 230 ok = true;
embeddedartists 1:47680ec5d783 231
embeddedartists 1:47680ec5d783 232 } while(0);
embeddedartists 1:47680ec5d783 233
embeddedartists 1:47680ec5d783 234 if (ok) break;
embeddedartists 1:47680ec5d783 235
embeddedartists 1:47680ec5d783 236 // try to run the calibrate mode sequence at most 2 times
embeddedartists 1:47680ec5d783 237 if (++attempts >= 2) break;
embeddedartists 1:47680ec5d783 238 }
embeddedartists 1:47680ec5d783 239
embeddedartists 1:47680ec5d783 240 return ok;
embeddedartists 1:47680ec5d783 241 }
embeddedartists 1:47680ec5d783 242
embeddedartists 1:47680ec5d783 243 bool AR1021I2C::getNextCalibratePoint(uint16_t* x, uint16_t* y) {
embeddedartists 1:47680ec5d783 244
embeddedartists 1:47680ec5d783 245 if (!_initialized) return false;
embeddedartists 1:47680ec5d783 246 if (x == NULL || y == NULL) return false;
embeddedartists 1:47680ec5d783 247
embeddedartists 1:47680ec5d783 248 int xInset = (_width * _inset / 100) / 2;
embeddedartists 1:47680ec5d783 249 int yInset = (_height * _inset / 100) / 2;
embeddedartists 1:47680ec5d783 250
embeddedartists 1:47680ec5d783 251 switch(_calibPoint) {
embeddedartists 1:47680ec5d783 252 case 0:
embeddedartists 1:47680ec5d783 253 *x = xInset;
embeddedartists 1:47680ec5d783 254 *y = yInset;
embeddedartists 1:47680ec5d783 255 break;
embeddedartists 1:47680ec5d783 256 case 1:
embeddedartists 1:47680ec5d783 257 *x = _width - xInset;
embeddedartists 1:47680ec5d783 258 *y = yInset;
embeddedartists 1:47680ec5d783 259 break;
embeddedartists 1:47680ec5d783 260 case 2:
embeddedartists 1:47680ec5d783 261 *x = _width - xInset;
embeddedartists 1:47680ec5d783 262 *y = _height - yInset;
embeddedartists 1:47680ec5d783 263 break;
embeddedartists 1:47680ec5d783 264 case 3:
embeddedartists 1:47680ec5d783 265 *x = xInset;
embeddedartists 1:47680ec5d783 266 *y = _height - yInset;
embeddedartists 1:47680ec5d783 267 break;
embeddedartists 1:47680ec5d783 268 default:
embeddedartists 1:47680ec5d783 269 return false;
embeddedartists 1:47680ec5d783 270 }
embeddedartists 1:47680ec5d783 271
embeddedartists 1:47680ec5d783 272 return true;
embeddedartists 1:47680ec5d783 273 }
embeddedartists 1:47680ec5d783 274
embeddedartists 1:47680ec5d783 275 bool AR1021I2C::waitForCalibratePoint(bool* morePoints, uint32_t timeout) {
embeddedartists 1:47680ec5d783 276 int result = 0;
embeddedartists 1:47680ec5d783 277 bool ret = false;
embeddedartists 1:47680ec5d783 278
embeddedartists 1:47680ec5d783 279 if (!_initialized) return false;
embeddedartists 1:47680ec5d783 280
embeddedartists 1:47680ec5d783 281 do {
embeddedartists 1:47680ec5d783 282 if (morePoints == NULL || _calibPoint >= AR1021_NUM_CALIB_POINTS) {
embeddedartists 1:47680ec5d783 283 break;
embeddedartists 1:47680ec5d783 284 }
embeddedartists 1:47680ec5d783 285
embeddedartists 1:47680ec5d783 286 // wait for response
embeddedartists 1:47680ec5d783 287 result = waitForCalibResponse(timeout);
embeddedartists 1:47680ec5d783 288 if (result != 0) {
embeddedartists 1:47680ec5d783 289 debug("wait for calibration response failed (%d)\n", result);
embeddedartists 1:47680ec5d783 290 break;
embeddedartists 1:47680ec5d783 291 }
embeddedartists 1:47680ec5d783 292
embeddedartists 1:47680ec5d783 293 _calibPoint++;
embeddedartists 1:47680ec5d783 294 *morePoints = (_calibPoint < AR1021_NUM_CALIB_POINTS);
embeddedartists 1:47680ec5d783 295
embeddedartists 1:47680ec5d783 296
embeddedartists 1:47680ec5d783 297 // no more points -> enable touch
embeddedartists 1:47680ec5d783 298 if (!(*morePoints)) {
embeddedartists 1:47680ec5d783 299
embeddedartists 1:47680ec5d783 300 // wait for calibration data to be written to eeprom
embeddedartists 1:47680ec5d783 301 // before enabling touch
embeddedartists 1:47680ec5d783 302 result = waitForCalibResponse(timeout);
embeddedartists 1:47680ec5d783 303 if (result != 0) {
embeddedartists 1:47680ec5d783 304 debug("wait for calibration response failed (%d)\n", result);
embeddedartists 1:47680ec5d783 305 break;
embeddedartists 1:47680ec5d783 306 }
embeddedartists 1:47680ec5d783 307
embeddedartists 1:47680ec5d783 308
embeddedartists 1:47680ec5d783 309 // clear chip-select since calibration is done;
embeddedartists 1:47680ec5d783 310 // _cs = 1;
embeddedartists 1:47680ec5d783 311
embeddedartists 1:47680ec5d783 312 result = cmd(AR1021_CMD_ENABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 1:47680ec5d783 313 if (result != 0) {
embeddedartists 1:47680ec5d783 314 debug("enable touch failed (%d)\n", result);
embeddedartists 1:47680ec5d783 315 break;
embeddedartists 1:47680ec5d783 316 }
embeddedartists 1:47680ec5d783 317
embeddedartists 1:47680ec5d783 318 _siqIrq.rise(this, &AR1021I2C::readTouchIrq);
embeddedartists 1:47680ec5d783 319 }
embeddedartists 1:47680ec5d783 320
embeddedartists 1:47680ec5d783 321 ret = true;
embeddedartists 1:47680ec5d783 322
embeddedartists 1:47680ec5d783 323 } while (0);
embeddedartists 1:47680ec5d783 324
embeddedartists 1:47680ec5d783 325
embeddedartists 1:47680ec5d783 326
embeddedartists 1:47680ec5d783 327 if (!ret) {
embeddedartists 1:47680ec5d783 328 // make sure to set chip-select off in case of an error
embeddedartists 1:47680ec5d783 329 // _cs = 1;
embeddedartists 1:47680ec5d783 330 // calibration must restart if an error occurred
embeddedartists 1:47680ec5d783 331 _calibPoint = AR1021_NUM_CALIB_POINTS+1;
embeddedartists 1:47680ec5d783 332 }
embeddedartists 1:47680ec5d783 333
embeddedartists 1:47680ec5d783 334
embeddedartists 1:47680ec5d783 335
embeddedartists 1:47680ec5d783 336 return ret;
embeddedartists 1:47680ec5d783 337 }
embeddedartists 1:47680ec5d783 338
embeddedartists 1:47680ec5d783 339 int AR1021I2C::cmd(char cmd, char* data, int len, char* respBuf, int* respLen,
embeddedartists 1:47680ec5d783 340 bool setCsOff) {
embeddedartists 1:47680ec5d783 341
embeddedartists 1:47680ec5d783 342 int ret = 0;
embeddedartists 7:48375cb50f3a 343 int readLen = (respLen == NULL) ? 0 : *respLen;
embeddedartists 1:47680ec5d783 344 for (int attempt = 1; attempt <= AR1021_RETRY; attempt++) {
embeddedartists 1:47680ec5d783 345 if (attempt > 1) {
embeddedartists 1:47680ec5d783 346 wait_ms(50);
embeddedartists 1:47680ec5d783 347 }
embeddedartists 1:47680ec5d783 348
embeddedartists 1:47680ec5d783 349 // command request
embeddedartists 1:47680ec5d783 350 // ---------------
embeddedartists 1:47680ec5d783 351 // 0x00 0x55 len cmd data
embeddedartists 1:47680ec5d783 352 // 0x00 = protocol command byte
embeddedartists 1:47680ec5d783 353 // 0x55 = header
embeddedartists 1:47680ec5d783 354 // len = data length + cmd (1)
embeddedartists 1:47680ec5d783 355 // data = data to send
embeddedartists 1:47680ec5d783 356
embeddedartists 1:47680ec5d783 357 _i2c.start();
embeddedartists 1:47680ec5d783 358 _i2c.write(AR1021_ADDR); //send write address
embeddedartists 1:47680ec5d783 359 _i2c.write(0x00);
embeddedartists 1:47680ec5d783 360 _i2c.write(0x55); //header
embeddedartists 1:47680ec5d783 361 _i2c.write(len+1); //message length
embeddedartists 1:47680ec5d783 362 _i2c.write(cmd);
embeddedartists 1:47680ec5d783 363 for (int i = 0; i < len; i++) {
embeddedartists 1:47680ec5d783 364 _i2c.write(data[i]);
embeddedartists 1:47680ec5d783 365 }
embeddedartists 1:47680ec5d783 366 wait_us(60);
embeddedartists 1:47680ec5d783 367 _i2c.stop();
embeddedartists 1:47680ec5d783 368
embeddedartists 1:47680ec5d783 369 // wait for response (siq goes high when response is available)
embeddedartists 1:47680ec5d783 370 Timer t;
embeddedartists 1:47680ec5d783 371 t.start();
embeddedartists 1:47680ec5d783 372 while(_siq.read() != 1 && t.read_ms() < AR1021_TIMEOUT);
embeddedartists 1:47680ec5d783 373
embeddedartists 1:47680ec5d783 374 if (t.read_ms() < AR1021_TIMEOUT) {
embeddedartists 1:47680ec5d783 375
embeddedartists 1:47680ec5d783 376 // command response
embeddedartists 1:47680ec5d783 377 // ---------------
embeddedartists 1:47680ec5d783 378 // 0x55 len status cmd data
embeddedartists 1:47680ec5d783 379 // 0x55 = header
embeddedartists 7:48375cb50f3a 380 // len = number of bytes following the len byte (i.e. including the status&cmd)
embeddedartists 1:47680ec5d783 381 // status = status
embeddedartists 1:47680ec5d783 382 // cmd = command ID
embeddedartists 1:47680ec5d783 383 // data = data to receive
embeddedartists 1:47680ec5d783 384 _i2c.start();
embeddedartists 1:47680ec5d783 385 _i2c.write(AR1021_ADDR + 1); //send read address
embeddedartists 7:48375cb50f3a 386 char header = _i2c.read(1); //header should always be 0x55
embeddedartists 1:47680ec5d783 387 if (header != 0x55) {
embeddedartists 1:47680ec5d783 388 ret = AR1021_ERR_NO_HDR;
embeddedartists 1:47680ec5d783 389 continue;
embeddedartists 1:47680ec5d783 390 }
embeddedartists 7:48375cb50f3a 391 char length = _i2c.read(1); //data length
embeddedartists 1:47680ec5d783 392 if (length < 2) {
embeddedartists 7:48375cb50f3a 393 ret = AR1021_ERR_INV_LEN; //must have at least status and command bytes
embeddedartists 1:47680ec5d783 394 continue;
embeddedartists 1:47680ec5d783 395 }
embeddedartists 7:48375cb50f3a 396 length -= 2;
embeddedartists 7:48375cb50f3a 397 if (length > readLen) {
embeddedartists 7:48375cb50f3a 398 ret = AR1021_ERR_INV_LEN; //supplied buffer is not enough
embeddedartists 1:47680ec5d783 399 continue;
embeddedartists 1:47680ec5d783 400 }
embeddedartists 7:48375cb50f3a 401
embeddedartists 7:48375cb50f3a 402 char status = _i2c.read(1); //command status
embeddedartists 7:48375cb50f3a 403 char usedCmd;
embeddedartists 7:48375cb50f3a 404 if (readLen <= 0) {
embeddedartists 7:48375cb50f3a 405 usedCmd = _i2c.read(0); //no data => read command byte + NACK
embeddedartists 7:48375cb50f3a 406 } else {
embeddedartists 7:48375cb50f3a 407 usedCmd = _i2c.read(1); //which command
embeddedartists 7:48375cb50f3a 408
embeddedartists 7:48375cb50f3a 409 //we need to send a NACK on the last read.
embeddedartists 7:48375cb50f3a 410 int i;
embeddedartists 7:48375cb50f3a 411 for (i = 0; i < (length-1); i++) {
embeddedartists 7:48375cb50f3a 412 respBuf[i] = _i2c.read(1);
embeddedartists 7:48375cb50f3a 413 }
embeddedartists 7:48375cb50f3a 414 respBuf[i] = _i2c.read(0); //last returned data byte + NACK
embeddedartists 7:48375cb50f3a 415 }
embeddedartists 7:48375cb50f3a 416 _i2c.stop();
embeddedartists 1:47680ec5d783 417
embeddedartists 7:48375cb50f3a 418
embeddedartists 7:48375cb50f3a 419 if (status != AR1021_RESP_STAT_OK) {
embeddedartists 7:48375cb50f3a 420 ret = -status;
embeddedartists 7:48375cb50f3a 421 continue;
embeddedartists 7:48375cb50f3a 422 }
embeddedartists 7:48375cb50f3a 423 if (usedCmd != cmd) {
embeddedartists 7:48375cb50f3a 424 ret = AR1021_ERR_INV_RESP;
embeddedartists 7:48375cb50f3a 425 continue;
embeddedartists 1:47680ec5d783 426 }
embeddedartists 1:47680ec5d783 427
embeddedartists 1:47680ec5d783 428 // success
embeddedartists 1:47680ec5d783 429 ret = 0;
embeddedartists 1:47680ec5d783 430 break;
embeddedartists 1:47680ec5d783 431
embeddedartists 1:47680ec5d783 432 } else {
embeddedartists 1:47680ec5d783 433 ret = AR1021_ERR_TIMEOUT;
embeddedartists 1:47680ec5d783 434 continue;
embeddedartists 1:47680ec5d783 435 }
embeddedartists 1:47680ec5d783 436 }
embeddedartists 1:47680ec5d783 437
embeddedartists 1:47680ec5d783 438 return ret;
embeddedartists 1:47680ec5d783 439 }
embeddedartists 1:47680ec5d783 440
embeddedartists 1:47680ec5d783 441 int AR1021I2C::waitForCalibResponse(uint32_t timeout) {
embeddedartists 1:47680ec5d783 442 Timer t;
embeddedartists 1:47680ec5d783 443 int ret = 0;
embeddedartists 1:47680ec5d783 444
embeddedartists 1:47680ec5d783 445 t.start();
embeddedartists 1:47680ec5d783 446
embeddedartists 1:47680ec5d783 447 // wait for siq
embeddedartists 1:47680ec5d783 448 while (_siq.read() != 1 &&
embeddedartists 1:47680ec5d783 449 (timeout == 0 || (uint32_t)t.read_ms() < (int)timeout));
embeddedartists 1:47680ec5d783 450
embeddedartists 1:47680ec5d783 451
embeddedartists 1:47680ec5d783 452 do {
embeddedartists 1:47680ec5d783 453
embeddedartists 1:47680ec5d783 454 if (timeout > 0 && (uint32_t)t.read_ms() >= timeout) {
embeddedartists 1:47680ec5d783 455 ret = AR1021_ERR_TIMEOUT;
embeddedartists 1:47680ec5d783 456 break;
embeddedartists 1:47680ec5d783 457 }
embeddedartists 1:47680ec5d783 458
embeddedartists 1:47680ec5d783 459 // command response
embeddedartists 1:47680ec5d783 460 // ---------------
embeddedartists 1:47680ec5d783 461 // 0x55 len status cmd data
embeddedartists 1:47680ec5d783 462 // 0x55 = header
embeddedartists 1:47680ec5d783 463 // len = number of bytes following the len byte (should be 2)
embeddedartists 1:47680ec5d783 464 // status = status
embeddedartists 1:47680ec5d783 465 // cmd = command ID
embeddedartists 1:47680ec5d783 466 _i2c.start();
embeddedartists 1:47680ec5d783 467 _i2c.write(AR1021_ADDR + 1); //send read address
embeddedartists 1:47680ec5d783 468 char header = _i2c.read(1); //header should always be 0x55
embeddedartists 1:47680ec5d783 469 char length = _i2c.read(1); //data length
embeddedartists 1:47680ec5d783 470
embeddedartists 1:47680ec5d783 471 if (header != 0x55) {
embeddedartists 1:47680ec5d783 472 ret = AR1021_ERR_NO_HDR;
embeddedartists 1:47680ec5d783 473 break;
embeddedartists 1:47680ec5d783 474 }
embeddedartists 1:47680ec5d783 475 if (length < 2) {
embeddedartists 1:47680ec5d783 476 ret = AR1021_ERR_INV_LEN;
embeddedartists 1:47680ec5d783 477 break;
embeddedartists 1:47680ec5d783 478 }
embeddedartists 1:47680ec5d783 479 char status = _i2c.read(1); //status
embeddedartists 1:47680ec5d783 480 char cmd = _i2c.read(0); //command, should be NACK'ed
embeddedartists 1:47680ec5d783 481 _i2c.stop();
embeddedartists 1:47680ec5d783 482 if (status != AR1021_RESP_STAT_OK) {
embeddedartists 1:47680ec5d783 483 ret = -status;
embeddedartists 1:47680ec5d783 484 break;
embeddedartists 1:47680ec5d783 485 }
embeddedartists 1:47680ec5d783 486 if (cmd != AR1021_CMD_CALIBRATE_MODE) {
embeddedartists 1:47680ec5d783 487 ret = AR1021_ERR_INV_RESP;
embeddedartists 1:47680ec5d783 488 break;
embeddedartists 1:47680ec5d783 489 }
embeddedartists 1:47680ec5d783 490
embeddedartists 1:47680ec5d783 491 // success
embeddedartists 1:47680ec5d783 492 ret = 0;
embeddedartists 1:47680ec5d783 493
embeddedartists 1:47680ec5d783 494 } while (0);
embeddedartists 1:47680ec5d783 495
embeddedartists 1:47680ec5d783 496 return ret;
embeddedartists 1:47680ec5d783 497 }
embeddedartists 1:47680ec5d783 498
embeddedartists 1:47680ec5d783 499
embeddedartists 1:47680ec5d783 500 void AR1021I2C::readTouchIrq() {
embeddedartists 1:47680ec5d783 501 while(_siq.read() == 1) {
embeddedartists 1:47680ec5d783 502
embeddedartists 1:47680ec5d783 503 // touch coordinates are sent in a 5-byte data packet
embeddedartists 1:47680ec5d783 504 _i2c.start();
embeddedartists 1:47680ec5d783 505 _i2c.write(AR1021_ADDR + 1); //send read address
embeddedartists 1:47680ec5d783 506 int pen = _i2c.read(1);
embeddedartists 1:47680ec5d783 507 int xlo = _i2c.read(1);
embeddedartists 1:47680ec5d783 508 int xhi = _i2c.read(1);
embeddedartists 1:47680ec5d783 509 int ylo = _i2c.read(1);
embeddedartists 1:47680ec5d783 510 int yhi = _i2c.read(0);
embeddedartists 1:47680ec5d783 511 _i2c.stop();
embeddedartists 1:47680ec5d783 512
embeddedartists 1:47680ec5d783 513 // pen down
embeddedartists 1:47680ec5d783 514 if ((pen&AR1021_PEN_MASK) == (1<<7|1<<0)) {
embeddedartists 1:47680ec5d783 515 _pen = 1;
embeddedartists 1:47680ec5d783 516 }
embeddedartists 1:47680ec5d783 517 // pen up
embeddedartists 1:47680ec5d783 518 else if ((pen&AR1021_PEN_MASK) == (1<<7)){
embeddedartists 1:47680ec5d783 519 _pen = 0;
embeddedartists 1:47680ec5d783 520 }
embeddedartists 1:47680ec5d783 521 // invalid value
embeddedartists 1:47680ec5d783 522 else {
embeddedartists 1:47680ec5d783 523 continue;
embeddedartists 1:47680ec5d783 524 }
embeddedartists 1:47680ec5d783 525
embeddedartists 1:47680ec5d783 526 _x = ((xhi<<7)|xlo);
embeddedartists 1:47680ec5d783 527 _y = ((yhi<<7)|ylo);
embeddedartists 1:47680ec5d783 528 }
embeddedartists 1:47680ec5d783 529 }
embeddedartists 1:47680ec5d783 530
embeddedartists 1:47680ec5d783 531 bool AR1021I2C::info(int* verHigh, int* verLow, int* resBits, int* type)
embeddedartists 1:47680ec5d783 532 {
embeddedartists 1:47680ec5d783 533 char buff[3] = {0,0,0};
embeddedartists 1:47680ec5d783 534 int read = 3;
embeddedartists 1:47680ec5d783 535 int res = cmd(AR1021_CMD_GET_VERSION, NULL, 0, buff, &read);
embeddedartists 1:47680ec5d783 536 if (res == 0) {
embeddedartists 1:47680ec5d783 537 *verHigh = buff[0];
embeddedartists 1:47680ec5d783 538 *verLow = buff[1];
embeddedartists 1:47680ec5d783 539 switch(buff[2] & 3) {
embeddedartists 1:47680ec5d783 540 case 0:
embeddedartists 1:47680ec5d783 541 *resBits = 8;
embeddedartists 1:47680ec5d783 542 break;
embeddedartists 1:47680ec5d783 543 case 1:
embeddedartists 1:47680ec5d783 544 *resBits = 10;
embeddedartists 1:47680ec5d783 545 break;
embeddedartists 1:47680ec5d783 546 case 2:
embeddedartists 1:47680ec5d783 547 *resBits = 12;
embeddedartists 1:47680ec5d783 548 break;
embeddedartists 1:47680ec5d783 549 case 3:
embeddedartists 1:47680ec5d783 550 *resBits = 12;
embeddedartists 1:47680ec5d783 551 break;
embeddedartists 1:47680ec5d783 552 default:
embeddedartists 1:47680ec5d783 553 res = 25;
embeddedartists 1:47680ec5d783 554 printf("Invalid resolution %d\n", buff[2]&3);
embeddedartists 1:47680ec5d783 555 break;
embeddedartists 1:47680ec5d783 556 }
embeddedartists 1:47680ec5d783 557 *type = buff[2]>>2;
embeddedartists 1:47680ec5d783 558 }
embeddedartists 1:47680ec5d783 559 return (res == 0);
embeddedartists 1:47680ec5d783 560 }