stmpe811_tsc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /****************************************************************************
  2. * drivers/input/stmpe811_tsc.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /* References:
  21. * "STMPE811 S-Touch advanced resistive touchscreen controller with 8-bit
  22. * GPIO expander," Doc ID 14489 Rev 6, CD00186725, STMicroelectronics"
  23. */
  24. /****************************************************************************
  25. * Included Files
  26. ****************************************************************************/
  27. #include <nuttx/config.h>
  28. #include <sys/types.h>
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <fcntl.h>
  34. #include <poll.h>
  35. #include <errno.h>
  36. #include <assert.h>
  37. #include <debug.h>
  38. #include <nuttx/kmalloc.h>
  39. #include <nuttx/arch.h>
  40. #include <nuttx/fs/fs.h>
  41. #include <nuttx/i2c/i2c_master.h>
  42. #include <nuttx/wqueue.h>
  43. #include <nuttx/random.h>
  44. #include <nuttx/arch.h>
  45. #include <nuttx/input/touchscreen.h>
  46. #include <nuttx/input/stmpe811.h>
  47. #include "stmpe811.h"
  48. #if defined(CONFIG_INPUT) && defined(CONFIG_INPUT_STMPE811) && !defined(CONFIG_STMPE811_TSC_DISABLE)
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. #define IO_IT_0 0x01
  53. #define IO_IT_1 0x02
  54. #define IO_IT_2 0x04
  55. #define IO_IT_3 0x08
  56. #define IO_IT_4 0x10
  57. #define IO_IT_5 0x20
  58. #define IO_IT_6 0x40
  59. #define IO_IT_7 0x80
  60. #define ALL_IT 0xFF
  61. #define IOE_JOY_IT (uint8_t)(IO_IT_3 | IO_IT_4 | IO_IT_5 | IO_IT_6 | IO_IT_7)
  62. #define IOE_TS_IT (uint8_t)(IO_IT_0 | IO_IT_1 | IO_IT_2)
  63. #define IOE_INMEMS_IT (uint8_t)(IO_IT_2 | IO_IT_3)
  64. #define EDGE_FALLING 0x01
  65. #define EDGE_RISING 0x02
  66. /* <! The value of the maximal timeout for I2C waiting loops */
  67. #define TIMEOUT_MAX 0x3000
  68. /****************************************************************************
  69. * Private Types
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Private Function Prototypes
  73. ****************************************************************************/
  74. /* Internal logic */
  75. static void stmpe811_notify(FAR struct stmpe811_dev_s *priv);
  76. static int stmpe811_sample(FAR struct stmpe811_dev_s *priv,
  77. FAR struct stmpe811_sample_s *sample);
  78. static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv,
  79. FAR struct stmpe811_sample_s *sample);
  80. /* Character driver methods */
  81. static int stmpe811_open(FAR struct file *filep);
  82. static int stmpe811_close(FAR struct file *filep);
  83. static ssize_t stmpe811_read(FAR struct file *filep, FAR char *buffer,
  84. size_t len);
  85. static int stmpe811_ioctl(FAR struct file *filep, int cmd,
  86. unsigned long arg);
  87. static int stmpe811_poll(FAR struct file *filep, struct pollfd *fds,
  88. bool setup);
  89. /* Initialization logic */
  90. static inline void stmpe811_tscinitialize(FAR struct stmpe811_dev_s *priv);
  91. /****************************************************************************
  92. * Private Data
  93. ****************************************************************************/
  94. /* This the vtable that supports the character driver interface */
  95. static const struct file_operations g_stmpe811fops =
  96. {
  97. stmpe811_open, /* open */
  98. stmpe811_close, /* close */
  99. stmpe811_read, /* read */
  100. NULL, /* write */
  101. NULL, /* seek */
  102. stmpe811_ioctl, /* ioctl */
  103. stmpe811_poll /* poll */
  104. };
  105. /****************************************************************************
  106. * Private Functions
  107. ****************************************************************************/
  108. /****************************************************************************
  109. * Name: stmpe811_notify
  110. *
  111. * Description:
  112. * Notify any threads waiting on touchscreen data that data is now
  113. * available for reading.
  114. *
  115. ****************************************************************************/
  116. static void stmpe811_notify(FAR struct stmpe811_dev_s *priv)
  117. {
  118. int i;
  119. /* If there are threads waiting on poll() for STMPE811 data to become
  120. * available, then wake them up now. NOTE: we wake up all waiting threads
  121. * because we do not know that they are going to do. If they all try to
  122. * read the data, then some make end up blocking after all.
  123. */
  124. for (i = 0; i < CONFIG_STMPE811_NPOLLWAITERS; i++)
  125. {
  126. struct pollfd *fds = priv->fds[i];
  127. if (fds)
  128. {
  129. fds->revents |= POLLIN;
  130. iinfo("Report events: %02x\n", fds->revents);
  131. nxsem_post(fds->sem);
  132. }
  133. }
  134. /* If there are threads waiting for read data, then signal one of them
  135. * that the read data is available.
  136. */
  137. if (priv->nwaiters > 0)
  138. {
  139. /* After posting this semaphore, we need to exit because the STMPE811
  140. * is no longer available.
  141. */
  142. nxsem_post(&priv->waitsem);
  143. }
  144. }
  145. /****************************************************************************
  146. * Name: stmpe811_sample
  147. *
  148. * Description:
  149. * Check if touchscreen sample data is available now and, if so, return
  150. * the sample data. This is part of the stmpe811_read logic.
  151. *
  152. * Assumption:
  153. * Pre-emption is disable to prevent the worker thread from running.
  154. * Otherwise, sampled data may continue to change.
  155. *
  156. ****************************************************************************/
  157. static int stmpe811_sample(FAR struct stmpe811_dev_s *priv,
  158. FAR struct stmpe811_sample_s *sample)
  159. {
  160. int ret = -EAGAIN;
  161. /* Is there new STMPE811 sample data available? */
  162. if (priv->penchange)
  163. {
  164. /* Yes.. the state has changed in some way. Return a copy of the
  165. * sampled data.
  166. */
  167. memcpy(sample, &priv->sample, sizeof(struct stmpe811_sample_s));
  168. /* Now manage state transitions */
  169. if (sample->contact == CONTACT_UP)
  170. {
  171. /* The sampling logic has detected pen-up in some condition other
  172. * than CONTACT_NONE. Set the next state to CONTACT_NONE: Further
  173. * pen-down reports will be ignored. Increment the ID so that
  174. * next contact ID will be unique
  175. */
  176. priv->sample.contact = CONTACT_NONE;
  177. priv->sample.valid = false;
  178. priv->id++;
  179. }
  180. else if (sample->contact == CONTACT_DOWN)
  181. {
  182. /* The sampling logic has detected pen-up in some condition other
  183. * than CONTACT_MOVE. Set the next state to CONTACT_MOVE: Further
  184. * samples collected while the pen is down will reported as
  185. * movement events.
  186. */
  187. priv->sample.contact = CONTACT_MOVE;
  188. }
  189. priv->penchange = false;
  190. ret = OK;
  191. }
  192. return ret;
  193. }
  194. /****************************************************************************
  195. * Name: stmpe811_waitsample
  196. *
  197. * Description:
  198. * Wait for a sample to become available (this is really part of the
  199. * stmpe811_read logic).
  200. *
  201. ****************************************************************************/
  202. static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv,
  203. FAR struct stmpe811_sample_s *sample)
  204. {
  205. int ret;
  206. /* Disable pre-emption to prevent the worker thread from running
  207. * asynchronously.
  208. */
  209. sched_lock();
  210. /* Now release the semaphore that manages mutually exclusive access to
  211. * the device structure. This may cause other tasks to become ready to
  212. * run, but they cannot run yet because pre-emption is disabled.
  213. */
  214. nxsem_post(&priv->exclsem);
  215. /* Try to get the a sample... if we cannot, then wait on the semaphore
  216. * that is posted when new sample data is available.
  217. */
  218. while (stmpe811_sample(priv, sample) < 0)
  219. {
  220. /* Wait for a change in the STMPE811 state */
  221. priv->nwaiters++;
  222. ret = nxsem_wait(&priv->waitsem);
  223. priv->nwaiters--;
  224. /* When we are re-awakened, pre-emption will again be disabled */
  225. if (ret < 0)
  226. {
  227. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  228. goto errout;
  229. }
  230. }
  231. /* Re-acquire the semaphore that manages mutually exclusive access to
  232. * the device structure. We may have to wait here. But we have our sample.
  233. * Interrupts and pre-emption will be re-enabled while we wait.
  234. */
  235. ret = nxsem_wait(&priv->exclsem);
  236. errout:
  237. /* Restore pre-emption. We might get suspended here but that is okay
  238. * because we already have our sample. Note: this means that if there
  239. * were two threads reading from the STMPE811 for some reason, the data
  240. * might be read out of order.
  241. */
  242. sched_unlock();
  243. return ret;
  244. }
  245. /****************************************************************************
  246. * Name: stmpe811_open
  247. *
  248. * Description:
  249. * Standard character driver open method.
  250. *
  251. ****************************************************************************/
  252. static int stmpe811_open(FAR struct file *filep)
  253. {
  254. #ifdef CONFIG_STMPE811_REFCNT
  255. FAR struct inode *inode;
  256. FAR struct stmpe811_dev_s *priv;
  257. uint8_t tmp;
  258. int ret;
  259. DEBUGASSERT(filep);
  260. inode = filep->f_inode;
  261. DEBUGASSERT(inode && inode->i_private);
  262. priv = (FAR struct stmpe811_dev_s *)inode->i_private;
  263. /* Get exclusive access to the driver data structure */
  264. ret = nxsem_wait(&priv->exclsem);
  265. if (ret < 0)
  266. {
  267. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  268. return ret;
  269. }
  270. /* Increment the reference count */
  271. tmp = priv->crefs + 1;
  272. if (tmp == 0)
  273. {
  274. /* More than 255 opens; uint8_t overflows to zero */
  275. ret = -EMFILE;
  276. goto errout_with_sem;
  277. }
  278. /* When the reference increments to 1, this is the first open event
  279. * on the driver.. and an opportunity to do any one-time initialization.
  280. */
  281. /* Save the new open count on success */
  282. priv->crefs = tmp;
  283. errout_with_sem:
  284. nxsem_post(&priv->exclsem);
  285. return ret;
  286. #else
  287. return OK;
  288. #endif
  289. }
  290. /****************************************************************************
  291. * Name: stmpe811_close
  292. *
  293. * Description:
  294. * Standard character driver close method.
  295. *
  296. ****************************************************************************/
  297. static int stmpe811_close(FAR struct file *filep)
  298. {
  299. #ifdef CONFIG_STMPE811_REFCNT
  300. FAR struct inode *inode;
  301. FAR struct stmpe811_dev_s *priv;
  302. int ret;
  303. DEBUGASSERT(filep);
  304. inode = filep->f_inode;
  305. DEBUGASSERT(inode && inode->i_private);
  306. priv = (FAR struct stmpe811_dev_s *)inode->i_private;
  307. /* Get exclusive access to the driver data structure */
  308. ret = nxsem_wait(&priv->exclsem);
  309. if (ret < 0)
  310. {
  311. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  312. return ret;
  313. }
  314. /* Decrement the reference count unless it would decrement a negative
  315. * value. When the count decrements to zero, there are no further
  316. * open references to the driver.
  317. */
  318. if (priv->crefs >= 1)
  319. {
  320. priv->crefs--;
  321. }
  322. nxsem_post(&priv->exclsem);
  323. #endif
  324. return OK;
  325. }
  326. /****************************************************************************
  327. * Name: stmpe811_read
  328. *
  329. * Description:
  330. * Standard character driver read method.
  331. *
  332. ****************************************************************************/
  333. static ssize_t stmpe811_read(FAR struct file *filep,
  334. FAR char *buffer, size_t len)
  335. {
  336. FAR struct inode *inode;
  337. FAR struct stmpe811_dev_s *priv;
  338. FAR struct touch_sample_s *report;
  339. struct stmpe811_sample_s sample;
  340. int ret;
  341. iinfo("len=%d\n", len);
  342. DEBUGASSERT(filep);
  343. inode = filep->f_inode;
  344. DEBUGASSERT(inode && inode->i_private);
  345. priv = (FAR struct stmpe811_dev_s *)inode->i_private;
  346. /* Verify that the caller has provided a buffer large enough to receive
  347. * the touch data.
  348. */
  349. if (len < SIZEOF_TOUCH_SAMPLE_S(1))
  350. {
  351. /* We could provide logic to break up a touch report into segments and
  352. * handle smaller reads... but why?
  353. */
  354. return -ENOSYS;
  355. }
  356. /* Get exclusive access to the driver data structure */
  357. ret = nxsem_wait(&priv->exclsem);
  358. if (ret < 0)
  359. {
  360. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  361. return ret;
  362. }
  363. /* Try to read sample data. */
  364. ret = stmpe811_sample(priv, &sample);
  365. if (ret < 0)
  366. {
  367. /* Sample data is not available now. We would ave to wait to get
  368. * receive sample data. If the user has specified the O_NONBLOCK
  369. * option, then just return an error.
  370. */
  371. if (filep->f_oflags & O_NONBLOCK)
  372. {
  373. ret = -EAGAIN;
  374. goto errout;
  375. }
  376. /* Wait for sample data */
  377. ret = stmpe811_waitsample(priv, &sample);
  378. if (ret < 0)
  379. {
  380. /* We might have been awakened by a signal */
  381. goto errout;
  382. }
  383. }
  384. /* In any event, we now have sampled STMPE811 data that we can report
  385. * to the caller.
  386. */
  387. report = (FAR struct touch_sample_s *)buffer;
  388. memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1));
  389. report->npoints = 1;
  390. report->point[0].id = sample.id;
  391. report->point[0].x = sample.x;
  392. report->point[0].y = sample.y;
  393. report->point[0].pressure = sample.z;
  394. add_ui_randomness((sample.x << 16) ^ (sample.y << 8) ^ sample.z);
  395. /* Report the appropriate flags */
  396. if (sample.contact == CONTACT_UP)
  397. {
  398. /* Pen is now up. Is the positional data valid? This is important to
  399. * know because the release will be sent to the window based on its
  400. * last positional data.
  401. */
  402. if (sample.valid)
  403. {
  404. report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID |
  405. TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
  406. }
  407. else
  408. {
  409. report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
  410. }
  411. }
  412. else if (sample.contact == CONTACT_DOWN)
  413. {
  414. /* First contact */
  415. report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
  416. TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
  417. }
  418. else /* if (sample->contact == CONTACT_MOVE) */
  419. {
  420. /* Movement of the same contact */
  421. report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID |
  422. TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
  423. }
  424. ret = SIZEOF_TOUCH_SAMPLE_S(1);
  425. errout:
  426. nxsem_post(&priv->exclsem);
  427. return ret;
  428. }
  429. /****************************************************************************
  430. * Name: stmpe811_ioctl
  431. *
  432. * Description:
  433. * Standard character driver ioctl method.
  434. *
  435. ****************************************************************************/
  436. static int stmpe811_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  437. {
  438. FAR struct inode *inode;
  439. FAR struct stmpe811_dev_s *priv;
  440. int ret;
  441. iinfo("cmd: %d arg: %ld\n", cmd, arg);
  442. DEBUGASSERT(filep);
  443. inode = filep->f_inode;
  444. DEBUGASSERT(inode && inode->i_private);
  445. priv = (FAR struct stmpe811_dev_s *)inode->i_private;
  446. /* Get exclusive access to the driver data structure */
  447. ret = nxsem_wait(&priv->exclsem);
  448. if (ret < 0)
  449. {
  450. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  451. return ret;
  452. }
  453. /* Process the IOCTL by command */
  454. switch (cmd)
  455. {
  456. case TSIOC_SETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
  457. {
  458. FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
  459. DEBUGASSERT(priv->config != NULL && ptr != NULL);
  460. priv->config->frequency = *ptr;
  461. }
  462. break;
  463. case TSIOC_GETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
  464. {
  465. FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
  466. DEBUGASSERT(priv->config != NULL && ptr != NULL);
  467. *ptr = priv->config->frequency;
  468. }
  469. break;
  470. default:
  471. ret = -ENOTTY;
  472. break;
  473. }
  474. nxsem_post(&priv->exclsem);
  475. return ret;
  476. }
  477. /****************************************************************************
  478. * Name: stmpe811_poll
  479. *
  480. * Description:
  481. * Standard character driver poll method.
  482. *
  483. ****************************************************************************/
  484. static int stmpe811_poll(FAR struct file *filep, FAR struct pollfd *fds,
  485. bool setup)
  486. {
  487. FAR struct inode *inode;
  488. FAR struct stmpe811_dev_s *priv;
  489. int ret;
  490. int i;
  491. iinfo("setup: %d\n", (int)setup);
  492. DEBUGASSERT(filep && fds);
  493. inode = filep->f_inode;
  494. DEBUGASSERT(inode && inode->i_private);
  495. priv = (FAR struct stmpe811_dev_s *)inode->i_private;
  496. /* Are we setting up the poll? Or tearing it down? */
  497. ret = nxsem_wait(&priv->exclsem);
  498. if (ret < 0)
  499. {
  500. /* This should only happen if the wait was canceled by an signal */
  501. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  502. return ret;
  503. }
  504. if (setup)
  505. {
  506. /* Ignore waits that do not include POLLIN */
  507. if ((fds->events & POLLIN) == 0)
  508. {
  509. ierr("ERROR: Missing POLLIN: revents: %08x\n", fds->revents);
  510. ret = -EDEADLK;
  511. goto errout;
  512. }
  513. /* This is a request to set up the poll. Find an available
  514. * slot for the poll structure reference
  515. */
  516. for (i = 0; i < CONFIG_STMPE811_NPOLLWAITERS; i++)
  517. {
  518. /* Find an available slot */
  519. if (!priv->fds[i])
  520. {
  521. /* Bind the poll structure and this slot */
  522. priv->fds[i] = fds;
  523. fds->priv = &priv->fds[i];
  524. break;
  525. }
  526. }
  527. if (i >= CONFIG_STMPE811_NPOLLWAITERS)
  528. {
  529. ierr("ERROR: No available slot found: %d\n", i);
  530. fds->priv = NULL;
  531. ret = -EBUSY;
  532. goto errout;
  533. }
  534. /* Should we immediately notify on any of the requested events? */
  535. if (priv->penchange)
  536. {
  537. stmpe811_notify(priv);
  538. }
  539. }
  540. else if (fds->priv)
  541. {
  542. /* This is a request to tear down the poll. */
  543. struct pollfd **slot = (struct pollfd **)fds->priv;
  544. DEBUGASSERT(slot != NULL);
  545. /* Remove all memory of the poll setup */
  546. *slot = NULL;
  547. fds->priv = NULL;
  548. }
  549. errout:
  550. nxsem_post(&priv->exclsem);
  551. return ret;
  552. }
  553. /****************************************************************************
  554. * Name: stmpe811_timeoutworker
  555. *
  556. * Description:
  557. * A timer has expired without receiving a pen up event. Check again.
  558. *
  559. ****************************************************************************/
  560. static void stmpe811_timeoutworker(FAR void *arg)
  561. {
  562. FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)arg;
  563. DEBUGASSERT(priv);
  564. /* Treat the timeout just like an interrupt occurred */
  565. stmpe811_tscworker(priv, stmpe811_getreg8(priv, STMPE811_INT_STA));
  566. }
  567. /****************************************************************************
  568. * Name: stmpe811_timeout
  569. *
  570. * Description:
  571. * A timer has expired without receiving a pen up event. Schedule work
  572. * to check again.
  573. *
  574. ****************************************************************************/
  575. static void stmpe811_timeout(wdparm_t arg)
  576. {
  577. FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)arg;
  578. int ret;
  579. /* Are we still stuck in the pen down state? */
  580. if (priv->sample.contact == CONTACT_DOWN ||
  581. priv->sample.contact == CONTACT_MOVE)
  582. {
  583. /* Yes... is the worker thread available? If not, then apparently
  584. * we have work already pending?
  585. */
  586. if (work_available(&priv->timeout))
  587. {
  588. /* Yes.. Transfer processing to the worker thread. Since STMPE811
  589. * interrupts are disabled while the work is pending, no special
  590. * action should be required to protect the work queue.
  591. */
  592. ret = work_queue(HPWORK, &priv->timeout,
  593. stmpe811_timeoutworker, priv, 0);
  594. if (ret != 0)
  595. {
  596. ierr("ERROR: Failed to queue work: %d\n", ret);
  597. }
  598. }
  599. }
  600. }
  601. /****************************************************************************
  602. * Name: stmpe811_tscinitialize
  603. *
  604. * Description:
  605. * Initialize the touchscreen controller. This is really a part of the
  606. * stmpe811_register logic,
  607. *
  608. ****************************************************************************/
  609. static inline void stmpe811_tscinitialize(FAR struct stmpe811_dev_s *priv)
  610. {
  611. uint8_t regval;
  612. iinfo("Initializing touchscreen controller\n");
  613. /* Enable TSC and ADC functions */
  614. regval = stmpe811_getreg8(priv, STMPE811_SYS_CTRL2);
  615. regval &= ~(SYS_CTRL2_TSC_OFF | SYS_CTRL2_ADC_OFF);
  616. stmpe811_putreg8(priv, STMPE811_SYS_CTRL2, regval);
  617. /* Enable the TSC global interrupts */
  618. regval = stmpe811_getreg8(priv, STMPE811_INT_EN);
  619. regval |= (uint32_t)(INT_TOUCH_DET | INT_FIFO_TH | INT_FIFO_OFLOW);
  620. stmpe811_putreg8(priv, STMPE811_INT_EN, regval);
  621. /* Select Sample Time, bit number and ADC Reference */
  622. stmpe811_putreg8(priv, STMPE811_ADC_CTRL1, priv->config->ctrl1);
  623. /* Wait for 20 ms */
  624. up_mdelay(20);
  625. /* Select the ADC clock speed */
  626. stmpe811_putreg8(priv, STMPE811_ADC_CTRL2, priv->config->ctrl2);
  627. /* Select TSC pins in non-GPIO mode (AF=0) */
  628. regval = stmpe811_getreg8(priv, STMPE811_GPIO_AF);
  629. regval &= ~(uint8_t)TSC_PIN_SET;
  630. stmpe811_putreg8(priv, STMPE811_GPIO_AF, regval);
  631. /* Select 2 nF filter capacitor */
  632. stmpe811_putreg8(priv, STMPE811_TSC_CFG, TSC_CFG_AVE_CTRL_4SAMPLES |
  633. TSC_CFG_TOUCH_DELAY_500US | TSC_CFG_SETTLING_500US);
  634. /* Select single point reading */
  635. stmpe811_putreg8(priv, STMPE811_FIFO_TH, 1);
  636. /* Reset and clear the FIFO. */
  637. stmpe811_putreg8(priv, STMPE811_FIFO_STA, FIFO_STA_FIFO_RESET);
  638. stmpe811_putreg8(priv, STMPE811_FIFO_STA, 0);
  639. /* set the data format for Z value: 7 fractional part and 1 whole part */
  640. stmpe811_putreg8(priv, STMPE811_TSC_FRACTIONZ, 0x01);
  641. /* Set the driving capability of the device for TSC pins: 50mA */
  642. stmpe811_putreg8(priv, STMPE811_TSC_IDRIVE, TSC_IDRIVE_50MA);
  643. /* Enable the TSC. Use no tracking index, touch-screen controller
  644. * operation mode (XYZ).
  645. */
  646. stmpe811_putreg8(priv, STMPE811_TSC_CTRL, TSC_CTRL_EN);
  647. /* Clear all the status pending bits */
  648. stmpe811_putreg8(priv, STMPE811_INT_STA, INT_ALL);
  649. }
  650. /****************************************************************************
  651. * Public Functions
  652. ****************************************************************************/
  653. /****************************************************************************
  654. * Name: stmpe811_register
  655. *
  656. * Description:
  657. * Enable TSC functionality. GPIO4-7 must be available. This function
  658. * will register the touchscreen driver as /dev/inputN where N is the minor
  659. * device number
  660. *
  661. * Input Parameters:
  662. * handle - The handle previously returned by stmpe811_register
  663. * minor - The input device minor number
  664. *
  665. * Returned Value:
  666. * Zero is returned on success. Otherwise, a negated errno value is
  667. * returned to indicate the nature of the failure.
  668. *
  669. ****************************************************************************/
  670. int stmpe811_register(STMPE811_HANDLE handle, int minor)
  671. {
  672. FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)handle;
  673. char devname[DEV_NAMELEN];
  674. int ret;
  675. iinfo("handle=%p minor=%d\n", handle, minor);
  676. DEBUGASSERT(priv);
  677. /* Get exclusive access to the device structure */
  678. ret = nxsem_wait(&priv->exclsem);
  679. if (ret < 0)
  680. {
  681. ierr("ERROR: nxsem_wait failed: %d\n", ret);
  682. return ret;
  683. }
  684. /* Make sure that the pins (4-7) need by the TSC are not already in use */
  685. if ((priv->inuse & TSC_PIN_SET) != 0)
  686. {
  687. ierr("ERROR: TSC pins is already in-use: %02x\n", priv->inuse);
  688. nxsem_post(&priv->exclsem);
  689. return -EBUSY;
  690. }
  691. /* Initialize the TS structure fields to their default values */
  692. priv->minor = minor;
  693. priv->penchange = false;
  694. priv->threshx = 0;
  695. priv->threshy = 0;
  696. /* Register the character driver */
  697. snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor);
  698. ret = register_driver(devname, &g_stmpe811fops, 0666, priv);
  699. if (ret < 0)
  700. {
  701. ierr("ERROR: Failed to register driver %s: %d\n", devname, ret);
  702. nxsem_post(&priv->exclsem);
  703. return ret;
  704. }
  705. /* Initialize the touchscreen controller */
  706. stmpe811_tscinitialize(priv);
  707. /* Inidicate that the touchscreen controller was successfully initialized */
  708. priv->inuse |= TSC_PIN_SET; /* Pins 4-7 are now in-use */
  709. priv->flags |= STMPE811_FLAGS_TSC_INITIALIZED; /* TSC function is initialized */
  710. nxsem_post(&priv->exclsem);
  711. return ret;
  712. }
  713. /****************************************************************************
  714. * Name: stmpe811_tscworker
  715. *
  716. * Description:
  717. * This function is called to handle a TSC interrupt. It is not really
  718. * an interrupt handle because it is called from the STMPE811 "bottom half"
  719. * logic that runs on the worker thread.
  720. *
  721. ****************************************************************************/
  722. void stmpe811_tscworker(FAR struct stmpe811_dev_s *priv, uint8_t intsta)
  723. {
  724. uint16_t xdiff; /* X difference used in thresholding */
  725. uint16_t ydiff; /* Y difference used in thresholding */
  726. uint16_t x; /* X position */
  727. uint16_t y; /* Y position */
  728. bool pendown; /* true: pen is down */
  729. DEBUGASSERT(priv != NULL);
  730. /* Cancel the missing pen up timer */
  731. wd_cancel(&priv->wdog);
  732. /* Check for pen up or down from the TSC_STA bit in STMPE811_TSC_CTRL. */
  733. pendown = !!(stmpe811_getreg8(priv, STMPE811_TSC_CTRL) & TSC_CTRL_TSC_STA);
  734. /* Handle the change from pen down to pen up */
  735. if (!pendown)
  736. {
  737. /* The pen is up.. reset thresholding variables. FIFOs will read zero
  738. * if there is no data available (hence the choice of (0,0))
  739. */
  740. priv->threshx = 0;
  741. priv->threshy = 0;
  742. /* Ignore the interrupt if the pen was already up (CONTACT_NONE == pen
  743. * up and already reported; CONTACT_UP == pen up, but not reported)
  744. */
  745. if (priv->sample.contact == CONTACT_NONE ||
  746. priv->sample.contact == CONTACT_UP)
  747. {
  748. goto ignored;
  749. }
  750. /* A pen-down to up transition has been detected. CONTACT_UP indicates
  751. * the initial loss of contact. The state will be changed to
  752. * CONTACT_NONE after the loss of contact is sampled.
  753. */
  754. priv->sample.contact = CONTACT_UP;
  755. }
  756. /* The pen is down... check for data in the FIFO */
  757. else if ((intsta & (INT_FIFO_TH | INT_FIFO_OFLOW)) != 0)
  758. {
  759. /* Read the next x and y positions from the FIFO. */
  760. #ifdef CONFIG_STMPE811_SWAPXY
  761. x = stmpe811_getreg16(priv, STMPE811_TSC_DATAX);
  762. y = stmpe811_getreg16(priv, STMPE811_TSC_DATAY);
  763. #else
  764. x = stmpe811_getreg16(priv, STMPE811_TSC_DATAY);
  765. y = stmpe811_getreg16(priv, STMPE811_TSC_DATAX);
  766. #endif
  767. /* If we have not yet processed the last pen up event, then we
  768. * cannot handle this pen down event. We will have to discard it. That
  769. * should be okay because there will be another FIFO event right behind
  770. * this one. Other kinds of data overruns are not harmful.
  771. *
  772. * Hmm.. a better design might be to disable FIFO interrupts when we
  773. * detect pen up. Then re-enable them when CONTACT_UP is reported.
  774. * That would save processing interrupts just to discard the data.
  775. */
  776. if (priv->sample.contact == CONTACT_UP)
  777. {
  778. /* We have not closed the loop on the last touch ... don't report
  779. * anything.
  780. */
  781. goto ignored;
  782. }
  783. /* Perform a thresholding operation so that the results will be more
  784. * stable. If the difference from the last sample is small, then ignore
  785. * the event. REVISIT: Should a large change in pressure also generate
  786. * a event?
  787. */
  788. xdiff = x > priv->threshx ? (x - priv->threshx) : (priv->threshx - x);
  789. ydiff = y > priv->threshy ? (y - priv->threshy) : (priv->threshy - y);
  790. if (xdiff < CONFIG_STMPE811_THRESHX && ydiff < CONFIG_STMPE811_THRESHY)
  791. {
  792. /* Little or no change in either direction ...
  793. * don't report anything.
  794. */
  795. goto ignored;
  796. }
  797. /* When we see a big difference, snap to the new x/y thresholds */
  798. priv->threshx = x;
  799. priv->threshy = y;
  800. /* Update the x/y position in the sample data */
  801. priv->sample.x = priv->threshx;
  802. priv->sample.y = priv->threshy;
  803. /* Update the Z pressure index */
  804. priv->sample.z = stmpe811_getreg8(priv, STMPE811_TSC_DATAZ);
  805. priv->sample.valid = true;
  806. /* If this is the first (acknowledged) pen down report, then report
  807. * this as the first contact. If contact == CONTACT_DOWN, it will be
  808. * set to set to CONTACT_MOVE after the contact is first sampled.
  809. */
  810. if (priv->sample.contact != CONTACT_MOVE)
  811. {
  812. /* First contact */
  813. priv->sample.contact = CONTACT_DOWN;
  814. }
  815. }
  816. /* Pen down, but no data in FIFO */
  817. else
  818. {
  819. /* Ignore the interrupt... wait until there is data in the FIFO */
  820. goto ignored;
  821. }
  822. /* We get here if (1) we just went from a pen down to a pen up state OR (2)
  823. * We just get a measurement from the FIFO in a pen down state. Indicate
  824. * the availability of new sample data for this ID.
  825. */
  826. priv->sample.id = priv->id;
  827. priv->penchange = true;
  828. /* Notify any waiters that new STMPE811 data is available */
  829. stmpe811_notify(priv);
  830. /* If we think that the pen is still down, the start/re-start the pen up
  831. * timer.
  832. */
  833. ignored:
  834. if (priv->sample.contact == CONTACT_DOWN ||
  835. priv->sample.contact == CONTACT_MOVE)
  836. {
  837. wd_start(&priv->wdog, STMPE811_PENUP_TICKS,
  838. stmpe811_timeout, (wdparm_t)priv);
  839. }
  840. /* Reset and clear all data in the FIFO */
  841. stmpe811_putreg8(priv, STMPE811_FIFO_STA, FIFO_STA_FIFO_RESET);
  842. stmpe811_putreg8(priv, STMPE811_FIFO_STA, 0);
  843. }
  844. #endif /* CONFIG_INPUT && CONFIG_INPUT_STMPE811 && !CONFIG_STMPE811_TSC_DISABLE */