A library with drivers for different peripherals on the LPC4088 QuickStart Board or related add-on boards.

Dependencies:   FATFileSystem

Dependents:   LPC4088test LPC4088test_ledonly LPC4088test_deleteall LPC4088_RAMtest ... more

Committer:
embeddedartists
Date:
Wed Jun 10 08:34:09 2015 +0000
Revision:
20:e1e36493f347
Parent:
19:74540582e639
Fixed compiler error in MCIFileSystem regarding us_ticker_api.h

Who changed what in which revision?

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