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

📄 teeprom.c.svn-base

📁 最新火热的CX32 源代码
💻 SVN-BASE
字号:
/************************************************************************/
/*																		*/
/* This file is related to EEPROM soft I2C function                     */
/* Application			: DPTV								            */
/* MCU					: TMP91CU27UG-9999    					        */
/* Main Clock			: fc=27MHz								        */
/*																		*/
/* Copyright (C) Trident Multimedia Technologies (Shanghai) Co., Ltd.   */
/*                    2005  All rights reserved.                        */
/* Revision:                                                            */
/* 12/10/2005     Created by Lewis, Archie								*/
/************************************************************************/


#include "tdefs.h"
#include "ti2c.h"
#include "TEEPROM.h"
#include "TI2CHIGH.h"
#include "tdptv.h"//dy 0118
#include "TVDebug.h"//dy 0118

#define _DECLARE_FASTI2C_2_
#include "tfasti2c.h"

static Void tdI2CSCL2(Byte state);
static Void tdI2CSDA2(Byte state);
static Byte tdI2CReadSDA2(Void);
static Byte tdI2CReadSCL2(Void);

extern Byte tdReadDefaultByte(Word wAddr);

#undef  tdI2CSCL
#define tdI2CSCL(bState) tdI2CSCL2(bState)

#undef  tdI2CSDA
#define tdI2CSDA(bState) tdI2CSDA2(bState) 

#undef  tdI2CReadSDA
#define tdI2CReadSDA() tdI2CReadSDA2()

#undef  tdI2CReadSCL
#define tdI2CReadSCL() tdI2CReadSCL2()

#undef  tdI2CEnableSDARead
#define tdI2CEnableSDARead()  SI2C_SDA_INMODE

#undef  tdI2CEnableSDAWrite
#define tdI2CEnableSDAWrite() SI2C_SDA_OUTMODE

#include "ti2cmid.c"

#define _TRY_TIMES_   5

/* the value will be changed according to the project */
#define s_ucSlaveAddr   0xA0

/**
 * this function is for EEPROM I2C speed modification
 */
static Void I2CDelay2(Void)
{
 //dy 051130  BYTE uc = 0x10;
 BYTE uc = 0x03;
  while(uc --);
}

/**
 * this function is for EEPROM I2C SCL write
 */
static Void tdI2CSCL2(Byte state)
{
    I2CDelay2();
    if(state)
    {
        SI2C_SCL_SET;
    }
    else
    {
        SI2C_SCL_CLR;
    }
}

/**
 * this function is for EEPROM I2C SDA write
 */
static Void tdI2CSDA2(Byte state) 
{
    I2CDelay2();
    if(state)
    {
        SI2C_SDA_SET;
    }
    else
    {
        SI2C_SDA_CLR;
    }
}

/**
 * this function is for EEPROM I2C SDA read
 */
static Byte tdI2CReadSDA2(Void)
{
	SI2C_SDA_GET;
	return SI2C_SDA_STATUS;
}

/**
 * this function is for EEPROM I2C SCL read
 */
static Byte tdI2CReadSCL2(Void)
{
	SI2C_SCL_GET;
	return SI2C_SCL_STATUS;
}

/**
 * this function is for EEPROM I2C open
 */
static Void tdEnableEEPROMI2C(Void)
{
    SI2C_SDA_SET;
    SI2C_SCL_SET;
    SI2C_ALL_OUTMODE;
}

/**
 * this function is for EEPROM I2C close
 */
static Void tdDisableEEPROMI2C(Void)
{
    SI2C_ALL_INMODE;
//  tdI2COpen(0);
}

static Byte tdSendEEPROMStart(Word wSubAddress)
{
    Byte Loop = 0;
    tdStart();
    while (!tdI2CSendByte((Byte)(s_ucSlaveAddr & 0xfe)))
    {
        Byte Loop2;
        Loop++;
		//tvDebugPrint("Error!\n");
        if (Loop == 0xFF) return _FALSE_;
        for (Loop2 = 0; Loop2 < 0xFF; Loop2++)
        {
            Loop2--; Loop2++;
        }
        tdStart();
    }
    if (tdI2CSendByte((Byte)(wSubAddress >> 8)) && tdI2CSendByte((Byte)wSubAddress))
        return _TRUE_;
    return _FALSE_;
}

Void tdEEPROMWrite(Word wSubAddress, RPByte rpBuf, Word wLength)
{
    IWord wLength2      = wLength;
    Idata RPByte rpBuf2 = rpBuf;
    IByte ucTryTimes    = _TRY_TIMES_;
    IByte ucPageOffset;

	if (!t_TOE.bEEPROMOnline)//dy 0118 add, if eep offline, do nothing
		return;

    tdEnableEEPROMI2C();
#ifdef  E2PROM_PIN_PROTECTOFF    
    E2PROM_PIN_PROTECTOFF;
#endif
    while (ucTryTimes--)
    {
        wLength  = wLength2;
        rpBuf    = rpBuf2;
        
        ucPageOffset = wSubAddress % 32;
        if(ucPageOffset != 0 && (ucPageOffset +  wLength > 32)) /* Across bonduary. Should write page twice */
        {
            if (tdSendEEPROMStart(wSubAddress))
            {
                while (ucPageOffset++ < 32)
                    if(!tdI2CSendByte(*rpBuf++)) goto quit;
                ucPageOffset = 32 - wSubAddress % 32;
                wSubAddress += ucPageOffset;
                wLength     -= ucPageOffset;
                tdStop();
            }
            else
                goto quit;
        }
        if (wLength > 32)
        {    
            while (wLength > 32)
            {
                Byte ucLen = 32;
                if (tdSendEEPROMStart(wSubAddress))
                {
                    while (ucLen--)
                        if(!tdI2CSendByte(*rpBuf++)) goto quit;
                    wLength     -= 32;
                    wSubAddress += 32;
                    tdStop();
                }
                else
                    goto quit;
            }
        }
        if (tdSendEEPROMStart(wSubAddress))
            while (wLength--)
                if(!tdI2CSendByte(*rpBuf++)) goto quit;
                
quit:
        tdStop();
        if (wLength == 0xFFFF)
            break;
    }
#ifdef E2PROM_PIN_PROTECTON
	E2PROM_PIN_PROTECTON;
#endif
    tdDisableEEPROMI2C();
}

#ifdef _DIFF_ROM_RAM_PTR_
Void tdEEPROMWriteGRam(Word wSubAddress, GPByte gpBuf, Word wLength)
{
    IWord wLength2      = wLength;
    Idata GPByte gpBuf2 = gpBuf;
    IByte ucTryTimes    = _TRY_TIMES_;
    IByte ucPageOffset;

    tdEnableEEPROMI2C();
#ifdef  E2PROM_PIN_PROTECTOFF    
    E2PROM_PIN_PROTECTOFF;
#endif
    while (ucTryTimes--)
    {
        wLength  = wLength2;
        gpBuf    = gpBuf2;
        
        ucPageOffset = wSubAddress % 32;
        if(ucPageOffset != 0 && (ucPageOffset +  wLength > 32)) /* Across bonduary. Should write page twice */
        {
            if (tdSendEEPROMStart(wSubAddress))
            {
                while (ucPageOffset++ < 32)
                    if(!tdI2CSendByte(*rpBuf++)) goto quit;
                ucPageOffset = 32 - wSubAddress % 32;
                wSubAddress += ucPageOffset;
                wLength     -= ucPageOffset;
                tdStop();
            } else
                goto quit;
        }

        if (wLength > 32)
        {    
            while (wLength > 32)
            {
                Byte ucLen = 32;
                if (tdSendEEPROMStart(wSubAddress))
                {
                    while (ucLen--)
                        if(!tdI2CSendByte(*gpBuf++)) goto quit;
                    wLength -= 32;
                    tdStop();
                } else
                    goto quit;
            }
        }
    
        if (tdSendEEPROMStart(wSubAddress))
            while (wLength--)
                if(!tdI2CSendByte(*gpBuf++)) break;

quit:
        tdStop();
        if (wLength == 0xFFFF)
            break;
    }
#ifdef E2PROM_PIN_PROTECTON
       E2PROM_PIN_PROTECTON;
#endif
    tdDisableEEPROMI2C();
}
#endif

Void tdEEPROMRead(Word wSubAddress, GPByte gpBuf, Word wLength)
{
    IWord wLength2     = wLength;
    Idata GPByte gpBuf2 = gpBuf;
    IByte ucTryTimes    = _TRY_TIMES_;

	if (t_TOE.bEEPROMOnline)//dy 0118 add, if eep offline, read from default value
	{
	    tdEnableEEPROMI2C();

	    while (ucTryTimes--)
	    {
	        wLength  = wLength2;
	        gpBuf    = gpBuf2;
	        if (tdSendEEPROMStart(wSubAddress))
	        {
	            tdStart();
	            if(tdI2CSendByte((Byte)(s_ucSlaveAddr | 0x01)))
	            {
	                while (wLength)
	                {
	                    *gpBuf++ = tdI2CReceiveByte();
	                    /*send ACK*/
	                    if(--wLength)
	                        tdACK();
	                }
	                /*send No ACK*/
	                tdNOACK();
	                tdStop();
	                break;
	            } 
	        }
	        
	        tdStop();
	    }
	    tdDisableEEPROMI2C();
	}
	else
	{
		tvDebugPrint2("[default read] %x %x\n", wSubAddress, wLength);
		while(wLength--)
		{
			*gpBuf = tdReadDefaultByte(wSubAddress++);
			//tvDebugPrint1(" %x   ",*gpBuf);
			gpBuf++;
		}
		//tvDebugPrint("\n");
	}
}

Void tdEEPROMWriteByte(Word wSubAddress, Byte ucValue)
{
    IByte ucTryTimes    = _TRY_TIMES_;

	if (!t_TOE.bEEPROMOnline)//dy 0118 add, if eep offline, do nothing
		return;

    tdEnableEEPROMI2C();
#ifdef  E2PROM_PIN_PROTECTOFF    
    E2PROM_PIN_PROTECTOFF;
#endif
    while (ucTryTimes--)
    {
        if (tdSendEEPROMStart(wSubAddress))
            if (tdI2CSendByte(ucValue))
            {
                tdStop();
                break;
            }
        tdStop();
    }
#ifdef E2PROM_PIN_PROTECTON
	E2PROM_PIN_PROTECTON;
#endif
    tdDisableEEPROMI2C();
}

Byte tdEEPROMReadByte(Word wSubAddress)
{
    IByte ucTryTimes = _TRY_TIMES_;
    IByte ucValue    = 0xFF;

	if (t_TOE.bEEPROMOnline)//dy 0118 add, if eep offline, read from default value
	{
	    tdEnableEEPROMI2C();

	    while (ucTryTimes--)
	    {
	        if (tdSendEEPROMStart(wSubAddress))
	        {
	            tdStart();
	            if(tdI2CSendByte((Byte)(s_ucSlaveAddr | 0x01)))
	            {
	                ucValue = tdI2CReceiveByte();
	                /*send No ACK*/
	                tdNOACK();
	                tdStop();
	                break;
	            }
	        }
	        tdStop();
	    }

	    tdDisableEEPROMI2C();
	}
	else
	{
		tvDebugPrint1("[default read byte] %x\n", wSubAddress);
		ucValue = tdReadDefaultByte(wSubAddress);
	}
    
    return ucValue;
}

Bool tdEEPROMFindDevice(Void)
{

	IByte ucTryTimes = _TRY_TIMES_;

	tdEnableEEPROMI2C();

	while (ucTryTimes--)
	{
		if (tdSendEEPROMStart(0x0000))
		{
			tdStop();
			break;   // OK, return;
		}
		tdStop();
		// wrong, try again
	}
		
	tdDisableEEPROMI2C();
	
	return ucTryTimes != 0xFF;

}

⌨️ 快捷键说明

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