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

VxWorks

开发平台:

C/C++

  1. /* iFlashMem.c - Intel 28F256A Flash Memory library */
  2. /* Copyright 1984-1992 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01b,28jan93,caf  made boot line begin at offset 0 (SPR #1933).
  8. 01a,30oct92,ccc  created.
  9. */
  10. /*
  11. DESCRIPTION
  12. This library contains routines to manipulate Intel 28F256A Flash memory.
  13. Read and write routines are included.
  14. The Intel 28F256A provides 32K bytes of non-volatile RAM, thus providing
  15. 64K bytes with 2 devices.
  16. The macros NV_RAM_ADRS and NV_RAM_SIZE must be defined to indicate the
  17. address and size of the Flash memory.
  18. INTERNAL
  19. The routine sysFlashDelay() is to delay 1uS.  It has not been timed, and
  20. should be timed because faster boards may have a problem.
  21. */
  22. #include "drv/mem/iFlash.h"
  23. #include "memLib.h"
  24. #include "stdlib.h"
  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.
  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.     offset += NV_BOOT_OFFSET;   /* boot line begins at <offset> = 0 */
  48.     if ((offset < 0) || (strLen < 0) || ((offset + strLen) > NV_RAM_SIZE))
  49.         return (ERROR);
  50.     bcopyBytes (NV_RAM_ADRS + offset, string, strLen);
  51.     string [strLen] = EOS;
  52.     return (OK);
  53.     }
  54. /******************************************************************************
  55. *
  56. * sysFlashDelay - delay for 1uS
  57. *
  58. * RETURNS: N/A
  59. *
  60. * NOMANUAL
  61. */
  62. void sysFlashDelay
  63.     (
  64.     int delayCount /* number of uS to delay */
  65.     )
  66.     {
  67.     int ix;
  68.     volatile int bump = 0;
  69.     for (ix = 0; ix < delayCount; ix++)
  70. {
  71. bump++;
  72. }
  73.     }
  74. /******************************************************************************
  75. *
  76. * sysFlashErase - erase the Intel 28F256A
  77. *
  78. * RETURNS: N/A
  79. *
  80. * NOMANUAL
  81. */
  82. void sysFlashErase (void)
  83.     {
  84.     int address;
  85.     int ix = 0;
  86.     volatile unsigned short * pFlashAdrs;
  87.     for (address = 0; address < NV_RAM_SIZE; address += 2)
  88. {
  89. pFlashAdrs = (unsigned short *) (NV_RAM_ADRS + address);
  90. for (ix = 0; ix < 25; ix++)
  91.     {
  92.     *pFlashAdrs = FLASH_CMD_PROG_SETUP;  /* write setup */
  93.     *pFlashAdrs = 0x0000;   /* data to write */
  94.     sysFlashDelay (10);  /* wait for write */
  95.     *pFlashAdrs = FLASH_CMD_PROG_VERIFY; /* verify command */
  96.     sysFlashDelay (6);  /* wait for verify */
  97.     if (*pFlashAdrs == 0) /* done? */
  98. break;
  99.     }
  100. }
  101.     /* now it is all zeros - time to erase */
  102.     address = 0;
  103.     pFlashAdrs = (unsigned short *) NV_RAM_ADRS;
  104.     for (ix = 0; ix < 3000; ix++)
  105. {
  106. *pFlashAdrs = FLASH_CMD_ERASE_SETUP; /* erase setup */
  107. *pFlashAdrs = FLASH_CMD_ERASE; /* erase */
  108. sysFlashDelay (1000);
  109. while (address < NV_RAM_SIZE)
  110.     {
  111.     *pFlashAdrs = FLASH_CMD_ERASE_VERIFY;  /* erase verify */
  112.     sysFlashDelay (6); /* wait for verify */
  113.     if (*pFlashAdrs != 0xffff) /* erased ? */
  114. break;
  115.     address += 2; /* next address */
  116.     pFlashAdrs = (unsigned short *) (NV_RAM_ADRS + address);
  117.     }
  118. if (address == NV_RAM_SIZE)
  119.     break; /* done */
  120. }
  121.     pFlashAdrs = (unsigned short *) NV_RAM_ADRS;
  122.     *pFlashAdrs = FLASH_CMD_RESET;
  123.     *pFlashAdrs = FLASH_CMD_RESET;
  124.     *pFlashAdrs = FLASH_CMD_READ_MEM;
  125.     }
  126. /******************************************************************************
  127. *
  128. * sysFlashWrite - write data to the Intel 28F256A Flash Memory
  129. *
  130. * RETURNS: N/A
  131. */
  132. void sysFlashWrite
  133.     (
  134.     unsigned short *pFlashBuffer /* string to be copied */
  135.     )
  136.     {
  137.     int address;
  138.     int ix = 0;
  139.     volatile unsigned short * pFlashAdrs;
  140.     for (address = 0; address < NV_RAM_SIZE; address += 2)
  141. {
  142. pFlashAdrs = (unsigned short *) (NV_RAM_ADRS + address);
  143. for (ix = 0; ix < 25; ix++)
  144.     {
  145.     *pFlashAdrs = FLASH_CMD_PROG_SETUP; /* write setup */
  146.     *pFlashAdrs = *pFlashBuffer; /* data to write */
  147.     sysFlashDelay (10); /* wait for write */
  148.     *pFlashAdrs = FLASH_CMD_PROG_VERIFY; /* verify command */
  149.     sysFlashDelay (6); /* wait for verify */
  150.     if (*pFlashAdrs == *pFlashBuffer) /* done? */
  151. break;
  152.     }
  153. pFlashBuffer ++;
  154. }
  155.     pFlashAdrs = (unsigned short *) NV_RAM_ADRS;
  156.     *pFlashAdrs = FLASH_CMD_RESET;
  157.     *pFlashAdrs = FLASH_CMD_RESET;
  158.     *pFlashAdrs = FLASH_CMD_READ_MEM;
  159.     }
  160. /******************************************************************************
  161. *
  162. * sysNvRamSet - write to non-volatile RAM
  163. *
  164. * This routine copies a specified string into non-volatile RAM.
  165. *
  166. * RETURNS: OK, or ERROR if access is outside the non-volatile RAM range.
  167. *
  168. * SEE ALSO: sysNvRamGet()
  169. *
  170. * INTERNAL
  171. * The Intel 28F256 is rated for 10,000 erase/program cycles minimum.
  172. * If multiple tasks are calling sysNvRamSet() and sysNvRamGet(),
  173. * they should use a semaphore to ensure mutually exclusive access to EEPROM.
  174. */
  175. STATUS sysNvRamSet
  176.     (
  177.     char *string,     /* string to be copied into non-volatile RAM */
  178.     int strLen,       /* maximum number of bytes to copy           */
  179.     int offset        /* byte offset into non-volatile RAM         */
  180.     )
  181.     {
  182.     char *tempBuffer; /* temp buffer for existing data */
  183.     offset += NV_BOOT_OFFSET;   /* boot line begins at <offset> = 0 */
  184.     if ((offset < 0) || (strLen < 0) || ((offset + strLen) > NV_RAM_SIZE))
  185.         return (ERROR);
  186.     /* see if contents are actually changing */
  187.     if (bcmp (NV_RAM_ADRS + offset, string, strLen) == 0)
  188. return (OK);
  189.     /* first read existing data */
  190.     tempBuffer = (char *) malloc (NV_RAM_SIZE);
  191.     bcopyBytes (NV_RAM_ADRS, tempBuffer, NV_RAM_SIZE);
  192.     sysFlashErase (); /* first erase device */
  193.     bcopyBytes (string, tempBuffer + offset, strLen);
  194.     sysFlashWrite ((unsigned short *) tempBuffer); /* write new buffer */
  195.     free (tempBuffer); /* return memory */
  196.     return (OK);
  197.     }