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

📄 intelflash.c

📁 个人所写TMS320VC5509外部接口源程序。包含有EMIF.C,EVMLOGIC.C,INTELFLASH.C,SDRAD.C,TIMER.C
💻 C
字号:
/*
 *  Copyright (C) 2001, Spectrum Digital, Inc.  All Rights Reserved.
 */
 
#include "5509.h"
#include "util.h"

#define FLASH_NORMALBLOCKS          31
#define FLASH_NORMALBLOCKSIZE       0x8000
#define FLASH_PARAMBLOCKS           8
#define FLASH_PARAMBLOCKSIZE        0x1000
#define FLASH_MFGID                 0x0089
#define FLASH_DEVID                 0x8890  

#define TIMEOUT_PROGRAM             200
#define TIMEOUT_ERASE               5000000

#define CMD_READ_ARRAY	        	0x00ff 	// Intel commands
#define CMD_READ_ID	            	0x0090
#define CMD_READ_STATUS	        	0x0070
#define CMD_CLEAR_STATUS	    	0x0050
#define CMD_WRITE_ARRAY         	0x0040
#define CMD_ALTWRITE_ARRAY      	0x0010
#define CMD_ERASE_ARRAY         	0x0020
#define CMD_CONFIRM_ERASE       	0x00d0
#define CMD_ERASE_SUSPEND       	0x00b0

#define BIT_R                   	0x0001  // Intel status
#define BIT_BLS                	 	0x0002
#define BIT_PSS                 	0x0004
#define BIT_VPPS                	0x0008
#define BIT_PS                  	0x0010
#define BIT_ES                  	0x0020
#define BIT_ESS                 	0x0040
#define BIT_WSMS                	0x0080


int Flash_Poll(unsigned long addr, unsigned short mask, unsigned short maskval, unsigned long usectimeout)
{
    unsigned short i, status;
    
    for (i = 0; i < usectimeout; i++)
    {
        // Read location
        status = Logic_ReadFlash(addr);
        
        // Check for bit
        if ((status & mask) == maskval)
            return 1;
        
        // Wait 1 usec before trying again    
        SWDelayUsec(1);
    }
    
    // Return 0 if timeout
    return 0;
}

unsigned short Flash_CheckMfgId()
{
    unsigned short mfgid, devid;
    
    // Issue Read ID command
    Logic_WriteFlash((unsigned long)0, CMD_READ_ID);

    // Read IDs
    mfgid = Logic_ReadFlash((unsigned long)0);
    devid = Logic_ReadFlash((unsigned long)1);
    
    // Check IDs
    if ((mfgid != FLASH_MFGID) || (devid != FLASH_DEVID))
        return ERR_FLASH_MFGID;
        
    return 0;
}

unsigned short Flash_EraseBlocks()
{
    unsigned long addr;
    int i;
    
    // Erase all blocks
    addr = 0;
    for (i = 0; i < FLASH_NORMALBLOCKS + FLASH_PARAMBLOCKS; i++)
    {
        // Issue the erase command
        Logic_WriteFlash(addr, CMD_CLEAR_STATUS);
        Logic_WriteFlash(addr, CMD_ERASE_ARRAY);
        Logic_WriteFlash(addr, CMD_CONFIRM_ERASE);

        // Wait for status bit to be set
        if (!Flash_Poll(addr, BIT_WSMS, BIT_WSMS, TIMEOUT_ERASE))
            return ERR_FLASH_ERASE;
            
        // Jump to next block
        addr += (i >= FLASH_NORMALBLOCKS) ? FLASH_PARAMBLOCKSIZE : FLASH_NORMALBLOCKSIZE;        
    }
    
    return 0;
}

unsigned short Flash_TestBlock(unsigned long addr, unsigned long size)
{
    unsigned short pattern;
    
    // Program and verify each location with a pattern
    while (size > 0)
    {
        pattern = addr & 0xffff;
        
        // Issue the program command
        Logic_WriteFlash(addr, CMD_CLEAR_STATUS);
        Logic_WriteFlash(addr, CMD_WRITE_ARRAY);
        Logic_WriteFlash(addr, pattern);
        
        // Wait for the programming to complete
        if (!Flash_Poll(addr, BIT_WSMS, BIT_WSMS, TIMEOUT_PROGRAM))
            return ERR_FLASH_PROGRAM;
            
        // Check the result
        Logic_WriteFlash(addr, CMD_READ_ARRAY);
        if (pattern != Logic_ReadFlash(addr))
        {
            //while(1);
            return ERR_FLASH_VERIFY;
        }
            
        addr++;
        size--;
    }

    return 0;
}

unsigned short Flash_Test()
{
    unsigned long addr, size;
    unsigned short status;
    int i;

#if(0)
    while(1)
    {
        *((unsigned short *)0x401000) = 0x0000;
        SWDelayMsec(1);
        *((unsigned short *)0x401000) = 0xffff;
        SWDelayMsec(1);
    }
#endif

    // Check manufacturer IDs
    if ((status = Flash_CheckMfgId()) > 0)
        return status;
    
    // Erase the device
    if ((status = Flash_EraseBlocks()) > 0)
        return status;
    
    // Test each block
    addr = 0;
    for (i = 0; i < FLASH_NORMALBLOCKS + FLASH_PARAMBLOCKS; i++)
    {
        size = (i >= FLASH_NORMALBLOCKS) ? FLASH_PARAMBLOCKSIZE : FLASH_NORMALBLOCKSIZE;
        
        if ((status = Flash_TestBlock(addr, size)) > 0)
            return status;
            
        addr += size;
    }
    
    return 0;
}

⌨️ 快捷键说明

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