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

📄 nvramaccess.c

📁 GM5621原代码
💻 C
字号:
/*
	$Workfile:   nvramaccess.c  $
	$Revision:   1.2  $
	$Date:   Aug 12 2003 09:07:38  $
*/
//******************************************************************
//
//          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: nvramacces.c
//
//  USAGE : This module contains device specific functions for I2C 
//			NVRAM chip.
//
//******************************************************************


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


//******************************************************************
//              L O C A L    D E F I N I T I O N S
//******************************************************************
	#define NVRAM_CODE			0xA0
	#define NVRAM_PAGE_SIZE		16
	#define	I2C_START_LIMIT		255
	#define NVRAM_MaxPage		7

//******************************************************************
//                          C O D E
//******************************************************************


//******************************************************************
// FUNCTION     :   NVRam_WriteControl
// USAGE        :   Write a control byte with NVRAM address
// DESCRIPTION  :   The function checks the received address and if
//                  the address does not exceed the nvram adress space
//                  sends the page address and the address within that
//                  page, preparing the nvram for the desired operation.
//
// INPUT        :   Address
// OUTPUT       :   Returns OK if successful or
//                  FAIL if not
// GLOBALS      :   NVRAM_MaxPage
// USED_REGS    :   None
//******************************************************************
BYTE NVRam_WriteControl(WORD address)
{
    BYTE Control, B_addr;

    if ((address >> 8) > NVRAM_MaxPage)
    {
        return FAIL;
    }

    Control = NVRAM_CODE | (((BYTE)(address >> 8)) << 1) | I2C_WR;
    B_addr = (BYTE)address;


    return gm_WriteI2C(Control, (BYTE *)&B_addr, 1, TRUE );

}




//******************************************************************
// FUNCTION     :   gm_NVRam_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 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
//******************************************************************
BYTE gm_NVRam_ReadBlock(WORD address, BYTE *pBlock, WORD length)
{
    BYTE Control, B_addr;

    if(length == 0)
        return FAIL;

    if(NVRam_WriteControl(address) == FAIL)	// Write current address
        return FAIL;

    Control = NVRAM_CODE | (((BYTE)(address >> 8)) << 1) | I2C_RD;
    B_addr = (BYTE)address;

    if(gm_WriteI2C(Control, (BYTE *)&B_addr, 1, TRUE ) != OK)
        return FAIL;


    return gm_ReadI2C(Control, pBlock, length, TRUE);

}

//******************************************************************
// FUNCTION     :   gm_NVRam_WriteStructure
// 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 OK
//                  writes the data block.
//
// INPUT        :   Address, Structure address, length
// OUTPUT       :   Returns 1 if successful or 0 if failed
// GLOBALS      :   None
// USED_REGS    :   None
//******************************************************************
BYTE gm_NVRam_WriteBlock(WORD address, BYTE *pBlock, WORD length)
{
    BYTE count,control;

    if(length == 0)
        return FAIL;					// length not valid if 0

    while (length > 0)
    {
        if(NVRam_WriteControl(address) == FAIL)
            return FAIL;

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

        // More than one page of data.
        // Write the max page size for the first write.
        if (length > count)
        {
            length -= count;
            address += count;
        }
        else
        {
            count = length;
            length = 0;
        }

        control = NVRAM_CODE | (((BYTE)(address >> 8)) << 1) | I2C_RD;

        if (gm_WriteI2C(control, pBlock, count, TRUE) != OK)
                return FAIL;

        pBlock += count;
    }
    return OK;
}



⌨️ 快捷键说明

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