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

📄 fbtcburndsk6713.c

📁 针对ti c6713 dsk的flashburn烧写程序
💻 C
字号:
/*******************************************************************************
 * Copyright 2004-2005 - Software Design Solutions, Inc.  All rights reserved.
 *
 * Portions of this work have been provided under license with Texas
 * Instruments Inc.
 * 
 * $RCSfile: FBTCBurnDSK6713.c,v $
 * $Revision: 1.13 $
 * 
 * Board-specific flash programming algorithm.  Porting the FBTC to
 * use a different flash will likely require modifying the routines
 * in this file.
 ******************************************************************************/
 
#include <stdio.h>
#include "FBTCBurn.h" 
#include "FBTCMain.h" 
#include "TargetConfig.h"

/**
 * Debug printing support - FBTC will run faster if debug printing is off
 */
#define DBPRINT 0


/* Memory definitions for DSK6713 */

/* Define EMIF Registers  */
static u32 * volatile EMIF_CE1 = (u32 *)0x1800004;
#define CE1_8                   0x02008800  /* reg to set CE1 as 8bit async */
#define CE1_32                  0x02008820  /* reg to set CE1 as 32bit async */

/*******************************************************************************
 * Static data used in this file
 ******************************************************************************/

/**
 * The current checksum value
 */
static u32 cksum = 0; 

/**
 * Override of the flash base address.   If not 0xffff, this value is used 
 * instead of FLASHBASE.
 */
static FLASH_DATA_TYPE *flashbaseov = (FLASH_DATA_TYPE *)0xffffffff;

/**
 * Override of the flash size.   If not 0xffffffff, this value is used instead 
 * of FLASHSIZE.
 */
static FLASH_IMAGE_INDEX_TYPE flashsizeov = 0xffffffffL;


/**
 * Flash memory addressing on the DSK6713
 * Flash is accessed via external memory in at the beginning of the CE1 memory 
 * space.  The flash is configured as 256K x 16 bit and only the low half of 
 * each 16-bit address is used to store one byte at each address.
 */

/*******************************************************************************
 * Static functions used in this file
 ******************************************************************************/

/** 
 * Convert an index to a flash address.   On the DSK6713, flash is one 
 * contiguous addressable space, so address = index + base_address
 *
 * @param [in] index - The flash index
 *
 * @return The flash address corresponding to the given flash index
 */
FLASH_DATA_TYPE *GetFlashAddr(FLASH_IMAGE_INDEX_TYPE index)
{
    return (FLASH_DATA_TYPE*)(GetFlashBase() + index);
}

/** 
 * Reset the flash to Read mode
 */
void ResetFlash()
{
    volatile FLASH_DATA_TYPE* volatile flashaddr;
    flashaddr = GetFlashAddr(0);  /* Don't care what address is used */
    *flashaddr = 0xF0;
}


/*******************************************************************************
 * Flash programming functions defined in FBTCBurn.h
 ******************************************************************************/

/** 
 * Set the flash memory base address, permitting the host to override the flash
 * base address given as FLBASE in TargetConfig.h
 *
 * @param [in] addr - New base address for flash programming
 *
 * @pre  - InitFlash() has been run
 * @post - The flash memory base address is updated
 */
void SetFlashBase(FLASH_DATA_TYPE *addr)
{
    flashbaseov = addr;
}


/**
 * Get the current flash memory base address.
 * 
 * @return - The current flash memory base address
 *
 * @pre  - InitFlash() has been run
 */
FLASH_DATA_TYPE *GetFlashBase(void)
{
    return (flashbaseov != (FLASH_DATA_TYPE *)0xffffffff) 
           ? flashbaseov : (FLASH_DATA_TYPE *)FLBASE;
}


/** 
 * Set the flash memory size, permitting the host to override the flash size
 * given as FLSIZEBYTES
 * 
 * @param [in] size - The new flash memory size, in bytes
 *
 * @pre  - InitFlash() has been run
 * @post - The flash memory size is updated
 */
void SetFlashSize(FLASH_IMAGE_INDEX_TYPE size)
{
    flashsizeov = size;
}


/** 
 * Get the current flash memory size
 * 
 * @return - The current flash memory size, in bytes
 *
 * @pre  - InitFlash() has been run
o */
FLASH_IMAGE_INDEX_TYPE GetFlashSize(void)
{
    return flashsizeov != 0xffffffffL ? flashsizeov : FLSIZEBYTES;
}


/** 
 * Initialize the state of flash programming
 *
 * @post - Flash programming is ready to begin at the flash base address
 */
void InitFlash(void)
{
    /* Nothing to do */
}


/** 
 * Burn data into flash memory at the given index
 *
 * @param [in] index  - Zero based index into flash memory to begin burning
 * @param [in] data   - Data to burn to flash, from the command message
 * @param [in] nBytes - Number of bytes to burn
 *
 * @pre  - InitFlash() has been run
 * @post - Flash at the current flash memory pointer is burned
 */
void BurnFlash(FLASH_IMAGE_INDEX_TYPE index, MSG_DATA_TYPE *data, u32 nBytes)
{
    u16 timeout;
    u8 c;
    volatile FLASH_DATA_TYPE* volatile flashaddr;

    /* Convenient pointers for sending commands to flash chip */
    volatile FLASH_DATA_TYPE* volatile p5 = GetFlashBase()+0x555;
    volatile FLASH_DATA_TYPE* volatile pA = GetFlashBase()+0x2AA;

    flashaddr = GetFlashAddr(index);

    ResetFlash();

#if DBPRINT
    printf("Burning %x bytes of flash at %x\n", nBytes, flashaddr);
#endif  

    while(nBytes--)         
    {
        /**
         * Prep AMD 4MBit (256K X 16) Flash Memory
         * for writing a byte.
         */     
        *p5 = 0xAA;
        *pA = 0x55;
        *p5 = 0xA0;

        *flashaddr = *data;
            
        /* Spin here 'til programming completes
         */
        c = *data++;
        timeout = 0;
        do timeout += 1;
        while(*flashaddr != c && timeout < (u16)0xffff);
            
        flashaddr++;
    }
}


/**
 * Get the contents of a flash location
 *
 * @param [in] index - Zero-based index into flash memory
 *
 * @return - The contents of flash memory at the location
 */
FLASH_DATA_TYPE GetFlashVal(FLASH_IMAGE_INDEX_TYPE index)
{
    volatile FLASH_DATA_TYPE* volatile flashaddr;

    /* Set External Flash for byte-addressability */
    *EMIF_CE1 = CE1_8; /* EMIF CE1 control, 8bit async */

    ResetFlash();

#if DBPRINT
    printf("Retrieving 1 byte of flash at %x\n", flashaddr);
#endif  

    flashaddr = GetFlashAddr(index);
    return *(u8 *)flashaddr ;
}


/**
 * Given a buffer of flash indices, 32 bits each, translate them
 * to a buffer of flash addresses.  Targets with paged memory may
 * encode the page number in the upper bits.
 *
 * @param [in] count - Count of indexes in the buffer
 * @param [in] data  - Buffer of flash indices 
 *
 * @return Flash addresses corresponding to the indices
 *
 * @pre  - InitFlash() has been run
 */
void GetFlashAddresses(u16 count)
{
    u16 i;
    for (i=0; i<count*4; i+=4)
    {
        u32 index;
        u32 addr;
        // Get the index from the command buffer
        index = GetData(i);
        index <<= 8;
        index |= GetData(i+1);
        index <<= 8;
        index |= GetData(i+2);
        index <<= 8;
        index |= GetData(i+3);
        
        // Compute the address corresponding to the given index
        addr = (u32)GetFlashAddr(index);
        
        // Write the address back to the buffer
        SetData(i,   (MSG_DATA_TYPE)((addr & 0xFF000000)>>24));
        SetData(i+1, (MSG_DATA_TYPE)((addr & 0x00FF0000)>>16));
        SetData(i+2, (MSG_DATA_TYPE)((addr & 0x0000FF00)>>8));
        SetData(i+3, (MSG_DATA_TYPE)(addr & 0x000000FF));
    }
}


/**
 * Sends a block of flash bytes to host
 *
 * @param [in] index  - Zero based index into flash memory
 * @param [in] nBytes - Count of bytes to send
 *
 * @pre  - InitFlash() has been run
 * @pre  - nBytes is not larger than MAXDATABYTES
 * @post - Flash contents have been sent to the host
 */
void SendFlashBufToHost(u16 cmd, FLASH_IMAGE_INDEX_TYPE index, u32 nBytes)
{
    volatile FLASH_DATA_TYPE* volatile flashaddr;
    flashaddr = GetFlashAddr(index);

    SetCmd(cmd);     
    SetArg(0, nBytes);      
    
    /* Set External Flash for byte-addressability */
    *EMIF_CE1 = CE1_8; /* EMIF CE1 control, 8bit async */
    
#if DBPRINT
    printf("Sending %x bytes from flash at %x\n", nBytes, flashaddr);
#endif  

    /* Copy bytes from flash to the message buffer */
    memcpy(theMessage+DATAINDEX, (void*)flashaddr, nBytes);
}


/**
 * Erase all of flash memory
 *
 * @pre  - InitFlash() has been run
 * @post - All of flash is erased
 */
void EraseFlash(void)
{
    /* Convenient pointers for sending commands to flash chip */
    volatile FLASH_DATA_TYPE* volatile p5 = GetFlashBase()+0x555;
    volatile FLASH_DATA_TYPE* volatile pA = GetFlashBase()+0x2AA;

    /* Set External Flash for byte-addressability */
    *EMIF_CE1 = CE1_8; /* EMIF CE1 control, 8bit async */

    ResetFlash();

    /* Code to erase AMD29LV400B
     * 4MBit (256K X 16) Flash Memory
     */
    *p5 = 0xAA;
    *pA = 0x55;
    *p5 = 0x80;

    *p5 = 0xAA;
    *pA = 0x55;
    *p5 = 0x10;
    
    CheckFlashErase();
    return;
}


/**
 * Erase an individual flash memory sector.  The definition of a flash
 * sector is implementation dependent
 *
 * @param [in] sector - Sector number to be erased - numbering is target 
 * dependent
 *
 * @pre  - InitFlash() has been run
 * @post - The given flash sector has been erased
 */
void EraseFlashSector(unsigned long sector)
{
    /* Not implemented on the C6713DSK version of FlashBurn */
    return;
}
 

/**
 * Check the flash erase status - return when the erasure is completed
 * 
 * @pre  - InitFlash() has been run
 * @post - When the function returns, flash erasure is complete
 */
void CheckFlashErase(void)
{
    /* Spin here 'til erasing completes
     */
    while(GetFlashVal(0) != 0xff)
        ;/* Do Nothing!*/       
}


/**
 * Initialize the checksum value
 *
 * @param [in] val - Initial value for the checksum
 *
 * @pre  - InitFlash() has been run
 * @post - The checksum is initialized
 */
void CKSSet(u16 val)
{
    cksum = val;
}
    

/**
 * Returns the current checksum value
 *
 * @return - The currently computed checksum value
 * 
 * @pre  - InitFlash() has been run
 */
u16 CKSGet(void)
{
    return (u16)cksum;
}


/** 
 * Compute a checksum of a buffer
 *
 * @param [in] index  - Zero based index into flash to start computing the 
 * checksum
 * @param [in] nBytes - Length of flash to use for the checksum, in bytes
 * 
 * @return - The value of the checksum
 *
 * @pre  - InitFlash() has been run
 * @post - Accumulated checksum is updated
 */
u16 CKSAccumBuf(FLASH_IMAGE_INDEX_TYPE index, u32 nBytes)
{
    volatile FLASH_DATA_TYPE* volatile flashaddr;
    flashaddr = GetFlashAddr(index);

    /* Set External Flash for byte-addressability */
    *EMIF_CE1 = CE1_8; /* EMIF CE1 control, 8bit async */

    ResetFlash();

#if DBPRINT
    printf("Computing checksum for %x bytes of flash at %x\n", nBytes, 
           flashaddr);
#endif  

    while(nBytes-- > 0)
    {
        cksum += *flashaddr++;
        if(cksum > (unsigned long)0x0000ffff)
        {
            cksum += 1;
            cksum &= (unsigned long)0x0000ffff;
        } 
    }
    return (u16)cksum;
}

⌨️ 快捷键说明

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