ixp425Eeprom.c
资源名称:ixp425BSP.rar [点击查看]
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:8k
源码类别:
VxWorks
开发平台:
C/C++
- /* ixp425Eeprom.c - ixp425Eeprom Philips PCF8582C-2T/03 512byte I2C EEPROM driver */
- /* Copyright 2002 Wind River Systems, Inc. */
- #include "copyright_wrs.h"
- /*
- modification history
- --------------------
- 01d,22aug02,jb Add 512 byte eeprom support
- 01c,15aug02,jb Adding byteNvRam locally to enable selective writing
- 01b,31jul02,jb Fix doc error
- 01a,05jun02,jb initial version...
- */
- /*
- DESCRIPTION
- This is the driver for the I2C EEPROM device. The EEPROM can act as a slave
- transmitter or receiver depending on whether you want to read or write to
- the device.
- This driver uses the I2C protocol and calls as outlined in ixp425I2c.c
- INCLUDES:
- ixp425I2c.h ixp425Eeprom.h
- SEE ALSO:
- .I "PCF85xxC-2 family Data Sheet"
- .I "IXP425 I2C Driver"
- */
- /* includes */
- #include "vxWorks.h"
- #include "taskLib.h"
- #include "sysLib.h"
- #include "ixp425I2c.h"
- #include "ixp425Eeprom.h"
- /* defines */
- #define IXP425_EEPROM_MAX_WRITE 8 /* 8 bytes page mode */
- #define IXP425_EEPROM_WRITE_DELAY ((sysClkRateGet()*100)/1000) /* E/W time delay = 100ms */
- /******************************************************************************
- *
- * ixp425EepromRead - Read "num" bytes from the EEPROM at "offset"
- *
- * RETURNS: The number of bytes successfully read; ERROR otherwise
- *
- */
- int ixp425EepromRead (UINT8 *buf, UINT32 num, UINT32 offset)
- {
- UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
- if ((num + offset) > IXP425_EEPROM_SIZE || buf == NULL)
- return (ERROR);
- offset &= 255;
- return(ixp425I2CReadTransfer(devAddr, buf, num, offset));
- }
- /******************************************************************************
- *
- * ixp425EepromWrite - Write "num" bytes to the EEPROM device at "offset"
- *
- * RETURNS: The number of bytes actually written; ERROR otherwise
- *
- */
- int ixp425EepromWrite (UINT8 *buf, UINT32 num, UINT32 offset)
- {
- UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
- int byteCnt = 0;
- int tmpCnt = 0;
- int currNum = num;
- if((num + offset) > IXP425_EEPROM_SIZE || buf == NULL)
- return (ERROR);
- offset &= 255;
- while(currNum > 0)
- {
- tmpCnt = IXP425_EEPROM_MAX_WRITE > currNum ? currNum : IXP425_EEPROM_MAX_WRITE;
- byteCnt = ixp425I2CWriteTransfer(devAddr, buf, tmpCnt, (UINT8)offset);
- taskDelay(IXP425_EEPROM_WRITE_DELAY);
- if(byteCnt != ERROR)
- {
- currNum -= byteCnt;
- buf += byteCnt;
- offset += byteCnt;
- }
- else
- {
- return(num);
- }
- }
- return(num);
- }
- /******************************************************************************
- *
- * ixp425EepromByteRead - Read and return one byte from the EEPROM device at "offset"
- * This function is used by sysNvRamGet()
- *
- * RETURNS: the byte read
- */
- char ixp425EepromByteRead (UINT32 offset)
- {
- UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
- char buf;
- offset &= 255;
- ixp425I2CReadTransfer(devAddr, &buf, 1, (UINT8)offset);
- return(buf);
- }
- /******************************************************************************
- *
- * ixp425EepromByteWrite - Write one byte to the EEPROM device at "offset"
- * This function is used by sysNvRamSet()
- *
- * RETURNS: N/A
- */
- void ixp425EepromByteWrite (UINT32 offset, char data)
- {
- UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
- if((offset + 1) <= IXP425_EEPROM_SIZE)
- {
- offset &= 255;
- ixp425I2CWriteTransfer(devAddr, &data, 1, (UINT8)offset);
- taskDelay(IXP425_EEPROM_WRITE_DELAY);
- }
- }
- #ifdef USE_EEPROM_STORAGE
- /* byteNvRam.c - byte-oriented generic NVRAM driver */
- /*
- modification history
- --------------------
- 01b,29oct96,wlf doc: cleanup.
- 01a,03jun96,dat written.
- */
- /*
- DESCRIPTION
- This driver is for byte-oriented non-volatile RAM (NVRAM). Typical uses
- are EEPROMs, static RAM, and any other byte-oriented device.
- There are other generic drivers for block-oriented devices like flash memory,
- and for boards that have no non-volatile memory at all.
- This is boilerplate driver code. It is to be included in source form
- into sysLib.c. Macros are used to provide the actual working elements
- while this code provides the generic logic.
- The following numeric value macros are required:
- NV_RAM_ADRS - address of first non-volatile byte
- NV_RAM_INTRVL - address interval between bytes
- NV_RAM_SIZE - total number of bytes in device
- NV_BOOT_OFFSET - Offset to first byte of boot line information
- The following procedural macros are used. If not defined by the
- specific BSP, then default procedures assuming static RAM with no
- special read/write requirements will be used.
- NV_RAM_READ(x) - Read and return one byte at offset (x).
- NV_RAM_WRITE(x,y) - Write data (y) at offset (x).
- NV_RAM_WR_ENBL - procedure to enable writing, if any
- NV_RAM_WR_DSBL - procedure to disable writing, if any
- */
- #include "config.h"
- /* default procedures assume static ram with no special r/w routines */
- #ifndef NV_RAM_WR_ENBL
- # define NV_RAM_WR_ENBL /* no write enable procedure */
- #endif /*NV_RAM_WR_ENBL*/
- #ifndef NV_RAM_WR_DSBL
- # define NV_RAM_WR_DSBL /* no write disable procedure */
- #endif /*NV_RAM_WR_DSBL*/
- #ifndef NV_RAM_READ
- # define NV_RAM_READ(x)
- (*(UCHAR *)((int)NV_RAM_ADRS + ((x) * NV_RAM_INTRVL)))
- #endif /*NV_RAM_READ*/
- #ifndef NV_RAM_WRITE
- # define NV_RAM_WRITE(x,y)
- (*(UCHAR *)((int)NV_RAM_ADRS + ((x) * NV_RAM_INTRVL)) = (y))
- #endif /*NV_RAM_WRITE*/
- /******************************************************************************
- *
- * sysNvRamGet - get the contents of non-volatile RAM
- *
- * This routine copies the contents of non-volatile memory into a specified
- * string. The string is terminated with an EOS.
- *
- * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
- *
- * SEE ALSO: sysNvRamSet()
- */
- STATUS sysNvRamGet
- (
- char *string, /* where to copy non-volatile RAM */
- int strLen, /* maximum number of bytes to copy */
- int offset /* byte offset into non-volatile RAM */
- )
- {
- offset += NV_BOOT_OFFSET; /* boot line begins at <offset> = 0 */
- if ((offset < 0)
- || (strLen < 0)
- || ((offset + strLen) > NV_RAM_SIZE))
- return (ERROR);
- while (strLen--)
- {
- *string = NV_RAM_READ (offset);
- string++, offset++;
- }
- *string = EOS;
- return (OK);
- }
- /*******************************************************************************
- *
- * sysNvRamSet - write to non-volatile RAM
- *
- * This routine copies a specified string into non-volatile RAM.
- *
- * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
- *
- * SEE ALSO: sysNvRamGet()
- */
- STATUS sysNvRamSet
- (
- char *string, /* string to be copied into non-volatile RAM */
- int strLen, /* maximum number of bytes to copy */
- int offset /* byte offset into non-volatile RAM */
- )
- {
- char chkBuffer[NV_RAM_SIZE + 1];
- STATUS result = OK;
- offset += NV_BOOT_OFFSET; /* boot line begins at <offset> = 0 */
- /* Make sure offset doesn't overrun */
- if ((offset < 0)
- || (strLen < 0)
- || ((offset + strLen) > NV_RAM_SIZE))
- return ERROR;
- /* Get current data into local buffer and compare */
- bzero(chkBuffer, NV_RAM_SIZE);
- if ( sysNvRamGet(chkBuffer, strLen, offset) != OK)
- return ERROR;
- /* If there is no difference, then just return */
- if ( bcmp(string, chkBuffer, strLen) == 0)
- return OK;
- NV_RAM_WR_ENBL;
- while (strLen--)
- {
- char data;
- data = *string; /* avoid any macro side effects */
- NV_RAM_WRITE (offset, data);
- /* verify data */
- if (NV_RAM_READ (offset) != (UCHAR)data)
- {
- result = ERROR;
- goto exit;
- }
- string++, offset++;
- }
- exit:
- NV_RAM_WR_DSBL;
- return result;
- }
- #endif /* USE_EEPROM_STORAGE */