ixp425Eeprom.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:8k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* ixp425Eeprom.c - ixp425Eeprom Philips PCF8582C-2T/03 512byte I2C EEPROM driver */
  2. /* Copyright 2002 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01d,22aug02,jb  Add 512 byte eeprom support
  8. 01c,15aug02,jb  Adding byteNvRam locally to enable selective writing
  9. 01b,31jul02,jb  Fix doc error
  10. 01a,05jun02,jb  initial version...
  11. */
  12. /*
  13. DESCRIPTION
  14. This is the driver for the I2C EEPROM device. The EEPROM can act as a slave
  15. transmitter or receiver depending on whether you want to read or write to
  16. the device.
  17. This driver uses the I2C protocol and calls as outlined in ixp425I2c.c 
  18. INCLUDES:
  19. ixp425I2c.h ixp425Eeprom.h
  20. SEE ALSO:
  21. .I "PCF85xxC-2 family Data Sheet"
  22. .I "IXP425 I2C Driver"
  23. */
  24. /* includes */
  25. #include "vxWorks.h"
  26. #include "taskLib.h"
  27. #include "sysLib.h"
  28. #include "ixp425I2c.h"
  29. #include "ixp425Eeprom.h"
  30. /* defines */
  31. #define IXP425_EEPROM_MAX_WRITE 8 /* 8 bytes page mode */
  32. #define IXP425_EEPROM_WRITE_DELAY ((sysClkRateGet()*100)/1000) /* E/W time delay = 100ms */
  33. /******************************************************************************
  34. *
  35. * ixp425EepromRead - Read "num" bytes from the EEPROM at "offset"
  36. *
  37. * RETURNS: The number of bytes successfully read; ERROR otherwise
  38. *    
  39. */
  40. int ixp425EepromRead (UINT8 *buf, UINT32 num, UINT32 offset)
  41.     {
  42.     UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
  43.     if ((num + offset) > IXP425_EEPROM_SIZE || buf == NULL)
  44.         return (ERROR);
  45.     offset &= 255;
  46.     return(ixp425I2CReadTransfer(devAddr, buf, num, offset));
  47.     }
  48. /******************************************************************************
  49. *
  50. * ixp425EepromWrite - Write "num" bytes to the EEPROM device at "offset"
  51. *
  52. * RETURNS: The number of bytes actually written; ERROR otherwise
  53. *    
  54. */
  55. int ixp425EepromWrite (UINT8 *buf, UINT32 num, UINT32 offset)
  56.     {
  57.     UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
  58.     int byteCnt = 0;
  59.     int tmpCnt = 0;
  60.     int currNum = num;
  61.     if((num + offset) > IXP425_EEPROM_SIZE || buf == NULL)
  62.         return (ERROR);
  63.     offset &= 255;
  64.     while(currNum > 0)
  65.         {
  66. tmpCnt = IXP425_EEPROM_MAX_WRITE > currNum ? currNum : IXP425_EEPROM_MAX_WRITE;
  67. byteCnt = ixp425I2CWriteTransfer(devAddr, buf, tmpCnt, (UINT8)offset);
  68. taskDelay(IXP425_EEPROM_WRITE_DELAY);
  69. if(byteCnt != ERROR)
  70.     {
  71.     currNum -= byteCnt;
  72.     buf += byteCnt;
  73.     offset += byteCnt;
  74.     }
  75. else
  76.     {
  77.     return(num);
  78.     }
  79. }
  80.     return(num);
  81.     }
  82. /******************************************************************************
  83. *
  84. * ixp425EepromByteRead - Read and return one byte from the EEPROM device at "offset"
  85. *  This function is used by sysNvRamGet()
  86. *
  87. * RETURNS: the byte read
  88. */
  89. char ixp425EepromByteRead (UINT32 offset)
  90.     {
  91.     UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
  92.     char buf;
  93.     offset &= 255;
  94.     ixp425I2CReadTransfer(devAddr, &buf, 1, (UINT8)offset);
  95.     return(buf);
  96.     }
  97. /******************************************************************************
  98. *
  99. * ixp425EepromByteWrite - Write one byte to the EEPROM device at "offset"
  100. *   This function is used by sysNvRamSet()
  101. *
  102. * RETURNS: N/A
  103. */
  104. void ixp425EepromByteWrite (UINT32 offset, char data)
  105.     {
  106.     UINT8 devAddr = IXP425_EEPROM_ADDR | ((offset/256)<< 1);
  107.     if((offset + 1) <= IXP425_EEPROM_SIZE)
  108.         {
  109.         offset &= 255;
  110.         ixp425I2CWriteTransfer(devAddr, &data, 1, (UINT8)offset);
  111.         taskDelay(IXP425_EEPROM_WRITE_DELAY);
  112. }
  113.     }
  114. #ifdef USE_EEPROM_STORAGE
  115. /* byteNvRam.c - byte-oriented generic NVRAM driver */
  116. /*
  117. modification history
  118. --------------------
  119. 01b,29oct96,wlf  doc: cleanup.
  120. 01a,03jun96,dat  written.
  121. */
  122. /*
  123. DESCRIPTION
  124. This driver is for byte-oriented non-volatile RAM (NVRAM).  Typical uses
  125. are EEPROMs, static RAM, and any other byte-oriented device.
  126. There are other generic drivers for block-oriented devices like flash memory,
  127. and for boards that have no non-volatile memory at all.
  128. This is boilerplate driver code.  It is to be included in source form
  129. into sysLib.c.  Macros are used to provide the actual working elements
  130. while this code provides the generic logic.
  131. The following numeric value macros are required:
  132.   NV_RAM_ADRS   - address of first non-volatile byte
  133.   NV_RAM_INTRVL   - address interval between bytes
  134.   NV_RAM_SIZE   - total number of bytes in device
  135.   NV_BOOT_OFFSET  - Offset to first byte of boot line information
  136. The following procedural macros are used.  If not defined by the
  137. specific BSP, then default procedures assuming static RAM with no
  138. special read/write requirements will be used.
  139.   NV_RAM_READ(x)    - Read and return one byte at offset (x).
  140.   NV_RAM_WRITE(x,y) - Write data (y) at offset (x).
  141.   NV_RAM_WR_ENBL    - procedure to enable writing, if any
  142.   NV_RAM_WR_DSBL    - procedure to disable writing, if any
  143. */
  144. #include "config.h"
  145. /* default procedures assume static ram with no special r/w routines */
  146. #ifndef NV_RAM_WR_ENBL
  147. #   define NV_RAM_WR_ENBL /* no write enable procedure */
  148. #endif /*NV_RAM_WR_ENBL*/
  149. #ifndef NV_RAM_WR_DSBL
  150. #   define NV_RAM_WR_DSBL /* no write disable procedure */
  151. #endif /*NV_RAM_WR_DSBL*/
  152. #ifndef NV_RAM_READ
  153. #   define NV_RAM_READ(x) 
  154. (*(UCHAR *)((int)NV_RAM_ADRS + ((x) * NV_RAM_INTRVL)))
  155. #endif /*NV_RAM_READ*/
  156. #ifndef NV_RAM_WRITE
  157. #   define NV_RAM_WRITE(x,y) 
  158. (*(UCHAR *)((int)NV_RAM_ADRS + ((x) * NV_RAM_INTRVL)) = (y))
  159. #endif /*NV_RAM_WRITE*/
  160. /******************************************************************************
  161. *
  162. * sysNvRamGet - get the contents of non-volatile RAM
  163. *
  164. * This routine copies the contents of non-volatile memory into a specified
  165. * string.  The string is terminated with an EOS.
  166. *
  167. * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
  168. *
  169. * SEE ALSO: sysNvRamSet()
  170. */
  171. STATUS sysNvRamGet
  172.     (
  173.     char *string,    /* where to copy non-volatile RAM    */
  174.     int strLen,      /* maximum number of bytes to copy   */
  175.     int offset       /* byte offset into non-volatile RAM */
  176.     )
  177.     {
  178.     
  179.     offset += NV_BOOT_OFFSET;   /* boot line begins at <offset> = 0 */
  180.     if ((offset < 0)
  181.      || (strLen < 0)
  182.      || ((offset + strLen) > NV_RAM_SIZE))
  183.         return (ERROR);
  184.     while (strLen--)
  185. {
  186. *string = NV_RAM_READ (offset);
  187. string++, offset++;
  188. }
  189.     *string = EOS;
  190.     return (OK);
  191.     }
  192. /*******************************************************************************
  193. *
  194. * sysNvRamSet - write to non-volatile RAM
  195. *
  196. * This routine copies a specified string into non-volatile RAM.
  197. *
  198. * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
  199. *
  200. * SEE ALSO: sysNvRamGet()
  201. */
  202. STATUS sysNvRamSet
  203.     (
  204.     char *string,     /* string to be copied into non-volatile RAM */
  205.     int strLen,       /* maximum number of bytes to copy           */
  206.     int offset        /* byte offset into non-volatile RAM         */
  207.     )
  208.     {
  209.     char chkBuffer[NV_RAM_SIZE + 1];
  210.     STATUS result = OK;
  211.     offset += NV_BOOT_OFFSET;   /* boot line begins at <offset> = 0 */
  212.     /* Make sure offset doesn't overrun */
  213.     if ((offset < 0)
  214.      || (strLen < 0)
  215.      || ((offset + strLen) > NV_RAM_SIZE))
  216.         return ERROR;
  217.     /* Get current data into local buffer and compare */
  218.     bzero(chkBuffer, NV_RAM_SIZE);
  219.     if ( sysNvRamGet(chkBuffer, strLen, offset) != OK)
  220.         return ERROR;
  221.     /* If there is no difference, then just return */
  222.     if ( bcmp(string, chkBuffer, strLen) == 0)
  223.         return OK;
  224.     NV_RAM_WR_ENBL;
  225.     while (strLen--)
  226. {
  227. char data;
  228. data = *string; /* avoid any macro side effects */
  229. NV_RAM_WRITE (offset, data);
  230. /* verify data */
  231. if (NV_RAM_READ (offset) != (UCHAR)data)
  232.     {
  233.     result = ERROR;
  234.     goto exit;
  235.     }
  236. string++, offset++;
  237. }
  238. exit:
  239.     NV_RAM_WR_DSBL;
  240.     return result;
  241.     }
  242. #endif /* USE_EEPROM_STORAGE */