omap3530/beagle_drivers/byd_touch/common/controller.cpp
branchBeagle_BSP_dev
changeset 95 450a8cf0c020
parent 85 d93b485c1325
--- 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;
 	}