📄 eeprom.c
字号:
/* eeprom.c - DCWCEC VME-182/183 EEPROM Library *//********************************************************************** * * Copyright (c) 2003-2005, Dy 4 Systems All rights reserved. * This Source Code is the Property of Dy 4 Systems Inc. and can * only be used in accordance with Source Code License * Agreement of Dy 4 Systems Inc. dba (doing business as) * CURTISS-WRIGHT CONTROLS EMBEDDED COMPUTING, "CWCEC". * **********************************************************************//*modification history-------------------- 01i,12dec05,tis rename Discovery_II directory to Discovery_III CR#12858.01h,22jul05,tis -add support for IPM EEPROM Read/Write for the 183 boards -update the IPM EEPROM API to support access to the both IPM eeprom banks CR#3353 -added bank boundary checks to the IPM EEPROM Read/Write routines -replaced timout hardcoded value with the macro MAX_TIMEOUT -renamed the macro I2C_MAIN_EEPROM_BANK1_ADDR to I2C_EEPROM_BANK1_ADDR -add the include file eeprom.h01g,07jun05,tis update eepromWrite to restrict access to the top 256 bytes in CCA-146 boards01f,18may05,tis -add support to read/write the eeprom's top bank CR#3532 -add the function sysSerialNumberGet() to read CCA-146 SN. -introduced new EEPROM macros01e,09may05,tis add support for CCA-14601d,18dec04,tis add the function eepromInit()01c,01nov04,tis add support for CCA-14501b,13apr04,mb modified each read/write function to allow for multiple retries01a,15apr03,dy4 created.*/#include "stdio.h"#include "stdlib.h"#include "h/drv/dy4/boardName.h"#ifdef VME_182#include "dy4182.h"#endif#ifdef CCA_145#include "cca145.h"#endif#ifdef CCA_146#include "cca146.h"#endif#ifdef VME_183#include "cwv183.h"#include "h/drv/mem/eeprom.h"#endif#ifdef VME_183#include "h/drv/discovery_III/i2c/gtI2c.h"#else#include "h/drv/discovery_II/i2c/gtI2c.h"#endif#ifdef INCLUDE_DY4DEBUG#include "h/drv/dy4Debug/dy4Debug.h"#else#define DEBUGP(a) /* normal debug output */#define DEBUGMP(a) /* message output */#define DEBUGEP(a) printf a /* error output */#define DEBUGRP(a) /* resource output */#define DEBUGIP(a) /* extra info output */#define DEBUGXP(a) /* temporary messages */#define DEBUGINTDP(a) /* interrupt level normal debug output */#define DEBUGINTMP(a) /* interrupt level message output */#define DEBUGINTEP(a) /* interrupt level error output */#define DEBUGINTIP(a) /* interrupt level extra info output */#define DEBUGINTXP(a) /* interrupt level temporary messages */#endifextern STATUS taskDelay ( int ticks);#define MAX_TIMEOUT 10/**************************************************************************** eepromRead - Read from EEPROM** DESCRIPTION: This function reads from the EEPROM at a certain offset and* writes the read data to the passed buffer.* Note: User can only read up to 16bytes at a time*** INPUT:* unsigned int offset: eeprom offset min 0, max 511* unsigned char *data: read buffer* unsigned int byteCount: number of bytes to read (min 1 , max 16)* RETURN: BSP_OK or * BSP_ERROR: -if the passed byteCount is more than 16 bytes.* -if the passed offset is out off range* -if eeprom read fails.******************************************************************************/BSP_STATUS eepromRead(unsigned int offset, unsigned char *data, unsigned int byteCount){ unsigned int status; unsigned int timeoutValue = 0; DEBUGMP(("eeprom read\n")); if (byteCount > EEPROM_PAGE_SIZE) { DEBUGEP(("eepromRead: cant read more than 16 bytes at a time.\n")); return(BSP_ERROR); } if ((offset >= EEPROM_BANK0_OFFSET) && ( offset <= EEPROM_BANK0_MAX_OFFSET)) { status = dy4I2cMasterRead(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR, offset, data, byteCount); timeoutValue++; while (status != STAT_I2C_OK) { if(timeoutValue >= MAX_TIMEOUT) { DEBUGEP(("eepromRead: exceeded read retries.\n")); return BSP_ERROR; } DEBUGMP(("eepromRead: retrying eeprom read...\n")); taskDelay(1); status = dy4I2cMasterRead(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR, offset, data, byteCount); timeoutValue++; } return BSP_OK; } else if ((offset >= EEPROM_BANK1_OFFSET) && ( offset <= EEPROM_BANK1_MAX_OFFSET)) { status = dy4I2cMasterRead(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR + I2C_EEPROM_BANK1_ADDR, offset - EEPROM_BANK1_OFFSET, data, byteCount); timeoutValue++; while (status != STAT_I2C_OK) { if(timeoutValue >= MAX_TIMEOUT) { DEBUGEP(("eepromRead: exceeded read retries.\n")); return BSP_ERROR; } DEBUGMP(("eepromRead: retrying eeprom read...\n")); taskDelay(1); status = dy4I2cMasterRead(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR + I2C_EEPROM_BANK1_ADDR, offset - EEPROM_BANK1_OFFSET, data, byteCount); timeoutValue++; } return BSP_OK; } else { DEBUGEP(("eepromRead: offset out of range\n")); return (BSP_ERROR); }}/**************************************************************************** eepromWrite - Write to EEPROM** DESCRIPTION: This function writes the contents of the passed buffer to the EEPROM* at a certain offset.* Note: User can only write up to 16bytes at a time*** INPUT:* unsigned int offset: eeprom offset min 0, max 511 (min 0, max 255 for CCA-146 boards)* unsigned char *data: write buffer* unsigned int byteCount: number of bytes to write (min 1 , max 16)* RETURN: BSP_OK or * BSP_ERROR: -if the passed byteCount is more than 16 bytes.* -if the passed offset is out off range* -if eeprom write fails.* Note CCA-146 only: This function can not write to the EEPROM offsets 256 -511 because the top* 256 bytes are reserved for storing the board's serial number.******************************************************************************/BSP_STATUS eepromWrite(unsigned int offset, unsigned char *data, unsigned int byteCount){ unsigned int status; unsigned int timeoutValue = 0; DEBUGMP(("eeprom write\n")); if (byteCount > EEPROM_PAGE_SIZE) { DEBUGEP(("eepromWrite: cant write more than 16 bytes at a time.\n")); return(BSP_ERROR); } if ((offset >= EEPROM_BANK0_OFFSET) && ( offset <= EEPROM_BANK0_MAX_OFFSET)) { status = dy4I2cMasterWrite(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR, offset, data, byteCount); timeoutValue++; while (status != STAT_I2C_OK) { if(timeoutValue >= MAX_TIMEOUT) { DEBUGEP(("eepromWrite: exceeded write retries.\n")); return BSP_ERROR; } DEBUGMP(("eepromWrite: eeprom write retry...\n")); taskDelay(1); status = dy4I2cMasterWrite(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR, offset, data, byteCount); timeoutValue++; } return BSP_OK; } else if ((offset >= EEPROM_BANK1_OFFSET) && ( offset <= EEPROM_BANK1_MAX_OFFSET)) {#ifdef CCA_146 /* * CCA-146 Only. * The Top 256 bytes of the EEPROM are reserved for Serial Number and cant be written */ DEBUGEP(("eepromRead: Can not write to the top 256 bytes of the EEPROM\n")); return (BSP_ERROR);#else status = dy4I2cMasterWrite(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR + I2C_EEPROM_BANK1_ADDR, offset - EEPROM_BANK1_OFFSET, data, byteCount); timeoutValue++; while (status != STAT_I2C_OK) { if(timeoutValue >= MAX_TIMEOUT) { DEBUGEP(("eepromWrite: exceeded write retries.\n")); return BSP_ERROR; } DEBUGMP(("eepromWrite: eeprom write retry...\n")); taskDelay(1); status = dy4I2cMasterWrite(I2C_EEPROM, I2C_MAIN_EEPROM_ADDR + I2C_EEPROM_BANK1_ADDR, offset - EEPROM_BANK1_OFFSET, data, byteCount); timeoutValue++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -