updated byd touch controller handling. needs debugging and update. Beagle_BSP_dev
authorLukasz Forynski <lukasz.forynski@gmail.com>
Tue, 02 Nov 2010 00:33:23 +0000
branchBeagle_BSP_dev
changeset 95 450a8cf0c020
parent 85 d93b485c1325
child 111 35fb7dda225a
updated byd touch controller handling. needs debugging and update.
omap3530/beagle_drivers/byd_touch/common/controller.cpp
omap3530/beagle_drivers/byd_touch/common/controller.h
omap3530/beagle_drivers/byd_touch/xyin/xyin.cpp
--- a/omap3530/beagle_drivers/byd_touch/common/controller.cpp	Thu Oct 07 00:37:22 2010 +0100
+++ b/omap3530/beagle_drivers/byd_touch/common/controller.cpp	Tue Nov 02 00:33:23 2010 +0000
@@ -43,52 +43,107 @@
 	SET_SLAVE_ADDR(iSpiBusId, KSpiSlaveAddr);
 	}
 
-TInt TTouchControllerInterface::Read(TUint8 aRegAddress, TUint8& aValue)
+
+// Writes number of bytes stored in the write buffer - to the controller.
+// aNumOfBytes specifies number of bytes including first byte of address.
+// The write buffer should contain the data from index=1 and it's length should be set
+// to number of registers to update + 1 (one byte for the address / command)
+TInt TTouchControllerInterface::WriteMultiple(TUint8 aStartAddress, TInt aNumBytes)
 	{
 	LOG_FUNCTION_CALL;
-	iSpiWriteBuffer[0] = KReadCommand;
-	iSpiWriteBuffer[1] = aRegAddress;
-	iSpiWriteBuffer[2] = 0; // TODO - might not be needed..
+	if(aNumBytes != iSpiWriteBuffer.Length())
+		{
 #ifdef VERBOSE_DEBUG
-	for (int i = 0; i <KSpiPacketLength; i++)
-		Kern::Printf("R0[i]: %d", iSpiReadBuffer[1]);
+		Kern::Printf("Transfer buffers are not properly set, line: %d", __LINE__);
 #endif
-	TInt r = IicBus::QueueTransaction(iSpiBusId, &iSpiTransaction);
-	if(r == KErrNone)
-		{
-		aValue = iSpiReadBuffer[2];
+		return KErrArgument;
 		}
-#ifdef VERBOSE_DEBUG
-	for (int i = 0; i <KSpiPacketLength; i++)
-		Kern::Printf("R1[i]: %d", iSpiReadBuffer[1]);
-#endif
-	return r;
+	iSpiReadBuffer.SetLength(aNumBytes);
+	iSpiWriteBuffer[0] = KWriteCommand | (aStartAddress << 1);
+	return IicBus::QueueTransaction(iSpiBusId, &iSpiTransaction);
+	}
+
+
+// Reads number of bytes and stores them into the read buffer
+// aNumOfBytes specifies number of bytes (excluding first byte of address)
+// The read buffer will contain the received data starting from index=1 (TODO: consider MidTPtr)
+TInt TTouchControllerInterface::ReadMultiple(TUint8 aStartAddress, TInt aNumBytes)
+	{
+	LOG_FUNCTION_CALL;
+	TInt r = KErrNone;
+
+	// if trying to read from address different that currently stored - first update the read register..
+	if(iCurrentReadStart != aStartAddress)
+		{
+		r = Write(KMasterReadStartAddr, aStartAddress);
+		if(r != KErrNone)
+			{
+			return r;
+			}
+		iCurrentReadStart = aStartAddress;
+		}
+
+	// make sure buffers are set to the requested size
+	iSpiReadBuffer.SetLength(aNumBytes + 1);
+	iSpiWriteBuffer.SetLength(aNumBytes + 1);
+	iSpiWriteBuffer[0] = KReadCommand;
+
+	return IicBus::QueueTransaction(iSpiBusId, &iSpiTransaction);
 	}
 
 TInt TTouchControllerInterface::Write(TUint8 aRegAddress, TUint8 aValue)
 	{
 	LOG_FUNCTION_CALL;
-	iSpiWriteBuffer[0] = KWriteCommand;
-	iSpiWriteBuffer[1] = aRegAddress;
-	iSpiWriteBuffer[2] = aValue;
+	iSpiWriteBuffer.SetLength(2);
+	iSpiReadBuffer.SetLength(2);
+	iSpiWriteBuffer[0] = KWriteCommand | (aRegAddress << 1);
+	iSpiWriteBuffer[1] = aValue;
+
 
 #ifdef VERBOSE_DEBUG
-	for (int i = 0; i < 3; i++)
+	for (int i = 0; i < iSpiWriteBuffer.Length(); i++)
 		Kern::Printf("WR0[i]: %d", iSpiWriteBuffer[i]);
 
-	for (int i = 0; i <KSpiPacketLength; i++)
-		Kern::Printf("W0[i]: %d", iSpiReadBuffer[1]);
+	for (int i = 0; i < iSpiReadBuffer.Length(); i++)
+		Kern::Printf("W0[i]: %d", iSpiReadBuffer[i]);
 #endif
 	TInt r = IicBus::QueueTransaction(iSpiBusId, &iSpiTransaction);
 
 #ifdef VERBOSE_DEBUG
-	for (int i = 0; i <KSpiPacketLength; i++)
-		Kern::Printf("W1[i]: %d", iSpiReadBuffer[1]);
+	for (int i = 0; i < iSpiReadBuffer.Length(); i++)
+		Kern::Printf("W1[i]: %d", iSpiReadBuffer[i]);
 #endif
 	return r;
 	}
 
-// Touch controller
+
+TInt TTouchControllerInterface::Read(TUint8 aRegAddress, TUint8& aValue)
+	{
+	LOG_FUNCTION_CALL;
+	TInt r = ReadMultiple(aRegAddress, 2);
+
+	if(r == KErrNone)
+		{
+		aValue = iSpiReadBuffer[1];
+		}
+	return r;
+	}
+
+
+//(TODO: consider MidTPtr)
+inline TDes& TTouchControllerInterface::ReadBuff()
+	{
+	return iSpiReadBuffer;
+	}
+
+//(TODO: consider MidTPtr)
+inline TDes& TTouchControllerInterface::WriteBuff()
+	{
+	return iSpiWriteBuffer;
+	}
+
+
+// Touch controller implementation
 TouchController::TouchController() :
 	iCallback(NULL)
 	{
@@ -108,31 +163,73 @@
 		r = GPIO::SetPinDirection(KResetPin, GPIO::EOutput);
 		if(r == KErrNone)
 			{
-//			GPIO::SetOutputState(KResetPin, GPIO::ELow);
+#ifdef RESET_LOW
+			GPIO::SetOutputState(KResetPin, GPIO::ELow);
+#else
 			GPIO::SetOutputState(KResetPin, GPIO::EHigh);
-//			GPIO::SetOutputState(KResetPin, GPIO::ELow);
+#endif
+			Kern::NanoWait(25000); // should be > 10us
+#ifdef RESET_LOW
 
-			Kern::NanoWait(25000); // should be > 10us
-//			GPIO::SetOutputState(KResetPin, GPIO::EHigh);
+			GPIO::SetOutputState(KResetPin, GPIO::EHigh);
+#else
 			GPIO::SetOutputState(KResetPin, GPIO::ELow);
-
+#endif
 			}
 		}
-//	Kern::NanoWait(1000000);
-//	SoftReset();
-//	Kern::NanoWait(1000000);
-
-	iInterface.Write(KMasterReadStartAddr, X1_H);
 	return r;
 	}
 TInt TouchController::SoftReset()
 	{
+	return iInterface.Write(KControl_0, KControl_0_SWRST);
+	}
+
+TInt TouchController::Configure(TTouchMode aMode)
+	{
 	LOG_FUNCTION_CALL;
-	for(TInt i = 0; i<4; i++)
+	TDes& writeBuffer = iInterface.WriteBuff();
+	writeBuffer.SetLength(14);
+
+	// value for KControl_0 register in writeBuffer[1]
+	switch(aMode)
 		{
-		iCtrlRegsCache[i] = 0;
+		case EModeSingle:
+			writeBuffer[1] = KControl_0_MODE_SINGLE;
+			break;
+		case EModeMulti:
+			writeBuffer[1] = KControl_0_MODE_MULTI;
+			break;
+		case EModeGesture:
+			writeBuffer[1] = KControl_0_MODE_GESTURE;
+			break;
 		}
-	return iInterface.Write(KControl_0, KControl_0_SWRST);
+
+//	const TUint8 KWindowXStart_Msb = 0x4; // R/W
+//	const TUint8 KWindowXStart_Lsb = 0x5; // R/W
+//	const TUint8 KWindowXStop_Msb  = 0x6; // R/W
+//	const TUint8 KWindowXStop_Lsb  = 0x7; // R/W
+//	const TUint8 KWindowYStart_Msb = 0x8; // R/W
+//	const TUint8 KWindowYStart_Lsb = 0x9; // R/W
+//	const TUint8 KWindowYStop_Msb  = 0xa; // R/W
+//	const TUint8 KWindowYStop_Lsb  = 0xb; // R/W
+//	const TUint8 KMasterReadStartAddr = 0xc; // R/W
+
+	writeBuffer[2] = (TInt8)(KControl_1_PAT_100 | KControl_1_PVT_200); // KControl_1 // TODO: update these values..
+	writeBuffer[3] = (TInt8)(KNumColumns << KControl_2_C_SHIFT);       // KControl_2 // TODO: update these values..
+	writeBuffer[4] = (TInt8)(KNumRows    << KControl_3_R_SHIFT);       // KControl_3 // TODO: update these values..
+
+	writeBuffer[5] = 0; // KWindowXStart_Msb // TODO: update these values..
+	writeBuffer[6] = 0; // KWindowXStart_Lsb // TODO: update these values..
+	writeBuffer[7] = 0; // KWindowXStop_Msb // TODO: update these values..
+	writeBuffer[8] = 0; // KWindowXStop_Lsb // TODO: update these values..
+
+	writeBuffer[9] = 0; // KWindowYStart_Msb // TODO: update these values..
+	writeBuffer[10] = 0; // KWindowYStart_Lsb // TODO: update these values..
+	writeBuffer[11] = 0; // KWindowYStop_Msb // TODO: update these values..
+	writeBuffer[12] = 0; // KWindowYStop_Lsb // TODO: update these values..
+	writeBuffer[13] = 0; // KMasterReadStartAddr // TODO: update these values..
+
+	return iInterface.WriteMultiple(KControl_0, 14);
 	}
 
 TInt TouchController::SetTouchMode(TTouchMode aMode)
@@ -141,13 +238,13 @@
 	iCtrlRegsCache[0] &= ~KControl_0_MODE_MASK; // clear all mode bits
 	switch(aMode)
 		{
-		case ESingle:
+		case EModeSingle:
 			iCtrlRegsCache[0] |= KControl_0_MODE_SINGLE;
 			break;
-		case EMulti:
+		case EModeMulti:
 			iCtrlRegsCache[0] |= KControl_0_MODE_MULTI;
 			break;
-		case EGesture:
+		case EModeGesture:
 			iCtrlRegsCache[0] |= KControl_0_MODE_GESTURE;
 			break;
 		}
@@ -203,6 +300,7 @@
 	return iInterface.Write(KControl_2, iCtrlRegsCache[2]);
 	}
 
+
 TInt TouchController::SetNumberOfRows(TUint aNumberOfRows)
 	{
 	LOG_FUNCTION_CALL;
@@ -211,55 +309,25 @@
 	return iInterface.Write(KControl_3, iCtrlRegsCache[3]);
 	}
 
+
 TInt TouchController::EnableWindowMode(TPoint aStart, TPoint aStop)
 	{
 	LOG_FUNCTION_CALL;
-
-	TBool error_occured = EFalse;
-	// setup window points
-	TInt r = iInterface.Write(KWindowXStart_Msb, (TUint8)(aStart.iX >> 8));
-	if(r != KErrNone)
-		error_occured = ETrue;
-
-	r = iInterface.Write(KWindowXStart_Lsb, (TUint8)(aStart.iX));
-	if(r != KErrNone)
-		error_occured = ETrue;
-
-	r = iInterface.Write(KWindowYStart_Msb, (TUint8)(aStart.iY >> 8));
-	if(r != KErrNone)
-		error_occured = ETrue;
-
-	r = iInterface.Write(KWindowYStart_Lsb, (TUint8)(aStart.iY));
-	if(r != KErrNone)
-		error_occured = ETrue;
-
-	r = iInterface.Write(KWindowXStop_Msb, (TUint8)(aStop.iX >> 8));
-	if(r != KErrNone)
-		error_occured = ETrue;
+	TDes& writeBuffer = iInterface.WriteBuff();
+	writeBuffer.SetLength(9);
 
-	r = iInterface.Write(KWindowXStop_Lsb, (TUint8)(aStop.iX));
-	if(r != KErrNone)
-		error_occured = ETrue;
-
-	r = iInterface.Write(KWindowYStop_Msb, (TUint8)(aStop.iY >> 8));
-	if(r != KErrNone)
-		error_occured = ETrue;
-
-	r = iInterface.Write(KWindowYStop_Lsb, (TUint8)(aStop.iY));
-	if(r != KErrNone)
-		error_occured = ETrue;
+	// setup window points
+	writeBuffer[0] = KWriteCommand | (KWindowXStart_Msb << 1); // address of first register to write..
+	writeBuffer[1] = (TUint8)(aStart.iX >> 8); // KWindowXStart_Msb
+	writeBuffer[2] = (TUint8)(aStart.iX);      // KWindowXStart_Lsb
+	writeBuffer[3] = (TUint8)(aStop.iX >> 8);  // KWindowXStop_Msb
+	writeBuffer[4] = (TUint8)(aStop.iX);       // KWindowXStop_Lsb
+	writeBuffer[5] = (TUint8)(aStart.iY >> 8); // KWindowYStart_Msb
+	writeBuffer[6] = (TUint8)(aStart.iY);      // KWindowYStart_Lsb
+	writeBuffer[7] = (TUint8)(aStop.iY >> 8);  // KWindowYStop_Msb
+	writeBuffer[8] = (TUint8)(aStop.iY);       // KWindowYStop_Lsb
 
-	// enable mode
-	if(!error_occured)
-		{
-		iCtrlRegsCache[1] |= ~KControl_1_WS; // set enable bit..
-		r = iInterface.Write(KControl_1, iCtrlRegsCache[1]);
-		}
-	else
-		{
-		r = KErrGeneral;
-		}
-	return r;
+	return iInterface.WriteMultiple(KWindowXStart_Msb, 9);
 	}
 
 TInt TouchController::DisableWindowMode()
@@ -272,7 +340,12 @@
 TInt TouchController::NumOfTouches()
 	{
 	TUint8 val = 0;
-	return iInterface.Read(KTouchNumberAndType, val);
+	TInt r = iInterface.Read(KTouchNumberAndType, val);
+	if (r == KErrNone)
+		{
+		r = val;
+		}
+	return r;
 	}
 
 TInt TouchController::GetMeasurements(TPoint* aPoints, TInt& aNumPoints)
@@ -280,55 +353,36 @@
 	LOG_FUNCTION_CALL;
 	TInt r = KErrArgument;
 	TInt num_points = 0;
+
 	if(aPoints)
 		{
-		// check how many points is there to read..
-		TUint8 val = 0;
-		r = iInterface.Read(KTouchNumberAndType, val);
+		r = iInterface.ReadMultiple(KTouchNumberAndType, 14);
 
-		//Kern::Printf("KTouchNumberAndType %x", val);
-		// if in multi mode - read all received, but only up to one otherwise..
-		num_points = val & (val & KTouchNumberAndType_MULTI ? KTouchNumberAndType_TouchNMask : 1);
-
-		// setup the transaction:
-		for (TInt i = 0; i < num_points; i++) // if anything was touched at all..
+		if(r == KErrNone)
 			{
-			// get X coordinate
-			r = iInterface.Read(X1_H + (i << 2), val);
-			if (r != KErrNone)
-				break;
+			TDes& readBuffer = iInterface.ReadBuff();
 
-			aPoints[i].iX = TInt(val << 8);
+			// check how many points is there to read..
+			TUint8 val = readBuffer[1]; // TODO: update this if MidTPtr could be used..
+	//		Kern::Printf("KTouchNumberAndType %x", val);
 
-			r = iInterface.Read(X1_L + (i << 2) , val);
-			if (r != KErrNone)
-				break;
-
-			aPoints[i].iX |= TInt(val);
+			// if in multi mode - read all received, but only up to one otherwise..
+			num_points = val & (val & KTouchNumberAndType_MULTI ? KTouchNumberAndType_TouchNMask : 1);
 
-			// get Y coordinate
-			r = iInterface.Read(Y1_H + (i << 2), val);
-			if (r != KErrNone)
-				break;
-
-			aPoints[i].iY = TInt(val << 8);
-
-			r = iInterface.Read(Y1_L + (i << 2) , val);
-			if (r != KErrNone)
-				break;
+			// read the coordinates..
+			for (TInt i = 0; i < num_points; i++) // if anything was touched at all..
+				{
+				// get X coordinate
+				aPoints[i].iX |= TInt(readBuffer[(i << 2) + 1] << 8); // X_H
+				aPoints[i].iX =  TInt(readBuffer[(i << 2) + 2]);      // X_L
 
-			aPoints[i].iY |= TInt(val);
-			}
-		}
+				// get Y coordinate
+				aPoints[i].iY |= TInt(readBuffer[(i << 2) + 3] << 8); // Yx_H
+				aPoints[i].iY =  TInt(readBuffer[(i << 2) + 4]);      // Y_L
+				}
 
-	// update number of points
-	if (r != KErrNone)
-		{
-		aNumPoints = 0;
-		}
-	else
-		{
-		aNumPoints = num_points;
+			aNumPoints = num_points;
+			}
 		}
 	return r;
 	}
--- a/omap3530/beagle_drivers/byd_touch/common/controller.h	Thu Oct 07 00:37:22 2010 +0100
+++ b/omap3530/beagle_drivers/byd_touch/common/controller.h	Tue Nov 02 00:33:23 2010 +0000
@@ -21,12 +21,18 @@
 
 // controller specific constants..
 const TUint KMaxNumPoints = 3;
+
+
+const TInt KNumColumns = 0x4; // TODO update these values..
+const TInt KNumRows = 0x4;
+
+
 typedef void (*TVoidCallback) (TAny*);
 
 
 // spi specific constants..
-const TUint KSpiPacketLength = 4;
-const TUint KStartByte = 0x56 << 1;
+const TUint KMaxPacketLength = 16;
+const TUint KStartByte = 0x80;
 const TUint KWrite     = 0;
 const TUint KRead      = 1;
 const TUint KReadCommand  = KStartByte | KRead;
@@ -61,15 +67,19 @@
 public:
 	inline TTouchControllerInterface();
 	inline TInt Read(TUint8 aRegAddress, TUint8& aValue);
+	inline TInt ReadMultiple(TUint8 aStartAddress, TInt aNumBytes);
 	inline TInt Write(TUint8 aRegAddress, TUint8 aValue);
-	inline TInt Write(TUint8 aRegAddress, TUint8* aValues, TInt aNumOfItems);
+	inline TInt WriteMultiple(TUint8 aStartAddress, TInt aNumBytes);
+	inline TDes& ReadBuff();
+	inline TDes& WriteBuff();
 
 
 private:
+	TInt iCurrentReadStart;
 	// SPI duplex transaction with two transfers for each direction
 	TPckgBuf<TConfigSpiV01> iSpiTransactionHeader;
-	TBuf8<KSpiPacketLength> iSpiWriteBuffer;
-	TBuf8<KSpiPacketLength> iSpiReadBuffer;
+	TBuf8<KMaxPacketLength> iSpiWriteBuffer;
+	TBuf8<KMaxPacketLength> iSpiReadBuffer;
 	TIicBusTransfer         iSpiTxTransfer;
 	TIicBusTransfer         iSpiRxTransfer;
 	TIicBusTransaction      iSpiTransaction;
@@ -81,9 +91,9 @@
 public:
 	enum TTouchMode
 		{
-		ESingle = 0,
-		EMulti,
-		EGesture
+		EModeSingle = 0,
+		EModeMulti,
+		EModeGesture
 		};
 
 	enum TResolution
@@ -109,6 +119,7 @@
 
 	TInt HardReset();
 	TInt SoftReset();
+	TInt Configure(TTouchMode aMode);
 	TInt SetTouchMode(TTouchMode aMode);
 	TInt SetResolution(TResolution aResolution);
 	TInt SetLongerSamplingRate(TUint aRate);
--- a/omap3530/beagle_drivers/byd_touch/xyin/xyin.cpp	Thu Oct 07 00:37:22 2010 +0100
+++ b/omap3530/beagle_drivers/byd_touch/xyin/xyin.cpp	Tue Nov 02 00:33:23 2010 +0000
@@ -284,7 +284,7 @@
 		r = GPIO::SetInterruptTrigger(KDataReadyPin, GPIO::EEdgeRising);
 		if(r == KErrNone)
 			{
-			GPIO::SetDebounceTime(KDataReadyPin, 500);
+			GPIO::SetDebounceTime(KDataReadyPin, 100);
 			SCM::SetPadConfig(CONTROL_PADCONF_MMC2_DAT0, SCM::EMsw, SCM::EMode4 | SCM::EInputEnable); // 133 (mmc2dat1)
 			r = GPIO::SetPinMode(KDataReadyPin, GPIO::EEnabled);
 			}
@@ -297,17 +297,7 @@
 
 		// some of the pre-configuration of the touch controller could be done here..
 		iController.HardReset();
-		iController.SetTouchMode(TouchController::ESingle);
-
-		TPoint winStart, winStop;
-		winStart.iX = 0;
-		winStart.iY = 0;
-		winStop.iX = 0xfff;
-		winStop.iY = 0xfff;
-		iController.EnableWindowMode(winStart, winStop);
-
-		iController.SetNumberOfRows(8);
-		iController.SetNumberOfColumns(8);
+		iController.Configure(TouchController::EModeSingle);
 
 		DigitiserPowerUp();
 		}
@@ -352,10 +342,20 @@
 	// Read from appropriate hardware register to determine whether digitiser panel is being touched
 	// Set penDown to ETrue if touched or EFalse if not touched.
 	//
+	TPoint points[4];
+	TInt num_points = 0;
+	TInt r = iController.GetMeasurements(points, num_points);
+	Kern::Printf("Num touches: %d", num_points);
+	Kern::Printf("Measurments: (r: %d)", r);
+	for(TInt i = 0; i < num_points; i++)
+		{
+		Kern::Printf("%d: iX: %d, iY: %d",i, points[i].iX, points[i].iY);
+		}
 
-//	DEBUG_PRINT(Kern::Printf("TS: S%d PD%d Sp%d", (TInt)iState, penDown?1:0, iSamplesCount));
-	TInt penDown = iController.NumOfTouches();
-	Kern::Printf("Num touches: %d", penDown);
+	TInt penDown = num_points;
+
+	//	DEBUG_PRINT(Kern::Printf("TS: S%d PD%d Sp%d", (TInt)iState, penDown?1:0, iSamplesCount));
+
 	if (iState==E_HW_PowerUp)
 		{
 		// waiting for pen to go up after switch on due to pen down or through the HAL
@@ -422,15 +422,6 @@
 	//
 	TPoint aResults;
 
-	TPoint points[4];
-	TInt num_points = 0;
-	TInt r = iController.GetMeasurements(points, num_points);
-	Kern::Printf("Measurments: (r: %d)", r);
-	for(TInt i = 0; i < 4/* num_points*/; i++)
-		{
-		Kern::Printf("%d: iX: %d, iY: %d",i, points[i].iX, points[i].iY);
-		}
-
 	// X axis
 	iX[iSamplesCount] = aResults.iX;
 	// Y axis