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

📄 gpio_i2c.c

📁 在Wince环境下面的用 gpio 来模拟 i2c的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
////////////////////////////////////////////////////////////////////
// Copyright Mercury's CO.,LTD.2007-2008
// Author : Mercury Xu
// Descript: this file for DEMO chip,it's a dll entry function 
// in this file include the DEMO_XXX function for driver the chip
// as a stream device.At the same time,in the bsp package we define
// other function for make suer that this device can reset.use 
// centrality define function from gGPIO.h head file.
////////////////////////////////////////////////////////////////////
//update log
//create by : 2008-03-21 mercury xu 
//modify  : 2008-04-01 mercury xu add little functions segment.as following
//modify  : 2008-04-01 mercury xu modify the operation functions.
//add       :2008-04-03 mercury xu add some functions and some define
//modify  : 2008-07-30 mercury xu change basic drvier to screen driver
/////////////////////////////////////////////////////////////////////
#include <drvlib.h>
#include <oal_at4x0a.h>
#include <cspregs.h>
#include <nkintr.h>
#include "halio.h"
#include "uhal.h"
#include "drvglob.h"
#include "DEMO.h"

/////////////////////////////////////////////////////////////////////

//=============================================================================
//Title : WrToDEMO
//Detail: Write Data To DEMO
//Input : addr data
//Output: none
//Author: Mercury Xu
//Data  : 2008-07-30
//=============================================================================

void WrToDEMO(unsigned char addr,unsigned char data)
{

        StartBit();

    RETAILMSG(1,(TEXT("WRITE DEMO address\r\n")));
         
        WrData(DEMO_WriteID);

        if  ( !ReceiveAck() ) //continue;
        
            {
            
    RETAILMSG(1,(TEXT("WRITE DEMO address\r\n")));
    
        WrData(addr);
        
            }
        
        if  ( !ReceiveAck() )// continue;
        
            {
            
    RETAILMSG(1,(TEXT("WRITE DEMO data\r\n")));

        WrData(data);

            }
        
        if  ( !ReceiveAck() )// continue;
        
            {
            
    RETAILMSG(1,(TEXT("Stop!\r\n")));

        StopBit();

        }
}

//=============================================================================
//Title : initial_DEMO
//Detail: initial DEMO chip
//Input : none
//Output: none
//Author: Mercury Xu
//Data  : 2008-07-30
//=============================================================================

void initial_DEMO()
{
     int i; 
     unsigned char  address=0x00;
     unsigned char error_ind;
     unsigned char DEMO_reg_data[98] = {};
     unsigned char DEMOdata;
     int loop_counter = 0;

    //DEMO_reset();

    /*
    for(i=0; i<50; i++)      
    {


        if(OperationDEMO(WRITE,&DEMOAddress[i] , 1))

        RETAILMSG(1,(TEXT("address write ok!\r\n")));
    
        if(OperationDEMO(WRITE,&DEMOTable[i] , 1))

            RETAILMSG(1,(TEXT("data write ok!\r\n")));

    }
     */

        for(i=0; i<50; i++)      
       {

        DEMOdata = DEMOTable[i];

        address = DEMOAddress[i];
        
        WrToDEMO(address,DEMOdata);

       }


    RETAILMSG(1,(TEXT("DEMO_Read!\r\n ")));
    do
    {   
        error_ind = OperationDEMO(READ, &(DEMO_reg_data[loop_counter]), 1);
        if(error_ind)
            {
            RETAILMSG(1,(TEXT("power up cts error!\r\n")));
            //return  (FALSE);
            }
        RETAILMSG(1,(TEXT("return val =%d\r\n"),DEMO_reg_data[loop_counter]));
        loop_counter++;
    }
    while(((DEMO_reg_data[0]) != 0x80) && (loop_counter < 0x62));  //loop_counter limit should guarantee at least 300us
    
    if(loop_counter >= 0x62)
    {
        RETAILMSG(1,(TEXT("out...this chip couldn't read any things!!!!\r\n")));
        
    
    }
    else
    {
        RETAILMSG(1,(TEXT("everything is normal....future is beatuiful...!\r\n ")));
    }
    

}

//=============================================================================
//Title : ReceiveAck
//Detail: Receive Ack from DEMO chip
//Input : none
//Output: int
//Author: Mercury Xu
//Data  : 2008-07-30
//=============================================================================

int ReceiveAck()
{
   
    int val;
    
    sdio_input_enable();

    sclk_high();

       sleepGPIO(100);

    val = sdio_getdata();

    RETAILMSG(1,(TEXT("return val =%d\r\n"),val));

    sclk_low();

    sdio_output_enable();

     sdio_low();

     sleepGPIO(500);
     
     return (val);
      
}

//=============================================================================
//Title : StopBit
//Detail: StopBit  from DEMO chip
//Input : none
//Output: none
//Author: Mercury Xu
//Data  : 2008-07-30
//=============================================================================

void StopBit()
{
 

       sdio_high();

       sleepGPIO(500);

    sclk_high();

}

//=============================================================================
//Title : StartBit
//Detail: StartBit  from DEMO chip
//Input : none
//Output: none
//Author: Mercury Xu
//Data  : 2008-07-30
//=============================================================================

void StartBit()
{
    
    
     sdio_low();

     sleepGPIO(500);

     sclk_low();


}


//=============================================================================
//Title : WrData
//Detail: WrData to DEMO chip
//Input : none
//Output: none
//Author: Mercury Xu
//Data  : 2008-07-30
//=============================================================================

void WrData(unsigned char data)
{

    int i; 

    for (i=0; i<8; i++)
    {
        if (data & 0x80)

            sdio_high();

        else

            sdio_low();

        sleepGPIO(500);
 
        sclk_high();
        
        data = (data<<1);
        
        sleepGPIO(500);

        sclk_low();
    }

//  sleepGPIO(5);
 
}

//////////////////////////////////////////////////////////////////////////////////
/*
void DEMO_reset()
{
RETAILMSG(1,(TEXT(" DEMO reset begin")));//add by mercury xu Mercury's 20080909

GPIO_OUTPUT_ZERO (GGPIO, GGPIO_GROUP(5), GGPIO_INDEX(17));

GPIO_OUTPUT_ENABLE (GGPIO, GGPIO_GROUP(5), GGPIO_INDEX(17));

    sleepGPIO(10);
    
GPIO_OUTPUT_ONE (GGPIO, GGPIO_GROUP(5), GGPIO_INDEX(17));
    
GPIO_OUTPUT_ENABLE (GGPIO, GGPIO_GROUP(5), GGPIO_INDEX(17));

RETAILMSG(1,(TEXT(" DEMO reset end")));//add by mercury xu Mercury's 20080909

//DEMO RESET
}
*/

///////////////////////////////////////////////////////////////////////////
//modify by mercury xu  Mercury's 20080401
//this segment for define some useful little function.usually use in the program.
//there are including some functions as following words
//sdio_high()
//sdio_low()
//sclk_high()
//sclk_low()
//sdio_output_enable()
//sdio_input_enable()
//sdio_getdata()
///////////////////////////////////////////////////////////////////////////
//update log
//create by mercury xu  20070401
///////////////////////////////////////////////////////////////////////////
void sleepGPIO(long usVal)
{
    DWORD StartLo, StartHi, TickCnt;

    if (usVal != 0)
    
    {   
    
        // Calculate the match tick count first, automatically cut the overflow part of result.

        TickCnt = usVal * TIMERTICK;

        if (TickCnt == MAXDWORD)

        {

            DEBUGMSG(1, (TEXT("ERROR: usWait(usVal) input parameter usVal:0x%x too big!!!"), usVal));

            return;

        }

        v_pOSTRegs->ostlatch = 1;

        StartLo = v_pOSTRegs->ostlatchlo;

        StartHi = v_pOSTRegs->ostlatchhi;

        do 
        {
            v_pOSTRegs->ostlatch = 1;

        }while ((v_pOSTRegs->ostlatchhi-StartHi)?((DWORD)(MAXDWORD-StartLo + v_pOSTRegs->ostlatchlo) < TickCnt):((DWORD)(v_pOSTRegs->ostlatchlo - StartLo) < TickCnt));
    
    }
    //end if
}

///////////////////////////////////////////////////////////////////////////
void sdio_high()
{
    
    GPIO_OUTPUT_ONE ();
    
    GPIO_OUTPUT_ENABLE ();    
    
    //modify by mercury for cnd620F 20080730 change the control to i2c
}
///////////////////////////////////////////////////////////////////////////
void sdio_low()
{
    
    GPIO_OUTPUT_ZERO ();
    
    GPIO_OUTPUT_ENABLE ();
    
    //modify by mercury for cnd620F 20080730 change the control to i2c
}
///////////////////////////////////////////////////////////////////////////
void sclk_high()
{
    
    GPIO_OUTPUT_ONE ();
    
    GPIO_OUTPUT_ENABLE ();
    
    //modify by mercury for cnd620F 20080730 change the control to i2c
}
///////////////////////////////////////////////////////////////////////////
void sclk_low()
{
    
    GPIO_OUTPUT_ZERO ();
    
    GPIO_OUTPUT_ENABLE ();
    
    //modify by mercury for cnd620F 20080730 change the control to i2c
}
//////////////////////////////////////////////////////////////////////////
void sdio_output_enable()
{
    
    GPIO_OUTPUT_ENABLE (); 
    
    //modify by mercury for cnd620F 20080730 change the control to i2c
}
//////////////////////////////////////////////////////////////////////////
void sdio_input_enable()
{
    
    GPIO_OUTPUT_DISABLE(); 
    
    GPIO_INT_ENABLE(); 
    
    //modify by mercury for cnd620F 20080730 change the control to i2c   
}
//////////////////////////////////////////////////////////////////////////
int sdio_getdata()
{
    
    GPIO_OUTPUT_DISABLE ();

    GPIO_INT_ENABLE();
    
    sleepGPIO(2000);
    
    return (GPIO_READ_INPUT ());
    
    //modify by mercury for cnd620F 20080730 change the control to i2c
}
//end segment
//////////////////////////////////////////////////////////////////////////////////
// si4730_reset
// this function use to make sure the chip reset when we use it first.
// step like follow descript:
// sdio low->reset low->sclk high->delay->reset high->delay->sdio high->delay
//////////////////////////////////////////////////////////////////////////////////
//update log 
//2008-03-29 add by mercury xu Mercury's for chip si4730 reset
//////////////////////////////////////////////////////////////////////////////////
void sdio_reset()
{
    sdio_low();

    GPIO_OUTPUT_ZERO ();
    GPIO_OUTPUT_ENABLE ();
    
    //modify by mercury for cnd620F 20080730 change the control to i2c

    sclk_high();
    
    sleepGPIO(1000);
    
    GPIO_OUTPUT_ONE ();
    
    GPIO_OUTPUT_ENABLE ();

    sleepGPIO(1000);
    
    sdio_high();
    
    sleepGPIO(1000);
}
//end segment
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
//modify by mercury xu ,add a function for si4730,this function use to operation this chip
//make sure the chip reset,and then,set a command power it up ,if this function can sent 
//command to chip,there will be make out some noises just like radio couldn't get any 
//programs,that means this chip can work.all the applcation use in the application .exe
//Mercury's for car v600 2008-03-27 
//this segment "as is" silicon lab's function.
//Mercury's co,.ltd.
/////////////////////////////////////////////////////////////////////////////////////////////////

extern "C" unsigned char OperationDEMO(T_OPERA_MODE operation, unsigned char *data, unsigned char numBytes)
{
//define some variables for this function
unsigned char controlword;
    unsigned char j;
    unsigned error;
    int i;
    int testNo;
    int testNo1;
    int returndata;
    controlword = 0;
    j = 0;
    error = 0;
    i = 0;
    testNo = 0;
       testNo1 = 0;
    returndata = 0;
//define variable end
//function begin
/***************************************************

START: make sure here SDIO_DIR =OUT, SCLK = 1,  SDIO = 1

****************************************************/
       
    sdio_low();

    sleepGPIO(500);

    sclk_low();

    sleepGPIO(500);

    RETAILMSG(1,(TEXT("I2C START!\r\n ")));

⌨️ 快捷键说明

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