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

📄 i2c_func.c

📁 sigmatel 软件模拟i2c驱动,此源码是配合touch pad使用案例.enjoy
💻 C
📖 第 1 页 / 共 2 页
字号:
//   Notes:         none
//<
////////////////////////////////////////////////////////////////////////////////
void I2cDelay(uint32_t ui32MicroSec)
{
    uint32_t delay = HW_DIGCTL_MICROSECONDS.U + ui32MicroSec;
    
    while(HW_DIGCTL_MICROSECONDS.U < delay);
}//end void I2cDelay(void)

////////////////////////////////////////////////////////////////////////////////
//
//>  Name:          void I2cStart(void)
//
//   Type:          Function
//
//   Description:   I2C start signal
//
//   Inputs:        none used.
//
//   Outputs:       none used
//
//   Notes:         none
//<
////////////////////////////////////////////////////////////////////////////////

void I2cStart(void)
{
    I2C_SDO_H;
    I2C_SCK_H;
    I2cDelay(10);
    I2C_SDO_L;
    I2cDelay(5);
    I2C_SCK_L;
    I2cDelay(10);
}//end void I2cStart(void)

////////////////////////////////////////////////////////////////////////////////
//
//>  Name:          void I2cStop(void)
//
//   Type:          Function
//
//   Description:   I2C stop signal
//
//   Inputs:        none used.
//
//   Outputs:       none used
//
//   Notes:         none
//<
////////////////////////////////////////////////////////////////////////////////
void I2cStop(void)
{
    I2C_SDO_L;
    I2cDelay(10);
    I2C_SCK_H;
    I2cDelay(5);
    I2C_SDO_H;
    I2cDelay(10);
}//end void Si4720_Stop(void)
BOOL I2c_sendaddr(uint8_t addr)
{
	uint8_t temp,i,j;
	//hw_gpio_ClrBit(I2CKey_RST);
       I2cDelay(200);
	for(i=0; i<255; i++)
        {
            I2C_SDO_FOR_OUTPUT;
            I2cStart();
            I2cDelay(20);
            
            temp = addr;        //Read data str address
            
            for(j=0; j<8; j++)
            {
                if(temp & 0x80)
                {
                    I2C_SDO_H;
                }
                else
                {
                    I2C_SDO_L;
                }
                
                I2cDelay(5);
                I2C_SCK_H;
                I2cDelay(5);
                I2C_SCK_L;
                I2cDelay(5);
                
                temp <<= 1;
            }//end for(j=0; j<8; j++)
            
            I2C_SDO_L;
            I2C_SDO_FOR_INPUT;
            I2C_SCK_H;
            
            for(j=0; j<4; j++)
            {
                I2cDelay(30);
                
                if(0 == I2C_SDO_DATA)
                {
                	//I2C_SCK_L;
                    return TRUE;
                }
            }//end for(k=0; k<4; k++)
            
            //I2C_SCK_L;
            I2C_SDO_FOR_OUTPUT;
            I2cStop();
            
            I2cDelay(1);
            if(i > 250)
                return  FALSE;
            
        }//end for(i=0; i<255; i++)
        return FALSE;

}
uint32_t I2c_data_transfers(uint8_t * pData,uint32_t size,BOOL read)
{
	uint32_t temp,i,j;
	I2C_SCK_L;
       I2cDelay(10);
        
        //Read data
        for(i=0; i<size; i++)
        {
            if (read)
            	{
            	temp = 0;
            	I2C_SDO_FOR_INPUT;
            	}
	     else
	     	{
	     	temp=pData[i];
		I2C_SDO_FOR_OUTPUT;
	     	}

		 
            
            
            for(j=0; j<8; j++)
            {
                if (!read)
                {
                	if (temp&0x80)
			{
				I2C_SDO_H;
			}
			else
			{
				I2C_SDO_L;
			}
			temp<<=1;
                }
                I2cDelay(4);
                I2C_SCK_H;
                I2cDelay(4);
                if (read)
                {
	              if(I2C_SDO_DATA)
	              {
	                  temp = (temp << 1) | 1;
	              }
	              else
	              {
	                  temp <<= 1;
	              }
                }
                
                I2C_SCK_L;
		I2cDelay(2);
            }//end for(j=0; j<8; j++)
/////////////////////check ack///////////////////////////////////////
	     if (read)
	     	{
			pData[i] = temp;
            
			I2C_SDO_FOR_OUTPUT;
			if( i==(size-1))
			{
			    I2C_SDO_H;
			}
			else
			{
			    I2C_SDO_L;
			}
	     	}
	    else
	    	{
	    		I2C_SDO_FOR_INPUT;
	    	}
            
            I2cDelay(4);
            I2C_SCK_H;
            I2cDelay(4);

	     if (!read)
	     {
	     	if (I2C_SDO_DATA)
	     	{
	     		I2cStop();
			return i;
	     	}
	     }
            I2C_SCK_L;
            I2cDelay(8);
            
            I2cDelay(80);
        }//end for(i=0; i<4; i++)
	I2C_SDO_FOR_OUTPUT;
	I2cStop();
	return i;
}
#endif
BOOL I2c_Process_Data(uint8_t * pdata,uint8_t size)
{
	//pdata[0]	-->mode
	//pdata[1]	-->button
	//pdata[2]	-->even
	//pdata[3]	-->unkown
	uint32_t button,even;
	os_msg_struct_t RotMsg;
	button=even=0;
	
	switch (pdata[1])
	{
	case UART_KEY_MENU:
            button = BTN_MENU;
            break;

        case UART_KEY_PLAY:
            button = BTN_PLAY;
            break;
	case UART_KEY_FF:
	 
        
            button = BTN_FF;
            break;
	
       case UART_KEY_RW:
            button = BTN_RW;
            break;

        case UART_KEY_ENTER:
            button = BTN_ENTER;
            break;

        case UART_KEY_VOLUP:
            button = BTN_VOLUP;
            break;
        case UART_KEY_VOLDN:
        
            button = BTN_VOLDN;
            break;

        case UART_KEY_PWR:
            button = BTN_PWR;
            break;
	case UART_KEY_PAUSE:
            button =BTN_REC;
		break;
	 default:
	 	return FALSE;
	}
	switch(pdata[2])
	{
	case UART_KEY_CLICK:
	    even = EVENT_CLICK;
	    break;

	case UART_KEY_DOWN:
	    even = EVENT_DOWN;
	    break;

	case UART_KEY_HOLD:
	    even = EVENT_HOLD;
	    break;

	case UART_KEY_UP:
	    even = EVENT_UP;
	    break;
	default:
		return FALSE;
	}
	BUILD_NONALLOCATED_MSG(&RotMsg, OS_MSG_UI_GROUP, MSG_UI_BTN_EVENT,BTN_EVENT_IS(button,even));
       tx_queue_send(g_ptx_queue_ui, &RotMsg, TX_NO_WAIT); 
	return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
//
//  Name    :   void UARTKey_MsgDeal(unsigned int MsgNum, uint32_t MsgWord)
//
//  Input   :   MsgNum
//
//  Output  :   none
//
//  Note    :   deal the msg about UART key
//
/////////////////////////////////////////////////////////////////////////////
extern BOOL UI_Init_Complete;
//void I2c_Key_MsgDeal(unsigned int MsgNum, uint32_t MsgWord)

void I2c_Read_key(void)
{
    if(! UI_Init_Complete) return;
	
#if 1
    {
        //unsigned char i, j, k, temp;
	unsigned char KeyBuf[4];
       //hw_gpio_ClrBit(I2CKey_RST);
       
       
#ifdef I2C_NEW_FUNCTION_TEST
       ddi_i2c_bitbang_read(CLOCK_TIMING_CHIOCE,I2C_ADDR_READ,KeyBuf,4,NULL);
#else
	I2cDelay(200);
	if (!I2c_sendaddr(I2C_ADDR_READ))
		return;
	I2c_data_transfers(KeyBuf,4,TRUE);
#endif
       I2c_Process_Data(KeyBuf,4);
    }
    #if 0
    if(((KeyBuf[1] != 0x00) && (KeyBuf[2] == 0xff) && (KeyBuf[3] == 0xff)) ||
       ((KeyBuf[1] == 0x00) && (KeyBuf[2] == 0x00) && (KeyBuf[3] != 0xff)))
    {
        //the slider
        if((KeyBuf[1] == 0x00) && (KeyBuf[2] == 0x00) && (KeyBuf[3] != 0xff))
        {
            FirKeySliderValue = SecKeySliderValue;
            SecKeySliderValue = ThirdKeySliderValue;
            ThirdKeySliderValue = KeyBuf[3];
            
            if((SecKeySliderValue > FirKeySliderValue) && (ThirdKeySliderValue > SecKeySliderValue))
            {
                u16ButtonValue = BTN_VOLDN;
            }
            else if((SecKeySliderValue < FirKeySliderValue) && (ThirdKeySliderValue < SecKeySliderValue))
            {
                u16ButtonValue = BTN_VOLUP;
            }
            
            u8_ButtonState = EVENT_CLICK;
            BUILD_NONALLOCATED_MSG(&RotMsg, OS_MSG_UI_GROUP, MSG_UI_BTN_EVENT,BTN_EVENT_IS(u16ButtonValue,u8_ButtonState));
            tx_queue_send(g_ptx_queue_ui, &RotMsg, TX_NO_WAIT); 
            return;
        }
        else if((KeyBuf[1] != 0x00) && (KeyBuf[2] == 0xff) && (KeyBuf[3] == 0xff))
        {
            switch(KeyBuf[1])
            {
                case UART_KEY_MENU:
                    u16ButtonValue = BTN_MENU;
                    break;
                
                case UART_KEY_PLAY:
                    u16ButtonValue = BTN_PLAY;
                    break;
                
                case UART_KEY_FF:
                    u16ButtonValue = BTN_FF;
                    break;
                
                case UART_KEY_RW:
                    u16ButtonValue = BTN_RW;
                    break;
                
                case UART_KEY_ENTER:
                    u16ButtonValue = BTN_ENTER;
                    break;
                
                case UART_KEY_PWR:
                    u16ButtonValue = BTN_PWR;
                    break;
            }//end switch(KeyBuf[1])
        }
        
        if(u16ButtonValue == LastButtonValue)
        { 
            ui_KeyHoldCnt++;
        
            if(1 == ui_KeyHoldCnt)
            {
                u8_ButtonState = EVENT_DOWN;
                BUILD_NONALLOCATED_MSG(&RotMsg, OS_MSG_UI_GROUP, MSG_UI_BTN_EVENT,BTN_EVENT_IS(u16ButtonValue,u8_ButtonState));
                tx_queue_send(g_ptx_queue_ui, &RotMsg, TX_NO_WAIT); 
            }
            else if((ui_KeyHoldCnt >= 10) && (ui_KeyHoldCnt % 10 == 0))
            {
                u8_ButtonState = EVENT_HOLD;
                BUILD_NONALLOCATED_MSG(&RotMsg, OS_MSG_UI_GROUP, MSG_UI_BTN_EVENT,BTN_EVENT_IS(u16ButtonValue,u8_ButtonState));
                tx_queue_send(g_ptx_queue_ui, &RotMsg, TX_NO_WAIT); 
            }
        }
        
        LastButtonValue = u16ButtonValue;
    }
    else
    {
        if(ui_KeyHoldCnt > 0)
        {
            if(/*(ui_KeyHoldCnt > 1) && */(ui_KeyHoldCnt < 10))
            {
                u8_ButtonState = EVENT_CLICK;
            }
            else
            {
                u8_ButtonState = EVENT_UP;
            }
            
            BUILD_NONALLOCATED_MSG(&RotMsg, OS_MSG_UI_GROUP, MSG_UI_BTN_EVENT,BTN_EVENT_IS(LastButtonValue,u8_ButtonState));
            tx_queue_send(g_ptx_queue_ui, &RotMsg, TX_NO_WAIT); 
        }//end if(ui_KeyHoldCnt > 5)

        FirKeySliderValue = 0;
        SecKeySliderValue = 0;
        ThirdKeySliderValue = 0;
        LastButtonValue = 0;
        ui_KeyHoldCnt = 0;
        return;
    }
    #endif
//    if(u16ButtonValue == BTN_PWR)
//        u8_ButtonState = EVENT_HOLD;
//    else
//        u8_ButtonState = EVENT_CLICK;
    
#endif
//test GPIO for I2C -------------------------------------------------- end


//    BUILD_NONALLOCATED_MSG(&RotMsg, OS_MSG_UI_GROUP, MSG_UI_BTN_EVENT,BTN_EVENT_IS(u16ButtonValue,u8_ButtonState));
//    tx_queue_send(g_ptx_queue_ui, &RotMsg, TX_NO_WAIT); 
}

⌨️ 快捷键说明

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