⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 synaot.c

📁 Synaptics电容式触摸芯片OT的IIC完整参考代码
💻 C
字号:


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// This file implements the OTLib functions that the customer application code calls
//
//	  - The functions are hardware independent, however depends on the Hardware 
//		Abstract Layer function calls
//    - The code/ library should be able to run on any host processor
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
// ******************************************
// ******************************************
// ==> WARNING: ** DO NOT EDIT This file <== 
// ******************************************
// ******************************************
 
#include "SynaOT.h"


// =========================
// * defines and constants *
// =========================

#define OT_READ		0x01	// I2C READ bit

// delays and timing routines

// I2C 4 usec delay
#define OT_QDEL	OT_HAL_delay_10th_usec(40);		

// I2C 4.7usec delay
#define OT_HDEL	OT_HAL_delay_10th_usec(47); 

// minimum time SCL must remain low while clocking a data bit
#define	OT_I2C_TLOW_DELAY         	OT_HAL_delay_10th_usec(47);
// minimum time SCL must remain high while clocking a data bit
#define	OT_I2C_THIGH_DELAY        	OT_HAL_delay_10th_usec(40);
// minimum time SCL must remain low after a stop condition
#define	OT_I2C_TBUF_DELAY         	OT_HAL_delay_10th_usec(47);
// Repeated Start condition setup time
#define	OT_I2C_TSU_STA_DELAY      	OT_HAL_delay_10th_usec(47);
// Start condition hold time
#define	OT_I2C_THD_STA_DELAY      	OT_HAL_delay_10th_usec(40);
// Stop condition setup time
#define	OT_I2C_TSU_STO_DELAY      	OT_HAL_delay_10th_usec(40);


//*****************************************************
//* I2C Bit Banging Host Processor Independent functions *
//*****************************************************

void  OT_SDA_HIGH(void);
void  OT_SCL_HIGH(void);

void  OT_SDA_LOW(void);
void  OT_SCL_LOW(void);

void	OT_I2C_SCL_TOGGLE(void);
void	OT_I2C_START(void);
void	OT_I2C_STOP(void);


//**********************************
//* I2C Byte Read/ Write functions *
//**********************************

// Put a Byte on the I2C bus line
// return OT_SUCCESS (ACK), or OT_FAILURE (NACK)
OT_U8 OT_I2C_PutByte(OT_U8 bByte);

// Get a Byte on the I2C bus line
OT_U8 OT_I2C_GetByte(OT_U8 bLast);


//*****************************
//* I2C Bit Banging functions *
//*****************************

void  OT_I2C_SCL_TOGGLE(void)
{
	OT_HDEL; 
	OT_SCL_HIGH(); 
	OT_HDEL; 
	OT_SCL_LOW(); 
}

void  OT_I2C_START(void)
{
	OT_SDA_LOW();
	OT_QDEL; 
	OT_SCL_LOW(); 
}

void  OT_I2C_STOP(void)
{
	OT_SDA_LOW();
	OT_HDEL; 
	OT_SCL_HIGH(); 
	OT_QDEL; 
	OT_SDA_HIGH(); 
	OT_HDEL;
}

// Set I2C Data Line as Low
void  OT_SDA_LOW(void)
{
	OT_HAL_SDA_OUT_MODE();
	OT_HAL_I2C_SDA_LO();
}

// Set I2C Clock Line as Low
void  OT_SCL_LOW(void)
{
	OT_HAL_SCL_OUT_MODE();
	OT_HAL_I2C_SCL_LO();
}

// Set I2C Data Line as High
void  OT_SDA_HIGH(void)
{
	OT_HAL_SDA_IN_MODE();
}


// Set I2C Clock Line as High, with clock stretching
// The clock stretching time out is approximately 0 usec

void  OT_SCL_HIGH(void)
{
	OT_S32 bDelay = OT_HAL_CLK_STRETCH_LIMIT;		
		
	OT_HAL_SCL_IN_MODE();
	
	while( (OT_HAL_BitRead_I2C_SCL() == 0) && (bDelay >= 0) )
	{
		bDelay--;
	}
	
	// decide on if error needs to be returned!
}


//**********************************
//* I2C Byte Read/ Write functions *
//**********************************

// Put a Byte on the I2C bus line
// return OT_SUCCESS (ACK), or OT_FAILURE (NACK)

OT_U8 OT_I2C_PutByte(OT_U8 Byte)
{
	OT_S8 Counter;		
	
	for (Counter=7; Counter>= 0; Counter--)
	{
		if ( Byte & (1<<Counter) )
		{
			OT_SDA_HIGH();
		}
		else
		{			
			OT_SDA_LOW();			// address bit
		}
		
		OT_I2C_SCL_TOGGLE();		// clock HI, delay, then LO
	}

	OT_SDA_HIGH();					// leave SDA HI
	
	OT_HDEL;
	OT_SCL_HIGH();					// clock back up
	OT_HDEL;

	Byte=OT_HAL_BitRead_I2C_SDA();	// get the ACK bit

	OT_SCL_LOW();
	OT_SDA_HIGH();					// leave with SDA HI

	if(Byte == 0)
			return OT_SUCCESS;		// return OT_SUCCESS (ACK), or OT_FAILURE (NACK)
	
	return OT_FAILURE;
}

// Get a Byte on the I2C bus line
OT_U8 OT_I2C_GetByte(OT_U8 Last)
{
	OT_S8 Counter;
	OT_U8 Val,Byte = 0;
		
	OT_SDA_HIGH();					// make sure pullups are ativated

	for(Counter=7; Counter>=0; Counter--)
	{
		OT_HDEL;
		OT_SCL_HIGH();				// clock HI
		Val=OT_HAL_BitRead_I2C_SDA();	  	 
		
		Byte <<= 1;
		if(Val) Byte |= 1;
		OT_HDEL;		
		OT_SCL_LOW();				// clock LO
	}
  
	if (Last)
		OT_SDA_HIGH();				// set NAK
	else
		OT_SDA_LOW();				// set ACK

	OT_I2C_SCL_TOGGLE();			// clock pulse
	OT_SDA_HIGH();					// leave with SDL HI


	return Byte;					// return received byte

			// Return error code (needs investigation)
}


// ========================
// * I2C public functions *
// ========================

// Initialize I2C communication
void OT_Init_BitBang_I2C(void)
{
	
	OT_SCL_HIGH();
	OT_SDA_HIGH();
	
}

// ========================================================================
// This is a function to Send a Byte sequence on the I2C bus
//
// Functions Name:
//  * OT_WriteI2C()
//
// Inputs: (
//  * I2CAddr7Bit, (7-bit I2C Device Address)
//  * Buffer (Buffer of bytes to be written)
// 		- High Byte of subaddress andthe Low Byte of subaddress
//		- are the first two bytes of pBuffer[]
//  * BytesToSend (No. of Bytes to write)
//
// Outputs:
//	Returns OT_SUCCESS or OT_FAILURE
//
// Assumptions:	
//	 The High Byte of subaddress and the Low Byte of subaddress
// 	 are the first two bytes of Buffer[]
// ========================================================================

OT_U8 OT_WriteI2C(OT_U8 I2CAddr7Bit, OT_U8 Buffer[], OT_U8 BytesToSend)		
{
	OT_U8 Count=0;
	
	OT_I2C_START();      			// do start transition

	if(OT_I2C_PutByte(I2CAddr7Bit<<1)== OT_FAILURE)	// send DEVICE address
	{
		OT_I2C_STOP();					// send STOP transition
		return OT_FAILURE;		// no bytes are written
	}
	
	//	the High Byte of subaddress and the Low Byte of subaddress
	// 	are the first two bytes of Buffer[]
	
	// send the data
	while (Count < BytesToSend)
	{
		if(OT_I2C_PutByte(Buffer[Count++]) == OT_FAILURE)
		{
			OT_I2C_STOP();					// send STOP transition
			return OT_FAILURE;
		}
	}

	OT_I2C_STOP();					// send STOP transition
	
	return OT_SUCCESS;
}


// ============================================================================
// This is a function to Read a Byte sequence on the I2C bus
//
// Functions Name:
//  * OT_ReadI2C()
//
// Inputs: (
//  * I2CAddr7Bit, (7-bit I2C Device Address)
//  * Buffer (Buffer of bytes to be read)
//  * BytesToSend (No. of Bytes to be read)
//
// Outputs:
//  	Returns OT_SUCCESS or OT_FAILURE
//	
// Assumptions:
//		It is assumed the High Byte and the Low Byte of subaddress are pre-set	
// ==============================================================================

OT_U8 OT_ReadI2C(OT_U8 I2CAddr7Bit, OT_U8 Buffer[], OT_U8 BytesToRead)
{
	OT_U8 Count=0;

	OT_I2C_START();					// do start transition

	// It is assumed the High Byte and the Low Byte of subaddress are pre-set
	
	if(OT_I2C_PutByte(I2CAddr7Bit<<1 | OT_READ) == OT_FAILURE) //send DEVICE address, with READ bit set
	{
		OT_I2C_STOP();					// send STOP transition
		return OT_FAILURE;
	}
	
	// receive data bytes

	while (Count < BytesToRead)
		{
			Buffer[Count] = OT_I2C_GetByte(Count == (BytesToRead-1));
			Count++;
		}

	OT_I2C_STOP();					// send STOP transition
	
	return OT_SUCCESS;		
			
			// instead of always returning success, investigage to return various error codes
}

// End of OT_BITBANG_I2C_MASTER Routines
	

// *****************************************************************************
// This is a function to Set ReadAddress of OneTouch Data registers
//
// Functions Name:
// * OT_Set_ReadAddress()
//
// Inputs:				
// * None
//
// Outputs:
//		Error Code		// ToDo: OT_Success or OT_FAIL
//	
// Dependencies:
//		OT_WriteI2C();
//
// Note:	All dependencies are hardware dependent and implemented in the
//			Hardware Abstraction Layer (HAL) of the host application code
// *****************************************************************************

OT_U8 OT_Set_ReadAddress(void)	
{
	OT_U8 pReg[2];
	
	pReg[0]	=	OT_DATA_REG_START_ADDR_HIGH;
	pReg[1]	=	OT_DATA_REG_START_ADDR_LOW;
	
	return OT_WriteI2C(OT_ADDR,pReg,2);
		
}


// *****************************************************************************
// This is a function to Initialize the OneTouch hardware interface
//
// Functions Name:
//  * OT_Init()
//
// Inputs: 
//  * None
//
// Outputs:
//  * The Error Code (or success code). "Refer to SynaOT.h
// 
// Dependencies:
//  * OT_HAL_BitRead_ATTN();
//  * OT_ReadI2C();
//  * OT_WriteI2C();
//
// Note:	All dependencies are hardware dependent and implemented in the
//			Hardware Abstraction Layer (HAL) of the host application code
// *****************************************************************************


OT_U8 OT_Init(void)
{	

	OT_U8  pBuffer[OT_NUM_CONFIG_BYTES];
	OT_U8  bTemp;				

	
	// wait for attention line with timeout
	// Customer Implements OT_Poll_Attn_Line_TimeOut(),
	// if not implemented, there is no timeout								
	if(OT_Poll_Attn_Line_TimeOut() == OT_FAILURE)
		return OT_FAILURE;
		
	
//	Write the configuration to the OneTouch
	if(OT_WriteI2C(OT_ADDR, g_OT_Config, OT_NUM_CONFIG_BYTES)== OT_FAILURE)		
		return OT_FAILURE;	// write to config failed
	
// read the entire configuration back from the device			
	if(OT_ReadI2C(OT_ADDR, pBuffer,(OT_NUM_CONFIG_BYTES-2)) == OT_FAILURE)
		return OT_FAILURE;
	
// verify the configuration registers are written correctly
	for(bTemp=0; bTemp < (OT_NUM_CONFIG_BYTES-2); bTemp++)
		if(pBuffer[bTemp] != g_OT_Config[bTemp+2])
				return OT_FAILURE;		
				
// set base address			
	if(OT_Set_ReadAddress() == OT_FAILURE)			
		return OT_FAILURE;							

// read the data register to deassert the attention line
		return OT_ReadI2C(OT_ADDR,pBuffer,1);
	
}

// *****************************************************************************
// This is a function to Read the OneTouch DATA Registers
//
// Functions Name:
//  * OT_ReadDataReg()
//
// Inputs:
//  * None
//
// Outputs:
//  * The Error Code (or success code). "Refer to SynaOT.h"
//
// Dependencies:
//  * OT_ReadI2C();
//  * OT_WriteI2C();
//
// Note:	All dependencies are hardware dependent and implemented in the
//			Hardware Abstraction Layer (HAL) of the host application code
// *****************************************************************************


OT_U8 OT_ReadDataReg(OT_U8 pBuffer[])
{
	
//	Read the data registers
//  OT_ReadI2C assumes the ReadAddress is preset

	if(OT_ReadI2C(OT_ADDR,pBuffer,OT_NUM_DATA_REG_BYTES) == OT_SUCCESS)	
		return OT_SUCCESS;		

	return OT_Init();		// Re-initialize OneTouch Device if Reading
							// of Data Registers failed
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -