| 1 | #include <Arduino.h> |
|---|
| 2 | |
|---|
| 3 | #if defined(__SAMD21G18A__) |
|---|
| 4 | #ifndef ADAFRUIT_FREETOUCH_H |
|---|
| 5 | #define ADAFRUIT_FREETOUCH_H |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | /*************** CONTROL A register ***************/ |
|---|
| 9 | #define PTC_REG_CONTROLA 0x42004C00 |
|---|
| 10 | #define PTC_BIT_ENABLE 0x02 |
|---|
| 11 | #define PTC_BIT_RUNINSTBY 0x04 |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | typedef union { |
|---|
| 15 | struct { |
|---|
| 16 | uint8_t SWRESET:1; |
|---|
| 17 | uint8_t ENABLE:1; |
|---|
| 18 | uint8_t RUNINSTANDBY:1; |
|---|
| 19 | uint8_t __pad0__:5; |
|---|
| 20 | } bit; |
|---|
| 21 | uint8_t reg; |
|---|
| 22 | } PTC_REG_CONTROLA_Type; |
|---|
| 23 | |
|---|
| 24 | /*************** CONTROL B register ***************/ |
|---|
| 25 | |
|---|
| 26 | #define PTC_REG_CONTROLB 0x42004C01 |
|---|
| 27 | #define PTC_BIT_SYNCFLAG 0x80 |
|---|
| 28 | |
|---|
| 29 | typedef union { |
|---|
| 30 | struct { |
|---|
| 31 | uint8_t __pad0__:7; |
|---|
| 32 | uint8_t SYNCFLAG:1; |
|---|
| 33 | } bit; |
|---|
| 34 | uint8_t reg; |
|---|
| 35 | } PTC_REG_CONTROLB_Type; |
|---|
| 36 | |
|---|
| 37 | /*************** UNK4C04 register ***************/ |
|---|
| 38 | |
|---|
| 39 | #define PTC_REG_UNK4C04 0x42004C04 |
|---|
| 40 | |
|---|
| 41 | typedef union { |
|---|
| 42 | uint8_t reg; |
|---|
| 43 | } PTC_REG_UNK4C04_Type; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /*************** CONTROL C register ***************/ |
|---|
| 47 | |
|---|
| 48 | #define PTC_REG_CONTROLC 0x42004C05 |
|---|
| 49 | #define PTC_BIT_INIT 0x01 |
|---|
| 50 | |
|---|
| 51 | typedef union { |
|---|
| 52 | struct { |
|---|
| 53 | uint8_t INIT:1; |
|---|
| 54 | uint8_t __pad0__:7; |
|---|
| 55 | } bit; |
|---|
| 56 | uint8_t reg; |
|---|
| 57 | } PTC_REG_CONTROLC_Type; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /*************** INT registers ***************/ |
|---|
| 62 | |
|---|
| 63 | typedef union { |
|---|
| 64 | struct { |
|---|
| 65 | uint8_t EOC:1; |
|---|
| 66 | uint8_t WCO:1; |
|---|
| 67 | uint8_t __pad0__:6; |
|---|
| 68 | } bit; |
|---|
| 69 | uint8_t reg; |
|---|
| 70 | } PTC_REG_INT_Type; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | #define PTC_REG_INTDISABLE 0x42004C08 |
|---|
| 74 | #define PTC_REG_INTENABLE 0x42004C09 |
|---|
| 75 | #define PTC_BIT_EOCINTEN 0x01 |
|---|
| 76 | #define PTC_BIT_WCOINTEN 0x02 |
|---|
| 77 | |
|---|
| 78 | #define PTC_REG_INTFLAGS 0x42004C0A |
|---|
| 79 | #define PTC_BIT_EOCINTFLAG 0x01 |
|---|
| 80 | #define PTC_BIT_WCOINTFLAG 0x02 |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /*************** FREQ CONTROL reg ***************/ |
|---|
| 84 | |
|---|
| 85 | typedef union { |
|---|
| 86 | struct { |
|---|
| 87 | uint8_t SAMPLEDELAY:4; |
|---|
| 88 | uint8_t FREQSPREADEN:1; |
|---|
| 89 | uint8_t __pad0__:3; |
|---|
| 90 | } bit; |
|---|
| 91 | uint8_t reg; |
|---|
| 92 | } PTC_REG_FREQCONTROL_Type; |
|---|
| 93 | |
|---|
| 94 | #define PTC_REG_FREQCONTROL 0x42004C0C |
|---|
| 95 | #define PTC_BIT_FREQSPREADEN 0x10 |
|---|
| 96 | #define PTC_REG_SAMPLEDELAY_MASK 0x0F |
|---|
| 97 | |
|---|
| 98 | /*************** CONVERT CONTROL reg ***************/ |
|---|
| 99 | |
|---|
| 100 | typedef union { |
|---|
| 101 | struct { |
|---|
| 102 | uint8_t ADCACCUM:3; |
|---|
| 103 | uint8_t __pad0__:4; |
|---|
| 104 | uint8_t CONVERT:1; |
|---|
| 105 | } bit; |
|---|
| 106 | uint8_t reg; |
|---|
| 107 | } __attribute__ ((packed)) PTC_REG_CONVCONTROL_Type; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | #define PTC_REG_CONVCONTROL 0x42004C0D |
|---|
| 111 | #define PTC_BIT_CONVSTARTED 0x80 |
|---|
| 112 | #define PTC_REG_ADCACC_MASK 0x07 |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | /*************** Y SELECT L+H reg ***************/ |
|---|
| 116 | |
|---|
| 117 | typedef union { |
|---|
| 118 | struct { |
|---|
| 119 | uint8_t Y0:1; |
|---|
| 120 | uint8_t Y1:1; |
|---|
| 121 | uint8_t Y2:1; |
|---|
| 122 | uint8_t Y3:1; |
|---|
| 123 | uint8_t Y4:1; |
|---|
| 124 | uint8_t Y5:1; |
|---|
| 125 | uint8_t Y6:1; |
|---|
| 126 | uint8_t Y7:1; |
|---|
| 127 | } bit; |
|---|
| 128 | uint8_t reg; |
|---|
| 129 | } __attribute__ ((packed)) PTC_REG_YSELECTL_Type; |
|---|
| 130 | |
|---|
| 131 | typedef union { |
|---|
| 132 | struct { |
|---|
| 133 | uint8_t Y8:1; |
|---|
| 134 | uint8_t Y9:1; |
|---|
| 135 | uint8_t Y10:1; |
|---|
| 136 | uint8_t Y11:1; |
|---|
| 137 | uint8_t Y12:1; |
|---|
| 138 | uint8_t Y13:1; |
|---|
| 139 | uint8_t Y14:1; |
|---|
| 140 | uint8_t Y15:1; |
|---|
| 141 | } bit; |
|---|
| 142 | uint8_t reg; |
|---|
| 143 | } __attribute__ ((packed)) PTC_REG_YSELECTH_Type; |
|---|
| 144 | |
|---|
| 145 | #define PTC_REG_YSELECT_L 0x42004C10 |
|---|
| 146 | #define PTC_REG_YSELECT_H 0x42004C11 |
|---|
| 147 | |
|---|
| 148 | #define PTC_REG_YENABLE_L 0x42004C14 |
|---|
| 149 | #define PTC_REG_YENABLE_H 0x42004C15 |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /*************** X SELECT L+H reg ***************/ |
|---|
| 153 | |
|---|
| 154 | typedef union { |
|---|
| 155 | struct { |
|---|
| 156 | uint8_t X0:1; |
|---|
| 157 | uint8_t X1:1; |
|---|
| 158 | uint8_t X2:1; |
|---|
| 159 | uint8_t X3:1; |
|---|
| 160 | uint8_t X4:1; |
|---|
| 161 | uint8_t X5:1; |
|---|
| 162 | uint8_t X6:1; |
|---|
| 163 | uint8_t X7:1; |
|---|
| 164 | } bit; |
|---|
| 165 | uint8_t reg; |
|---|
| 166 | } __attribute__ ((packed)) PTC_REG_XSELECTL_Type; |
|---|
| 167 | |
|---|
| 168 | typedef union { |
|---|
| 169 | struct { |
|---|
| 170 | uint8_t X8:1; |
|---|
| 171 | uint8_t X9:1; |
|---|
| 172 | uint8_t X10:1; |
|---|
| 173 | uint8_t X11:1; |
|---|
| 174 | uint8_t X12:1; |
|---|
| 175 | uint8_t X13:1; |
|---|
| 176 | uint8_t X14:1; |
|---|
| 177 | uint8_t X15:1; |
|---|
| 178 | } bit; |
|---|
| 179 | uint8_t reg; |
|---|
| 180 | } __attribute__ ((packed)) PTC_REG_XSELECTH_Type; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | #define PTC_REG_XSELECT_L 0x42004C12 |
|---|
| 184 | #define PTC_REG_XSELECT_H 0x42004C13 |
|---|
| 185 | |
|---|
| 186 | #define PTC_REG_XENABLE_L 0x42004C16 |
|---|
| 187 | #define PTC_REG_XENABLE_H 0x42004C17 |
|---|
| 188 | |
|---|
| 189 | /*************** Compensation Cap reg ***************/ |
|---|
| 190 | |
|---|
| 191 | typedef union { |
|---|
| 192 | struct { |
|---|
| 193 | uint8_t VALUE:8; |
|---|
| 194 | } bit; |
|---|
| 195 | uint8_t reg; |
|---|
| 196 | } __attribute__ ((packed)) PTC_REG_COMPCAPL_Type; |
|---|
| 197 | |
|---|
| 198 | typedef union { |
|---|
| 199 | struct { |
|---|
| 200 | uint8_t VALUE:6; |
|---|
| 201 | uint8_t __pad0__:2; |
|---|
| 202 | } bit; |
|---|
| 203 | uint8_t reg; |
|---|
| 204 | } __attribute__ ((packed)) PTC_REG_COMPCAPH_Type; |
|---|
| 205 | |
|---|
| 206 | #define PTC_REG_COMPCAPL 0x42004C18 |
|---|
| 207 | #define PTC_REG_COMPCAPH 0x42004C19 |
|---|
| 208 | |
|---|
| 209 | /*************** Int Cap reg ***************/ |
|---|
| 210 | |
|---|
| 211 | typedef union { |
|---|
| 212 | struct { |
|---|
| 213 | uint8_t VALUE:6; |
|---|
| 214 | uint8_t __pad0__:2; |
|---|
| 215 | } bit; |
|---|
| 216 | uint8_t reg; |
|---|
| 217 | } __attribute__ ((packed)) PTC_REG_INTCAP_Type; |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | #define PTC_REG_INTCAP 0x42004C1A |
|---|
| 221 | |
|---|
| 222 | /*************** Series resistor reg ***************/ |
|---|
| 223 | |
|---|
| 224 | typedef union { |
|---|
| 225 | struct { |
|---|
| 226 | uint8_t RESISTOR:2; |
|---|
| 227 | uint8_t __pad0__:6; |
|---|
| 228 | } bit; |
|---|
| 229 | uint8_t reg; |
|---|
| 230 | } __attribute__ ((packed)) PTC_REG_SERRES_Type; |
|---|
| 231 | |
|---|
| 232 | #define PTC_REG_SERIESRES 0x42004C1B |
|---|
| 233 | |
|---|
| 234 | /*************** conversion result reg ***************/ |
|---|
| 235 | |
|---|
| 236 | typedef union { |
|---|
| 237 | struct { |
|---|
| 238 | uint8_t LOWBYTE; |
|---|
| 239 | uint8_t HIGHBYTE; |
|---|
| 240 | } byte; |
|---|
| 241 | uint16_t reg; |
|---|
| 242 | } __attribute__ ((packed)) PTC_REG_CONVRESULT_Type; |
|---|
| 243 | |
|---|
| 244 | #define PTC_REG_CONVRESULT_L 0x42004C1C |
|---|
| 245 | #define PTC_REG_CONVRESULT_H 0x42004C1D |
|---|
| 246 | |
|---|
| 247 | /*************** burst mode reg ***************/ |
|---|
| 248 | |
|---|
| 249 | typedef union { |
|---|
| 250 | struct { |
|---|
| 251 | uint8_t __pad0__:2; |
|---|
| 252 | uint8_t CTSLOWPOWER:1; |
|---|
| 253 | uint8_t __pad1__:1; |
|---|
| 254 | uint8_t BURSTMODE:4; |
|---|
| 255 | } bit; |
|---|
| 256 | uint8_t reg; |
|---|
| 257 | } __attribute__ ((packed)) PTC_REG_BURSTMODE_Type; |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | #define PTC_REG_BURSTMODE 0x42004C20 |
|---|
| 261 | #define PTC_REG_BURSTMODE_MASK 0xF0 |
|---|
| 262 | #define PTC_BIT_CTSLOWPOWER 0x04 |
|---|
| 263 | |
|---|
| 264 | /*************** etc unused reg ***************/ |
|---|
| 265 | |
|---|
| 266 | #define PTC_REG_XYENABLE 0x42004C16 |
|---|
| 267 | #define PTC_BIT_XYENABLE 0x02 |
|---|
| 268 | |
|---|
| 269 | #define PTC_REG_WCO_MODE 0x42004C21 |
|---|
| 270 | #define PTC_REG_WCO_MODE_MASK 0x07 |
|---|
| 271 | |
|---|
| 272 | #define PTC_SET_WCO_THRESHHOLD_A_L 0x42004C24 |
|---|
| 273 | #define PTC_SET_WCO_THRESHHOLD_A_H 0x42004C25 |
|---|
| 274 | #define PTC_SET_WCO_THRESHHOLD_B_L 0x42004C26 |
|---|
| 275 | #define PTC_SET_WCO_THRESHHOLD_B_H 0x42004C27 |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | typedef struct { |
|---|
| 280 | __IO PTC_REG_CONTROLA_Type CONTROLA; // 0x42004C00 |
|---|
| 281 | __IO PTC_REG_CONTROLB_Type CONTROLB; // 0x42004C01 |
|---|
| 282 | uint8_t __pad4c02__; // 0x42004C02 unknown |
|---|
| 283 | uint8_t __pad4c03__; // 0x42004C03 unknown |
|---|
| 284 | __IO PTC_REG_UNK4C04_Type UNK4C04; // 0x42004C04 unknown |
|---|
| 285 | __IO PTC_REG_CONTROLC_Type CONTROLC; // 0x42004C05 |
|---|
| 286 | uint8_t __pad4c06__; // 0x42004C06 unknown |
|---|
| 287 | uint8_t __pad4c07__; // 0x42004C07 unknown |
|---|
| 288 | __IO PTC_REG_INT_Type INTDISABLE; // 0x42004C08 |
|---|
| 289 | __IO PTC_REG_INT_Type INTENABLE; // 0x42004C09 |
|---|
| 290 | __I PTC_REG_INT_Type INTFLAGS; // 0x42004C0A |
|---|
| 291 | uint8_t __pad4c0b__; // 0x42004C0B unknown |
|---|
| 292 | __IO PTC_REG_FREQCONTROL_Type FREQCONTROL; //0x42004C0C |
|---|
| 293 | __IO PTC_REG_CONVCONTROL_Type CONVCONTROL; // 0x42004C0D |
|---|
| 294 | uint8_t __pad4c0e__; // 0x42004C0E unknown |
|---|
| 295 | uint8_t __pad4c0f__; // 0x42004C0F unknown |
|---|
| 296 | __IO PTC_REG_YSELECTL_Type YSELECTL; // 0x42004C10 |
|---|
| 297 | __IO PTC_REG_YSELECTL_Type YSELECTH; // 0x42004C11 |
|---|
| 298 | __IO PTC_REG_XSELECTL_Type XSELECTL; // 0x42004C12 |
|---|
| 299 | __IO PTC_REG_XSELECTL_Type XSELECTH; // 0x42004C13 |
|---|
| 300 | __IO PTC_REG_YSELECTL_Type YENABLEL; // 0x42004C14 |
|---|
| 301 | __IO PTC_REG_YSELECTL_Type YENABLEH; // 0x42004C15 |
|---|
| 302 | __IO PTC_REG_XSELECTL_Type XENABLEL; // 0x42004C16 |
|---|
| 303 | __IO PTC_REG_XSELECTL_Type XENABLEH; // 0x42004C17 |
|---|
| 304 | |
|---|
| 305 | __IO PTC_REG_COMPCAPL_Type COMPCAPL; // 0x42004C18 |
|---|
| 306 | __IO PTC_REG_COMPCAPH_Type COMPCAPH; // 0x42004C19 |
|---|
| 307 | __IO PTC_REG_INTCAP_Type INTCAP; // 0x42004C1A |
|---|
| 308 | __IO PTC_REG_SERRES_Type SERRES; // 0x42004C1B |
|---|
| 309 | |
|---|
| 310 | __IO PTC_REG_CONVRESULT_Type RESULT; // 0x42004C1C + 0x42004C1D |
|---|
| 311 | uint8_t __pad4c1e__; // 0x42004C1E unknown |
|---|
| 312 | uint8_t __pad4c1f__; // 0x42004C1F unknown |
|---|
| 313 | __IO PTC_REG_BURSTMODE_Type BURSTMODE; // 0x42004C20 |
|---|
| 314 | } Qtouch_Ptc; |
|---|
| 315 | |
|---|
| 316 | #define QTOUCH_PTC (( Qtouch_Ptc *)0x42004C00U) |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | #define PTC_REG_INTDISABLE 0x42004C08 |
|---|
| 320 | #define PTC_REG_INTENABLE 0x42004C09 |
|---|
| 321 | #define PTC_BIT_EOCINTEN 0x01 |
|---|
| 322 | #define PTC_BIT_WCOINTEN 0x02 |
|---|
| 323 | |
|---|
| 324 | #define PTC_REG_INTFLAGS 0x42004C0A |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | /* Touch library oversampling (filter) setting */ |
|---|
| 329 | typedef enum tag_oversample_level_t { |
|---|
| 330 | OVERSAMPLE_1, |
|---|
| 331 | OVERSAMPLE_2, |
|---|
| 332 | OVERSAMPLE_4, |
|---|
| 333 | OVERSAMPLE_8, |
|---|
| 334 | OVERSAMPLE_16, |
|---|
| 335 | OVERSAMPLE_32, |
|---|
| 336 | OVERSAMPLE_64 |
|---|
| 337 | } |
|---|
| 338 | oversample_t; |
|---|
| 339 | |
|---|
| 340 | /* Touch library series resistor setting */ |
|---|
| 341 | typedef enum tag_series_resistor_t { |
|---|
| 342 | RESISTOR_0, |
|---|
| 343 | RESISTOR_20K, |
|---|
| 344 | RESISTOR_50K, |
|---|
| 345 | RESISTOR_100K, |
|---|
| 346 | } |
|---|
| 347 | series_resistor_t; |
|---|
| 348 | |
|---|
| 349 | typedef enum tag_freq_mode_t { |
|---|
| 350 | FREQ_MODE_NONE, |
|---|
| 351 | FREQ_MODE_HOP, |
|---|
| 352 | FREQ_MODE_SPREAD, |
|---|
| 353 | FREQ_MODE_SPREAD_MEDIAN |
|---|
| 354 | } |
|---|
| 355 | freq_mode_t; |
|---|
| 356 | |
|---|
| 357 | typedef enum tag_freq_hop_t { |
|---|
| 358 | FREQ_HOP_1, |
|---|
| 359 | FREQ_HOP_2, |
|---|
| 360 | FREQ_HOP_3, |
|---|
| 361 | FREQ_HOP_4, |
|---|
| 362 | FREQ_HOP_5, |
|---|
| 363 | FREQ_HOP_6, |
|---|
| 364 | FREQ_HOP_7, |
|---|
| 365 | FREQ_HOP_8, |
|---|
| 366 | FREQ_HOP_9, |
|---|
| 367 | FREQ_HOP_10, |
|---|
| 368 | FREQ_HOP_11, |
|---|
| 369 | FREQ_HOP_12, |
|---|
| 370 | FREQ_HOP_13, |
|---|
| 371 | FREQ_HOP_14, |
|---|
| 372 | FREQ_HOP_15, |
|---|
| 373 | FREQ_HOP_16 |
|---|
| 374 | } |
|---|
| 375 | freq_hop_t; |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | class Adafruit_CPlay_FreeTouch { |
|---|
| 380 | public: |
|---|
| 381 | Adafruit_CPlay_FreeTouch(int p = 0, oversample_t f = OVERSAMPLE_4, series_resistor_t r = RESISTOR_0, freq_mode_t fh = FREQ_MODE_NONE); |
|---|
| 382 | bool begin(void); |
|---|
| 383 | |
|---|
| 384 | uint16_t measure(void); |
|---|
| 385 | uint16_t measureRaw(void); |
|---|
| 386 | |
|---|
| 387 | private: |
|---|
| 388 | void ptcInitSettings(void); |
|---|
| 389 | void ptcConfigIOpin(void); |
|---|
| 390 | uint16_t startPtcAcquire(void); |
|---|
| 391 | |
|---|
| 392 | void setupClock(void); |
|---|
| 393 | int getYLine(void); |
|---|
| 394 | void selectYLine(void); |
|---|
| 395 | void setOversampling(oversample_t lvl); |
|---|
| 396 | void setSeriesResistor(series_resistor_t res); |
|---|
| 397 | void setFreqHopping(freq_mode_t fh, freq_hop_t hops = FREQ_HOP_1); |
|---|
| 398 | void setCompCap(uint16_t cc); |
|---|
| 399 | void setIntCap(uint8_t ic); |
|---|
| 400 | |
|---|
| 401 | void runInStandby(boolean en); |
|---|
| 402 | void enablePTC(boolean en); |
|---|
| 403 | void enableWCOint(boolean en); |
|---|
| 404 | void enableEOCint(boolean en); |
|---|
| 405 | void clearWCOintFlag(void); |
|---|
| 406 | void clearEOCintFlag(void); |
|---|
| 407 | void ptcAcquire(void); |
|---|
| 408 | void sync_config(void); |
|---|
| 409 | |
|---|
| 410 | // debugging helper! |
|---|
| 411 | void snapshotRegsAndPrint(uint32_t base, uint8_t numregs); |
|---|
| 412 | void printPTCregs(uint32_t base, uint8_t *regs, uint8_t num); |
|---|
| 413 | void printHex(uint8_t h, boolean newline); |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | int pin; // arduino pin # |
|---|
| 417 | int8_t yline; // the Y select line (see datasheet) |
|---|
| 418 | oversample_t oversample; |
|---|
| 419 | series_resistor_t seriesres; |
|---|
| 420 | freq_mode_t freqhop; |
|---|
| 421 | freq_hop_t hops; |
|---|
| 422 | uint16_t compcap; |
|---|
| 423 | uint8_t intcap; |
|---|
| 424 | }; |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | #endif |
|---|
| 428 | #endif |
|---|