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

VxWorks

开发平台:

C/C++

  1. /* x2212Mem.c - Xicor X2212 EEPROM non-volatile RAM library */
  2. /* Copyright 1984-1992 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01d,28jan93,caf  made boot line begin at offset 0 (SPR #1933).
  8. 01c,16oct92,caf  fixed X2212_RECALL register access, fixed <offset> usage,
  9.  added read before write in sysNvRamSet(). (SPR 1651).
  10.  defined X2212_RAM_SIZE.
  11. 01b,02sep92,ccc  renamed x2212Lib.c to x2212Mem.c.
  12. 01a,26jun92,caf  derived from version 01c of hkv3d/sysLib.c.
  13. */
  14. /*
  15. DESCRIPTION
  16. This library contains routines to manipulate Xicor X2212 EEPROM.
  17. Read and write routines are included.
  18. The Xicor X2212 EEPROM provides 128 bytes of non-volatile RAM.
  19. The macros X2212_RAM, X2212_RECALL and X2212_STORE must be defined to
  20. indicate the address of the X2212 RAM, the address of the recall operation,
  21. and the address of the store operation, respectively.
  22. */
  23. /* defines */
  24. #define X2212_RAM_SIZE 128
  25. /******************************************************************************
  26. *
  27. * sysNvRamGet - get the contents of non-volatile RAM
  28. *
  29. * This routine copies the contents of non-volatile memory into a specified
  30. * string.  The string will be terminated with an EOS.
  31. *
  32. * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
  33. *
  34. * SEE ALSO: sysNvRamSet()
  35. *
  36. * INTERNAL
  37. * If multiple tasks are calling sysNvRamSet() and sysNvRamGet(),
  38. * they should use a semaphore to ensure mutually exclusive access to EEPROM.
  39. */
  40. STATUS sysNvRamGet
  41.     (
  42.     char *string,    /* where to copy non-volatile RAM    */
  43.     int strLen,      /* maximum number of bytes to copy   */
  44.     int offset       /* byte offset into non-volatile RAM */
  45.     )
  46.     {
  47.     FAST int ix;
  48.     FAST char * pNvRam;
  49.     volatile char bitBucket;
  50.     offset += NV_BOOT_OFFSET; /* boot line begins at <offset> = 0 */
  51.     if ((offset < 0) || (strLen < 0) || ((offset + strLen) > X2212_RAM_SIZE))
  52.         return (ERROR);
  53.     pNvRam = ((char *) X2212_RAM) + (2 * offset);
  54.     /* read from NVRAM into shadow RAM */
  55.     bitBucket = *((char *) X2212_RECALL);
  56.     /* wait at least 1 microsecond for recall operation to complete */
  57.     taskDelay (1);
  58.     /*
  59.      * Construct the string from NV nibbles.  Nibbles are ordered as:
  60.      *
  61.      * <lower addresses> (high nibble)(low nibble)... <higher addresses>
  62.      *
  63.      * Odd addresses contain low order nibbles, even addresses contain
  64.      * high order nibbles.
  65.      */
  66.     for (ix = 0; ix < strLen; ix ++)
  67. {
  68. *string ++ = (*pNvRam << 4) | (*(pNvRam + 1) & 0xf);
  69. pNvRam += 2;
  70. }
  71.     *string = EOS;
  72.     return (OK);
  73.     }
  74. /*******************************************************************************
  75. *
  76. * sysNvRamSet - write to non-volatile RAM
  77. *
  78. * This routine copies a specified string into non-volatile RAM.
  79. *
  80. * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
  81. *
  82. * SEE ALSO: sysNvRamGet()
  83. *
  84. * INTERNAL
  85. * The XICOR X2212 EEPROM is rated for 10,000 store cycles, minimum.
  86. * If multiple tasks are calling sysNvRamSet() and sysNvRamGet(),
  87. * they should use a semaphore to ensure mutually exclusive access to EEPROM.
  88. */
  89. STATUS sysNvRamSet
  90.     (
  91.     char *string,     /* string to be copied into non-volatile RAM */
  92.     int strLen,       /* maximum number of bytes to copy           */
  93.     int offset        /* byte offset into non-volatile RAM         */
  94.     )
  95.     {
  96.     FAST int ix;
  97.     FAST char * pNvRam;
  98.     volatile char bitBucket;
  99.     offset += NV_BOOT_OFFSET; /* boot line begins at <offset> = 0 */
  100.     if ((offset < 0) || (strLen < 0) || ((offset + strLen) > X2212_RAM_SIZE))
  101.         return (ERROR);
  102.     pNvRam = ((char *) X2212_RAM) + (2 * offset);
  103.     /* read from NVRAM into shadow RAM */
  104.     bitBucket = *((char *) X2212_RECALL);
  105.     /* wait at least 1 microsecond for recall operation to complete */
  106.     taskDelay (1);
  107.     /*
  108.      * Construct the NV nibbles from string.  Nibbles are ordered as:
  109.      *
  110.      * <lower addresses> (high nibble)(low nibble)... <higher addresses>
  111.      *
  112.      * Odd addresses contain low order nibbles, even addresses contain
  113.      * high order nibbles.
  114.      */
  115.     for (ix = 0; ix < strLen; ix ++)
  116. {
  117. *pNvRam ++ = (string [ix] >> 4) & 0xf;
  118. *pNvRam ++ = string [ix] & 0xf;
  119. }
  120.     /* write from shadow RAM to NVRAM */
  121.     (void) vxTas ((char *) X2212_STORE);
  122.     /* wait at least 10 milliseconds for store operation to complete */
  123.     taskDelay (sysClkRateGet() / 100 + 1);
  124.     return (OK);
  125.     }