⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s29gl256n.cpp

📁 系统通过JTAG接口烧结挂接在LXFXP2 上的s29gl256N的FLASH的代码,
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************/ 
/*                                                                     */ 
/* Module Name   : S29GL256N.cpp                                         */ 
/*                                                                     */ 
/* Description   : Implementation of a flash module for the SPANSION   */ 
/*                 S29GL256N flash chip.                               */ 
/*                                                                     */ 
/*                                                                     */ 
/* Document Ref. : - Datasheets for the S29GL256N.                     */ 
/*                                                                     */ 
/* Function List : Exported                                            */ 
/* Note          : The flash memory sub-system consists of 1 S29GL256N */ 
/*                 flash chips,forming a 16-bit data bus. Totally      */
/*                 32M Bytes                                           */ 
/*                                                                     */ 
/* modification history :                                              */ 
/***********************************************************************/ 
 
/***********************************************************************
* 头文件包含
************************************************************************/ 
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include "Jtag_operates.h"
#include "S29GL256N.h"  

/***********************************************************************
* 函数声明
************************************************************************/ 
int flashManID(WORD *rst); 
int flashDevID(WORD *rst); 
WORD ReadBlkSR(int offset);   
void S29glResetFlash(); 
void S29glWrBufAbortResetFlash();
void WindFlashMSDelay(int numMSecond);
bool S29glCheckToggleRdy(int * Dst, WORD expeted);


int S29GL256NErase(int offset, int nBlock); 


/****************************************************/ 
int S29GL128MInitialization(void) 
{ 
    DWORD address; 
	WORD tmp = 0;

 	LPT_initialization();
 	
    /*Reset Device*/  
    S29glWrBufAbortResetFlash(); 
    S29glResetFlash(); 
              
    /* read ManID */   
    if(OK != flashManID(&tmp)) return ERROR; 
 
    /* read Device ID */       
    if(OK != flashDevID(&tmp)) return ERROR; 
 
#ifdef INIT_TEST  
    printf("->Initializing now. And complete\n"); 
#endif  
           
    for(address = FLASHSTARTADDRESS;address < FLASHENDADDRESS;address += FLASH_SECTOR_SIZE/FLASH_ACCESS_OFFSET) 
    {       
        /* read block lock configuration 0-blk unlocked 1-blk locked */   
        tmp = ReadBlkSR((address & FLASH_MASK) + 2 * FLASH_ACCESS_OFFSET); 
        if(tmp == FLASH_CODE_LOCK_MASK)
        { 
            S29glResetFlash(); 
            return ERROR; 
        } 
    } 
 
    S29glResetFlash(); 
     
#ifdef INIT_TEST  
    printf("\n"); 
#endif  

    return OK; 
} 


/******************************************************************************
*
* S29GL128MRead:   Read FLASH Data
* Input:           offset, address offset of data to be read
*                  bufAddr, buffer pointer
*                  dataLen, length of data to be read, 1 is 4 word
* Output:          none
* return:          OK or ERROR
*
******************************************************************************/ 
int S29GL128MRead(int offset, WORD *bufAddr, int dataLen) 
{ 
    int i = 0; 
 
    if(offset < 0) return ERROR; 
 
    if((offset + dataLen * FLASH_ACCESS_OFFSET) > ROM_FLASH_SIZE) 
        return ERROR; 
 
    if((offset % FLASH_ACCESS_OFFSET) != 0) 
        return ERROR; 
 
    if(bufAddr == NULL) 
        return ERROR; 
 
    S29glResetFlash(); 
 
    for(i = 0; i < dataLen; i++) 
    { 
        *(bufAddr + i) = read_command(FLASHSTARTADDRESS + offset + i * FLASH_ACCESS_OFFSET);
    } 
     
    return OK; 
} 


/******************************************************************************
* S29GL128MWrite:   save data to FLASH
* Input:            offset, address offset to be written 
*                   Ramaddr, Address of data to write
*                   dataLen, length of data to be read, 1 is 4 word
* Output:           none
* return:           OK or ERROR
*        dataLen in number of words
******************************************************************************/ 
int S29gl256nWrite(int offset,WORD *bufAddr,int dataLen)  
{
    WORD wordtemp,wtmp,temp; 
    WORD *ramA;
    int flashA,loop;
    int ramValue = 0;   
        
    if(offset < 0)   return ERROR; 
 
    if((offset + dataLen * FLASH_ACCESS_OFFSET) > ROM_FLASH_SIZE) 
    {
        return ERROR; 
    }
 
    if((offset % FLASH_ACCESS_OFFSET) != 0) 
    {
        return ERROR; 
    }


#if 0
//Moved to special function of the dialog    
/************************************************************************************/
/*                                      ERASE OPERATION                             */
/************************************************************************************/
    if(OK == S29GL256NErase(offset, (dataLen + offset%FLASH_SECTOR_SIZE)/FLASH_SECTOR_SIZE))
    {
#ifdef ERASE_TEST  
        printf("->The erase process:dr = 0x%08lX,block = %ld successful!\n"); 
#endif          
    }
    else
    {
#ifdef ERASE_TEST  
        printf("->Error in erase process:dr = 0x%08lX,block = %ld!\n",
            offset, (dataLen + offset%FLASH_SECTOR_SIZE)/FLASH_SECTOR_SIZE); 
#endif              
    } 
#endif
/***********************************************************************************/
/*                        PROGRAM  OPERATION                                       */
/***********************************************************************************/ 
    ramA     = bufAddr;  
    wordtemp = dataLen;  
    flashA   = FLASHSTARTADDRESS + offset;  
              
    while(wordtemp > 0) 
    { 
        write_command(FLASH_UNLOCK_OFFSET1, FLASH_UNLOCK_CMD1);       
        write_command(FLASH_UNLOCK_OFFSET2, FLASH_UNLOCK_CMD2);         
        write_command(FLASH_UNLOCK_OFFSET1, FLASH_PROGRAM_CMD);          
        write_command(flashA, *ramA); 

        wtmp = *(ramA); 
 
        if(S29glCheckToggleRdy((int*)flashA,wtmp)) 
        { 
            S29glWrBufAbortResetFlash(); 
            return ERROR; 
        }       
             
        wordtemp--; 
        flashA          += FLASH_ACCESS_OFFSET; 
        ramA            += 1;//WORD 字节                 
    } 
     
    S29glResetFlash(); 
   
    flashA  = FLASHSTARTADDRESS + offset; 
    ramA    = bufAddr;  
 
    WindFlashMSDelay(1); 
 
    for(loop = 0; loop < dataLen; loop++) 
    { 
        temp = read_command(FLASHSTARTADDRESS + offset + loop * FLASH_ACCESS_OFFSET); 
        if(temp != ramValue)
        { 
            return ERROR; 
        }  
    } 
    
    return OK; 
} 
 
/*************************************************************************
* S29GL128MErase
*
* Erase one or more contiguous Flash erasable blocks
*
* Parameters:
*    offset             The offset address of erasing
*    nBlock         : Number of blocks to erase
*
* Returns:
*    OK on success, failed otherwise
************************************************************************/ 
int S29GL256NErase(int offset, int nBlock) 
{ 
    WORD temp = 0; 
    int rcval = ERROR; 
    int i = 0, ii = 0; 
    int Flashaddr =  FLASHSTARTADDRESS + offset;     
    int FlashEraseEndAdr = Flashaddr + nBlock * FLASH_SECTOR_SIZE; 
 
    if (Flashaddr & (FLASH_ACCESS_OFFSET - 1)) 
    { 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -