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

📄 iic.c

📁 M3355的源代码
💻 C
字号:
/*====================================================================
 *
 *  Copyright (C) 1997 Acer Labs, Inc.  All Rights Reserved.
 *                  
 *  File:   iic.c  
 *                                           
 *  Contents: I2c bus services routines for M6311/M3325
 *           
 *  History:
 *   Date           By         Reason
 *  ===========    =========   ======
 *  19-May-1999    Charlemage  for timing/ack process
 *  23-Aug-2001    Dick Ma     Porting to M6311/M3325
 ********************************************************************/

#include <comsub.h>
#include <gpio.h>
#include <xgpio.h>
#include <iic.h>
#include <platform.h>


//add by sidney for 4360 end
/*----------------------------------------------------------------------
 * Function_Name: I2cInit
 *
 * Description:	Initialize the IIC bus
 *
 * Arguments:
 *
 * Return Value: void
 *----------------------------------------------------------------------*/
void I2cInit(void)
{
    /* IIC Controled by SW !!! Using GPIO */
    XSET_GPIO_W(IIC_CL | IIC_DA);/* set IIC-CL, IIC-DA to output */
    XSET_GPIO_HI(IIC_CL | IIC_DA);/* pull IIC-CL, IIC-DA high */
}


/*----------------------------------------------------------------------
 * Function_Name: I2cDelay
 *
 * Description:	Delay
 *
 * Arguments:
 *     WORD w : delay (M3325 at 108M)
 *
 * Return Value: void
 *----------------------------------------------------------------------*/
//adam move this function to t_imia1.c

/*----------------------------------------------------------------------
 * Function_Name: I2cStart
 *
 * Description:	Generate I2cStart Condition:
 *	   Stream Format:
 *     SCL   ---------\_
 *     SDA   =---\____  
 *     width  Ta | Tb |
 *           Ta>600ns, Tb >600ns
 *
 * Arguments:
 *
 * Return Value: 
 *     int IIC_OK		0
 *     int IIC_CL_LOCK_ERR	1
 *     int IIC_DA_LOCK_ERR	2
 *----------------------------------------------------------------------*/
int I2cStart()
{
    I2cDelay(3);/* delay 3us */
    XSET_GPIO_W(IIC_CL | IIC_DA);/* make sure IIC-CL, IIC-DA is output */
    XSET_GPIO_HI(IIC_CL | IIC_DA);/* pull IIC-CL, IIC-DA high */
    I2cDelay(3);/* delay 3us */
    if(!XGET_GPIO_DATA(IIC_CL))
        return IIC_CL_LOCK_ERR;
    if(!XGET_GPIO_DATA(IIC_DA))
        return IIC_DA_LOCK_ERR;
    XSET_GPIO_LOW(IIC_DA);/* pull IIC-DA low */
    I2cDelay(1);/* delay 1us */
    XSET_GPIO_LOW(IIC_CL);/* pull IIC-CL low */
    return IIC_OK;
}

/*----------------------------------------------------------------------
 * Function_Name: I2cStop
 *
 * Description:	Generate I2cStop Condition:
 *     Stream Format:
 *     SCL   \____/---------------
 *     SDA    ________/------\____
 *     width     | Ta |  Tc  |
 *           Ta>600ns, Tc>1.3us (from next I2cStart bit)
 *
 * Arguments:
 *
 * Return Value: void
 *     int IIC_OK		0
 *     int IIC_CL_LOCK_ERR	1
 *     int IIC_DA_LOCK_ERR	2
 *----------------------------------------------------------------------*/
int I2cStop()
{
    I2cDelay(1);/* delay 1us */
    XSET_GPIO_W(IIC_CL | IIC_DA);/* make sure IIC-CL, IIC-DA is output */
    XSET_GPIO_LOW(IIC_CL | IIC_DA);/* pull IIC-CL, IIC-DA low */
    I2cDelay(3);/* delay 3us */
    XSET_GPIO_HI(IIC_CL);/* pull IIC-CL high */
    I2cDelay(1);/* delay 1us */
    if(!XGET_GPIO_DATA(IIC_CL))
        return IIC_CL_LOCK_ERR;
    XSET_GPIO_HI(IIC_DA);/* pull IIC-DA high */
    I2cDelay(3);/* delay 3us */
    if(!XGET_GPIO_DATA(IIC_DA))
        return IIC_DA_LOCK_ERR;
    return IIC_OK;
}

/*----------------------------------------------------------------------
 * Function_Name: I2cSetBit
 *
 * Description:	Set a BIT (Hi or Low)
 *     Stream Format:
 *     SCL   \________/----\
 *     SDA   ??DDDDDDDDDDDDD  
 *     width |   Tc   | Ta |
 *           Tc>1.3us, Ta>600ns
 *
 * Arguments:
 *     BYTE i	: Set(1) or Clear(0) this bit on iic bus
 *
 * Return Value: void
 *----------------------------------------------------------------------*/
void I2cSetBit(BYTE b)
{
    XSET_GPIO_LOW(IIC_CL);/* pull IIC-CL low */
    I2cDelay(1);/* delay 1us */
    XSET_GPIO_W(IIC_DA);/* make sure IIC-DA is output */
    if(b)
        XSET_GPIO_HI(IIC_DA);/* pull IIC-DA high */
    else
        XSET_GPIO_LOW(IIC_DA);/* pull IIC-DA low */
    I2cDelay(2);/* delay 2us */
    XSET_GPIO_HI(IIC_CL);/* pull IIC-CL high */
    I2cDelay(2);/* delay 2us */
    XSET_GPIO_LOW(IIC_CL);/* pull IIC-CL low */
}

/*----------------------------------------------------------------------
 * Function_Name: I2cGetBit
 *
 * Description:	Set a BIT (Hi or Low)
 *     Stream Format:
 *     SCL   \________/----  \
 *     SDA   ??DDDDDDDDDDDDD  
 *     width |   Tc   | Ta   |
 *           Tc>1.3us, Ta>600ns
 *
 * Arguments:
 *
 * Return Value: void
 *     BYTE i	: Get bit on iic bus
 *----------------------------------------------------------------------*/
BYTE I2cGetBit()
{
    BYTE ret;
    XSET_GPIO_LOW(IIC_CL);/* pull IIC-CL low */
    //	SET_GPIO_HI(IIC_DA);/* pull IIC-DA high */
    XSET_GPIO_R(IIC_DA);/* make sure IIC-DA is input */
    I2cDelay(3);/* delay 3us */
    XSET_GPIO_HI(IIC_CL);/* pull IIC-CL high */
    I2cDelay(1);/* delay 1us */
    if(XGET_GPIO_DATA(IIC_DA))
        ret=1;
    else
        ret=0;
    I2cDelay(1);/* delay 1us */
    XSET_GPIO_LOW(IIC_CL);/* pull IIC-CL low */
    return ret;
}

/*----------------------------------------------------------------------
 * Function_Name: I2cSetByte
 *
 * Description:	Perform a byte write process
 *     Stream Format:
 *     SCL   \___/-\___/-\___/-\___/-\___/-\___/-\___/-\___/-\__/-\
 *     SDA   --< B7>-< B6>-< B5>-< B4>-< B3>-< B2>-< B1>-< B0>-Check
 *     Clock Low: 6u, High: 4u.                                 Ack
 *     (DATA exchanged at CLK Low, ready at SCL High)
 *
 * Arguments:
 *     BYTE bData - DATA to send on iic bus
 *
 * Return Value: 
 *     The /ack signal returned from slave
 *----------------------------------------------------------------------*/
BYTE I2cSetByte(BYTE bData)
{
    BYTE i;
    for(i=0;i<8;i++)
    {
        if(bData&0x80)
            I2cSetBit(1);
        else
            I2cSetBit(0);
        bData<<=1;
    }
    return(I2cGetBit());
}

/*----------------------------------------------------------------------
 * Function_Name: I2cGetByte
 *
 * Description:	Perform a byte read process
 *     Stream Format:
 *     SCL   \___/-\___/-\___/-\___/-\___/-\___/-\___/-\___/-\___/-\
 *     SDA   --< B7>-< B6>-< B5>-< B4>-< B3>-< B2>-< B1>-< B0>-(/Ack)
 *     Clock Low: 6u, High: 4u. 
 *     (DATA exchanged at CLK Low, ready at SCL High)
 *
 * Arguments:
 *     BYTE bACK	- host send ack to slave device on iic bus, 
 *                (Note: no ack after the last byte read)
 *
 * Return Value: 
 *     bData get from iic bus
 *----------------------------------------------------------------------*/
BYTE I2cGetByte(BYTE bACK)
{
    BYTE ret=0,i;
    for(i=0;i<8;i++)
    {
        ret<<=1;
        ret|=I2cGetBit();
    }
    I2cSetBit(bACK);
    return ret;
}

⌨️ 快捷键说明

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