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

📄 iic.c

📁 在CCS环境下的一个视频采集源代码
💻 C
字号:
/***********************************************************/
/*  Copyright 2003 by SEED Incorporated.				   */
/*  All rights reserved. Property of SEED Incorporated.    */ 
/*  											           */
/***********************************************************/
#include "iic.h"      

#define I2CDELAY(iterations)  {      \
    volatile Int j;                  \
    for(j = 0; j < iterations; j ++); \
}   

#define DELAY_TIME 0x4000
#define I2CCLKx 15

// SEED series config
I2C_Config SEEDDM642IIC_Configt = {
    0,  /* master mode,  i2coar;采用主模式   */
    0,  /* no interrupt, i2cimr;只写,不读,采用无中断方式*/
    (20-5), /* scl low time, i2cclkl;  */
    (20-5), /* scl high time,i2cclkh;  */
    2,  /* configure later, i2ccnt;*/
    0,  /* configure later, i2csar;*/
    0x4620, /* master tx mode,     */
            /* i2c runs free,      */
            /* 8-bit data + NACK   */
            /* no repeat mode      */
    (75-1), /* 2MHz clock, i2cpsc  */
};

I2C_Config SEEDDM642IIC_Configr = {
    0,  /* master mode,  i2coar;采用主模式   */
    0,  /* no interrupt, i2cimr;只读,不写,采用无中断方式*/
    (20-5), /* scl low time, i2cclkl;  */
    (20-5), /* scl high time,i2cclkh;  */
    1,  /* configure later, i2ccnt;*/
    0,  /* configure later, i2csar;*/
    0x4C20, /* master rx mode,     */
            /* i2c runs free,      */
            /* 8-bit data + NACK   */
            /* no repeat mode      */
    (75-1), /* 2MHz clock, i2cpsc  */
};

// DM642 official series config
static const I2C_Config EVM642VIDEOIIC_Config = {
	0,  /* master mode,  i2coar;   */
	0,  /* no interrupt, i2cimr;   */
	(I2CCLKx), /* scl low time, i2cclkl;  */
	(I2CCLKx), /* scl high time,i2cclkh;  */
	1,  /* configure later, i2ccnt;*/
	0,  /* configure later, i2csar;*/
	0x4620, /* master tx mode,     */
	        /* i2c runs free,      */
	        /* 8-bit data + NACK   */
	        /* no repeat mode      */
	(75-1), /* 4MHz clock, i2cpsc  */
};

static const I2C_Config EVM642VIDEOIIC_Config_Read = {
	0,  /* master mode,  i2coar;   */
	0,  /* no interrupt, i2cimr;   */
	(I2CCLKx), /* scl low time, i2cclkl;  */
	(I2CCLKx), /* scl high time,i2cclkh;  */	
	1,  /* configure later, i2ccnt;*/
	0,  /* configure later, i2csar;*/
	0x4C20, /* master rcv mode,    */
	        /* i2c runs free,      */
	        /* 8-bit data + NACK   */
	        /* no repeat mode      */
	(75-1), /* 4MHz clock, i2cpsc  */
};

/*
 * ======== _IIC_write ========
 * This function performs write operation via I2C bus.
 */
/* Spin in a delay loop for delay iterations */
void EVMDM642_wait(Uint32 delay)
{
    volatile Uint32 i, n;
    
    n = 0;
    for (i = 0; i < delay; i++)
    {
        n = n + 1;
    }
}

/* Spin in a delay loop for delay microseconds */
void EVMDM642_waitusec(Uint32 delay)
{
    EVMDM642_wait(delay * 21);
}

void SEED_IIC_write(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress,
              Uint8 data
              )
{             
    I2C_Config prevI2CCfg;
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
	//I2C_start(hI2C);
    /* Save old settings */
    I2C_getConfig(hI2C, &prevI2CCfg);
    
    /* Restore settings for AIC23 */
    SEEDDM642IIC_Configt.i2csar = devAddress;
    I2C_config(hI2C, &SEEDDM642IIC_Configt);

    /* Submit the MSB for transmit */
    I2C_RSETH(hI2C, I2CDXR, (subAddress) & 0xff);
    
    /* Generate start condition, starts transmission */
    I2C_start(hI2C);
    
    /* Wait until MSB transmit is done */
    while(!I2C_xrdy(hI2C));

    /* Submit the LSB for transmit */ 
    I2C_RSETH(hI2C, I2CDXR,data);    
 //   EVMDM642_waitusec(350);
    
    /* Generate stop condition */
    I2C_sendStop(hI2C);  

    /* Wait until bus is free */
    while (I2C_bb(hI2C));
            
    EVMDM642_waitusec(350);

    /* Reconfigure I2C with old settings */
    I2C_config(hI2C, &prevI2CCfg);              
}     

/*
 * ======== _IIC_read ========
 * This function performs read from operation via I2C bus.
 */

void SEED_IIC_read(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress,
              Uint8 *data
              )
{
    I2C_Config prevI2CCfg;
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
    /* Save old settings */
    I2C_getConfig(hI2C, &prevI2CCfg);
    
    /* Restore settings for AIC23 */
    SEEDDM642IIC_Configt.i2csar = devAddress;
    I2C_config(hI2C, &SEEDDM642IIC_Configt);

    /* Submit the MSB for transmit */
    I2C_RSETH(hI2C, I2CDXR, (subAddress) & 0xff);
    
    /* Generate start condition, starts transmission */
    I2C_start(hI2C);
    
    /* Wait until MSB transmit is done */
    while(!I2C_xrdy(hI2C));
        
    /* Generate stop condition */
    I2C_sendStop(hI2C);  
	EVMDM642_waitusec(20);

    /* Reconfigure I2C with old settings */
    I2C_config(hI2C, &prevI2CCfg);
  	I2C_getConfig(hI2C, &prevI2CCfg);
    /*从发送到接收需一段时间转换*/
    EVMDM642_waitusec(0x200);
    /* Restore settings for AIC23 */
    SEEDDM642IIC_Configr.i2csar = devAddress;
    I2C_config(hI2C, &SEEDDM642IIC_Configr);
    /* Generate start condition, starts transmission */
    I2C_start(hI2C);
    while(I2C_FGETH(hI2C,I2CSTR,ARDY));
    /* Wait until MSB transmit is done */
    while(!I2C_rrdy(hI2C));

    /* Submit the MSB for transmit */
    *data = I2C_RGETH(hI2C, I2CDRR);
        
    /* Generate stop condition */
    I2C_sendStop(hI2C);  
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C)); 

    /* Short delay for AIC23 to accept command */        
    EVMDM642_waitusec(20);

    /* Reconfigure I2C with old settings */
    I2C_config(hI2C, &prevI2CCfg);          
}


/*
 * ======== _IIC_write ========
 * This function performs write operation via I2C bus.
 */

void _IIC_write(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress,
              Uint8 *data,
              Uint16  numBytes
              )
{             
    Int i;
    I2C_Config prevIICConfig;
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    /* save old settings */
    I2C_getConfig(hI2C, &prevIICConfig);
    
    /* write entire i2c control structure to i2c registers */
    I2C_config(hI2C, (I2C_Config *)&EVM642VIDEOIIC_Config);

    /* configure the I2C slave address register */
    I2C_RSETH(hI2C, I2CSAR, devAddress);
    
    /* set I2C count register */
    I2C_RSETH(hI2C, I2CCNT, numBytes + 1);
      
    /* write the sub address */
    I2C_RSETH(hI2C, I2CDXR, subAddress);
   	    
    /* Generate start condition */
    I2C_start(hI2C);
    
  	I2CDELAY(DELAY_TIME);

    /* write the data */ 
    for(i = 0; i < numBytes; i ++) {
        while(!I2C_xrdy(hI2C));
        I2C_writeByte(hI2C, *data++);
     	I2CDELAY(DELAY_TIME);
    }

    /* Generate stop condition */
    I2C_sendStop(hI2C); 
    
   	I2CDELAY(DELAY_TIME);        
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

   	I2CDELAY(DELAY_TIME);        
    /* now restore the previous I2C settings */
    
    /* write entire previous i2c control structure to i2c registers */
    I2C_config(hI2C, &prevIICConfig);
    
   	I2CDELAY(DELAY_TIME);        
}     



/*
 * ======== _IIC_read ========
 * This function performs read operation via I2C bus.
 */

void _IIC_read(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress,
              Uint8 *data,
              Uint16  numBytes
              )
{             
    Int i;
    I2C_Config prevIICConfig;
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    /* save old settings */
    I2C_getConfig(hI2C, &prevIICConfig);
    
    /* write entire i2c control structure to i2c registers */
    I2C_config(hI2C, (I2C_Config *)&EVM642VIDEOIIC_Config_Read);
    
	/* Issue a SubAddress write to the THS8200 */    
    _IIC_write(hI2C, devAddress, subAddress, NULL, 0);

    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
    /* configure the I2C slave address register */
    I2C_RSETH(hI2C, I2CSAR, devAddress);
    
    /* set I2C count register */
    I2C_RSETH(hI2C, I2CCNT, numBytes);	
    
    /* Generate start condition */
    I2C_start(hI2C);
    
  	I2CDELAY(DELAY_TIME);
    
    
    /* write the data */ 
    for(i = 0; i < numBytes; i ++) {
        while(!I2C_rrdy(hI2C));
        *data++ = I2C_readByte(hI2C);
     	I2CDELAY(DELAY_TIME);
    }

    /* Generate stop condition */
    I2C_sendStop(hI2C); 
    
   	I2CDELAY(DELAY_TIME);        
    /* Wait until bus is free */
    while (I2C_bb(hI2C));

   	I2CDELAY(DELAY_TIME);        
    /* now restore the previous I2C settings */
    
    /* write entire previous i2c control structure to i2c registers */
    I2C_config(hI2C, &prevIICConfig);
    
   	I2CDELAY(DELAY_TIME);        
}     

void Mega88_IIC_write(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint8 data1,
			  Uint8 data2
              )
{   
    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
	// config:
	// ACK, RUN FREE, NO START, RESERVE, NO STOP, MASTER, XMT, 7BIT
	// NO REPEAT, NO LOOPBACK, NRST, NONE START, NO FREE DATA, 8BIT, 
	I2C_RSETH(hI2C, I2CSAR, devAddress);
 	I2C_RSETH(hI2C, I2CMDR, 0x4620);
    I2C_RSETH(hI2C, I2CCNT, 2);

    /* Generate start condition, starts transmission */
    I2C_start(hI2C);
 
  	I2CDELAY(DELAY_TIME);

    /* write the data */ 
    while(!I2C_xrdy(hI2C));

    I2C_writeByte(hI2C, data1);

 	I2CDELAY(DELAY_TIME);

    while(!I2C_xrdy(hI2C));

    I2C_writeByte(hI2C, data2);

 	I2CDELAY(DELAY_TIME);

    /* Generate stop condition */
    I2C_sendStop(hI2C);  
    
   	I2CDELAY(DELAY_TIME);        

    /* Wait until bus is free */
    while (I2C_bb(hI2C));

    EVMDM642_waitusec(200);    
}

void Mega88_IIC_read(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint8 *data
              )
{    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
	// config:
	// ACK, RUN FREE, START, STOP, MASTER, RCV, 7BIT
	// REPEAD, NO LOOPBACK, NRST, NONE START, NO FREE DATA, 8BIT, 
	I2C_RSETH(hI2C, I2CSAR, devAddress);
 	I2C_RSETH(hI2C, I2CMDR, 0x6CA0);
    //I2C_RSETH(hI2C, I2CMDR, 0x4C20);
    I2C_RSETH(hI2C, I2CCNT, 1);

    /* Generate start condition, starts transmission */
    I2C_start(hI2C);
 
  	I2CDELAY(DELAY_TIME);

    while(!I2C_rrdy(hI2C));

    *data = I2C_RGETH(hI2C, I2CDRR);
       
  	I2CDELAY(DELAY_TIME);

    /* Generate stop condition */
    I2C_sendStop(hI2C);  
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C)); 

    EVMDM642_waitusec(20);    
}


/*
 * ======== eeprom_IIC_write ========
 * This function performs write operation via I2C bus.
 */

void Eeprom_IIC_write(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress1,
              Uint32  subAddress2,
              Uint8 data
              )
{             
    I2C_Config prevI2CCfg;
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
	//I2C_start(hI2C);
    /* Save old settings */
    I2C_getConfig(hI2C, &prevI2CCfg);
    
    /* Restore settings for AIC23 */
    I2C_config(hI2C, &SEEDDM642IIC_Configt);

	/* Generate start condition, starts transmission */
    I2C_start(hI2C);

	I2C_RSETH(hI2C, I2CSAR, devAddress);

    /* Submit the MSB for transmit */
    I2C_RSETH(hI2C, I2CDXR, (subAddress1) & 0xff);
    
    /* Wait until MSB transmit is done */
    while(!I2C_xrdy(hI2C));
	
	I2C_RSETH(hI2C, I2CDXR, (subAddress2) & 0xff);

	while(!I2C_xrdy(hI2C));

    /* Submit the LSB for transmit */ 
    I2C_writeByte(hI2C, data);

    I2CDELAY(DELAY_TIME);    
 //   EVMDM642_waitusec(350);
    
    /* Generate stop condition */
    I2C_sendStop(hI2C);  

    /* Wait until bus is free */
    while (I2C_bb(hI2C));
            
    EVMDM642_waitusec(350);

    /* Reconfigure I2C with old settings */
    I2C_config(hI2C, &prevI2CCfg);              
}  



/*
 * ========eeprom _IIC_read ========
 * This function performs read operation via I2C bus.
 */

void Eeprom_IIC_read(I2C_Handle hI2C,
              Uint8 devAddress,
              Uint32  subAddress1,
              Uint32  subAddress2,
              Uint8 *data
              )
{             
    I2C_Config prevI2CCfg;
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C));
    
    /* Save old settings */
    I2C_getConfig(hI2C, &prevI2CCfg);
    
    /* Restore settings for AIC23 */
    I2C_config(hI2C, &SEEDDM642IIC_Configt);
	
	/* Generate start condition, starts transmission */
    I2C_start(hI2C);

	I2C_RSETH(hI2C, I2CSAR, devAddress);

    /* Submit the MSB for transmit */
    I2C_RSETH(hI2C, I2CDXR, (subAddress1) & 0xff);
    
    /* Wait until MSB transmit is done */
    while(!I2C_xrdy(hI2C));
    
    I2C_RSETH(hI2C, I2CDXR, (subAddress2) & 0xff);
    
    while(!I2C_xrdy(hI2C));
        
    /* Generate stop condition */
    I2C_sendStop(hI2C);  
	EVMDM642_waitusec(20);

    /* Reconfigure I2C with old settings */
    I2C_config(hI2C, &prevI2CCfg);
  	I2C_getConfig(hI2C, &prevI2CCfg);
    /*从发送到接收需一段时间转换*/
    EVMDM642_waitusec(0x200);
    /* Restore settings for AIC23 */

    I2C_config(hI2C, &SEEDDM642IIC_Configr);

	I2C_RSETH(hI2C, I2CSAR, devAddress);

    /* Generate start condition, starts transmission */
    I2C_start(hI2C);

    /* Wait until MSB transmit is done */
    while(!I2C_rrdy(hI2C));

    /* Submit the MSB for transmit */
    *data = I2C_readByte(hI2C);
    
	I2CDELAY(DELAY_TIME);

    /* Generate stop condition */
    I2C_sendStop(hI2C);  
    
    /* Wait until bus is free */
    while (I2C_bb(hI2C)); 

    /* Short delay for AIC23 to accept command */        
    EVMDM642_waitusec(20);

    /* Reconfigure I2C with old settings */
    I2C_config(hI2C, &prevI2CCfg);          
}

   



⌨️ 快捷键说明

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