STMPE811 - not tested yet

ugfx_release_2.6
Joel Bodenmann 2013-03-19 22:59:04 +01:00
parent ffea4aa12e
commit 38e55c3921
3 changed files with 95 additions and 81 deletions

View File

@ -37,38 +37,16 @@
#include "ginput/lld/mouse.h"
#if defined(GINPUT_MOUSE_USE_CUSTOM_BOARD) && GINPUT_MOUSE_USE_CUSTOM_BOARD
#include "ginput_lld_mouse_board.h"
#elif defined(BOARD_EMBEST_DMSTF4BB)
#include "ginput_lld_mouse_board_embest_dmstf4bb.h"
#else
#include "ginput_lld_mouse_board_example.h"
#endif
static uint16_t sampleBuf[7];
static coord_t lastx, lasty;
static coord_t lastx, lasty, lastz;
/**
* @brief 7-point median filtering code for touch samples
*
* @note This is an internally used routine only.
*
* @notapi
*/
static void filter(void) {
uint16_t temp;
int i,j;
for(i = 0; i < 4; i++) {
for(j = i; j < 7; j++) {
if(sampleBuf[i] > sampleBuf[j]) {
/* Swap the values */
temp = sampleBuf[i];
sampleBuf[i] = sampleBuf[j];
sampleBuf[j] = temp;
}
}
}
/* set the active window of the stmpe811. bl is bottom left, tr is top right */
static void setActiveWindow(uint16_t bl_x, uint16_t bl_y, uint16_t tr_x, uint16_t tr_y) {
write_reg(STMPE811_REG_WDW_TR_X, 2, tr_x);
write_reg(STMPE811_REG_WDW_TR_Y, 2, tr_y);
write_reg(STMPE811_REG_WDW_BL_X, 2, bl_x);
write_reg(STMPE811_REG_WDW_BL_Y, 2, bl_y);
}
/**
@ -78,6 +56,25 @@ static void filter(void) {
*/
void ginput_lld_mouse_init(void) {
init_board();
write_reg(STMPE811_REG_SYS_CTRL1, 1, 0x02); // software chip reset
write_reg(STMPE811_REG_SYS_CTRL2, 1, 0x04); // temp. sensor clock on, GPIO clock off, touch clock on, ADC clock on
write_reg(STMPE811_REG_INT_EN, 1, 0x03); //0x03
write_reg(STMPE811_REG_ADC_CTRL1, 1, 0x49); //ADC conversion time = 80 clock ticks, 12-bit ADC, internacl voltage refernce
chThdSleepMicroseconds(1000);
write_reg(STMPE811_REG_ADC_CTRL2, 1, 0x01); //ADC speed 3.25MHz
write_reg(STMPE811_REG_GPIO_AF, 1, 0x00); //GPIO alternate function - OFF
write_reg(STMPE811_REG_TSC_CFG, 1, 0xA3); //avaraging 4, Touch detect delay 1ms, Panel driver settling time 1ms
write_reg(STMPE811_REG_FIFO_TH, 1, 0x01); //FIFO trashold =1
write_reg(STMPE811_REG_FIFO_STA, 1, 0x01); //FIFO reset enable
write_reg(STMPE811_REG_FIFO_STA, 1, 0x00); //FIFO reset disable
write_reg(STMPE811_REG_TSC_FRACT_XYZ, 1, 0x07); //Z axis data format
write_reg(STMPE811_REG_TSC_I_DRIVE, 1, 0x01); //50mA touchscreen line current
write_reg(STMPE811_REG_TSC_CTRL, 1, 0x03); //X&Y only, TSC enable
write_reg(STMPE811_REG_INT_STA, 1, 0xFF); //clear all interrupts
write_reg(STMPE811_REG_INT_CTRL, 1, 0x01); //level interrupt, enable intrrupts
}
/**
@ -95,44 +92,29 @@ void ginput_lld_mouse_init(void) {
* @notapi
*/
void ginput_lld_mouse_get_reading(MouseReading *pt) {
uint16_t i;
uint16_t buf;
// If touch-off return the previous results
if (!getpin_pressed()) {
// if not touched, return the previous results
if(!getpin_pressed()) {
pt->x = lastx;
pt->y = lasty;
pt->z = 0;
pt->buttons = 0;
return;
}
// Read the port to get the touch settings
aquire_bus();
/* Get the X value
* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled while reading.
* Finally switch on PENIRQ once again - perform a dummy read.
* Once we have the readings, find the medium using our filter function
*/
read_value(0xD1);
for(i = 0; i < 7; i++)
sampleBuf[i] = read_value(0xD1);
read_value(0xD0);
filter();
lastx = (coord_t)sampleBuf[3];
/* Get the X value */
buf = read_reg(STMPE811_REG_TSC_DATA_X, 2);
lastx = (coord_t)(buf);
/* Get the Y value using the same process as above */
read_value(0x91);
for(i = 0; i < 7; i++)
sampleBuf[i] = read_value(0x91);
read_value(0x90);
filter();
lasty = (coord_t)sampleBuf[3];
/* Get the Y value */
buf = read_reg(STMPE811_REG_TSC_DATA_Y, 2);
lasty = (coord_t)(buf);
/* Get the Z value */
buf = read_reg(STMPE811_REG_TSC_DATA_Z, 1);
lastz = (buf & 0x00FF);
// Release the bus
release_bus();
// Return the results
pt->x = lastx;
pt->y = lasty;
@ -142,3 +124,4 @@ void ginput_lld_mouse_get_reading(MouseReading *pt) {
#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */
/** @} */

View File

@ -30,7 +30,7 @@
#ifndef _GINPUT_LLD_MOUSE_BOARD_H
#define _GINPUT_LLD_MOUSE_BOARD_H
static const I2CConfig i2ccfg2 = {
static const I2CConfig i2ccfg = {
OPMODE_I2C,
400000,
FAST_DUTY_CYCLE_2,
@ -41,12 +41,12 @@ static const I2CConfig i2ccfg2 = {
*
* @notapi
*/
static inline void init_board(void) {
palSetPadMode(GPIOC, 13, PAL_MODE_INPUT); /* TP IRQ */
palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(4)); /* I2C2 SCL */
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(4)); /* I2C2 SDA */
static void init_board(void) {
palSetPadMode(GPIOC, 13, PAL_MODE_INPUT | PAL_STM32_PUDR_FLOATING); /* TP IRQ */
palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN); /* SCL */
palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN); /* SDA */
i2cStart(&I2CD2, &i2ccfg2);
i2cStart(&I2CD1, &i2ccfg);
}
/**
@ -60,34 +60,61 @@ static inline bool_t getpin_pressed(void) {
}
/**
* @brief Aquire the bus ready for readings
* @brief Write a value into a certain register
*
* @param[in] reg The register address
* @param[in] n The amount of bytes (one or two)
* @param[in] val The value
*
* @notapi
*/
static inline void aquire_bus(void) {
i2cAcquireBus(&I2CD2);
}
static void write_reg(uint8_t reg, uint8_t n, uint16_t val) {
uint8_t txbuf[1];
/**
* @brief Release the bus after readings
*
* @notapi
*/
static inline void release_bus(void) {
i2cReleaseBus(&I2CD2);
}
i2cAcquireBus(&I2CD1);
txbuf[0] = reg;
i2cMasterTransmitTimeout(&I2CD1, STMPE811_ADDR, txbuf, 1, NULL, 0, MS2ST(STMPE811_TIMEOUT));
if(n == 1) {
txbuf[0] = val;
i2cMasterTransmitTimeout(&I2CD1, STMPE811_ADDR, txbuf, 1, NULL, 0, MS2ST(STMPE811_TIMEOUT));
} else if(n == 3) {
txbuf[0] = ((val & 0xFF00) >> 8);
txbuf[1] = (val & 0x00FF);
i2cMasterTransmitTimeout(&I2CD1, STMPE811_ADDR, txbuf, 2, NULL, 0, MS2ST(STMPE811_TIMEOUT));
}
i2cReleaseBus(&I2CD1);
}
/**
* @brief Read a value from touch controller
* @return The value read from the controller
* @brief Read the value of a certain register
*
* params[in] port The controller port to read.
* @param[in] reg The register address
* @param[in] n The amount of bytes (one or two)
*
* @notapi
*/
static inline uint16_t read_value(uint16_t port) {
#error "STMPE811: Implement this driver first!"
static uint16_t read_reg(uint8_t reg, uint8_t n) {
uint8_t txbuf[1], rxbuf[2];
uint16_t ret;
i2cAcquireBus(&I2CD1);
txbuf[0] = reg;
i2cMasterTransmitTimeout(&I2CD1, 0x82 >> 1, txbuf, 1, rxbuf, 2, MS2ST(STMPE811_TIMEOUT));
i2cReleaseBus(&I2CD1);
if(n == 1)
ret = rxbuf[0];
else if (n == 2)
ret = ((rxbuf[0] << 8) | (rxbuf[1] & 0xFF));
return ret;
}
#endif /* _GINPUT_LLD_MOUSE_BOARD_H */
/** @} */

View File

@ -29,7 +29,11 @@
#ifndef _STMPE811_H
#define _STMPE811_H
/* STMPE811 registers */
// Slave address
#define STMPE811_ADDR (0x82 >> 1)
// Maximum timeout
#define STMPE811_TIMEOUT 0x3000
// Identification registers
#define STMPE811_REG_CHP_ID 0x00 // 16-bit