📄 flashmem.c
字号:
/* flashMem.c - Flash memory device driver */
/*
DESCRIPTION
This library contains routines to manipulate flash memory. Read and write
routines are included.
The macro values FLASH_ADRS, FLASH_SIZE, and FLASH_WIDTH must be defined to
indicate the address, size (in bytes), and the data-access width (in bytes) of
the flash memory.
If the flash memory needs to be overlaid, and the section of the memory that
needs to be overlaid is less than FLASH_SIZE, then, for efficiency, define
FLASH_SIZE_WRITABLE to the size (in bytes) of the overlay section.
The routine sysFlashDelay() creates a delay for a specified number of
microseconds. The timing loop can be adjusted on a board-dependent basis by
defining the function sysFlashBoardDelay and values for the following macros,
.iP
SYS_FLASH_DELAY_SHIFT
.iP
SYS_FLASH_DELAY_ADJ
.iP
SYS_FLASH_DELAY_INCR
.LP
To use the routine sysFlashBoardDelay(), the macro SYS_FLASH_BOARD_DELAY
should be defined.
The macro FLASH_NO_OVERLAY should be defined when calls to sysFlashSet()
are expected to erase the flash and reprogram it with only the new data.
The macro SYS_FLASH_TYPE should be defined for flash devices that cannot be
auto-selected. This macro should be set to a flash device code defined in the
header files, <drv/mem/flash28.h> and <drv/mem/flash29.h>
To support flash devices that that need to turn on/off write protect features
(special programming voltages or other write-enable features), the macro
SYS_FLASH_WRITE, and the routines, sysFlashWriteEnable() and
sysFlashFlashWriteDisable() should be defined.
INTERNAL:
The FLASH_SIZE_WRITABLE concept doesn't work very well. It just limits the
amount of flash that is writable, so why bother. What it was really
intended to address was flash that is only block writable, i.e. you
can only write a complete block at a time. To properly handle block
memory, you must copy the old block of memory to a buffer, update the
part of the buffer that is to be changed, and then write back the
complete buffer in a single write operation.
The accesses to non-volatile memory, and flash control registers needs
to be abstracted. Macros should be used for all actual i/o operations.
*/
#include "flashMem.h"
#include "vxworks.h"
#include "string.h"
/* defines */
/* Establish default values for DELAY parameters */
#ifndef SYS_FLASH_DELAY_SHIFT
# define SYS_FLASH_DELAY_SHIFT 0
#endif /*SYS_FLASH_DELAY_SHIFT*/
#ifndef SYS_FLASH_DELAY_ADJ
# define SYS_FLASH_DELAY_ADJ 0
#endif /* SYS_FLASH_DELAY_ADJ */
#ifndef SYS_FLASH_DELAY_INCR
# define SYS_FLASH_DELAY_INCR 1
#endif /* SYS_FLASH_DELAY_INCR */
/* Names of routines, or null values */
#ifdef SYS_FLASH_BOARD_DELAY
# define SYS_FLASH_BOARD_DELAY_RTN() sysFlashBoardDelay ()
#else
# define SYS_FLASH_BOARD_DELAY_RTN()
#endif /* SYS_FLASH_BOARD_DELAY */
#ifdef FLASH_SIZE_WRITEABLE
# define FLASH_MEM_SIZE FLASH_SIZE_WRITEABLE
#else
# define FLASH_MEM_SIZE FLASH_SIZE
#endif /* FLASH_SIZE_WRITEABLE */
/* Operation status bits for Flash 29Fxxx devices */
#define Q7(ix) ((ix & 0x80) >> 7) /* DQ7 bit */
#define Q5(ix) ((ix & 0x20) >> 5) /* DQ5 bit */
#ifndef FLASH_CAST
#define FLASH_CAST (UINT8 *)
#endif
int flashDelayShift = SYS_FLASH_DELAY_SHIFT;
int flashDelayAdj = SYS_FLASH_DELAY_ADJ;
int flashDelayIncr = SYS_FLASH_DELAY_INCR;
/* forward declarations */
#ifdef __STDC__
void sysFlashDelay (int delayCount);
STATUS sysFlashDataPoll (volatile UINT8 * pFA, UINT8 value,int pollSize);
STATUS sysFlashErase (UINT8 eraseType,volatile UINT8* pFA,int sectorSize );
STATUS sysFlashWrite (UINT8 * pFB, int size, int offset);
#else /* __STDC__ */
void sysFlashDelay ();
STATUS sysFlashDataPoll ();
STATUS sysFlashErase ();
STATUS sysFlashWrite ();
#endif /* __STDC__ */
/******************************************************************************
*
* sysFlashGet - get the contents of flash memory
*
* This routine copies the contents of flash memory into a specified
* string. The string is terminated with an EOS.
*
* RETURNS: OK, or ERROR if access is outside the flash memory range.
*
* SEE ALSO: sysFlashSet()
*
* INTERNAL
* If multiple tasks are calling sysFlashSet() and sysFlashGet(),
* they should use a semaphore to ensure mutually exclusive access.
*/
STATUS sysFlashGet
(
char * string, /* where to copy flash memory */
int strLen, /* maximum number of bytes to copy */
int offset /* byte offset into flash memory */
)
{
if ((offset < 0) || (strLen < 0) || ((offset + strLen) > FLASH_SIZE))
return (ERROR);
bcopyBytes ((char *) (FLASH_ADRS + offset), string, strLen);
string [strLen] = EOS;
return (OK);
}
/******************************************************************************
*
* sysFlashDelay - create a delay for a specified number of microseconds
*
* This routine implements a busy wait for a specified number of microseconds.
* The timing loop can be adjusted on a board-dependent basis by
* defining values for the following macros:
* .iP
* SYS_FLASH_DELAY_SHIFT
* .iP
* SYS_FLASH_DELAY_ADJ
* .iP
* SYS_FLASH_DELAY_INCR
* .LP
* The values SYS_FLASH_DELAY_SHIFT and SYS_FLASH_DELAY_ADJ
* convert microseconds into a board-dependent tick-count.
* This routine can call a user-defined hook, sysFlashBoardDelay(),
* which creates a delay for a number of board-dependent ticks as
* specified by SYS_FLASH_DELAY_INCR. To use sysFlashBoardDelay(), define
* SYS_FLASH_BOARD_DELAY in config.h.
*
* RETURNS: N/A
*
* SEE ALSO: sysFlashErase(), sysFlashWrite()
*/
void sysFlashDelay
(
int delayCount /* number of uSec to delay */
)
{
int ix;
delayCount <<= flashDelayShift; /* board-dependent shift */
delayCount += flashDelayAdj; /* board-dependent addition */
for (ix = 0; ix < delayCount; ix += flashDelayIncr)
SYS_FLASH_BOARD_DELAY_RTN ();
}
/******************************************************************************
*
* sysFlashDataPoll - wait for a flash device operation to complete
*
* This routine polls a specified address on a 29LV flash device
* until the device operation at that location completes or times out.
*
* While a flash operation is in progress, a read on the device
* returns on Q7 (data bit 7) the complement of the previous value of Q7. Once
* the flash operation has completed, the Q7 bit returns the true data
* of the last write. Subsequent reads return the correct data in Q0-7.
*
* The Q5 bit implements a timeout functionality. When a currently
* executing flash operation exceeds specified limits, the Q5 bit is set (to 1).
*
* RETURNS: OK, or ERROR if the timeout (!Q5) occurs before the device operation
* completes.
*
* SEE ALSO: sysFlashErase(), sysFlashWrite()
*/
STATUS sysFlashDataPoll
(
volatile UINT8 * pFA, /* programmed address to poll */
UINT8 value, /* data programmed to poll address */
int pollSize /* flash size(bytes) to be polled */
)
{
STATUS retVal = OK;
volatile UINT8 * pTest = (UINT8 *) pFA;
int ix; /* byte counter */
int vBit; /* programmed value of DQ7 */
for (ix = (pollSize - 1); (ix >= 0 ) && (retVal == OK); ix--, pTest++)
{
vBit = Q7(value);
while (Q7(*pTest) != vBit)
if (Q5(*pTest) == 1) /* timeout ? */
break;
if (Q7(*pTest) != vBit) /* check Q7 & Q5 race */
retVal = ERROR;
}
return (retVal);
}
/******************************************************************************
*
* sysFlashErase - erase the contents of flash memory
*
* This routine clears the contents of flash memory.
*
* Flash 28F\f2xxx\f1 devices are erased by writing a flash erase command to
* the device and verifying that each flash location is set to a high value
* (0xFF).
*
* Flash 29F\f2xxx\f1 devices are erased by writing the six-byte erase code
* into specific address locations, which sets all byte locations to a high
* value (0xFF).
*
* RETURNS: OK, or ERROR if the contents of flash memory cannot be erased.
*/
STATUS sysFlashErase
(
UINT8 eraseType, /* type of flash memory on-board */
volatile UINT8* pFA, /* the begining address to be erased */
int sectorSize /* sector size */
)
{
STATUS retVal = OK;
switch (eraseType)
{
case (CHIP_ERASE):
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_CHIP_ERASE;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FOURTH;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_FIFTH;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_SIXTH;
do {
retVal = sysFlashDataPoll (pFA, (UINT8) 0xffffffff,FLASH_SIZE);
} while ((*pFA != (UINT8) 0xffffffff) && (retVal == OK));
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_READ_RESET;
break;
case (SECTOR_ERASE):
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_CHIP_ERASE;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FOURTH;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_FIFTH;
*pFA = FLASH29_CMD_SECTOR;
do {
retVal = sysFlashDataPoll (pFA, (UINT8) 0xffffffff,sectorSize);
} while ((*pFA != (UINT8) 0xffffffff) && (retVal == OK));
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_READ_RESET;
break;
default:
retVal = ERROR;
}
return (retVal);
}
/******************************************************************************
*
* sysFlashWrite - write data to flash memory
*
* This routine copies specified data of a specified length, <size>, into a
* specified offset, <offset>, in the flash memory. Data is passed as a string,
* <pFB>, if not NULL. If NULL, data is taken as a repeated sequence of
* <value>.
* The parameter <flashType> should be set to the flash device code.
* The parameter <offset> must be appropriately aligned for the width of
* the Flash devices in use.
*
* Flash 28F\f2xxx\f1 devices are programmed by a sequence of operations:
* .iP
* set up device to write
* .iP
* perform write
* .iP
* verify the write
* .LP
*
* Flash 29F\f2xxx\f1 devices are programmed by a sequence of operations:
* .iP
* set up device to write
* .iP
* perform write
* .iP
* wait for the write to complete
* .LP
*
* RETURNS: OK, or ERROR if the write operation fails.
*
* SEE ALSO: sysFlashSet()
*/
STATUS sysFlashWrite
(
UINT8 * pFB, /* string to be copied; use <value> if NULL */
int size, /* size to program in bytes */
int offset /* byte offset into flash memory */
)
{
UINT8 value; /* value to program */
volatile UINT8 * pFA; /* flash address */
STATUS retVal = OK;
/* unlock bypass */
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND;
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_UNLOCK_BYPASS;
for (pFA = FLASH_CAST (FLASH_ADRS + offset); pFA < FLASH_CAST
(FLASH_ADRS + size + offset) && (retVal == OK); pFA++)
{
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_UNLOCK_BYPASS_PROG;
if (pFB != NULL)
value = *pFB++;
*pFA = value; /* data to write */
do {
retVal = sysFlashDataPoll (pFA, (UINT8) value,1);
} while ((*pFA != value) && (retVal == OK));
}
*(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_AUTOSELECT;
*(FLASH_CAST FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_UNLOCK_BYPASS_RESET;
/* *(FLASH_CAST FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_READ_RESET; */
return (retVal);
}
#if false
/******************************************************************************
*
* sysFlashSet - write to flash memory
*
* This routine copies a specified string into flash memory after calling
* sysFlashErase() and clearing flash memory.
*
* If FLASH_NO_OVERLAY is defined, the parameter <offset> must be
* appropriately aligned for the Flash devices in use (device width,
* sector size etc.).
*
* If the specified string must be overlaid on the contents of flash memory,
* undefine FLASH_NO_OVERLAY.
*
* RETURNS: OK, or ERROR if the write fails or the input parameters are
* out of range.
*
* SEE ALSO: sysFlashErase(), sysFlashGet(), sysFlashTypeGet(), sysFlashWrite()
*
* INTERNAL
* If multiple tasks are calling sysFlashSet() and sysFlashGet(),
* they should use a semaphore to ensure mutually exclusive access to flash
* memory.
*/
STATUS sysFlashSet
(
char *string, /* string to be copied into flash memory */
int strLen, /* maximum number of bytes to copy */
int offset /* byte offset into flash memory */
)
{
#ifndef FLASH_NO_OVERLAY
char *tempBuffer;
#endif /* FLASH_NO_OVERLAY */
if ((offset < 0) || (strLen < 0) || ((offset + strLen) > FLASH_MEM_SIZE))
return (ERROR);
/* see if contents are actually changing */
if (bcmp ((char *) (FLASH_ADRS + offset), string, strLen) == 0)
return (OK);
#ifndef FLASH_NO_OVERLAY
/* first read existing data */
if (tempBuffer =(char*) malloc(FLASH_SIZE), tempBuffer == NULL)
return (ERROR);
bcopyBytes ((char *) FLASH_ADRS, tempBuffer, FLASH_MEM_SIZE);
bcopyBytes (string, (tempBuffer + offset), strLen);
#endif /* FLASH_NO_OVERLAY */
if (sysFlashErase (CHIP_ERASE,(UINT8 *)FLASH_ADRS,0) == ERROR) /* erase device */
{
#ifndef FLASH_NO_OVERLAY
free (tempBuffer);
#endif
#ifndef FLASH_NO_OVERLAY /* program device */
if (sysFlashWrite (FLASH_CAST (tempBuffer), FLASH_MEM_SIZE, 0)
== ERROR)
free (tempBuffer);
#else /* FLASH_NO_OVERLAY */
if (sysFlashWrite (FLASH_CAST (string), strLen, offset,0) ==
ERROR)
{
#endif /* FLASH_NO_OVERLAY */
return (ERROR);
}
#ifndef FLASH_NO_OVERLAY
free (tempBuffer);
#endif
return (OK);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -