Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This program decodes decodes and shows two png images.

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Fri Oct 03 13:30:09 2014 +0000
Revision:
0:b567d56a59d7
First version

Who changed what in which revision?

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