int up_buttonevent(int x, int y, int buttons) { FAR struct up_dev_s *priv = (FAR struct up_dev_s *)&g_simtouchscreen; bool pendown; /* true: pen is down */ ivdbg("x=%d y=%d buttons=%02x\n", x, y, buttons); ivdbg("contact=%d nwaiters=%d\n", priv->sample.contact, priv->nwaiters); /* Any button press will count as pendown. */ pendown = (buttons != 0); /* Handle the change from pen down to pen up */ if (!pendown) { /* Ignore the pend up if the pen was already up (CONTACT_NONE == pen up and * already reported. CONTACT_UP == pen up, but not reported) */ if (priv->sample.contact == CONTACT_NONE) { return OK; } /* Not yet reported */ priv->sample.contact = CONTACT_UP; } else { /* Save the measurements */ priv->sample.x = x; priv->sample.y = y; /* Note the availability of new measurements */ /* If this is the first (acknowledged) pen down report, then report * this as the first contact. If contact == CONTACT_DOWN, it will be * set to set to CONTACT_MOVE after the contact is first sampled. */ if (priv->sample.contact != CONTACT_MOVE) { /* First contact */ priv->sample.contact = CONTACT_DOWN; } } /* Indicate the availability of new sample data for this ID */ priv->sample.id = priv->id; priv->penchange = true; /* Notify any waiters that new touchscreen data is available */ up_notify(priv); return OK; }
static int ajoy_open(FAR struct file *filep) { FAR struct inode *inode; FAR struct ajoy_upperhalf_s *priv; FAR struct ajoy_open_s *opriv; #ifndef CONFIG_DISABLE_POLL FAR const struct ajoy_lowerhalf_s *lower; ajoy_buttonset_t supported; #endif int ret; DEBUGASSERT(filep && filep->f_inode); inode = filep->f_inode; DEBUGASSERT(inode->i_private); priv = (FAR struct ajoy_upperhalf_s *)inode->i_private; /* Get exclusive access to the driver structure */ ret = ajoy_takesem(&priv->au_exclsem); if (ret < 0) { ivdbg("ERROR: ajoy_takesem failed: %d\n", ret); return ret; } /* Allocate a new open structure */ opriv = (FAR struct ajoy_open_s *)kmm_zalloc(sizeof(struct ajoy_open_s)); if (!opriv) { ivdbg("ERROR: Failled to allocate open structure\n"); ret = -ENOMEM; goto errout_with_sem; } /* Initialize the open structure */ #ifndef CONFIG_DISABLE_POLL lower = priv->au_lower; DEBUGASSERT(lower && lower->al_supported); supported = lower->al_supported(lower); opriv->ao_pollevents.ap_press = supported; opriv->ao_pollevents.ap_release = supported; #endif /* Attach the open structure to the device */ opriv->ao_flink = priv->au_open; priv->au_open = opriv; /* Attach the open structure to the file structure */ filep->f_priv = (FAR void *)opriv; ret = OK; errout_with_sem: ajoy_givesem(&priv->au_exclsem); return ret; }
static int ads7843e_open(FAR struct file *filep) { #ifdef CONFIG_ADS7843E_REFCNT FAR struct inode *inode; FAR struct ads7843e_dev_s *priv; uint8_t tmp; int ret; ivdbg("Opening\n"); DEBUGASSERT(filep); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct ads7843e_dev_s *)inode->i_private; /* Get exclusive access to the driver data structure */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); return -EINTR; } /* Increment the reference count */ tmp = priv->crefs + 1; if (tmp == 0) { /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; goto errout_with_sem; } /* When the reference increments to 1, this is the first open event * on the driver.. and an opportunity to do any one-time initialization. */ /* Save the new open count on success */ priv->crefs = tmp; errout_with_sem: sem_post(&priv->devsem); return ret; #else ivdbg("Opening\n"); return OK; #endif }
int ajoy_register(FAR const char *devname, FAR const struct ajoy_lowerhalf_s *lower) { FAR struct ajoy_upperhalf_s *priv; int ret; DEBUGASSERT(devname && lower); /* Allocate a new ajoystick driver instance */ priv = (FAR struct ajoy_upperhalf_s *) kmm_zalloc(sizeof(struct ajoy_upperhalf_s)); if (!priv) { ivdbg("ERROR: Failed to allocate device structure\n"); return -ENOMEM; } /* Make sure that all ajoystick interrupts are disabled */ DEBUGASSERT(lower->al_enable); lower->al_enable(lower, 0, 0, NULL, NULL); /* Initialize the new ajoystick driver instance */ priv->au_lower = lower; sem_init(&priv->au_exclsem, 0, 1); DEBUGASSERT(lower->al_buttons); priv->au_sample = lower->al_buttons(lower); /* And register the ajoystick driver */ ret = register_driver(devname, &ajoy_fops, 0666, priv); if (ret < 0) { ivdbg("ERROR: register_driver failed: %d\n", ret); goto errout_with_priv; } return OK; errout_with_priv: sem_destroy(&priv->au_exclsem); kmm_free(priv); return ret; }
static uint16_t ads7843e_sendcmd(FAR struct ads7843e_dev_s *priv, uint8_t cmd) { uint8_t buffer[2]; uint16_t result; /* Select the ADS7843E */ SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); /* Send the command */ (void)SPI_SEND(priv->spi, cmd); ads7843e_waitbusy(priv); /* Read the data */ SPI_RECVBLOCK(priv->spi, buffer, 2); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); result = ((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1]; result = result >> 4; ivdbg("cmd:%02x response:%04x\n", cmd, result); return result; }
static uint16_t max11802_sendcmd(FAR struct max11802_dev_s *priv, uint8_t cmd, int *tags) { uint8_t buffer[2]; uint16_t result; /* Select the MAX11802 */ SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); /* Send the command */ (void)SPI_SEND(priv->spi, cmd); /* Read the data */ SPI_RECVBLOCK(priv->spi, buffer, 2); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); result = ((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1]; *tags = result & 0xF; result >>= 4; /* Get rid of tags */ ivdbg("cmd:%02x response:%04x\n", cmd, result); return result; }
static uint16_t ads7843e_sendcmd(FAR struct ads7843e_dev_s *priv, uint8_t cmd) { uint8_t buffer[2]; uint16_t result; /* Select the ADS7843E */ SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); /* Send the command */ (void)SPI_SEND(priv->spi, cmd); /* Wait a tiny amount to make sure that the aquisition time is complete */ up_udelay(3); /* 3 microseconds */ /* Read the 12-bit data (LS 4 bits will be padded with zero) */ SPI_RECVBLOCK(priv->spi, buffer, 2); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); result = ((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1]; result = result >> 4; ivdbg("cmd:%02x response:%04x\n", cmd, result); return result; }
static int tsc_attach(FAR struct ads7843e_config_s *state, xcpt_t isr) { /* Attach the XPT2046 interrupt */ ivdbg("Attaching %p to IRQ %d\n", isr, SAM_TSC_IRQ); return irq_attach(SAM_TSC_IRQ, isr); }
static ssize_t ajoy_read(FAR struct file *filep, FAR char *buffer, size_t len) { FAR struct inode *inode; FAR struct ajoy_upperhalf_s *priv; FAR const struct ajoy_lowerhalf_s *lower; int ret; DEBUGASSERT(filep && filep->f_inode); inode = filep->f_inode; DEBUGASSERT(inode->i_private); priv = (FAR struct ajoy_upperhalf_s *)inode->i_private; /* Make sure that the buffer is sufficiently large to hold at least one * complete sample. * * REVISIT: Should also check buffer alignment. */ if (len < sizeof(struct ajoy_sample_s)) { ivdbg("ERROR: buffer too small: %lu\n", (unsigned long)len); return -EINVAL; } /* Get exclusive access to the driver structure */ ret = ajoy_takesem(&priv->au_exclsem); if (ret < 0) { ivdbg("ERROR: ajoy_takesem failed: %d\n", ret); return ret; } /* Read and return the current state of the joystick buttons */ lower = priv->au_lower; DEBUGASSERT(lower && lower->al_sample); ret = lower->al_sample(lower, (FAR struct ajoy_sample_s *)buffer); if (ret >= 0) { ret = sizeof(struct ajoy_sample_s); } ajoy_givesem(&priv->au_exclsem); return (ssize_t)ret; }
static bool tsc_pendown(FAR struct ads7843e_config_s *state) { /* The /PENIRQ value is active low */ bool pendown = !sam_gpioread(GPIO_TSC_IRQ); ivdbg("pendown:%d\n", pendown); return pendown; }
static bool tsc_pendown(FAR struct ads7843e_config_s *state) { /* REVISIT: This might need to be inverted */ bool pendown = sam3u_gpioread(GPIO_TCS_IRQ); ivdbg("pendown:%d\n", pendown); return pendown; }
int sim_tcinitialize(int minor) { FAR struct up_dev_s *priv = ( FAR struct up_dev_s *)&g_simtouchscreen; char devname[DEV_NAMELEN]; int ret; ivdbg("minor: %d\n", minor); /* Debug-only sanity checks */ DEBUGASSERT(minor >= 0 && minor < 100); /* Initialize the touchscreen device driver instance */ memset(priv, 0, sizeof(struct up_dev_s)); sem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ sem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ priv->minor = minor; /* Register the device as an input device */ (void)snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor); ivdbg("Registering %s\n", devname); ret = register_driver(devname, &up_fops, 0666, priv); if (ret < 0) { idbg("register_driver() failed: %d\n", ret); goto errout_with_priv; } /* Enable X11 event processing from the IDLE loop */ g_eventloop = 1; /* And return success */ return OK; errout_with_priv: sem_destroy(&priv->waitsem); sem_destroy(&priv->devsem); return ret; }
static int up_sample(FAR struct up_dev_s *priv, FAR struct up_sample_s *sample) { int ret = -EAGAIN; /* Is there new touchscreen sample data available? */ ivdbg("penchange=%d contact=%d id=%d\n", priv->penchange, sample->contact, priv->id); if (priv->penchange) { /* Yes.. the state has changed in some way. Return a copy of the * sampled data. */ memcpy(sample, &priv->sample, sizeof(struct up_sample_s )); /* Now manage state transitions */ if (sample->contact == CONTACT_UP) { /* Next.. no contract. Increment the ID so that next contact ID will be unique */ priv->sample.contact = CONTACT_NONE; priv->id++; } else if (sample->contact == CONTACT_DOWN) { /* First report -- next report will be a movement */ priv->sample.contact = CONTACT_MOVE; } priv->penchange = false; ivdbg("penchange=%d contact=%d id=%d\n", priv->penchange, priv->sample.contact, priv->id); ret = OK; } return ret; }
static int tc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { #if 1 ivdbg("cmd: %d arg: %ld\n", cmd, arg); return -ENOTTY; /* None yet supported */ #else FAR struct inode *inode; FAR struct tc_dev_s *priv; int ret; ivdbg("cmd: %d arg: %ld\n", cmd, arg); DEBUGASSERT(filep); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct tc_dev_s *)inode->i_private; /* Get exclusive access to the driver data structure */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); return -EINTR; } /* Process the IOCTL by command */ switch (cmd) { /* ADD IOCTL COMMAND CASES HERE */ default: ret = -ENOTTY; break; } sem_post(&priv->devsem); return ret; #endif }
static int ads7843e_close(FAR struct file *filep) { #ifdef CONFIG_ADS7843E_REFCNT FAR struct inode *inode; FAR struct ads7843e_dev_s *priv; int ret; ivdbg("Closing\n"); DEBUGASSERT(filep); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct ads7843e_dev_s *)inode->i_private; /* Get exclusive access to the driver data structure */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); return -EINTR; } /* Decrement the reference count unless it would decrement a negative * value. When the count decrements to zero, there are no further * open references to the driver. */ if (priv->crefs >= 1) { priv->crefs--; } sem_post(&priv->devsem); #endif ivdbg("Closing\n"); return OK; }
static int ads7843e_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { FAR struct inode *inode; FAR struct ads7843e_dev_s *priv; int ret; ivdbg("cmd: %d arg: %ld\n", cmd, arg); DEBUGASSERT(filep); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct ads7843e_dev_s *)inode->i_private; /* Get exclusive access to the driver data structure */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); return -EINTR; } /* Process the IOCTL by command */ switch (cmd) { case TSIOC_SETFREQUENCY: /* arg: Pointer to uint32_t frequency value */ { FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg); DEBUGASSERT(priv->config != NULL && ptr != NULL); priv->config->frequency = SPI_SETFREQUENCY(priv->spi, *ptr); } break; case TSIOC_GETFREQUENCY: /* arg: Pointer to uint32_t frequency value */ { FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg); DEBUGASSERT(priv->config != NULL && ptr != NULL); *ptr = priv->config->frequency; } break; default: ret = -ENOTTY; break; } sem_post(&priv->devsem); return ret; }
static void up_notify(FAR struct up_dev_s *priv) { #ifndef CONFIG_DISABLE_POLL int i; #endif /* If there are threads waiting for read data, then signal one of them * that the read data is available. */ ivdbg("contact=%d nwaiters=%d\n", priv->sample.contact, priv->nwaiters); if (priv->nwaiters > 0) { /* After posting this semaphore, we need to exit because the touchscreen * is no longer avaialable. */ sem_post(&priv->waitsem); } /* If there are threads waiting on poll() for touchscreen data to become availabe, * then wake them up now. NOTE: we wake up all waiting threads because we * do not know that they are going to do. If they all try to read the data, * then some make end up blocking after all. */ #ifndef CONFIG_DISABLE_POLL for (i = 0; i < CONFIG_SIM_TCNWAITERS; i++) { struct pollfd *fds = priv->fds[i]; if (fds) { fds->revents |= POLLIN; ivdbg("Report events: %02x\n", fds->revents); sem_post(fds->sem); } } #endif }
static void tsc_enable(FAR struct ads7843e_config_s *state, bool enable) { /* Attach and enable, or detach and disable */ ivdbg("IRQ:%d enable:%d\n", SAM_TSC_IRQ, enable); if (enable) { sam_gpioirqenable(SAM_TSC_IRQ); } else { sam_gpioirqdisable(SAM_TSC_IRQ); } }
static bool tsc_busy(FAR struct ads7843e_config_s *state) { #if defined(CONFIG_DEBUG_INPUT) && defined(CONFIG_DEBUG_VERBOSE) static bool last = (bool)-1; #endif /* REVISIT: This might need to be inverted */ bool busy = sam3u_gpioread(GPIO_TCS_BUSY); #if defined(CONFIG_DEBUG_INPUT) && defined(CONFIG_DEBUG_VERBOSE) if (busy != last) { ivdbg("busy:%d\n", busy); last = busy; } #endif return busy; }
void sim_tcuninitialize(void) { FAR struct up_dev_s *priv = ( FAR struct up_dev_s *)&g_simtouchscreen; char devname[DEV_NAMELEN]; int ret; /* Get exclusive access */ do { ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); } } while (ret != OK); /* Stop the event loop (Hmm.. the caller must be sure that there are no * open references to the touchscreen driver. This might better be * done in close() using a reference count). */ g_eventloop = 0; /* Un-register the device*/ (void)snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor); ivdbg("Un-registering %s\n", devname); ret = unregister_driver(devname); if (ret < 0) { idbg("uregister_driver() failed: %d\n", ret); } /* Clean up any resources. Ouch! While we are holding the semaphore? */ sem_destroy(&priv->waitsem); sem_destroy(&priv->devsem); }
static int stmpe811_checkid(FAR struct stmpe811_dev_s *priv) { uint16_t devid = 0; /* Read device ID */ devid = stmpe811_getreg8(priv, STMPE811_CHIP_ID); devid = (uint32_t)(devid << 8); devid |= (uint32_t)stmpe811_getreg8(priv, STMPE811_CHIP_ID+1); ivdbg("devid: %04x\n", devid); if (devid != (uint16_t)CHIP_ID) { /* ID is not Correct */ return -ENODEV; } return OK; }
static bool tsc_busy(FAR struct ads7843e_config_s *state) { #if defined(CONFIG_DEBUG_INPUT) && defined(CONFIG_DEBUG_VERBOSE) static bool last = (bool)-1; #endif /* BUSY is high impedance when CS is high (not selected). When CS is * is low, BUSY is active high. */ bool busy = sam_gpioread(GPIO_TCS_BUSY); #if defined(CONFIG_DEBUG_INPUT) && defined(CONFIG_DEBUG_VERBOSE) if (busy != last) { ivdbg("busy:%d\n", busy); last = busy; } #endif return busy; }
static void max11802_notify(FAR struct max11802_dev_s *priv) { #ifndef CONFIG_DISABLE_POLL int i; #endif /* If there are threads waiting for read data, then signal one of them * that the read data is available. */ if (priv->nwaiters > 0) { /* After posting this semaphore, we need to exit because the sample * is no longer available. */ sem_post(&priv->waitsem); } /* If there are threads waiting on poll() for MAX11802 data to become * available, then wake them up now. NOTE: we wake up all waiting * threads because we do not know that they are going to do. If they * all try to read the data, then some make end up blocking after all. */ #ifndef CONFIG_DISABLE_POLL for (i = 0; i < CONFIG_MAX11802_NPOLLWAITERS; i++) { struct pollfd *fds = priv->fds[i]; if (fds) { fds->revents |= POLLIN; ivdbg("Report events: %02x\n", fds->revents); sem_post(fds->sem); } } #endif }
static ssize_t ads7843e_read(FAR struct file *filep, FAR char *buffer, size_t len) { FAR struct inode *inode; FAR struct ads7843e_dev_s *priv; FAR struct touch_sample_s *report; struct ads7843e_sample_s sample; int ret; ivdbg("buffer:%p len:%d\n", buffer, len); DEBUGASSERT(filep); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct ads7843e_dev_s *)inode->i_private; /* Verify that the caller has provided a buffer large enough to receive * the touch data. */ if (len < SIZEOF_TOUCH_SAMPLE_S(1)) { /* We could provide logic to break up a touch report into segments and * handle smaller reads... but why? */ idbg("Unsupported read size: %d\n", len); return -ENOSYS; } /* Get exclusive access to the driver data structure */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ idbg("sem_wait: %d\n", errno); DEBUGASSERT(errno == EINTR); return -EINTR; } /* Try to read sample data. */ ret = ads7843e_sample(priv, &sample); if (ret < 0) { /* Sample data is not available now. We would ave to wait to get * receive sample data. If the user has specified the O_NONBLOCK * option, then just return an error. */ ivdbg("Sample data is not available\n"); if (filep->f_oflags & O_NONBLOCK) { ret = -EAGAIN; goto errout; } /* Wait for sample data */ ret = ads7843e_waitsample(priv, &sample); if (ret < 0) { /* We might have been awakened by a signal */ idbg("ads7843e_waitsample: %d\n", ret); goto errout; } } /* In any event, we now have sampled ADS7843E data that we can report * to the caller. */ report = (FAR struct touch_sample_s *)buffer; memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1)); report->npoints = 1; report->point[0].id = sample.id; report->point[0].x = sample.x; report->point[0].y = sample.y; /* Report the appropriate flags */ if (sample.contact == CONTACT_UP) { /* Pen is now up. Is the positional data valid? This is important to * know because the release will be sent to the window based on its * last positional data. */ if (sample.valid) { report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID | TOUCH_POS_VALID; } else { report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID; } } else if (sample.contact == CONTACT_DOWN) { /* First contact */ report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID | TOUCH_POS_VALID; } else /* if (sample->contact == CONTACT_MOVE) */ { /* Movement of the same contact */ report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID | TOUCH_POS_VALID; } ivdbg(" id: %d\n", report->point[0].id); ivdbg(" flags: %02x\n", report->point[0].flags); ivdbg(" x: %d\n", report->point[0].x); ivdbg(" y: %d\n", report->point[0].y); ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: sem_post(&priv->devsem); ivdbg("Returning: %d\n", ret); return ret; }
static int ads7843e_waitsample(FAR struct ads7843e_dev_s *priv, FAR struct ads7843e_sample_s *sample) { irqstate_t flags; int ret; /* Interrupts me be disabled when this is called to (1) prevent posting * of semaphores from interrupt handlers, and (2) to prevent sampled data * from changing until it has been reported. * * In addition, we will also disable pre-emption to prevent other threads * from getting control while we muck with the semaphores. */ sched_lock(); flags = irqsave(); /* Now release the semaphore that manages mutually exclusive access to * the device structure. This may cause other tasks to become ready to * run, but they cannot run yet because pre-emption is disabled. */ sem_post(&priv->devsem); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. */ while (ads7843e_sample(priv, sample) < 0) { /* Wait for a change in the ADS7843E state */ ivdbg("Waiting..\n"); priv->nwaiters++; ret = sem_wait(&priv->waitsem); priv->nwaiters--; if (ret < 0) { /* If we are awakened by a signal, then we need to return * the failure now. */ idbg("sem_wait: %d\n", errno); DEBUGASSERT(errno == EINTR); ret = -EINTR; goto errout; } } ivdbg("Sampled\n"); /* Re-acquire the semaphore that manages mutually exclusive access to * the device structure. We may have to wait here. But we have our sample. * Interrupts and pre-emption will be re-enabled while we wait. */ ret = sem_wait(&priv->devsem); errout: /* Then re-enable interrupts. We might get interrupt here and there * could be a new sample. But no new threads will run because we still * have pre-emption disabled. */ irqrestore(flags); /* Restore pre-emption. We might get suspended here but that is okay * because we already have our sample. Note: this means that if there * were two threads reading from the ADS7843E for some reason, the data * might be read out of order. */ sched_unlock(); return ret; }
int ads7843e_register(FAR struct spi_dev_s *spi, FAR struct ads7843e_config_s *config, int minor) { FAR struct ads7843e_dev_s *priv; char devname[DEV_NAMELEN]; #ifdef CONFIG_ADS7843E_MULTIPLE irqstate_t flags; #endif int ret; ivdbg("spi: %p minor: %d\n", spi, minor); /* Debug-only sanity checks */ DEBUGASSERT(spi != NULL && config != NULL && minor >= 0 && minor < 100); /* Create and initialize a ADS7843E device driver instance */ #ifndef CONFIG_ADS7843E_MULTIPLE priv = &g_ads7843e; #else priv = (FAR struct ads7843e_dev_s *)kmalloc(sizeof(struct ads7843e_dev_s)); if (!priv) { idbg("kmalloc(%d) failed\n", sizeof(struct ads7843e_dev_s)); return -ENOMEM; } #endif /* Initialize the ADS7843E device driver instance */ memset(priv, 0, sizeof(struct ads7843e_dev_s)); priv->spi = spi; /* Save the SPI device handle */ priv->config = config; /* Save the board configuration */ priv->wdog = wd_create(); /* Create a watchdog timer */ priv->threshx = INVALID_THRESHOLD; /* Initialize thresholding logic */ priv->threshy = INVALID_THRESHOLD; /* Initialize thresholding logic */ sem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ sem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* Make sure that interrupts are disabled */ config->clear(config); config->enable(config, false); /* Attach the interrupt handler */ ret = config->attach(config, ads7843e_interrupt); if (ret < 0) { idbg("Failed to attach interrupt\n"); goto errout_with_priv; } idbg("Mode: %d Bits: 8 Frequency: %d\n", CONFIG_ADS7843E_SPIMODE, CONFIG_ADS7843E_FREQUENCY); /* Lock the SPI bus so that we have exclusive access */ ads7843e_lock(spi); /* Configure the SPI interface */ ads7843e_configspi(spi); /* Enable the PEN IRQ */ ads7843e_sendcmd(priv, ADS7843_CMD_ENABPENIRQ); /* Unlock the bus */ ads7843e_unlock(spi); /* Register the device as an input device */ (void)snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor); ivdbg("Registering %s\n", devname); ret = register_driver(devname, &ads7843e_fops, 0666, priv); if (ret < 0) { idbg("register_driver() failed: %d\n", ret); goto errout_with_priv; } /* If multiple ADS7843E devices are supported, then we will need to add * this new instance to a list of device instances so that it can be * found by the interrupt handler based on the recieved IRQ number. */ #ifdef CONFIG_ADS7843E_MULTIPLE priv->flink = g_ads7843elist; g_ads7843elist = priv; irqrestore(flags); #endif /* Schedule work to perform the initial sampling and to set the data * availability conditions. */ ret = work_queue(HPWORK, &priv->work, ads7843e_worker, priv, 0); if (ret != 0) { idbg("Failed to queue work: %d\n", ret); goto errout_with_priv; } /* And return success (?) */ return OK; errout_with_priv: sem_destroy(&priv->devsem); #ifdef CONFIG_ADS7843E_MULTIPLE kfree(priv); #endif return ret; }
static int ads7843e_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup) { FAR struct inode *inode; FAR struct ads7843e_dev_s *priv; int ret; int i; ivdbg("setup: %d\n", (int)setup); DEBUGASSERT(filep && fds); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct ads7843e_dev_s *)inode->i_private; /* Are we setting up the poll? Or tearing it down? */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); return -EINTR; } if (setup) { /* Ignore waits that do not include POLLIN */ if ((fds->events & POLLIN) == 0) { ret = -EDEADLK; goto errout; } /* This is a request to set up the poll. Find an available * slot for the poll structure reference */ for (i = 0; i < CONFIG_ADS7843E_NPOLLWAITERS; i++) { /* Find an available slot */ if (!priv->fds[i]) { /* Bind the poll structure and this slot */ priv->fds[i] = fds; fds->priv = &priv->fds[i]; break; } } if (i >= CONFIG_ADS7843E_NPOLLWAITERS) { fds->priv = NULL; ret = -EBUSY; goto errout; } /* Should we immediately notify on any of the requested events? */ if (priv->penchange) { ads7843e_notify(priv); } } else if (fds->priv) { /* This is a request to tear down the poll. */ struct pollfd **slot = (struct pollfd **)fds->priv; DEBUGASSERT(slot != NULL); /* Remove all memory of the poll setup */ *slot = NULL; fds->priv = NULL; } errout: sem_post(&priv->devsem); return ret; }
static ssize_t up_read(FAR struct file *filep, FAR char *buffer, size_t len) { FAR struct inode *inode; FAR struct up_dev_s *priv; FAR struct touch_sample_s *report; struct up_sample_s sample; int ret; ivdbg("len=%d\n", len); DEBUGASSERT(filep); inode = filep->f_inode; DEBUGASSERT(inode && inode->i_private); priv = (FAR struct up_dev_s *)inode->i_private; /* Verify that the caller has provided a buffer large enough to receive * the touch data. */ if (len < SIZEOF_TOUCH_SAMPLE_S(1)) { /* We could provide logic to break up a touch report into segments and * handle smaller reads... but why? */ return -ENOSYS; } /* Get exclusive access to the driver data structure */ ret = sem_wait(&priv->devsem); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ DEBUGASSERT(errno == EINTR); return -EINTR; } /* Try to read sample data. */ ret = up_sample(priv, &sample); if (ret < 0) { /* Sample data is not available now. We would ave to wait to get * receive sample data. If the user has specified the O_NONBLOCK * option, then just return an error. */ if (filep->f_oflags & O_NONBLOCK) { ret = -EAGAIN; goto errout; } /* Wait for sample data */ ret = up_waitsample(priv, &sample); if (ret < 0) { /* We might have been awakened by a signal */ goto errout; } } /* In any event, we now have sampled touchscreen data that we can report * to the caller. */ report = (FAR struct touch_sample_s *)buffer; memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1)); report->npoints = 1; report->point[0].id = priv->id; report->point[0].x = sample.x; report->point[0].y = sample.y; report->point[0].h = 1; report->point[0].w = 1; report->point[0].pressure = 42; /* Report the appropriate flags */ if (sample.contact == CONTACT_UP) { /* Pen is now up */ report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID; } else if (sample.contact == CONTACT_DOWN) { /* First contact */ report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID | TOUCH_POS_VALID | TOUCH_PRESSURE_VALID; } else /* if (sample->contact == CONTACT_MOVE) */ { /* Movement of the same contact */ report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID | TOUCH_POS_VALID | TOUCH_PRESSURE_VALID; } ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: ivdbg("Returning %d\n", ret); sem_post(&priv->devsem); return ret; }
static void max11802_worker(FAR void *arg) { FAR struct max11802_dev_s *priv = (FAR struct max11802_dev_s *)arg; FAR struct max11802_config_s *config; uint16_t x; uint16_t y; uint16_t xdiff; uint16_t ydiff; bool pendown; int ret; int tags, tags2; ASSERT(priv != NULL); /* Get a pointer the callbacks for convenience (and so the code is not so * ugly). */ config = priv->config; DEBUGASSERT(config != NULL); /* Disable the watchdog timer. This is safe because it is started only * by this function and this function is serialized on the worker thread. */ wd_cancel(priv->wdog); /* Lock the SPI bus so that we have exclusive access */ max11802_lock(priv->spi); /* Start coordinate measurement */ (void)max11802_sendcmd(priv, MAX11802_CMD_MEASUREXY, &tags); /* Get exclusive access to the driver data structure */ do { ret = sem_wait(&priv->devsem); /* This should only fail if the wait was cancelled by an signal * (and the worker thread will receive a lot of signals). */ DEBUGASSERT(ret == OK || errno == EINTR); } while (ret < 0); /* Check for pen up or down by reading the PENIRQ GPIO. */ pendown = config->pendown(config); /* Handle the change from pen down to pen up */ if (pendown) { ivdbg("\nPD\n"); } else { ivdbg("\nPU\n"); } if (!pendown) { /* The pen is up.. reset thresholding variables. */ priv->threshx = INVALID_THRESHOLD; priv->threshy = INVALID_THRESHOLD; /* Ignore the interrupt if the pen was already up (CONTACT_NONE == * pen up and already reported; CONTACT_UP == pen up, but not * reported). */ ivdbg("\nPC%d\n", priv->sample.contact); if (priv->sample.contact == CONTACT_NONE || priv->sample.contact == CONTACT_UP) { goto ignored; } /* The pen is up. NOTE: We know from a previous test, that this is a * loss of contact condition. This will be changed to CONTACT_NONE * after the loss of contact is sampled. */ priv->sample.contact = CONTACT_UP; } /* It is a pen down event. If the last loss-of-contact event has not been * processed yet, then we have to ignore the pen down event (or else it * will look like a drag event) */ else if (priv->sample.contact == CONTACT_UP) { /* If we have not yet processed the last pen up event, then we * cannot handle this pen down event. We will have to discard it. * That should be okay because we will set the timer to to sample * again later. */ ivdbg("Previous pen up event still in buffer\n"); max11802_notify(priv); wd_start(priv->wdog, MAX11802_WDOG_DELAY, max11802_wdog, 1, (uint32_t)priv); goto ignored; } else { /* Wait for data ready * * Note: MAX11802 signals the readiness of the results using * the lowest 4 bits of the result. However these are the * last bits to be read out of the device. It appears that * the hardware value can change in the middle of the readout, * causing the upper bits to be still invalid even though lower * bits indicate valid result. * * We work around this by reading the registers once more after * the tags indicate they are ready. */ int readycount = 0; do { #ifdef CONFIG_MAX11802_SWAPXY x = max11802_sendcmd(priv, MAX11802_CMD_YPOSITION, &tags); y = max11802_sendcmd(priv, MAX11802_CMD_XPOSITION, &tags2); #else x = max11802_sendcmd(priv, MAX11802_CMD_XPOSITION, &tags); y = max11802_sendcmd(priv, MAX11802_CMD_YPOSITION, &tags2); #endif if (tags != 0xF && tags2 != 0xF) { readycount++; } } while (readycount < 2); /* Continue to sample the position while the pen is down */ wd_start(priv->wdog, MAX11802_WDOG_DELAY, max11802_wdog, 1, (uint32_t)priv); /* Check if data is valid */ if ((tags & 0x03) != 0) { ivdbg("Touch ended before measurement\n"); goto ignored; } /* Perform a thresholding operation so that the results will be more * stable. If the difference from the last sample is small, then * ignore the event. * * REVISIT: Should a large change in pressure also generate a event? */ xdiff = x > priv->threshx ? (x - priv->threshx) : (priv->threshx - x); ydiff = y > priv->threshy ? (y - priv->threshy) : (priv->threshy - y); /* Check the thresholds. Bail if there is no significant difference */ if (xdiff < CONFIG_MAX11802_THRESHX && ydiff < CONFIG_MAX11802_THRESHY) { /* Little or no change in either direction ... don't report anything. */ goto ignored; } /* When we see a big difference, snap to the new x/y thresholds */ priv->threshx = x; priv->threshy = y; /* Update the x/y position in the sample data */ priv->sample.x = priv->threshx; priv->sample.y = priv->threshy; /* The X/Y positional data is now valid */ priv->sample.valid = true; /* If this is the first (acknowledged) pen down report, then report * this as the first contact. If contact == CONTACT_DOWN, it will be * set to set to CONTACT_MOVE after the contact is first sampled. */ if (priv->sample.contact != CONTACT_MOVE) { /* First contact */ priv->sample.contact = CONTACT_DOWN; } } /* Indicate the availability of new sample data for this ID */ priv->sample.id = priv->id; priv->penchange = true; /* Notify any waiters that new MAX11802 data is available */ max11802_notify(priv); ignored: config->enable(config, true); /* Release our lock on the state structure and unlock the SPI bus */ sem_post(&priv->devsem); max11802_unlock(priv->spi); }
int max11802_register(FAR struct spi_dev_s *spi, FAR struct max11802_config_s *config, int minor) { FAR struct max11802_dev_s *priv; char devname[DEV_NAMELEN]; #ifdef CONFIG_MAX11802_MULTIPLE irqstate_t flags; #endif int ret; ivdbg("spi: %p minor: %d\n", spi, minor); /* Debug-only sanity checks */ DEBUGASSERT(spi != NULL && config != NULL && minor >= 0 && minor < 100); /* Create and initialize a MAX11802 device driver instance */ #ifndef CONFIG_MAX11802_MULTIPLE priv = &g_max11802; #else priv = (FAR struct max11802_dev_s *)kmm_malloc(sizeof(struct max11802_dev_s)); if (!priv) { idbg("kmm_malloc(%d) failed\n", sizeof(struct max11802_dev_s)); return -ENOMEM; } #endif /* Initialize the MAX11802 device driver instance */ memset(priv, 0, sizeof(struct max11802_dev_s)); priv->spi = spi; /* Save the SPI device handle */ priv->config = config; /* Save the board configuration */ priv->wdog = wd_create(); /* Create a watchdog timer */ priv->threshx = INVALID_THRESHOLD; /* Initialize thresholding logic */ priv->threshy = INVALID_THRESHOLD; /* Initialize thresholding logic */ sem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ sem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* Make sure that interrupts are disabled */ config->clear(config); config->enable(config, false); /* Attach the interrupt handler */ ret = config->attach(config, max11802_interrupt); if (ret < 0) { idbg("Failed to attach interrupt\n"); goto errout_with_priv; } idbg("Mode: %d Bits: 8 Frequency: %d\n", CONFIG_MAX11802_SPIMODE, CONFIG_MAX11802_FREQUENCY); /* Lock the SPI bus so that we have exclusive access */ max11802_lock(spi); /* Configure MAX11802 registers */ SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); (void)SPI_SEND(priv->spi, MAX11802_CMD_MODE_WR); (void)SPI_SEND(priv->spi, MAX11802_MODE); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); (void)SPI_SEND(priv->spi, MAX11802_CMD_AVG_WR); (void)SPI_SEND(priv->spi, MAX11802_AVG); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); (void)SPI_SEND(priv->spi, MAX11802_CMD_TIMING_WR); (void)SPI_SEND(priv->spi, MAX11802_TIMING); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); (void)SPI_SEND(priv->spi, MAX11802_CMD_DELAY_WR); (void)SPI_SEND(priv->spi, MAX11802_DELAY); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); /* Test that the device access was successful. */ SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, true); (void)SPI_SEND(priv->spi, MAX11802_CMD_MODE_RD); ret = SPI_SEND(priv->spi, 0); SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN, false); /* Unlock the bus */ max11802_unlock(spi); if (ret != MAX11802_MODE) { idbg("max11802 mode readback failed: %02x\n", ret); goto errout_with_priv; } /* Register the device as an input device */ (void)snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor); ivdbg("Registering %s\n", devname); ret = register_driver(devname, &max11802_fops, 0666, priv); if (ret < 0) { idbg("register_driver() failed: %d\n", ret); goto errout_with_priv; } /* If multiple MAX11802 devices are supported, then we will need to add * this new instance to a list of device instances so that it can be * found by the interrupt handler based on the recieved IRQ number. */ #ifdef CONFIG_MAX11802_MULTIPLE flags = irqsave(); priv->flink = g_max11802list; g_max11802list = priv; irqrestore(flags); #endif /* Schedule work to perform the initial sampling and to set the data * availability conditions. */ ret = work_queue(HPWORK, &priv->work, max11802_worker, priv, 0); if (ret != 0) { idbg("Failed to queue work: %d\n", ret); goto errout_with_priv; } /* And return success (?) */ return OK; errout_with_priv: sem_destroy(&priv->devsem); #ifdef CONFIG_MAX11802_MULTIPLE kmm_free(priv); #endif return ret; }