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

📄 nvram32k.c

📁 GM5621原代码
💻 C
字号:
/*
	$Workfile:   nvram32k.c  $
	$Revision:   1.5  $
	$Date:   Feb 06 2004 16:21:02  $
*/
//******************************************************************
//
//          Copyright (C) 2001. GENESIS MICROCHIP INC.
//  All rights reserved.  No part of this program may be reproduced.
//
//  Genesis Microchip Inc., 165 Commerce Valley Dr. West
//          Thornhill, Ontario, Canada, L3T 7V8
//	Genesis Microchip Corp., 2150 Gold Street
//			Alviso, CA 95002	USA
//
//================================================================
//
//  MODULE: nvram32k.c
//
//  USAGE : This module contains device specific functions for I2C
//			32K NVRAM chip, or two byte address chips.
//			To use this driver, substitute these Read/Write functions for
//       the built in internal ROM functions by initializing the NVRAM:
//	gm_InitNVRAMLong( gmc_RomDirectory, &gmv_Size_RomDirectory,
//							nvram32k_ReadBlock, nvram32k_WriteBlock );
//			see InitNVRAM in nvram\nv_functions.c
//******************************************************************


//******************************************************************
//                  I N C L U D E    F I L E S
//******************************************************************
#include "..\inc\all.h"
#include "mem.h"

// default project doesn't need 32kBit support, so #if statement below
// is to keep the code size smaller.  If 32kBit support is needed, define
// NVRAM_32KBIT (see inc\options.h)
#if NVRAM_32KBIT

#define DEBUG_NVRAM32  1

#if  DEBUG_MSG &&	DEBUG_NVRAM32
	#define	msg(a,b)		gm_Print((const char far *)a,b)
#else
	#define msg(a,b)
#endif

//******************************************************************
//              L O C A L    D E F I N I T I O N S
//******************************************************************
#define NVRAM_CODE			0xA0
#define NVRAM_MAX_PAGE		0x0f
#define NVRAM_PAGE_SIZE		32
#define NVRAM_WRITE_DELAY  10 

static BYTE __near writeControl(WORD address);
//******************************************************************
//                          C O D E
//******************************************************************

//******************************************************************
// FUNCTION     :   nvram32k_ReadBlock
// USAGE        :   Read block data from specified address
//                  and length in NVRAM
// DESCRIPTION  :   The function checks first if the block length
//                  is not zero, if the data length does not exceed
//                  the total nvram capacity and if everything is gmd_OK
//                  reads the data block.
//
// INPUT        :   Address, Structure address, length
// OUTPUT       :   Returns 1 if successful or 0 if failed
// GLOBALS      :   NVRAM_MaxPage
// USED_REGS    :   None
//******************************************************************
gmt_RET_STAT nvram32k_ReadBlock(WORD address, BYTE *pBlock, WORD length)
{
	BYTE Control;
	BYTE RealAddr[2];

	if( (length == 0)|| ((address >> 8) > NVRAM_MAX_PAGE) )
	{
		return ERR_READ;
	}

	RealAddr[0] = (BYTE)(address >> 8);
	RealAddr[1] = (BYTE)address;

	writeControl(address);

	Control = NVRAM_CODE;

	if( gm_ReadI2cBlock(0, Control, pBlock, length, gmd_TRUE) != gmd_OK )
	{
		msg("nvram32k_ReadBlock Error!!", 0);
		return ERR_READ;
	}
	return gmd_OK;
}
//******************************************************************
// FUNCTION     :   nvram32k_WriteBlock
// USAGE        :   Write block data to specified address
//                  and length in NVRAM
// DESCRIPTION  :   The function checks first if the block length
//                  is not zero, if the data length does not exceed
//                  the total nvram capacity and if everything is gmd_OK
//                  writes the data block.
//
// INPUT        :   Address, Structure address, length
// OUTPUT       :   Returns 1 if successful or 0 if failed
// GLOBALS      :   None
// USED_REGS    :   None
//******************************************************************
gmt_RET_STAT nvram32k_WriteBlock(WORD address, BYTE *pBlock, WORD length)
{
	BYTE count,control,stop;
	BYTE buff[NVRAM_PAGE_SIZE+2];

	if((length == 0)|| ((address >> 8) > NVRAM_MAX_PAGE) )
  	{
  		return ERR_WRITE;
  	}

	while (length > 0)
	{
		buff[0] = (BYTE)(address >> 8);
		buff[1] = (BYTE)address;

		count = NVRAM_PAGE_SIZE - (address & (NVRAM_PAGE_SIZE - 1));

		// If more than one page of data, write the max page size
		// for the first write.
		if(length > count)
		{
			length -= count;
			address += count;
			stop = gmd_FALSE;
		}
		else
		{
			count = length;
			length = 0;
			stop = gmd_TRUE;
		}

		memcpy(&buff[2],pBlock,count);

		control = NVRAM_CODE;

		if (gm_WriteI2cBlock(0, control, buff, count+2, stop) != gmd_OK)
		{
			msg("nvram32k_WriteBlock Error!!", 0);
			return gmd_FAIL;
		}
		gm_Delay1ms(NVRAM_WRITE_DELAY); // wait for NVRAM to complete write.

		pBlock += count;
	}
	return gmd_OK;
}
//******************************************************************
// FUNCTION     :   writeControl
// USAGE        :   Write a control byte with 2 byte NVRAM address
//
// INPUT        :   Address
// OUTPUT       :   Returns gmd_OK if successful or
//                  gmd_FAIL if not
// GLOBALS      :   NVRAM_MaxPage
// USED_REGS    :   None
//******************************************************************
static BYTE __near writeControl(WORD address)
{
	BYTE Control;
	BYTE RealAddr[2];

	RealAddr[0] = (BYTE)(address >> 8);
	RealAddr[1] = (BYTE)address;

	Control = NVRAM_CODE;

	return gm_WriteI2cBlock(0, Control, RealAddr, 2, 0);

}

#endif //UNUSED && 0

⌨️ 快捷键说明

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