📄 i28f128.c
字号:
#include <stdio.h>#include "tffs/flflash.h"#include "tffs/backgrnd.h"/* support 28f320 28f640 28f128*/#undef CFI_DEBUG #ifdef CFI_DEBUG#define DEBUG_PRINT printf#else#undef DEBUG_PRINT#endif/* JEDEC ids for this MTD */#define I28F128_FLASH 0x18 /* device code 28F128 */#define I28F640_FLASH 0x17 /* device code 28F640 */#define I28F320_FLASH 0x16 /* device code 28F320 */#define SETUP_ERASE 0x2020#define SETUP_WRITE 0x4040#define CLEAR_STATUS 0x5050#define READ_STATUS 0x7070#define READ_ID 0x9090#define SUSPEND_ERASE 0xb0b0#define CONFIRM_ERASE 0xd0d0#define RESUME_ERASE 0xd0d0#define READ_ARRAY 0xffff#define STAT_WSMS 0x0080/* Defintion for Bit macro */#ifndef BIT #define BIT(bitNumber) (1 << (bitNumber))#endif /* BIT */static FLStatus i28f128Write(FLFlash vol, CardAddress address, const void FAR1 *buffer, int length, FLBoolean overwrite){ unsigned long writeTimeout; int i; FlashWPTR flashPtr; FlashWPTR ptr; STATUS retVal; #ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Entering i28f128Write function.\n"); DEBUG_PRINT("Debug: address=0x%x, length=%d, overwrite=%d\n", address, length, overwrite);#endif if ( flWriteProtected(vol.socket) ) return flWriteProtect; if ((length & 1) || (address & 1)) /* Only write words on word-boundary */ return flBadParameter; flashPtr = (FlashWPTR) flMap(vol.socket, address); /* Set timeout to 5 seconds from now */ writeTimeout = flMsecCounter + 5000; retVal = OK; ptr = flashPtr; for (i = 0; i < length ; i += 2, ptr++) { unsigned short value; value = *((FlashWPTR)(buffer + i)); *ptr = SETUP_WRITE; *ptr = value; do { /*ptr = READ_STATUS;*/ if (flMsecCounter > writeTimeout) { retVal = ERROR; *ptr = CLEAR_STATUS; break; } }while((*ptr & STAT_WSMS) != STAT_WSMS); if (retVal == ERROR) { *ptr = READ_ARRAY; break; } if (*ptr & (BIT(5) | BIT(4) | BIT(3) | BIT(1))) { retVal = ERROR; *ptr = CLEAR_STATUS; } if (retVal == ERROR) { *ptr = READ_ARRAY; break; } *ptr = READ_ARRAY; } if (retVal == ERROR) {#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: In i28f128Write, write failed\n");#endif return flWriteFault; } if (tffscmp((void FAR0 *)flashPtr, buffer, length)) {#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: In i28f128Write, write data error\n"); { int i; /*dump data*/ for (i = 0; i < length/2; i++) { unsigned short data; data = *((unsigned short*)(buffer + i * 2)); DEBUG_PRINT("%x==>%x\n", data, *(flashPtr+i)); } }#endif return flWriteFault; }#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: In i28f128Write, erase OK\n");#endif return flOK; }static FLStatus i28f128Erase (FLFlash vol, int firstBlock, int numOfBlocks){ unsigned long writeTimeout; FlashWPTR ptr; int i; STATUS retVal; #ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Entering i28f128Erase function.\n"); DEBUG_PRINT("Debug: firstblock=%d, numOfBlocks=%d.\n", firstBlock, numOfBlocks);#endif if ( flWriteProtected(vol.socket) ) return flWriteProtect; /* Set timeout to 5 seconds from now */ writeTimeout = flMsecCounter + 5000; retVal = OK; for (i = 0; i < numOfBlocks; i++) { ptr = (FlashWPTR) flMap(vol.socket, (firstBlock + i) * vol.erasableBlockSize); #ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Erase ptr=0x%x.\n", ptr);#endif *ptr = SETUP_ERASE; *ptr = CONFIRM_ERASE; do { if (flMsecCounter > writeTimeout) { retVal = ERROR; *ptr = CLEAR_STATUS; break; } }while((*ptr & STAT_WSMS) != STAT_WSMS); if (retVal == ERROR) { *ptr = READ_ARRAY; break; } if (*ptr & (BIT(5) | BIT(4) | BIT(3) | BIT(1))) { retVal = ERROR; *ptr = CLEAR_STATUS; } if (retVal == ERROR) { *ptr = READ_ARRAY; break; } *ptr = READ_ARRAY; } if (retVal == ERROR) {#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: In i28f128Erase, erase failed\n");#endif return flWriteFault; }#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: In i28f128Erase, erase OK\n");#endif return flOK; }FLStatus i28f128Identify(FLFlash vol){ FlashWPTR flashPtr;#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Entering i28f128Identify.\n");#endif flSetWindowBusWidth(vol.socket,16);/* use 16-bits */ flSetWindowSpeed(vol.socket,120); /* 120 nsec. */ flSetWindowSize(vol.socket,2); /* 8 KBytes */ flashPtr = (FlashWPTR) flMap(vol.socket,0); flashPtr[0] = READ_ARRAY; flashPtr[1] = READ_ID; switch (flashPtr[1]) { case I28F640_FLASH: vol.type = I28F640_FLASH; vol.chipSize = 0x800000;#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Detect Intel 28f640\n");#endif break; case I28F320_FLASH: vol.type = I28F320_FLASH; vol.chipSize = 0x400000;#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Detect Intel 28f320\n");#endif break; case I28F128_FLASH: vol.type = I28F128_FLASH; vol.chipSize = 0x1000000;#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Detect Intel 28f128\n");#endif break; default:#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: Can't detect flash type\n");#endif vol.type = NOT_FLASH;#ifdef DEBUG_PRINT DEBUG_PRINT("Debug: failed to identify 16-bit Intel media.\n");#endif return flUnknownMedia; /* not ours */ } flashPtr[0] = READ_ARRAY; vol.interleaving = 1; vol.noOfChips = 1; vol.erasableBlockSize = 128*1024; /* Register our flash handlers */ vol.write = i28f128Write; vol.erase = i28f128Erase; #ifdef DEBUG_PRINT DEBUG_PRINT("Debug: identified 16-bit Intel media.\n");#endif return flOK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -