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

📄 i2c_bus.c

📁 以ST公司CPU为核心的彩色电视机的完整源程序。
💻 C
📖 第 1 页 / 共 2 页
字号:

       I2CCTR &= ~I2C_CLEAR;   /* enable i2c-bus interface */
 
       }
 
   bus_flags = 0x00;

   bus_flags &= ~I2C_GOOD_F;					/* set bus fail */
	
   while (fast_timers[I2CERRORTIMER] > 0)     /* check the bus whether free or not */
       {
       if ((I2CSTR2 & (I2C_SCLIN | I2C_SDAIN)) == (I2C_SCLIN | I2C_SDAIN))
           { 
           bus_flags |= I2C_GOOD_F;
           bus_flags &= ~I2C_INT_OK_F;
           
           RESTORE_PPR;
           return TRUE;
           }
      }
      
   RESTORE_PPR;
   return FALSE;
}

/*****************************************************************************
INPUTS     : EEPROM location address
OUTPUTS    : EEPROM location value
	     see start
	     see write_byte
	     see read_byte
	     see stop
DESCRIPTION: This function reads the EEPROM byte at the address specified.
	     It first generates an I2C start condition then it writes out
	     the EEPROM chip address. Following this chip address, the
	     address of the EEPROM location to read from is written out.
	     Then a new I2C start condition is generated and the EEPROM
	     chip address is written out again. Then, the data byte is
	     read and the session is ended by generating a stop condition
	     (see "Random Address Read" in the data-sheet).
NOTE     : It only supports 24c08 (not 24c04) .
*****************************************************************************/
unsigned char read_eeprom(unsigned int address)
{
    unsigned char i;     /* Temporary storage */
    
    unsigned char repeaterr;
    
    repeaterr = I2C_REPEAT_NUM;
    i=0x00;
    while(repeaterr--)
       {
       if(!I2cCheck()) continue; /* the bus can use or not */
       /* Set EEPROM device address (write) */
       asm ("ld %0,%H1
             sla %0
             or %0,%2" : "=r"(i) : "r"(address), "i"(EEPROM_DEVICE_ADDRESS | WRITE_BIT));
       I2cSendByte(i,STARTTRUE,STOPFALSE); /* send to bus with start-sign,without stop-sign */

	   /* Set EEPROM location address */
	   asm ("ld %0,%L1" : "=r"(i) : "r"(address));
       I2cSendByte(i,STARTFALSE,STOPFALSE);
    
       /* Set EEPROM device address (read) with start-sign */
       asm ("ld %0,%H1
             sla %0
             or %0,%2" : "=r"(i) : "r"(address), "i"(EEPROM_DEVICE_ADDRESS | READ_BIT));
       I2cSendByte(i,STARTTRUE,STOPFALSE); 
	
       i = I2cReadByte(ACKFALSE,STOPTRUE);
       
       if( I2cOK() ) break; /* if the operation is done correctly ,exit loop */
       }

   return i;
}

/*****************************************************************************
INPUTS     : EEPROM location address
	     Byte to write into the EEPROM
OUTPUTS    : see start
	     see write_byte
	     see stop
	     see millisecond_delay
DESCRIPTION: This function writes into the EEPROM at the address specified.
	     It first generates an I2C start condition then it writes out
	     the EEPROM chip address. Following this chip address, the
	     address of the EEPROM location to write into is written out.
	     Then, the data byte is written out and the session is ended
	     by generating a stop condition (see "Byte Write" in the
	     data-sheet).
*****************************************************************************/
void write_eeprom(unsigned int address, unsigned char value)
{
	unsigned char i; 

    unsigned char repeaterr;
    
    repeaterr = I2C_REPEAT_NUM;
    while(repeaterr--)
       {
       if(!I2cCheck()) continue; /* the bus can use or not */

       /* Set EEPROM device address (write) */
       asm ("ld %0,%H1
             sla %0
             or %0,%2" : "=r"(i) : "r"(address), "i"(EEPROM_DEVICE_ADDRESS | WRITE_BIT));
       I2cSendByte(i,STARTTRUE,STOPFALSE); /* send to bus with start-sign,without stop-sign */

       /* Set EEPROM location address */
       asm ("ld %0,%L1" : "=r"(i) : "r"(address));
       I2cSendByte(i,STARTFALSE,STOPFALSE); /* send to bus without start-sign,without stop-sign */

       I2cSendByte(value,STARTFALSE,STOPTRUE); /* send to bus with start-sign,with stop-sign */
       
       if( I2cOK() ) break; /* if the operation is done correctly ,exit loop */
       }

   millisecond_delay(5);
}

/*****************************************************************************
INPUTS     : index
OUTPUTS    : index value
			see start
			see write_byte
			see read_byte
			see read word
			see stop
DESCRIPTION: This function reads the video control register (stv2238).
	     It first generates an I2C start condition then it writes out
	     the video chip address. Then, the data byte is read and  the
	     the session is ended by generating a stop condition (see
	     "Bus decoder" in the data-sheet).
*****************************************************************************/
 void read_video_new( void )
{
   unsigned char i;

   unsigned char repeaterr;
    
   repeaterr = I2C_REPEAT_NUM;
   
   while(repeaterr--)
       {
       if(!I2cCheck()) continue; /* the bus can use or not */

       I2cSendByte(VIDEO_PROCESSOR_DEVICE_ADDRESS | READ_BIT , STARTTRUE , STOPFALSE);
       
       for(i=0;i<3;i++)
           {
           read_data[i]=I2cReadByte(ACKTRUE,STOPFALSE);
           }
       read_data[3]= I2cReadByte(ACKFALSE,STOPTRUE);
       
       if( I2cOK() ) break; /* if the operation is done correctly ,exit loop */
       }

}

/*****************************************************************************
INPUTS     : Video index,value
	     Byte to write into the specified register
OUTPUTS    : see start
	     see write_byte
	     see stop
DESCRIPTION: This function writes into the video processor at the address 
	     specified. It first generates an I2C start condition then it 
	     writes out the video chip address. Following this chip address, 
	     the address of the video register to write into is written out.
	     Then, the data byte is written out and the session is ended
	     by generating a stop condition.
*****************************************************************************/
void write_video(unsigned char address, unsigned char value)
{
	
   unsigned char repeaterr;
    
   repeaterr = I2C_REPEAT_NUM;
   
   while(repeaterr--)
       {
       if(!I2cCheck()) continue; /* the bus can use or not */

       I2cSendByte(VIDEO_PROCESSOR_DEVICE_ADDRESS | WRITE_BIT , STARTTRUE , STOPFALSE);
       I2cSendByte(address, STARTFALSE , STOPFALSE);
   
       I2cSendByte(value , STARTFALSE , STOPTRUE);

       if( I2cOK() ) break; /* if the operation is done correctly ,exit loop */
       }

}


/******************************************************************
WRITE_NICAM
******************************************************************/
#ifdef NICAM

void write_nicam(unsigned char address, unsigned char value)
{
   unsigned char repeaterr;
    
   repeater = I2C_REPEAT_NUM;
   
   while(repeater--)
       {
       if(!I2cCheck()) continue; /* the bus can use or not */
       
       I2cSendByte(NICAM_PROCESSOR_DEVICE_ADDRESS | WRITE_BIT , STARTTRUE , STOPFALSE);
       I2cSendByte(address , STARTFALSE , STOPFALSE );
       I2cSendByte(value , STARTFALSE , STOPTRUE );
       
       if( I2cOK() ) break; /* if the operation is done correctly ,exit loop */
       }

}
/********************************************************************

*********************************************************************/
unsigned char read_nicam (unsigned char address)
{
   unsigned char i;
   unsigned char repeaterr;
    
   repeater = I2C_REPEAT_NUM;
   
   while(repeater--)
       {
       if(!I2cCheck()) continue; /* the bus can use or not */
       
       I2cSendByte(NICAM_PROCESSOR_DEVICE_ADDRESS | WRITE_BIT , STARTTRUE , STOPFALSE);
       I2cSendByte(address , STARTFALSE , STOPFALSE );

       I2cSendByte(NICAM_PROCESSOR_DEVICE_ADDRESS | READ_BIT , STARTTRUE , STOPFALSE);
       i = I2cReadByte(ACKFALSE , STOPTRUE);
      
       if( I2cOK() ) break; /* if the operation is done correctly ,exit loop */
       }

	return i;
}


#endif

⌨️ 快捷键说明

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