📄 i28f256p30mtd16bit.c
字号:
#include <vxWorks.h>
#include <stdio.h>
#include <taskLib.h>
#include <Assert.h>
#include "tffs/flflash.h"
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
extern UINT32 sysFlashSize; /* FLASH64_SIZE */
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* project-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*chip wide mode*/
#define I28FXXXJ_16BIT_MODE /*now fore to 16x mode, by drive BYTE# low*/
#define Flash
FLFlash volume;
FLFlash volume2;
FLFlash volume3;
/*i28fxxxj3 flash command*/
#define READ_ARRAY 0x00ff
#define READ_ID 0x0090
#define READ_QUERY 0x0098
#define READ_STATUS 0x0070
#define CLEAR_STATUS 0x0050
#define WRITE_BUFF 0x00e8
#define BUFF_ENHANCED 0x0080
#define WRITE_BUF_CONFIRM 0x00d0
#define WRITE_WORD_BYTE 0x0040
#define ERASE_BLOCK_STEP1 0x0020
#define ERASE_BLOCK_STEP2 0x00d0
#define SUSPENT_BLOCK 0x00b0
#define RESUME_BLOCK 0x00d0
#define LOCK_BITSET 0x0060
#define LOCK_BITCLEAR 0x0060
#define PROG_PROTECT 0x00c0
#define STOP_CMD 0x00ff
#define FLASH_STATUS_READY 0x0080
#define FLASH_BUF_CNT 0x40
#define FLASH_28F320J 0x16 /* device code 28F320J */
#define FLASH_28F640J 0x17 /* device code 28F640J */
#define FLASH_28F128J 0x18 /* device code 28F128J */
#define FLASH_28F256P30 0x1C /* device code 28F128J */
typedef struct FlashChipInfor
{
UINT8 uManCode;
UINT8 uDevId;
UINT32 uBlockSize;
UINT32 uBlockCount;
UINT32 uChipSize;
} strFlashChipInfor;
strFlashChipInfor gIntelChip[] = {
{0x89, FLASH_28F320J, 0x20000, 32, 0x400000},
{0x89, FLASH_28F640J, 0x20000, 64, 0x800000},
{0x89, FLASH_28F128J, 0x20000, 128, 0x1000000},
{0x89, FLASH_28F256P30, 0x20000/*bytes!!!*/, 256, 0x2000000}
};
UINT16 gNoOfChipType = NELEMENTS (gIntelChip); /* number of MTDs */
#define I28FXXXJ_MTD_CHIP_CNT (1)
#define I28FXXXJ_MTD_INTERLEAVE (1)
#define DEBUG_READ 0x00000001
#define DEBUG_WRITE 0x00000002
#define DEBUG_PROGRAM 0x00000004
#define DEBUG_ERASE 0x00000008
#define DEBUG_ID 0x00000010
#define DEBUG_MAP 0x00000020
#define DEBUG_PROG32 0x00000040
#define DEBUG_PROGERR 0x00000080
#define DEBUG_ALWAYS 0xffffffff
#define DEBUG
#ifdef DEBUG
LOCAL UINT32 debug = DEBUG_PROGERR;//DEBUG_PROGRAM | DEBUG_ERASE;
#define DEBUG_PRINT(mask, string) \
if ((debug & mask) || (mask == DEBUG_ALWAYS)) \
printErr string
#else
#define DEBUG_PRINT(mask, string)
#endif
#define IOSync()
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
FLStatus i28fxxxjMTDIdentify(FLFlash* pVol);
void FAR0* i28fxxxjMap(FLFlash* pVol, CardAddress address, int length)
{
UINT32 flashBaseAddr = (pVol->socket->window.baseAddress << 12);
void FAR0* pFlash = (void FAR0*) (flashBaseAddr + address);
DEBUG_PRINT(DEBUG_MAP, ("Mapping 0x%08x bytes at 0x%08x to %p\n", length,
(unsigned int) address, pFlash));
return(pFlash);
}
static FLStatus i28fxxxjEraseSector(FlashWPTR pSectorAddr)
{
FLStatus rv = flTimedOut;
UINT32 timeout;
*pSectorAddr = STOP_CMD;
IOSync();
*pSectorAddr = CLEAR_STATUS;
IOSync();
*pSectorAddr = ERASE_BLOCK_STEP1;
IOSync();
*pSectorAddr = ERASE_BLOCK_STEP2;
IOSync();
for (timeout = (volatile unsigned long)flMsecCounter + 3000; (volatile unsigned long)flMsecCounter < timeout;)
{
if((*pSectorAddr&FLASH_STATUS_READY) == FLASH_STATUS_READY)
{
rv = flOK;
break;
}
}
*pSectorAddr = READ_STATUS;
IOSync();
*pSectorAddr = READ_ARRAY;
*pSectorAddr = STOP_CMD;
IOSync();
*pSectorAddr = CLEAR_STATUS;
return rv;
}
static FLStatus i28fxxxjFlashWriteBuf(FlashWPTR pFlashAddr, UINT16 *pSource, UINT32 uLength)
{
FLStatus rv = flTimedOut;
UINT32 timeout;
UINT32 regAddr;
UINT16 expect;
UINT16 *pSrcTemp = pSource;
UINT16 *cur_add, *source;
assert(uLength<=FLASH_BUF_CNT);
if(uLength%2 != 0)
goto ODDOP;
*pFlashAddr = WRITE_BUFF;
IOSync();
for (timeout = (volatile unsigned long)flMsecCounter + 3000; (volatile unsigned long)flMsecCounter < timeout;)
{
if((*pFlashAddr&FLASH_STATUS_READY) == FLASH_STATUS_READY)
{
rv = flOK;
break;
}
*pFlashAddr = WRITE_BUFF;
}
if(rv != flOK)
{
return rv;
}
*pFlashAddr = (UINT16)(uLength/2 - 1);
IOSync();
//printf("length is %d, value 0x%0x vs 0x%x\n", uLength,pFlashAddr[0], pSrcTemp[0] );
for (regAddr=(UINT32)pFlashAddr;
regAddr-(UINT32)pFlashAddr < (uLength);
regAddr+=sizeof(UINT16))
{
expect = *pSrcTemp;
*((UINT16 *)regAddr) = expect;
pSrcTemp++;
}
*pFlashAddr = WRITE_BUF_CONFIRM;
IOSync();
for (timeout = (volatile unsigned long)flMsecCounter + 3000; (volatile unsigned long)flMsecCounter < timeout;)
{
if((*pFlashAddr&FLASH_STATUS_READY) == FLASH_STATUS_READY)
{
rv = flOK;
break;
}
*pFlashAddr = READ_STATUS;
}
*pFlashAddr = STOP_CMD;
IOSync();
*pFlashAddr = CLEAR_STATUS;
/*verifi the data*/
*pFlashAddr = STOP_CMD;
pSrcTemp = pSource;
for(regAddr = (UINT32)pFlashAddr;
regAddr - (UINT32)pFlashAddr < (uLength);
regAddr+=sizeof(UINT16))
{
if(*((FlashWPTR)regAddr) != *((FlashWPTR)pSrcTemp++))
{
return flWriteFault;
}
}
return rv;
ODDOP:
for(cur_add = (UINT16 *)pFlashAddr, source = pSource;
(UINT32)cur_add - (UINT32)pFlashAddr < (uLength);
cur_add++, source++)
{
*((FlashWPTR)cur_add) = CLEAR_STATUS;
IOSync();
*((FlashWPTR)cur_add) = WRITE_WORD_BYTE;
IOSync();
*((FlashWPTR)cur_add) = *((FlashWPTR)source);
for (timeout = (volatile unsigned long)flMsecCounter + 3000; (volatile unsigned long)flMsecCounter < timeout;)
{
if((*((FlashWPTR)cur_add)&FLASH_STATUS_READY) == FLASH_STATUS_READY)
{
rv = flOK;
break;
}
}
if(rv != flOK)
{
printf("i28fxxxjFlashWriteBuf reture1 \n");
return rv;
}
*((FlashWPTR)cur_add) = CLEAR_STATUS;
IOSync();
*((FlashWPTR)cur_add) = STOP_CMD;
}
cur_add--; /* can't advance beyond end of array */
*((FlashWPTR)cur_add) = CLEAR_STATUS;
IOSync();
*((FlashWPTR)cur_add) = READ_ARRAY;
IOSync();
/*verifi the data*/
for(cur_add = (UINT16 *)pFlashAddr, source = pSource;
(UINT32)cur_add - (UINT32)pFlashAddr < (uLength);
cur_add++, source++)
{
if(*((FlashWPTR)cur_add) != *((FlashWPTR)source))
{
return flWriteFault;
}
}
return rv;
}
FLStatus i28fxxxjWrite(FLFlash* pVol, CardAddress address, const void FAR1 *buffer,
int length,FLBoolean overwrite)
{
FlashWPTR pSectorAddr;
int nLength, nLenTemp;
FLStatus rv;
CardAddress addrTemp;
UINT32 pBuf = (UINT32)buffer;
//DEBUG_PRINT(DEBUG_ALWAYS,("\n@@@@@@@@@@@@overwrite = %d\n",overwrite));
//DEBUG_PRINT(DEBUG_ALWAYS,("\ni28fxxxjWrite() length = 0x%x,address = 0x%x overwrite=%d",length,address,(int)overwrite));
if ((UINT32)address >= (UINT32)pVol->chipSize || (UINT32)(address+length-1) >= (UINT32)pVol->chipSize)
{
DEBUG_PRINT(DEBUG_ALWAYS, ("flash(0x%04x), Invalid data to write: addr 0x%08x, length %d\n",
pVol->type, (UINT32)address, length));
return flBadLength;
}
addrTemp = address;
nLength = length;
pSectorAddr = i28fxxxjMap(pVol, addrTemp, 0);
if ( nLength < FLASH_BUF_CNT )
nLenTemp = nLength;
else
nLenTemp = FLASH_BUF_CNT - ((UINT32)pSectorAddr&(FLASH_BUF_CNT-1));
rv = i28fxxxjFlashWriteBuf(pSectorAddr, (UINT16 *)pBuf, nLenTemp);
if(rv==flOK)
{
//printf("write :succed write to 0x%0x\n", (UINT32)pSectorAddr );
//printf("#");
nLength-= nLenTemp;
addrTemp += nLenTemp;
pBuf += nLenTemp;
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -