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

📄 devi2c.c

📁 vxworks MPC8541 BSP
💻 C
字号:
#include <vxworks.h>
#include <devi2c.h>
#include <errno.h>
#include <stdio.h>
#include <sysEpic.h>
#include "logLib.h"
#include <config.h>

#ifdef INCLUDE_MOT_I2C

extern void   sysMsDelay(UINT delay );
extern STATUS intConnect (VOIDFUNCPTR *, VOIDFUNCPTR, int);
extern int   intEnable(int);
extern int   intDisable(int);


static I2C_CTRL     i2cCtrl;
static int          I2CRepeatStart = 0;

#define I2C_SYNC    WRS_ASM ("sync;isync")

/*******************************************************************************
*
* I2cRegRead - 
*
*/
static UINT8 I2cRegRead( UINT32 reg )
{
	UINT8 RegVal;
    UINT8 *addr;
    
    addr = (UINT8 *)(CCSBAR + reg);
    RegVal = *addr;
    I2C_SYNC;
    sysMsDelay(1);

    return RegVal;	
}

/*******************************************************************************
*
* I2cRegWrite - 
*
*/
static void I2cRegWrite(UINT32 WriteType,UINT32 reg ,UINT8 Val1,UINT8 Val2 )
{
    UINT8  *addr;
    UINT32 tempVal;

    addr = (UINT8 *)(CCSBAR+ reg);
    
    switch(WriteType)
    {
        case WRITE_ONLY:
            *addr = Val1;            
            break;            
        case WRITE_OR:        
            tempVal = I2cRegRead(reg);
            tempVal |= Val1;
            *addr   = tempVal;
            break;
            
        case WRITE_AND:
            tempVal = I2cRegRead(reg);
            tempVal &= Val1;
            *addr   =  tempVal;
            break;
            
        case WRITE_AND_OR:
            tempVal = I2cRegRead(reg);
            tempVal &= Val1;
            tempVal |= Val2;
            *addr   =  tempVal;
            break;
            
        default:
            logMsg("The WriteType is error!\n",1,2,3,4,5,6);            
    }

    I2C_SYNC ;
    sysMsDelay(1);
        
	return;
}

/*******************************************************************************
*
* I2cStart - 
*
*/
static int I2cStart (void)
{
   
    int timeout=0;
  
    if(I2CRepeatStart == 1) 
    {
        I2cRegWrite(WRITE_OR, M85xx_I2CCR, 
                    (M85xx_I2CCR_RSTA |M85xx_I2CCR_MSTA |M85xx_I2CCR_MTX),0);
        I2CRepeatStart = 0;  /* one repeat start only, so clear this bit */
        return OK;
    }

    while ((I2cRegRead(M85xx_I2CSR) & M85xx_I2CSR_MBB))
    {	
	    timeout++;
        if( timeout > I2C_TIMEOUT )
        {
            logMsg("I2cbus is busy\n",1,2,3,4,5,6);
            return ERROR;
        }
    }    
    
    I2cRegWrite(WRITE_OR,M85xx_I2CCR,(M85xx_I2CCR_MSTA|M85xx_I2CCR_MTX),0);

    I2CRepeatStart = 1;

	return OK;
}



/*******************************************************************************
*
* I2cStop - 
*
*/
static void I2cStop (void)
{
    I2cRegWrite(WRITE_AND, M85xx_I2CCR, 0x02,0);

    I2CRepeatStart = 0;

    return ;
}

/*******************************************************************************
*
* motI2cInt - 
*
*/
static int motI2cInt(void)
{
    UINT32 StatusReg,CtrlReg;

    StatusReg = I2cRegRead(M85xx_I2CSR);
    
	I2cRegWrite(WRITE_AND,M85xx_I2CSR,(~M85xx_I2CSR_MIF),0x0);

	if (StatusReg & M85xx_I2CSR_MAL)
    {
	    logMsg("I2C ERROR Arbitration is lost\n",1,2,3,4,5,6);
		return ERROR;
	}

	if (!(StatusReg & M85xx_I2CSR_MCF))
    {
	    logMsg("I2C ERROR: byte transfer is not completed\n",1,2,3,4,5,6);
		return ERROR;
    }

	if (StatusReg & M85xx_I2CSR_RXAK)
    {
	    logMsg("I2C ERROR: No Acknowledge receive\n",1,2,3,4,5,6);
		return ERROR;
    }

    CtrlReg = I2cRegRead(M85xx_I2CCR);

    if( CtrlReg & M85xx_I2CCR_MTX)
    {
        if(i2cCtrl.i2cRecving == 1)
        {
            if(i2cCtrl.curdata == i2cCtrl.dataLen-1)
                I2cStart();
                     
            if(i2cCtrl.curdata == i2cCtrl.dataLen)
            {
                I2cRegWrite(WRITE_AND,M85xx_I2CCR,(~M85xx_I2CCR_MTX),0);
                i2cCtrl.dataBuf[0] = I2cRegRead(M85xx_I2CDR);
                sysMsDelay(1);
                i2cCtrl.dataBuf[0] = I2cRegRead(M85xx_I2CDR); 
                I2cRegWrite(WRITE_AND,M85xx_I2CCR,(~M85xx_I2CCR_TXAK),0);
                i2cCtrl.i2cRecving = 0;
            }
        }
         if(i2cCtrl.curdata == i2cCtrl.dataLen)
        {
            i2cCtrl.i2cSending = 0;
            I2cStop();
            return OK;
        }

        I2cRegWrite(WRITE_ONLY,M85xx_I2CDR, i2cCtrl.dataBuf[i2cCtrl.curdata],0);
        i2cCtrl.curdata += 1;
    }
   
	return OK;        
   
}
/*******************************************************************************
*
* motI2cInit - 
*
*/
void motI2cInit(void)
{  
    if( i2cCtrl.i2cInited == 1)
    { 
        I2cRegWrite( WRITE_OR, M85xx_I2CCR, M85xx_I2CCR_MEN, 0);
        return;
    }
   
    intDisable(EPIC_I2C_INT_VEC);
  
    I2cRegWrite( WRITE_AND_OR, M85xx_I2CFDR,(~M85xx_I2CFDR_MASK),0x3F );

    I2cRegWrite( WRITE_OR, M85xx_I2CCR, M85xx_I2CCR_MEN, 0);

    I2cRegWrite( WRITE_AND,M85xx_I2CCR,(~( M85xx_I2CCR_MSTA | M85xx_I2CCR_MTX|M85xx_I2CCR_MIEN)), 0);
   
    i2cCtrl.i2cSem = semBCreate(SEM_Q_FIFO, SEM_FULL);

    intConnect ( (VOIDFUNCPTR *)EPIC_I2C_INT_VEC, (VOIDFUNCPTR)motI2cInt, (int)0);
    intEnable(EPIC_I2C_INT_VEC);
    i2cCtrl.intrEnabled = TRUE;

    i2cCtrl.i2cInited = 1;

    return;
}

#if 1/*def I2C_POLL_ENABLE*/

/*******************************************************************************
*
* I2cWait - 
*
*/
static int I2cWaitAckIN (void)
{
	UINT32 StatusReg;
    UINT32 timeout =0;

	do
    {
        StatusReg  = I2cRegRead(M85xx_I2CSR);

		if (!(StatusReg & M85xx_I2CSR_MIF))
        { 
            timeout++;
            if(timeout >I2C_TIMEOUT)
            {
                logMsg("I2cWaitAckIN Error\n",1,2,3,4,5,6);
                return ERROR;
            }
       		continue;
            
        }
        
		I2cRegWrite(WRITE_AND,M85xx_I2CSR,(~M85xx_I2CSR_MIF),0x0);

		if (StatusReg & M85xx_I2CSR_MAL)
        {
		    logMsg("I2cWaitAckIN: Arbitration is lost\n",1,2,3,4,5,6);
			return ERROR;
		}

		if (!(StatusReg & M85xx_I2CSR_MCF))
        {
		    logMsg("I2cWaitAckIN: byte transfer is not completed\n",1,2,3,4,5,6);
			return ERROR;
        }

		if (StatusReg & M85xx_I2CSR_RXAK)
        {
			logMsg("I2cWaitAckIN: No Acknowledge receive\n",1,2,3,4,5,6);
			return ERROR;
        }
		return OK;
        
    } while (1);
}

/*******************************************************************************
*
* I2cSend - 
*
*/
static int I2cSend (UINT8 *data, int len)
{
    int i;

    if (i2cCtrl.intrEnabled == TRUE)
    {
        intDisable(EPIC_I2C_INT_VEC);
        i2cCtrl.intrEnabled = FALSE;
    }

    I2cRegWrite(WRITE_OR,M85xx_I2CCR,M85xx_I2CCR_MEN|M85xx_I2CCR_MTX,0);

    if(ERROR == I2cStart())
    {
        logMsg("I2c start error\n",1,2,3,4,5,6);
        return ERROR;
    }
    
    for (i=0; i < len; i++)
    {
        if((i2cCtrl.i2cRecving == 1) && (i == len-1))
            I2cStart();
        
        I2cRegWrite(WRITE_ONLY,M85xx_I2CDR, data[i],0);

        if (I2cWaitAckIN () == ERROR)
			return ERROR;
	}

    if(i2cCtrl.i2cRecving == 1)
    {
        I2cRegWrite(WRITE_AND,M85xx_I2CCR,(~M85xx_I2CCR_MTX),0);

        i2cCtrl.dataBuf[0] = I2cRegRead(M85xx_I2CDR);
        sysMsDelay(1);
        i2cCtrl.dataBuf[0] = I2cRegRead(M85xx_I2CDR); 

        i2cCtrl.i2cRecving = 0;

       I2cRegWrite(WRITE_AND,M85xx_I2CCR,(~M85xx_I2CCR_TXAK),0);
    }
    I2cStop();
	return OK;
}

#else

/*******************************************************************************
*
* I2cSend - 
*
*/
static int I2cSend (UINT8 *data, int len)
{
    int i=0;

    if(ERROR == I2cStart())
    {
        logMsg("I2c start error\n",1,2,3,4,5,6);
        return ERROR;
    }
    i2cCtrl.i2cSending = 1;
    
    I2cRegWrite(WRITE_OR,M85xx_I2CCR,M85xx_I2CCR_MEN|M85xx_I2CCR_MTX|M85xx_I2CCR_MIEN,0);

    I2cRegWrite(WRITE_ONLY,M85xx_I2CDR, data[0],0); 
    i2cCtrl.curdata += 1;
        
    do
    {
        if (!i2cCtrl.i2cSending)
            return OK;
    } while (i++ < I2C_TIMEOUT);

    logMsg("I2C ERROR: slave device no response!\n",1,2,3,4,5,6);

	return ERROR;
}

#endif

/*******************************************************************************
*
* motI2cRead - 
*
*/
STATUS motI2cRead(UINT8 dev, UINT8 addr, UINT8 *data)
{

    semTake(i2cCtrl.i2cSem, WAIT_FOREVER);

    /* Command interface to stop.  This is for previous error exits. */
    I2cStop(); 
    motI2cInit();
     
	I2cRegWrite(WRITE_AND,M85xx_I2CSR,(~M85xx_I2CSR_MIF),0x0);

    i2cCtrl.dataBuf[0] = dev & ~1;
    i2cCtrl.dataBuf[1] = addr | 0x80;  /*CDCE706 bit7:0-block read/write,1-byte read/write*/
    i2cCtrl.dataBuf[2] = dev | 1;
    i2cCtrl.dataLen = 3;   
    i2cCtrl.curdata = 0;
    i2cCtrl.i2cRecving =1;
    if(ERROR == I2cSend(i2cCtrl.dataBuf, i2cCtrl.dataLen))
    {
        logMsg("motI2cRead:I2cSend 1 ERROR\n",1,2,3,4,5,6);
        semGive(i2cCtrl.i2cSem);
        return ERROR;
    }   
    *data = i2cCtrl.dataBuf[0];     
    
    semGive(i2cCtrl.i2cSem);

    return *data;
}

/*******************************************************************************
*
* motI2cWrite - 
*
*/
STATUS motI2cWrite(UINT8 dev, UINT8 addr, UINT8 data)
{
    int   retVal;

    semTake(i2cCtrl.i2cSem, WAIT_FOREVER);
    I2cStop();
    motI2cInit();
     
    i2cCtrl.dataBuf[0] = dev & ~0x01;
    i2cCtrl.dataBuf[1] = addr| 0x80;
    i2cCtrl.dataBuf[2] = data;
    i2cCtrl.dataLen = 3;   
    i2cCtrl.curdata = 0;
    i2cCtrl.i2cRecving = 0;
    retVal = I2cSend(i2cCtrl.dataBuf,i2cCtrl.dataLen); 
    
    semGive(i2cCtrl.i2cSem);

    return retVal;
}

void WriteEEprom(UINT8 dev, UINT8 addr, UINT8 data)
{
   int writeaddr;
   motI2cWrite(dev, addr, data);
   writeaddr = addr | 0x80;
   motI2cWrite(dev, 26, writeaddr);
}

UINT8 ReadEEprom(int dev,int reg,int num)
{
    UINT8 i;
    UINT8 data =0;
    
    for (i=0;i<num;i++)
    {
        motI2cRead(dev, i+reg, &data);
        
        printf("%x\t",data);
        if ((i+1)%5==0)
            printf("\n");
    }
    printf("\n");
    return OK;
}


UINT8 I2cWriteICS706(UINT8 dev,int num)
{
    UINT8 i;
    UINT8 data[27]={ 0x01,0xf3,0x35,0x78,0x1B,0x00,0x80,0x00,0x00,
                     0x20,0x30,0x42,0x00,0x0A,0x0a,0x5a,0x01,0x01,
                     0x01,0x28,0x28,0x2a,0x23,0x23,0x23,0x00,0x00};
    
    
    for (i=1;i<=num;i++)
    {
        motI2cWrite(dev, i, data[i]);
    }

 /*   motI2cWrite(dev, 26, num);*/

    return OK;
}

#endif /* INCLUDE_MOT_I2C */

⌨️ 快捷键说明

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