From 38e55c392163c564121d344db6325e6d4780a406 Mon Sep 17 00:00:00 2001
From: Joel Bodenmann <joel@unormal.org>
Date: Tue, 19 Mar 2013 22:59:04 +0100
Subject: STMPE811 - not tested yet

---
 drivers/ginput/touch/STMPE811/ginput_lld_mouse.c   | 103 +++++++++------------
 .../ginput_lld_mouse_board_embest_dmstf4bb.h       |  73 ++++++++++-----
 drivers/ginput/touch/STMPE811/stmpe811.h           |   6 +-
 3 files changed, 98 insertions(+), 84 deletions(-)

(limited to 'drivers')

diff --git a/drivers/ginput/touch/STMPE811/ginput_lld_mouse.c b/drivers/ginput/touch/STMPE811/ginput_lld_mouse.c
index bacb1f1f..b2f7ba76 100644
--- a/drivers/ginput/touch/STMPE811/ginput_lld_mouse.c
+++ b/drivers/ginput/touch/STMPE811/ginput_lld_mouse.c
@@ -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 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];
-
-	// Release the bus
-	release_bus();
-	
+
+	/* Get the X value */
+	buf = read_reg(STMPE811_REG_TSC_DATA_X, 2);
+	lastx = (coord_t)(buf);
+
+	/* 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);
+
 	// 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 */
 /** @} */
+
diff --git a/drivers/ginput/touch/STMPE811/ginput_lld_mouse_board_embest_dmstf4bb.h b/drivers/ginput/touch/STMPE811/ginput_lld_mouse_board_embest_dmstf4bb.h
index fa43f4ac..490b8dca 100644
--- a/drivers/ginput/touch/STMPE811/ginput_lld_mouse_board_embest_dmstf4bb.h
+++ b/drivers/ginput/touch/STMPE811/ginput_lld_mouse_board_embest_dmstf4bb.h
@@ -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
  *
- * @notapi
- */
-static inline void aquire_bus(void) {
-	i2cAcquireBus(&I2CD2);
-}
-
-/**
- * @brief   Release the bus after readings
+ * @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 release_bus(void) {
-	i2cReleaseBus(&I2CD2);
-}
+static void write_reg(uint8_t reg, uint8_t n, uint16_t val) {
+	uint8_t txbuf[1];
+
+	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 */
 /** @} */
+
diff --git a/drivers/ginput/touch/STMPE811/stmpe811.h b/drivers/ginput/touch/STMPE811/stmpe811.h
index 8396a844..9da8e1df 100644
--- a/drivers/ginput/touch/STMPE811/stmpe811.h
+++ b/drivers/ginput/touch/STMPE811/stmpe811.h
@@ -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
-- 
cgit v1.2.3