2014-10-13 07:44:15 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms of the GFX License. If a copy of
|
|
|
|
* the license was not distributed with this file, you can obtain one at:
|
|
|
|
*
|
|
|
|
* http://ugfx.org/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
|
|
|
|
|
|
|
|
#define GMOUSE_DRIVER_VMT GMOUSEVMT_STMPE811
|
2015-11-21 09:27:08 +00:00
|
|
|
#include "../../../../src/ginput/ginput_driver_mouse.h"
|
2014-10-13 07:44:15 +00:00
|
|
|
|
|
|
|
// Hardware definitions
|
2015-11-21 09:27:08 +00:00
|
|
|
#include "stmpe811.h"
|
2014-10-13 07:44:15 +00:00
|
|
|
|
2014-10-13 21:01:01 +00:00
|
|
|
// Get the hardware interface
|
|
|
|
#include "gmouse_lld_STMPE811_board.h"
|
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
// Extra settings for the users gfxconf.h file. See readme.txt
|
|
|
|
#ifndef GMOUSE_STMPE811_SELF_CALIBRATE
|
2018-02-27 07:44:21 +00:00
|
|
|
#define GMOUSE_STMPE811_SELF_CALIBRATE GFXOFF
|
2014-11-07 02:05:23 +00:00
|
|
|
#endif
|
|
|
|
#ifndef GMOUSE_STMPE811_READ_PRESSURE
|
2018-02-27 07:44:21 +00:00
|
|
|
#define GMOUSE_STMPE811_READ_PRESSURE GFXOFF
|
2014-11-07 02:05:23 +00:00
|
|
|
#endif
|
|
|
|
#ifndef GMOUSE_STMPE811_TEST_MODE
|
2018-02-27 07:44:21 +00:00
|
|
|
#define GMOUSE_STMPE811_TEST_MODE GFXOFF
|
2014-11-07 02:05:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* This chip has some problems which required careful coding to overcome.
|
|
|
|
*
|
|
|
|
* The interrupt pin seems to be unreliable, at least on some boards, so we at most
|
|
|
|
* use the pin for filtering results to reduce cpu load.
|
|
|
|
* The symptoms are that readings will just stop due to the irq not being asserted
|
|
|
|
* even though there are items in the fifo. Another interrupt source such as a
|
|
|
|
* touch transition will restart the irq.
|
|
|
|
*
|
|
|
|
* There is no fifo entry created when a touch up event occurs. We must therefore
|
|
|
|
* generate a pseudo result on touch up. Fortunately the touch detection appears
|
|
|
|
* reliable and so we turn off the driver GMOUSE_VFLG_POORUPDOWN setting. In practice
|
|
|
|
* if touch is up we always return a pseudo event as this saves having to remember the
|
|
|
|
* previous touch state.
|
|
|
|
*
|
|
|
|
* Z readings range from around 90 (fully touched) to around 150 (on the verge of non-touched).
|
|
|
|
* Note the above is on the STM32F429i-Discovery board. Other boards may be different.
|
|
|
|
* To be conservative we use 255 as touch off, anything else is a touch on.
|
|
|
|
*
|
|
|
|
* GMOUSE_STMPE811_TEST_MODE is designed to be used with the "touch_raw_readings" tool which shows
|
|
|
|
* a steady stream of raw readings.
|
|
|
|
*
|
|
|
|
* Settings that may need tweaking on other hardware:
|
|
|
|
* The settling times. We have set these conservatively at 1ms.
|
|
|
|
* The reading window. We set this to 16 just to reduce noise. High-res panels may need a lower value.
|
|
|
|
*/
|
2018-06-23 03:02:07 +00:00
|
|
|
static gBool MouseInit(GMouse* m, unsigned driverinstance) {
|
2014-10-13 07:44:15 +00:00
|
|
|
if (!init_board(m, driverinstance))
|
2018-06-23 03:02:07 +00:00
|
|
|
return gFalse;
|
2014-10-13 07:44:15 +00:00
|
|
|
|
|
|
|
aquire_bus(m);
|
|
|
|
|
|
|
|
write_reg(m, STMPE811_REG_SYS_CTRL1, 0x02); // Software chip reset
|
|
|
|
gfxSleepMilliseconds(10);
|
|
|
|
|
|
|
|
write_reg(m, STMPE811_REG_SYS_CTRL2, 0x0C); // Temperature sensor clock off, GPIO clock off, touch clock on, ADC clock on
|
|
|
|
|
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
2014-11-07 02:05:23 +00:00
|
|
|
write_reg(m, STMPE811_REG_INT_EN, 0x03); // Interrupt on INT pin when there is a sample or a touch transition.
|
2014-10-13 07:44:15 +00:00
|
|
|
#else
|
2014-11-07 02:05:23 +00:00
|
|
|
write_reg(m, STMPE811_REG_INT_EN, 0x00); // Don't Interrupt on INT pin
|
2014-10-13 07:44:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
write_reg(m, STMPE811_REG_ADC_CTRL1, 0x48); // ADC conversion time = 80 clock ticks, 12-bit ADC, internal voltage refernce
|
|
|
|
gfxSleepMilliseconds(2);
|
|
|
|
write_reg(m, STMPE811_REG_ADC_CTRL2, 0x01); // ADC speed 3.25MHz
|
|
|
|
write_reg(m, STMPE811_REG_GPIO_AF, 0x00); // GPIO alternate function - OFF
|
2014-11-07 02:05:23 +00:00
|
|
|
write_reg(m, STMPE811_REG_TSC_CFG, 0xA3); // Averaging 4, touch detect delay 1ms, panel driver settling time 1ms
|
|
|
|
write_reg(m, STMPE811_REG_FIFO_TH, 0x01); // FIFO threshold = 1
|
2014-10-13 07:44:15 +00:00
|
|
|
write_reg(m, STMPE811_REG_FIFO_STA, 0x01); // FIFO reset enable
|
|
|
|
write_reg(m, STMPE811_REG_FIFO_STA, 0x00); // FIFO reset disable
|
|
|
|
write_reg(m, STMPE811_REG_TSC_FRACT_XYZ, 0x07); // Z axis data format
|
2014-11-07 02:05:23 +00:00
|
|
|
write_reg(m, STMPE811_REG_TSC_I_DRIVE, 0x01); // max 50mA touchscreen line current
|
|
|
|
#if GMOUSE_STMPE811_READ_PRESSURE
|
|
|
|
write_reg(m, STMPE811_REG_TSC_CTRL, 0x30); // X&Y&Z, 16 reading window
|
|
|
|
write_reg(m, STMPE811_REG_TSC_CTRL, 0x31); // X&Y&Z, 16 reading window, TSC enable
|
|
|
|
#else
|
|
|
|
write_reg(m, STMPE811_REG_TSC_CTRL, 0x32); // X&Y, 16 reading window
|
|
|
|
write_reg(m, STMPE811_REG_TSC_CTRL, 0x33); // X&Y, 16 reading window, TSC enable
|
2014-10-13 07:44:15 +00:00
|
|
|
#endif
|
2014-11-07 02:05:23 +00:00
|
|
|
write_reg(m, STMPE811_REG_INT_STA, 0xFF); // Clear all interrupts
|
|
|
|
write_reg(m, STMPE811_REG_INT_CTRL, 0x01); // Level interrupt, enable interrupts
|
2014-10-13 07:44:15 +00:00
|
|
|
|
|
|
|
release_bus(m);
|
2018-06-23 03:02:07 +00:00
|
|
|
return gTrue;
|
2014-10-13 07:44:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:02:07 +00:00
|
|
|
static gBool read_xyz(GMouse* m, GMouseReading* pdr)
|
2014-10-13 07:44:15 +00:00
|
|
|
{
|
2014-11-07 02:05:23 +00:00
|
|
|
#if GMOUSE_STMPE811_TEST_MODE
|
|
|
|
static GMouseReading n;
|
|
|
|
#endif
|
|
|
|
uint8_t status;
|
2014-10-13 07:44:15 +00:00
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
// Button information will be regenerated
|
2014-10-13 07:44:15 +00:00
|
|
|
pdr->buttons = 0;
|
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
#if GMOUSE_STMPE811_TEST_MODE
|
|
|
|
aquire_bus(m);
|
2014-10-13 07:44:15 +00:00
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
// Set the buttons to match various touch signals
|
|
|
|
if ((read_byte(m, STMPE811_REG_TSC_CTRL) & 0x80))
|
|
|
|
pdr->buttons |= 0x02;
|
2014-10-13 07:44:15 +00:00
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
status = read_byte(m, STMPE811_REG_FIFO_STA);
|
|
|
|
if (!(status & 0x20))
|
|
|
|
pdr->buttons |= 0x04;
|
2014-10-13 07:44:15 +00:00
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
|
|
|
if (getpin_irq(m))
|
|
|
|
pdr->buttons |= 0x08;
|
2014-10-13 07:44:15 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
if ((status & 0x20)) {
|
|
|
|
// Nothing in the fifo - just return the last position and pressure
|
|
|
|
pdr->x = n.x;
|
|
|
|
pdr->y = n.y;
|
|
|
|
pdr->z = n.z;
|
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
|
|
|
write_reg(m, STMPE811_REG_INT_STA, 0xFF);
|
|
|
|
#endif
|
|
|
|
release_bus(m);
|
2018-06-23 03:02:07 +00:00
|
|
|
return gTrue;
|
2014-11-07 02:05:23 +00:00
|
|
|
}
|
2014-10-13 07:44:15 +00:00
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
#else
|
|
|
|
// Is there a new sample or a touch transition
|
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
|
|
|
if(!getpin_irq(m))
|
2018-06-23 03:02:07 +00:00
|
|
|
return gFalse;
|
2014-10-13 07:44:15 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
// Is there something in the fifo
|
|
|
|
status = read_byte(m, STMPE811_REG_FIFO_STA);
|
|
|
|
if ((status & 0x20)) {
|
|
|
|
|
|
|
|
// Nothing in the fifo.
|
|
|
|
|
|
|
|
// If not touched return the pseudo result
|
|
|
|
if (!(read_byte(m, STMPE811_REG_TSC_CTRL) & 0x80)) {
|
|
|
|
|
|
|
|
pdr->z = gmvmt(m)->z_min;
|
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
|
|
|
write_reg(m, STMPE811_REG_INT_STA, 0xFF);
|
|
|
|
#endif
|
|
|
|
release_bus(m);
|
2018-06-23 03:02:07 +00:00
|
|
|
return gTrue;
|
2014-11-07 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No new result
|
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
|
|
|
write_reg(m, STMPE811_REG_INT_STA, 0xFF);
|
|
|
|
#endif
|
|
|
|
release_bus(m);
|
2018-06-23 03:02:07 +00:00
|
|
|
return gFalse;
|
2014-11-07 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Time to get some readings
|
|
|
|
pdr->x = (coord_t)read_word(m, STMPE811_REG_TSC_DATA_X);
|
|
|
|
pdr->y = (coord_t)read_word(m, STMPE811_REG_TSC_DATA_Y);
|
|
|
|
#if GMOUSE_STMPE811_READ_PRESSURE
|
|
|
|
pdr->z = (coord_t)read_byte(m, STMPE811_REG_TSC_DATA_Z);
|
|
|
|
#else
|
|
|
|
pdr->z = gmvmt(m)->z_max;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !GMOUSE_STMPE811_SLOW_CPU
|
|
|
|
if (!(status & 0xC0)) {
|
|
|
|
// Is there more data to come
|
|
|
|
if (!(read_byte(m, STMPE811_REG_FIFO_STA) & 0x20))
|
|
|
|
_gmouseWakeup(m);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Clear the rest of the fifo
|
|
|
|
{
|
|
|
|
write_reg(m, STMPE811_REG_FIFO_STA, 0x01); // FIFO reset enable
|
|
|
|
write_reg(m, STMPE811_REG_FIFO_STA, 0x00); // FIFO reset disable
|
2014-10-13 07:44:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 02:05:23 +00:00
|
|
|
// All done
|
|
|
|
#if GMOUSE_STMPE811_GPIO_IRQPIN
|
|
|
|
write_reg(m, STMPE811_REG_INT_STA, 0xFF);
|
|
|
|
#endif
|
2014-10-13 07:44:15 +00:00
|
|
|
release_bus(m);
|
2014-11-07 02:05:23 +00:00
|
|
|
|
|
|
|
#if GMOUSE_STMPE811_TEST_MODE
|
|
|
|
// Save the result for later
|
|
|
|
n.x = pdr->x;
|
|
|
|
n.y = pdr->y;
|
|
|
|
n.z = pdr->z;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Rescale X,Y if we are using self-calibration
|
|
|
|
#if GMOUSE_STMPE811_SELF_CALIBRATE
|
2015-02-23 08:15:22 +00:00
|
|
|
#if GDISP_NEED_CONTROL
|
|
|
|
switch(gdispGGetOrientation(m->display)) {
|
2015-02-28 06:26:37 +00:00
|
|
|
default:
|
2015-02-23 08:15:22 +00:00
|
|
|
case GDISP_ROTATE_0:
|
|
|
|
case GDISP_ROTATE_180:
|
|
|
|
pdr->x = gdispGGetWidth(m->display) - pdr->x / (4096/gdispGGetWidth(m->display));
|
|
|
|
pdr->y = pdr->y / (4096/gdispGGetHeight(m->display));
|
2015-02-25 07:06:49 +00:00
|
|
|
break;
|
2015-02-23 08:15:22 +00:00
|
|
|
case GDISP_ROTATE_90:
|
|
|
|
case GDISP_ROTATE_270:
|
|
|
|
pdr->x = gdispGGetHeight(m->display) - pdr->x / (4096/gdispGGetHeight(m->display));
|
|
|
|
pdr->y = pdr->y / (4096/gdispGGetWidth(m->display));
|
2015-02-25 07:06:49 +00:00
|
|
|
break;
|
2015-02-23 08:15:22 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
pdr->x = gdispGGetWidth(m->display) - pdr->x / (4096/gdispGGetWidth(m->display));
|
|
|
|
pdr->y = pdr->y / (4096/gdispGGetHeight(m->display));
|
|
|
|
#endif
|
2014-11-07 02:05:23 +00:00
|
|
|
#endif
|
|
|
|
|
2018-06-23 03:02:07 +00:00
|
|
|
return gTrue;
|
2014-10-13 07:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const GMouseVMT const GMOUSE_DRIVER_VMT[1] = {{
|
|
|
|
{
|
|
|
|
GDRIVER_TYPE_TOUCH,
|
|
|
|
#if GMOUSE_STMPE811_SELF_CALIBRATE
|
2014-11-07 02:05:23 +00:00
|
|
|
GMOUSE_VFLG_TOUCH | GMOUSE_VFLG_ONLY_DOWN,
|
2014-10-13 07:44:15 +00:00
|
|
|
#else
|
2014-11-07 02:05:23 +00:00
|
|
|
GMOUSE_VFLG_TOUCH | GMOUSE_VFLG_ONLY_DOWN | GMOUSE_VFLG_CALIBRATE | GMOUSE_VFLG_CAL_TEST,
|
2014-10-13 07:44:15 +00:00
|
|
|
#endif
|
|
|
|
sizeof(GMouse) + GMOUSE_STMPE811_BOARD_DATA_SIZE,
|
|
|
|
_gmouseInitDriver,
|
|
|
|
_gmousePostInitDriver,
|
|
|
|
_gmouseDeInitDriver
|
|
|
|
},
|
2014-11-07 02:05:23 +00:00
|
|
|
0, // z_max - 0 indicates full touch
|
|
|
|
255, // z_min
|
|
|
|
150, // z_touchon
|
|
|
|
255, // z_touchoff
|
2014-10-13 07:44:15 +00:00
|
|
|
{ // pen_jitter
|
2014-11-07 02:05:23 +00:00
|
|
|
GMOUSE_STMPE811_PEN_CALIBRATE_ERROR, // calibrate
|
|
|
|
GMOUSE_STMPE811_PEN_CLICK_ERROR, // click
|
2014-10-13 07:44:15 +00:00
|
|
|
GMOUSE_STMPE811_PEN_MOVE_ERROR // move
|
|
|
|
},
|
|
|
|
{ // finger_jitter
|
|
|
|
GMOUSE_STMPE811_FINGER_CALIBRATE_ERROR, // calibrate
|
|
|
|
GMOUSE_STMPE811_FINGER_CLICK_ERROR, // click
|
|
|
|
GMOUSE_STMPE811_FINGER_MOVE_ERROR // move
|
|
|
|
},
|
|
|
|
MouseInit, // init
|
|
|
|
0, // deinit
|
|
|
|
read_xyz, // get
|
|
|
|
0, // calsave
|
|
|
|
0 // calload
|
|
|
|
}};
|
|
|
|
|
|
|
|
#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */
|
|
|
|
|