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

📄 flash.c

📁 tms320vc5509a usb boot
💻 C
字号:
/*********************************************************************
 *          (C) COPYRIGHT TEXAS INSTRUMENTS, INC. 2000, 2001
 * flash.c -- Flash Memory handlers for dsk 5510
 */

/* Change Log:
 */
/*
 * $Log: flash.c,v $
 * Revision 1.3  2001/06/27 17:55:14  heeschen
 * v00.32 Beta Prep
 * Capturing all files to prepare for Beta Release
 *
 * Revision 1.2  2001/04/19 18:56:50  heeschen
 * v00.30 Alpha - Updated comments
 *
 *
 */

#include  "flash.h"

#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

#define	DQ7				(1u<<7)
#define	DQ6				(1u<<6)
#define	DQ5				(1u<<5)
#define	DQ3				(1u<<3)
#define	DQ2				(1u<<2)

// Device definitions
#define FLASH_BASE            0x200000
#define FLASH_SIZE            0x100000 // in bytes
#define LATCH_ADDR            ((volatile unsigned *)0x401000)

#define FLASH_SECTORS       39

/* Constant table containing end address of each sector */
unsigned long sector_end[FLASH_SECTORS] = {
    FLASH_BASE + 0x007fff, /* Sector 0  */
    FLASH_BASE + 0x00ffff, /* Sector 1  */
    FLASH_BASE + 0x017fff, /* Sector 2  */
    FLASH_BASE + 0x01ffff, /* Sector 3  */
    FLASH_BASE + 0x027fff, /* Sector 4  */
    FLASH_BASE + 0x02ffff, /* Sector 5  */
    FLASH_BASE + 0x037fff, /* Sector 6  */
    FLASH_BASE + 0x03ffff, /* Sector 7  */    
    FLASH_BASE + 0x047fff, /* Sector 8  */
    FLASH_BASE + 0x04ffff, /* Sector 9  */
    FLASH_BASE + 0x057fff, /* Sector 10 */
    FLASH_BASE + 0x05ffff, /* Sector 11 */
    FLASH_BASE + 0x067fff, /* Sector 12 */
    FLASH_BASE + 0x06ffff, /* Sector 13 */
    FLASH_BASE + 0x077fff, /* Sector 14 */
    FLASH_BASE + 0x07ffff, /* Sector 15 */
    FLASH_BASE + 0x087fff, /* Sector 16 */
    FLASH_BASE + 0x08ffff, /* Sector 17 */
    FLASH_BASE + 0x097fff, /* Sector 18 */
    FLASH_BASE + 0x09ffff, /* Sector 19 */
    FLASH_BASE + 0x0a7fff, /* Sector 20 */
    FLASH_BASE + 0x0affff, /* Sector 21 */
    FLASH_BASE + 0x0b7fff, /* Sector 22 */
    FLASH_BASE + 0x0bffff, /* Sector 23 */
    FLASH_BASE + 0x0c7fff, /* Sector 24 */
    FLASH_BASE + 0x0cffff, /* Sector 25 */
    FLASH_BASE + 0x0d7fff, /* Sector 26 */
    FLASH_BASE + 0x0dffff, /* Sector 27 */
    FLASH_BASE + 0x0e7fff, /* Sector 28 */
    FLASH_BASE + 0x0effff, /* Sector 29 */
    FLASH_BASE + 0x0f7fff, /* Sector 30 */
    
    FLASH_BASE + 0x0f8fff, /* Sector 31 */
    FLASH_BASE + 0x0f9fff, /* Sector 32 */
    FLASH_BASE + 0x0fafff, /* Sector 33 */
    FLASH_BASE + 0x0fbfff, /* Sector 34 */
    FLASH_BASE + 0x0fcfff, /* Sector 35 */
    FLASH_BASE + 0x0fdfff, /* Sector 36 */
    FLASH_BASE + 0x0fefff, /* Sector 37 */
    FLASH_BASE + 0x0fffff, /* Sector 39 */
};

int erasetable[FLASH_SECTORS];

void FlsWrite(unsigned long Faddr, unsigned short Data)
{
    *LATCH_ADDR = (Faddr >> 12 & 0xff);
    *((unsigned short *)(FLASH_BASE + (Faddr & 0x0fff))) = Data;  
}

unsigned short FlsRead(unsigned long Faddr)
{
    unsigned short status;
    
    *LATCH_ADDR = (Faddr >> 12 & 0xff);
    status = *((unsigned short *)(FLASH_BASE + (Faddr & 0x0fff)));
    return status;
}

/* Reset the Flash Memory. Places chip in read mode
 */ 
void flash_reset()
{
    FlsWrite(FLASH_BASE_ADDR, CMD_READ_ARRAY);
}

void flash_init()
{
    int i;
    
    /* Mark all sectors as unerased */
    for (i = 0; i < FLASH_SECTORS; i++)
        erasetable[i] = 0;
}

/* Erase a segment of Flash memory */
void flash_erase(unsigned long start, unsigned long length)
{
    int i;
    unsigned long sector_base, end;
    
    /* Calculate extents of range to erase */
    end = start + length - 1;
    
    /* Walk through each sector, erase any sectors within range */
    sector_base = FLASH_BASE;
    for (i = 0; i < FLASH_SECTORS; i++)
    {
        if (!erasetable[i] && (start <= sector_end[i]) && (end >= sector_base))
        {
            /* Start sector erase sequence */
            FlsWrite(sector_base, CMD_CLEAR_STATUS);        
            FlsWrite(sector_base, CMD_ERASE_ARRAY);        
            FlsWrite(sector_base, CMD_CONFIRM_ERASE);
            
            /* Wait for erase to complete */
            while ((FlsRead(FLASH_BASE_ADDR) & BIT_WSMS) == 0);
            
            /* Mark sector as erased */
            erasetable[i] = 1;
                    
            /* Put back in read mode */
            flash_reset();                  
        }
        
        /* Advance to next sector */
        sector_base = sector_end[i] + 1;
    }
}


/* Writes length words from source to dest in flash.
 */
int flash_write(u16 *source, u16 *dest, u16 length)
{
	u16 *pdst = dest;
	u16 *psrc = source;
	u16	i,len = length;
    u16 j;
    u16 status;

    flash_reset();

    for(i = 0; i < len; i++) 
    {
        /* Erase Flash if necessary */
        flash_erase((unsigned long)dest, (unsigned long)length);
        
        /* Initiate Flash program operation */
        FlsWrite(FLASH_BASE_ADDR, CMD_CLEAR_STATUS);        
        FlsWrite((unsigned long)pdst, CMD_WRITE_ARRAY);        
        FlsWrite((unsigned long)pdst, *psrc);    
		
        for (j = 0; j < 10; j++);
        
        /* Wait until program is complete */
        do {
            status = FlsRead((unsigned long)pdst);
        } while((status & BIT_WSMS) == 0);

        psrc++;
        pdst++;
	}
	
    FlsWrite(FLASH_BASE_ADDR, CMD_READ_ARRAY);   
	return OK;
}



/*****************************************************
 *  int chip_erase()
 *  
 *  This function commands an entire flash erasure
 *  returns immediately. Use checkFor32bitCompletion()
 *  to see when erasing is complete.
 *
 *  Parameters:
 *      none
 * 
 *  Return:
 *  - OK success
 *  - ERROR failure
 * 
 *  Notes:
 * 
 */
void chip_erase(void)
{    
	return;
}

int EraseDone()
{
    return ERROR;
}

⌨️ 快捷键说明

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