ugfx/drivers/touchscreen/XPT2046/touchscreen_lld.c

242 lines
6.5 KiB
C
Raw Normal View History

2012-08-13 02:12:42 +00:00
/*
ChibiOS/GFX - Copyright (C) 2012
2012-08-13 02:12:42 +00:00
Joel Bodenmann aka Tectu <joel@unormal.org>
This file is part of ChibiOS/GFX.
2012-08-13 02:12:42 +00:00
ChibiOS/GFX is free software; you can redistribute it and/or modify
2012-08-13 02:12:42 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/GFX is distributed in the hope that it will be useful,
2012-08-13 02:12:42 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
2012-11-09 23:05:01 +00:00
* @file drivers/touchscreen/XPT2046/touchscreen_lld.c
* @brief Touchscreen Driver subsystem low level driver source.
2012-08-13 02:12:42 +00:00
*
2012-11-09 23:05:01 +00:00
* @addtogroup TOUCHSCREEN
2012-08-13 02:12:42 +00:00
* @{
*/
#include "ch.h"
#include "hal.h"
2012-11-09 23:05:01 +00:00
#include "touchscreen.h"
2012-08-13 02:12:42 +00:00
2012-11-09 23:05:01 +00:00
#if GFX_USE_TOUCHSCREEN /*|| defined(__DOXYGEN__)*/
2012-08-13 02:12:42 +00:00
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables. */
/*===========================================================================*/
#if !defined(__DOXYGEN__)
/* Local copy of the current touchpad driver */
2012-11-09 23:05:01 +00:00
static const TouchscreenDriver *tsDriver;
static uint16_t sampleBuf[7];
#endif
2012-08-13 02:12:42 +00:00
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver interrupt handlers. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/* ---- Required Routines ---- */
/**
2012-11-09 23:05:01 +00:00
* @brief Low level Touchscreen driver initialization.
2012-08-13 02:12:42 +00:00
*
2012-11-09 23:05:01 +00:00
* @param[in] ts The touchscreen driver struct
2012-11-02 19:26:06 +00:00
*
2012-08-13 02:12:42 +00:00
* @notapi
*/
2012-11-09 23:05:01 +00:00
void ts_lld_init(const TouchscreenDriver *ts) {
tsDriver = ts;
2012-11-09 23:05:01 +00:00
if(tsDriver->direct_init)
spiStart(tsDriver->spip, tsDriver->spicfg);
}
/**
2012-11-09 23:05:01 +00:00
* @brief Reads a conversion from the touchscreen
*
2012-11-09 23:05:01 +00:00
* @param[in] cmd The command bits to send to the touchscreen
*
* @return The read value 12-bit right-justified
*
* @note This function only reads data, it is assumed that the pins are
* configured properly and the bus has been acquired beforehand
*
* @notapi
*/
2012-11-09 23:05:01 +00:00
uint16_t ts_lld_read_value(uint8_t cmd) {
2012-08-16 10:52:34 +00:00
static uint8_t txbuf[3] = {0};
static uint8_t rxbuf[3] = {0};
uint16_t ret;
2012-08-16 10:52:34 +00:00
txbuf[0] = cmd;
2012-11-09 23:05:01 +00:00
spiExchange(tsDriver->spip, 3, txbuf, rxbuf);
2012-08-16 10:52:34 +00:00
ret = (rxbuf[1] << 5) | (rxbuf[2] >> 3);
2012-08-16 10:52:34 +00:00
return ret;
}
/**
2012-11-09 23:05:01 +00:00
* @brief 7-point median filtering code for touchscreen samples
*
* @note This is an internally used routine only.
*
* @notapi
*/
2012-11-09 23:05:01 +00:00
static void ts_lld_filter(void) {
2012-08-16 10:52:34 +00:00
uint16_t temp;
int i,j;
for(i = 0; i < 4; i++) {
2012-11-09 23:05:01 +00:00
for(j = i; j < 7; j++) {
2012-08-16 10:52:34 +00:00
if(sampleBuf[i] > sampleBuf[j]) {
/* Swap the values */
temp = sampleBuf[i];
sampleBuf[i] = sampleBuf[j];
sampleBuf[j] = temp;
}
}
}
2012-08-13 02:12:42 +00:00
}
/**
* @brief Reads out the X direction.
*
* @note The samples are median filtered for greater noise reduction
2012-11-06 22:55:45 +00:00
*
2012-08-13 02:12:42 +00:00
* @notapi
*/
2012-11-09 23:05:01 +00:00
uint16_t ts_lld_read_x(void) {
2012-08-16 10:52:34 +00:00
int i;
#if defined(SPI_USE_MUTUAL_EXCLUSION)
2012-11-09 23:05:01 +00:00
spiAcquireBus(tsDriver->spip);
#endif
2012-11-09 23:05:01 +00:00
TOUCHSCREEN_SPI_PROLOGUE();
palClearPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
2012-08-16 10:52:34 +00:00
/* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled.
*/
2012-11-09 23:05:01 +00:00
ts_lld_read_value(0xD1);
2012-08-16 10:52:34 +00:00
for(i = 0; i < 7; i++) {
2012-11-09 23:05:01 +00:00
sampleBuf[i] = ts_lld_read_value(0xD1);
2012-08-16 10:52:34 +00:00
}
2012-08-13 02:12:42 +00:00
2012-08-16 10:52:34 +00:00
/* Switch on PENIRQ once again - perform a dummy read */
2012-11-09 23:05:01 +00:00
ts_lld_read_value(0xD0);
2012-08-13 02:12:42 +00:00
2012-11-09 23:05:01 +00:00
palSetPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
TOUCHSCREEN_SPI_EPILOGUE();
2012-08-13 02:12:42 +00:00
#if defined(SPI_USE_MUTUAL_EXCLUSION)
2012-11-09 23:05:01 +00:00
spiReleaseBus(tsDriver->spip);
#endif
2012-08-16 10:52:34 +00:00
/* Find the median - use selection sort */
2012-11-09 23:05:01 +00:00
ts_lld_filter();
2012-08-16 10:52:34 +00:00
return sampleBuf[3];
2012-08-13 02:12:42 +00:00
}
/*
* @brief Reads out the Y direction.
2012-08-13 02:12:42 +00:00
*
* @notapi
*/
2012-11-09 23:05:01 +00:00
uint16_t ts_lld_read_y(void) {
2012-08-16 10:52:34 +00:00
int i;
#if defined(SPI_USE_MUTUAL_EXCLUSION)
2012-11-09 23:05:01 +00:00
spiAcquireBus(tsDriver->spip);
#endif
2012-11-09 23:05:01 +00:00
TOUCHSCREEN_SPI_PROLOGUE();
palClearPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
2012-08-13 02:12:42 +00:00
2012-08-16 10:52:34 +00:00
/* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled.
*/
2012-11-09 23:05:01 +00:00
ts_lld_read_value(0x91);
2012-08-16 10:52:34 +00:00
for(i = 0; i < 7; i++) {
2012-11-09 23:05:01 +00:00
sampleBuf[i] = ts_lld_read_value(0x91);
2012-08-16 10:52:34 +00:00
}
2012-08-16 10:52:34 +00:00
/* Switch on PENIRQ once again - perform a dummy read */
2012-11-09 23:05:01 +00:00
ts_lld_read_value(0x90);
2012-11-09 23:05:01 +00:00
palSetPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
TOUCHSCREEN_SPI_EPILOGUE();
#ifdef SPI_USE_MUTUAL_EXCLUSION
2012-11-09 23:05:01 +00:00
spiReleaseBus(tsDriver->spip);
#endif
2012-08-13 02:12:42 +00:00
2012-08-16 10:52:34 +00:00
/* Find the median - use selection sort */
2012-11-09 23:05:01 +00:00
ts_lld_filter();
2012-08-13 02:12:42 +00:00
2012-08-16 10:52:34 +00:00
return sampleBuf[3];
2012-08-13 02:12:42 +00:00
}
/* ---- Optional Routines ---- */
#if TOUCHSCREEN_HAS_PRESSED || defined(__DOXYGEN__)
/*
2012-11-09 23:05:01 +00:00
* @brief for checking if touchscreen is pressed or not.
2012-08-13 02:12:42 +00:00
*
* @return 1 if pressed / 0 if not pressed
*
* @notapi
*/
2012-11-10 15:46:02 +00:00
uint8_t ts_lld_pressed(void) {
2012-11-09 23:05:01 +00:00
return (!palReadPad(tsDriver->tsIRQPort, tsDriver->tsIRQPin));
2012-08-13 02:12:42 +00:00
}
#endif
2012-11-09 23:05:01 +00:00
#if TOUCHSCREEN_HAS_PRESSURE || defined(__DOXYGEN__)
/*
2012-08-13 02:12:42 +00:00
* @brief Reads out the Z direction / pressure.
*
* @notapi
*/
2012-11-09 23:05:01 +00:00
uint16_t ts_lld_read_z(void) {
2012-08-13 02:12:42 +00:00
/* ToDo */
return 42;
}
#endif
2012-11-09 23:05:01 +00:00
#endif /* GFX_USE_TOUCHSCREEN */
2012-08-13 02:12:42 +00:00
/** @} */