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:
Thu Jan 30 08:50:47 2014 +0100
Revision:
12:15597e45eea0
Parent:
4:b32cf4ef45c5
Child:
19:74540582e639
Added license information

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 4:b32cf4ef45c5 124 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 125 debug("disable touch failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 126 break;
embeddedartists 4:b32cf4ef45c5 127 }
embeddedartists 4:b32cf4ef45c5 128
embeddedartists 4:b32cf4ef45c5 129 char regOffset = 0;
embeddedartists 4:b32cf4ef45c5 130 int regOffLen = 1;
embeddedartists 4:b32cf4ef45c5 131 result = cmd(AR1021_CMD_REGISTER_START_ADDR_REQUEST, NULL, 0,
embeddedartists 4:b32cf4ef45c5 132 &regOffset, &regOffLen);
embeddedartists 4:b32cf4ef45c5 133 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 134 debug("register offset request failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 135 break;
embeddedartists 4:b32cf4ef45c5 136 }
embeddedartists 4:b32cf4ef45c5 137
embeddedartists 4:b32cf4ef45c5 138 // enable calibrated coordinates
embeddedartists 4:b32cf4ef45c5 139 // high, low address, len, value
embeddedartists 4:b32cf4ef45c5 140 char toptions[4] = {0x00, AR1021_REG_TOUCH_OPTIONS+regOffset, 0x01, 0x01};
embeddedartists 4:b32cf4ef45c5 141 result = cmd(AR1021_CMD_REGISTER_WRITE, toptions, 4, NULL, 0);
embeddedartists 4:b32cf4ef45c5 142 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 143 debug("register write request failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 144 break;
embeddedartists 4:b32cf4ef45c5 145 }
embeddedartists 4:b32cf4ef45c5 146
embeddedartists 4:b32cf4ef45c5 147 // save registers to eeprom
embeddedartists 4:b32cf4ef45c5 148 result = cmd(AR1021_CMD_REGISTER_WRITE_TO_EEPROM, NULL, 0, NULL, 0);
embeddedartists 4:b32cf4ef45c5 149 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 150 debug("register write to eeprom failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 151 break;
embeddedartists 4:b32cf4ef45c5 152 }
embeddedartists 4:b32cf4ef45c5 153
embeddedartists 4:b32cf4ef45c5 154 // enable touch
embeddedartists 4:b32cf4ef45c5 155 result = cmd(AR1021_CMD_ENABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 4:b32cf4ef45c5 156 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 157 debug("enable touch failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 158 break;
embeddedartists 4:b32cf4ef45c5 159 }
embeddedartists 4:b32cf4ef45c5 160
embeddedartists 4:b32cf4ef45c5 161 _siqIrq.rise(this, &AR1021::readTouchIrq);
embeddedartists 4:b32cf4ef45c5 162
embeddedartists 4:b32cf4ef45c5 163 _initialized = true;
embeddedartists 4:b32cf4ef45c5 164 ok = true;
embeddedartists 4:b32cf4ef45c5 165
embeddedartists 4:b32cf4ef45c5 166 } while(0);
embeddedartists 4:b32cf4ef45c5 167
embeddedartists 4:b32cf4ef45c5 168 if (ok) break;
embeddedartists 4:b32cf4ef45c5 169
embeddedartists 4:b32cf4ef45c5 170 // try to run the initialize sequence at most 2 times
embeddedartists 4:b32cf4ef45c5 171 if(++attempts >= 2) break;
embeddedartists 4:b32cf4ef45c5 172 }
embeddedartists 4:b32cf4ef45c5 173
embeddedartists 4:b32cf4ef45c5 174
embeddedartists 4:b32cf4ef45c5 175 return ok;
embeddedartists 4:b32cf4ef45c5 176 }
embeddedartists 4:b32cf4ef45c5 177
embeddedartists 4:b32cf4ef45c5 178 bool AR1021::calibrateStart() {
embeddedartists 4:b32cf4ef45c5 179 bool ok = false;
embeddedartists 4:b32cf4ef45c5 180 int result = 0;
embeddedartists 4:b32cf4ef45c5 181 int attempts = 0;
embeddedartists 4:b32cf4ef45c5 182
embeddedartists 4:b32cf4ef45c5 183 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 184
embeddedartists 4:b32cf4ef45c5 185 _siqIrq.rise(NULL);
embeddedartists 4:b32cf4ef45c5 186
embeddedartists 4:b32cf4ef45c5 187 while(1) {
embeddedartists 4:b32cf4ef45c5 188
embeddedartists 4:b32cf4ef45c5 189 do {
embeddedartists 4:b32cf4ef45c5 190 // disable touch
embeddedartists 4:b32cf4ef45c5 191 result = cmd(AR1021_CMD_DISABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 4:b32cf4ef45c5 192 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 193 debug("disable touch failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 194 break;
embeddedartists 4:b32cf4ef45c5 195 }
embeddedartists 4:b32cf4ef45c5 196
embeddedartists 4:b32cf4ef45c5 197 char regOffset = 0;
embeddedartists 4:b32cf4ef45c5 198 int regOffLen = 1;
embeddedartists 4:b32cf4ef45c5 199 result = cmd(AR1021_CMD_REGISTER_START_ADDR_REQUEST, NULL, 0,
embeddedartists 4:b32cf4ef45c5 200 &regOffset, &regOffLen);
embeddedartists 4:b32cf4ef45c5 201 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 202 debug("register offset request failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 203 break;
embeddedartists 4:b32cf4ef45c5 204 }
embeddedartists 4:b32cf4ef45c5 205
embeddedartists 4:b32cf4ef45c5 206 // set insets
embeddedartists 4:b32cf4ef45c5 207 // enable calibrated coordinates
embeddedartists 4:b32cf4ef45c5 208 // high, low address, len, value
embeddedartists 4:b32cf4ef45c5 209 char insets[4] = {0x00, AR1021_REG_CALIB_INSETS+regOffset, 0x01, _inset};
embeddedartists 4:b32cf4ef45c5 210 result = cmd(AR1021_CMD_REGISTER_WRITE, insets, 4, NULL, 0);
embeddedartists 4:b32cf4ef45c5 211 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 212 debug("register write request failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 213 break;
embeddedartists 4:b32cf4ef45c5 214 }
embeddedartists 4:b32cf4ef45c5 215
embeddedartists 4:b32cf4ef45c5 216 // calibration mode
embeddedartists 4:b32cf4ef45c5 217 char calibType = 4;
embeddedartists 4:b32cf4ef45c5 218 result = cmd(AR1021_CMD_CALIBRATE_MODE, &calibType, 1, NULL, 0, false);
embeddedartists 4:b32cf4ef45c5 219 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 220 debug("calibration mode failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 221 break;
embeddedartists 4:b32cf4ef45c5 222 }
embeddedartists 4:b32cf4ef45c5 223
embeddedartists 4:b32cf4ef45c5 224 _calibPoint = 0;
embeddedartists 4:b32cf4ef45c5 225 ok = true;
embeddedartists 4:b32cf4ef45c5 226
embeddedartists 4:b32cf4ef45c5 227 } while(0);
embeddedartists 4:b32cf4ef45c5 228
embeddedartists 4:b32cf4ef45c5 229 if (ok) break;
embeddedartists 4:b32cf4ef45c5 230
embeddedartists 4:b32cf4ef45c5 231 // try to run the calibrate mode sequence at most 2 times
embeddedartists 4:b32cf4ef45c5 232 if (++attempts >= 2) break;
embeddedartists 4:b32cf4ef45c5 233 }
embeddedartists 4:b32cf4ef45c5 234
embeddedartists 4:b32cf4ef45c5 235 return ok;
embeddedartists 4:b32cf4ef45c5 236 }
embeddedartists 4:b32cf4ef45c5 237
embeddedartists 4:b32cf4ef45c5 238 bool AR1021::getNextCalibratePoint(uint16_t* x, uint16_t* y) {
embeddedartists 4:b32cf4ef45c5 239
embeddedartists 4:b32cf4ef45c5 240 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 241 if (x == NULL || y == NULL) return false;
embeddedartists 4:b32cf4ef45c5 242
embeddedartists 4:b32cf4ef45c5 243 int xInset = (_width * _inset / 100) / 2;
embeddedartists 4:b32cf4ef45c5 244 int yInset = (_height * _inset / 100) / 2;
embeddedartists 4:b32cf4ef45c5 245
embeddedartists 4:b32cf4ef45c5 246 switch(_calibPoint) {
embeddedartists 4:b32cf4ef45c5 247 case 0:
embeddedartists 4:b32cf4ef45c5 248 *x = xInset;
embeddedartists 4:b32cf4ef45c5 249 *y = yInset;
embeddedartists 4:b32cf4ef45c5 250 break;
embeddedartists 4:b32cf4ef45c5 251 case 1:
embeddedartists 4:b32cf4ef45c5 252 *x = _width - xInset;
embeddedartists 4:b32cf4ef45c5 253 *y = yInset;
embeddedartists 4:b32cf4ef45c5 254 break;
embeddedartists 4:b32cf4ef45c5 255 case 2:
embeddedartists 4:b32cf4ef45c5 256 *x = _width - xInset;
embeddedartists 4:b32cf4ef45c5 257 *y = _height - yInset;
embeddedartists 4:b32cf4ef45c5 258 break;
embeddedartists 4:b32cf4ef45c5 259 case 3:
embeddedartists 4:b32cf4ef45c5 260 *x = xInset;
embeddedartists 4:b32cf4ef45c5 261 *y = _height - yInset;
embeddedartists 4:b32cf4ef45c5 262 break;
embeddedartists 4:b32cf4ef45c5 263 default:
embeddedartists 4:b32cf4ef45c5 264 return false;
embeddedartists 4:b32cf4ef45c5 265 }
embeddedartists 4:b32cf4ef45c5 266
embeddedartists 4:b32cf4ef45c5 267 return true;
embeddedartists 4:b32cf4ef45c5 268 }
embeddedartists 4:b32cf4ef45c5 269
embeddedartists 4:b32cf4ef45c5 270 bool AR1021::waitForCalibratePoint(bool* morePoints, uint32_t timeout) {
embeddedartists 4:b32cf4ef45c5 271 int result = 0;
embeddedartists 4:b32cf4ef45c5 272 bool ret = false;
embeddedartists 4:b32cf4ef45c5 273
embeddedartists 4:b32cf4ef45c5 274 if (!_initialized) return false;
embeddedartists 4:b32cf4ef45c5 275
embeddedartists 4:b32cf4ef45c5 276 do {
embeddedartists 4:b32cf4ef45c5 277 if (morePoints == NULL || _calibPoint >= AR1021_NUM_CALIB_POINTS) {
embeddedartists 4:b32cf4ef45c5 278 break;
embeddedartists 4:b32cf4ef45c5 279 }
embeddedartists 4:b32cf4ef45c5 280
embeddedartists 4:b32cf4ef45c5 281 // wait for response
embeddedartists 4:b32cf4ef45c5 282 result = waitForCalibResponse(timeout);
embeddedartists 4:b32cf4ef45c5 283 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 284 debug("wait for calibration response failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 285 break;
embeddedartists 4:b32cf4ef45c5 286 }
embeddedartists 4:b32cf4ef45c5 287
embeddedartists 4:b32cf4ef45c5 288 _calibPoint++;
embeddedartists 4:b32cf4ef45c5 289 *morePoints = (_calibPoint < AR1021_NUM_CALIB_POINTS);
embeddedartists 4:b32cf4ef45c5 290
embeddedartists 4:b32cf4ef45c5 291
embeddedartists 4:b32cf4ef45c5 292 // no more points -> enable touch
embeddedartists 4:b32cf4ef45c5 293 if (!(*morePoints)) {
embeddedartists 4:b32cf4ef45c5 294
embeddedartists 4:b32cf4ef45c5 295 // wait for calibration data to be written to eeprom
embeddedartists 4:b32cf4ef45c5 296 // before enabling touch
embeddedartists 4:b32cf4ef45c5 297 result = waitForCalibResponse(timeout);
embeddedartists 4:b32cf4ef45c5 298 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 299 debug("wait for calibration response failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 300 break;
embeddedartists 4:b32cf4ef45c5 301 }
embeddedartists 4:b32cf4ef45c5 302
embeddedartists 4:b32cf4ef45c5 303
embeddedartists 4:b32cf4ef45c5 304 // clear chip-select since calibration is done;
embeddedartists 4:b32cf4ef45c5 305 _cs = 1;
embeddedartists 4:b32cf4ef45c5 306
embeddedartists 4:b32cf4ef45c5 307 result = cmd(AR1021_CMD_ENABLE_TOUCH, NULL, 0, NULL, 0);
embeddedartists 4:b32cf4ef45c5 308 if (result != 0) {
embeddedartists 4:b32cf4ef45c5 309 debug("enable touch failed (%d)\n", result);
embeddedartists 4:b32cf4ef45c5 310 break;
embeddedartists 4:b32cf4ef45c5 311 }
embeddedartists 4:b32cf4ef45c5 312
embeddedartists 4:b32cf4ef45c5 313 _siqIrq.rise(this, &AR1021::readTouchIrq);
embeddedartists 4:b32cf4ef45c5 314 }
embeddedartists 4:b32cf4ef45c5 315
embeddedartists 4:b32cf4ef45c5 316 ret = true;
embeddedartists 4:b32cf4ef45c5 317
embeddedartists 4:b32cf4ef45c5 318 } while (0);
embeddedartists 4:b32cf4ef45c5 319
embeddedartists 4:b32cf4ef45c5 320
embeddedartists 4:b32cf4ef45c5 321
embeddedartists 4:b32cf4ef45c5 322 if (!ret) {
embeddedartists 4:b32cf4ef45c5 323 // make sure to set chip-select off in case of an error
embeddedartists 4:b32cf4ef45c5 324 _cs = 1;
embeddedartists 4:b32cf4ef45c5 325 // calibration must restart if an error occurred
embeddedartists 4:b32cf4ef45c5 326 _calibPoint = AR1021_NUM_CALIB_POINTS+1;
embeddedartists 4:b32cf4ef45c5 327 }
embeddedartists 4:b32cf4ef45c5 328
embeddedartists 4:b32cf4ef45c5 329
embeddedartists 4:b32cf4ef45c5 330
embeddedartists 4:b32cf4ef45c5 331 return ret;
embeddedartists 4:b32cf4ef45c5 332 }
embeddedartists 4:b32cf4ef45c5 333
embeddedartists 4:b32cf4ef45c5 334 int AR1021::cmd(char cmd, char* data, int len, char* respBuf, int* respLen,
embeddedartists 4:b32cf4ef45c5 335 bool setCsOff) {
embeddedartists 4:b32cf4ef45c5 336
embeddedartists 4:b32cf4ef45c5 337 int ret = 0;
embeddedartists 4:b32cf4ef45c5 338
embeddedartists 4:b32cf4ef45c5 339 // command request
embeddedartists 4:b32cf4ef45c5 340 // ---------------
embeddedartists 4:b32cf4ef45c5 341 // 0x55 len cmd data
embeddedartists 4:b32cf4ef45c5 342 // 0x55 = header
embeddedartists 4:b32cf4ef45c5 343 // len = data length + cmd (1)
embeddedartists 4:b32cf4ef45c5 344 // data = data to send
embeddedartists 4:b32cf4ef45c5 345
embeddedartists 4:b32cf4ef45c5 346 _cs = 0;
embeddedartists 4:b32cf4ef45c5 347
embeddedartists 4:b32cf4ef45c5 348 _spi.write(0x55);
embeddedartists 4:b32cf4ef45c5 349 wait_us(50); // according to data sheet there must be an inter-byte delay of ~50us
embeddedartists 4:b32cf4ef45c5 350 _spi.write(len+1);
embeddedartists 4:b32cf4ef45c5 351 wait_us(50);
embeddedartists 4:b32cf4ef45c5 352 _spi.write(cmd);
embeddedartists 4:b32cf4ef45c5 353 wait_us(50);
embeddedartists 4:b32cf4ef45c5 354
embeddedartists 4:b32cf4ef45c5 355 for(int i = 0; i < len; i++) {
embeddedartists 4:b32cf4ef45c5 356 _spi.write(data[i]);
embeddedartists 4:b32cf4ef45c5 357 wait_us(50);
embeddedartists 4:b32cf4ef45c5 358 }
embeddedartists 4:b32cf4ef45c5 359
embeddedartists 4:b32cf4ef45c5 360
embeddedartists 4:b32cf4ef45c5 361 // wait for response (siq goes high when response is available)
embeddedartists 4:b32cf4ef45c5 362 Timer t;
embeddedartists 4:b32cf4ef45c5 363 t.start();
embeddedartists 4:b32cf4ef45c5 364 while(_siq.read() != 1 && t.read_ms() < 1000);
embeddedartists 4:b32cf4ef45c5 365
embeddedartists 4:b32cf4ef45c5 366
embeddedartists 4:b32cf4ef45c5 367 // command response
embeddedartists 4:b32cf4ef45c5 368 // ---------------
embeddedartists 4:b32cf4ef45c5 369 // 0x55 len status cmd data
embeddedartists 4:b32cf4ef45c5 370 // 0x55 = header
embeddedartists 4:b32cf4ef45c5 371 // len = number of bytes following the len byte
embeddedartists 4:b32cf4ef45c5 372 // status = status
embeddedartists 4:b32cf4ef45c5 373 // cmd = command ID
embeddedartists 4:b32cf4ef45c5 374 // data = data to receive
embeddedartists 4:b32cf4ef45c5 375
embeddedartists 4:b32cf4ef45c5 376
embeddedartists 4:b32cf4ef45c5 377 do {
embeddedartists 4:b32cf4ef45c5 378
embeddedartists 4:b32cf4ef45c5 379 if (t.read_ms() >= 1000) {
embeddedartists 4:b32cf4ef45c5 380 ret = AR1021_ERR_TIMEOUT;
embeddedartists 4:b32cf4ef45c5 381 break;
embeddedartists 4:b32cf4ef45c5 382 }
embeddedartists 4:b32cf4ef45c5 383
embeddedartists 4:b32cf4ef45c5 384 int head = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 385 if (head != 0x55) {
embeddedartists 4:b32cf4ef45c5 386 ret = AR1021_ERR_NO_HDR;
embeddedartists 4:b32cf4ef45c5 387 break;
embeddedartists 4:b32cf4ef45c5 388 }
embeddedartists 4:b32cf4ef45c5 389
embeddedartists 4:b32cf4ef45c5 390 wait_us(50);
embeddedartists 4:b32cf4ef45c5 391 int len = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 392 if (len < 2) {
embeddedartists 4:b32cf4ef45c5 393 ret = AR1021_ERR_INV_LEN;
embeddedartists 4:b32cf4ef45c5 394 break;
embeddedartists 4:b32cf4ef45c5 395 }
embeddedartists 4:b32cf4ef45c5 396
embeddedartists 4:b32cf4ef45c5 397 wait_us(50);
embeddedartists 4:b32cf4ef45c5 398 int status = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 399 if (status != AR1021_RESP_STAT_OK) {
embeddedartists 4:b32cf4ef45c5 400 ret = -status;
embeddedartists 4:b32cf4ef45c5 401 break;
embeddedartists 4:b32cf4ef45c5 402 }
embeddedartists 4:b32cf4ef45c5 403
embeddedartists 4:b32cf4ef45c5 404 wait_us(50);
embeddedartists 4:b32cf4ef45c5 405 int cmdId = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 406 if (cmdId != cmd) {
embeddedartists 4:b32cf4ef45c5 407 ret = AR1021_ERR_INV_RESP;
embeddedartists 4:b32cf4ef45c5 408 break;
embeddedartists 4:b32cf4ef45c5 409 }
embeddedartists 4:b32cf4ef45c5 410
embeddedartists 4:b32cf4ef45c5 411 if ( ((len-2) > 0 && respLen == NULL)
embeddedartists 4:b32cf4ef45c5 412 || ((len-2) > 0 && respLen != NULL && *respLen < (len-2))) {
embeddedartists 4:b32cf4ef45c5 413 ret = AR1021_ERR_INV_RESPLEN;
embeddedartists 4:b32cf4ef45c5 414 break;
embeddedartists 4:b32cf4ef45c5 415 }
embeddedartists 4:b32cf4ef45c5 416
embeddedartists 4:b32cf4ef45c5 417 for (int i = 0; i < len-2;i++) {
embeddedartists 4:b32cf4ef45c5 418 wait_us(50);
embeddedartists 4:b32cf4ef45c5 419 respBuf[i] = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 420 }
embeddedartists 4:b32cf4ef45c5 421 if (respLen != NULL) {
embeddedartists 4:b32cf4ef45c5 422 *respLen = len-2;
embeddedartists 4:b32cf4ef45c5 423 }
embeddedartists 4:b32cf4ef45c5 424
embeddedartists 4:b32cf4ef45c5 425 // make sure we wait 50us before issuing a new cmd
embeddedartists 4:b32cf4ef45c5 426 wait_us(50);
embeddedartists 4:b32cf4ef45c5 427
embeddedartists 4:b32cf4ef45c5 428 } while (0);
embeddedartists 4:b32cf4ef45c5 429
embeddedartists 4:b32cf4ef45c5 430
embeddedartists 4:b32cf4ef45c5 431
embeddedartists 4:b32cf4ef45c5 432 // disable chip-select if setCsOff is true or if an error occurred
embeddedartists 4:b32cf4ef45c5 433 if (setCsOff || ret != 0) {
embeddedartists 4:b32cf4ef45c5 434 _cs = 1;
embeddedartists 4:b32cf4ef45c5 435 }
embeddedartists 4:b32cf4ef45c5 436
embeddedartists 4:b32cf4ef45c5 437
embeddedartists 4:b32cf4ef45c5 438
embeddedartists 4:b32cf4ef45c5 439 return ret;
embeddedartists 4:b32cf4ef45c5 440 }
embeddedartists 4:b32cf4ef45c5 441
embeddedartists 4:b32cf4ef45c5 442 int AR1021::waitForCalibResponse(uint32_t timeout) {
embeddedartists 4:b32cf4ef45c5 443 Timer t;
embeddedartists 4:b32cf4ef45c5 444 int ret = 0;
embeddedartists 4:b32cf4ef45c5 445
embeddedartists 4:b32cf4ef45c5 446 t.start();
embeddedartists 4:b32cf4ef45c5 447
embeddedartists 4:b32cf4ef45c5 448 // wait for siq
embeddedartists 4:b32cf4ef45c5 449 while (_siq.read() != 1 &&
embeddedartists 4:b32cf4ef45c5 450 (timeout == 0 || (uint32_t)t.read_ms() < (int)timeout));
embeddedartists 4:b32cf4ef45c5 451
embeddedartists 4:b32cf4ef45c5 452
embeddedartists 4:b32cf4ef45c5 453 do {
embeddedartists 4:b32cf4ef45c5 454
embeddedartists 4:b32cf4ef45c5 455 if (timeout > 0 && (uint32_t)t.read_ms() >= timeout) {
embeddedartists 4:b32cf4ef45c5 456 ret = AR1021_ERR_TIMEOUT;
embeddedartists 4:b32cf4ef45c5 457 break;
embeddedartists 4:b32cf4ef45c5 458 }
embeddedartists 4:b32cf4ef45c5 459
embeddedartists 4:b32cf4ef45c5 460 int head = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 461 if (head != 0x55) {
embeddedartists 4:b32cf4ef45c5 462 ret = AR1021_ERR_NO_HDR;
embeddedartists 4:b32cf4ef45c5 463 break;
embeddedartists 4:b32cf4ef45c5 464 }
embeddedartists 4:b32cf4ef45c5 465
embeddedartists 4:b32cf4ef45c5 466 wait_us(50);
embeddedartists 4:b32cf4ef45c5 467 int len = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 468 if (len != 2) {
embeddedartists 4:b32cf4ef45c5 469 ret = AR1021_ERR_INV_LEN;
embeddedartists 4:b32cf4ef45c5 470 break;
embeddedartists 4:b32cf4ef45c5 471 }
embeddedartists 4:b32cf4ef45c5 472
embeddedartists 4:b32cf4ef45c5 473 wait_us(50);
embeddedartists 4:b32cf4ef45c5 474 int status = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 475 if (status != AR1021_RESP_STAT_OK) {
embeddedartists 4:b32cf4ef45c5 476 ret = -status;
embeddedartists 4:b32cf4ef45c5 477 break;
embeddedartists 4:b32cf4ef45c5 478 }
embeddedartists 4:b32cf4ef45c5 479
embeddedartists 4:b32cf4ef45c5 480 wait_us(50);
embeddedartists 4:b32cf4ef45c5 481 int cmdId = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 482 if (cmdId != 0x14) {
embeddedartists 4:b32cf4ef45c5 483 ret = AR1021_ERR_INV_RESP;
embeddedartists 4:b32cf4ef45c5 484 break;
embeddedartists 4:b32cf4ef45c5 485 }
embeddedartists 4:b32cf4ef45c5 486
embeddedartists 4:b32cf4ef45c5 487
embeddedartists 4:b32cf4ef45c5 488 // make sure we wait 50us before issuing a new cmd
embeddedartists 4:b32cf4ef45c5 489 wait_us(50);
embeddedartists 4:b32cf4ef45c5 490
embeddedartists 4:b32cf4ef45c5 491 } while (0);
embeddedartists 4:b32cf4ef45c5 492
embeddedartists 4:b32cf4ef45c5 493
embeddedartists 4:b32cf4ef45c5 494 return ret;
embeddedartists 4:b32cf4ef45c5 495 }
embeddedartists 4:b32cf4ef45c5 496
embeddedartists 4:b32cf4ef45c5 497
embeddedartists 4:b32cf4ef45c5 498 void AR1021::readTouchIrq() {
embeddedartists 4:b32cf4ef45c5 499 while(_siq.read() == 1) {
embeddedartists 4:b32cf4ef45c5 500
embeddedartists 4:b32cf4ef45c5 501 _cs = 0;
embeddedartists 4:b32cf4ef45c5 502
embeddedartists 4:b32cf4ef45c5 503 // touch coordinates are sent in a 5-byte data packet
embeddedartists 4:b32cf4ef45c5 504
embeddedartists 4:b32cf4ef45c5 505 int pen = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 506 wait_us(50);
embeddedartists 4:b32cf4ef45c5 507
embeddedartists 4:b32cf4ef45c5 508 int xlo = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 509 wait_us(50);
embeddedartists 4:b32cf4ef45c5 510
embeddedartists 4:b32cf4ef45c5 511 int xhi = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 512 wait_us(50);
embeddedartists 4:b32cf4ef45c5 513
embeddedartists 4:b32cf4ef45c5 514 int ylo = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 515 wait_us(50);
embeddedartists 4:b32cf4ef45c5 516
embeddedartists 4:b32cf4ef45c5 517 int yhi = _spi.write(0);
embeddedartists 4:b32cf4ef45c5 518 wait_us(50);
embeddedartists 4:b32cf4ef45c5 519
embeddedartists 4:b32cf4ef45c5 520 _cs = 1;
embeddedartists 4:b32cf4ef45c5 521
embeddedartists 4:b32cf4ef45c5 522
embeddedartists 4:b32cf4ef45c5 523 // pen down
embeddedartists 4:b32cf4ef45c5 524 if ((pen&AR1021_PEN_MASK) == (1<<7|1<<0)) {
embeddedartists 4:b32cf4ef45c5 525 _pen = 1;
embeddedartists 4:b32cf4ef45c5 526 }
embeddedartists 4:b32cf4ef45c5 527 // pen up
embeddedartists 4:b32cf4ef45c5 528 else if ((pen&AR1021_PEN_MASK) == (1<<7)){
embeddedartists 4:b32cf4ef45c5 529 _pen = 0;
embeddedartists 4:b32cf4ef45c5 530 }
embeddedartists 4:b32cf4ef45c5 531 // invalid value
embeddedartists 4:b32cf4ef45c5 532 else {
embeddedartists 4:b32cf4ef45c5 533 continue;
embeddedartists 4:b32cf4ef45c5 534 }
embeddedartists 4:b32cf4ef45c5 535
embeddedartists 4:b32cf4ef45c5 536 _x = ((xhi<<7)|xlo);
embeddedartists 4:b32cf4ef45c5 537 _y = ((yhi<<7)|ylo);
embeddedartists 4:b32cf4ef45c5 538
embeddedartists 4:b32cf4ef45c5 539 }
embeddedartists 4:b32cf4ef45c5 540 }
embeddedartists 4:b32cf4ef45c5 541