📄 fbtcburndsk5402.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: FBTCBurnDSK5402.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 <board.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
/*******************************************************************************
* Static data used in this file
******************************************************************************/
/**
* The current checksum value
*/
static u32 cksum = 0;
/**
* Non-zero if flash is erasing, zero once the flash erase is complete
*/
static int ErsStatus = 0;
/**
* The next index of flash memory available for writing.
*/
static FLASH_IMAGE_INDEX_TYPE flashnextindex = 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 *)0xffff;
/**
* Override of the flash size. If not 0xffffffff, this value is used instead
* of FLASHSIZE.
*/
static FLASH_IMAGE_INDEX_TYPE flashsizeov = 0xffffffffL;
/*******************************************************************************
* Static functions used in this file
******************************************************************************/
#define FLASHE_BIT 0x020 /** The flash enable bit */
/**
* Set the External Memory to Flash
*/
void FlashEnable()
{
port4 |= FLASHE_BIT;
}
/**
* Set the External Memory to SRAM
*/
void FlashDisable()
{
port4 &= ~FLASHE_BIT;
}
/**
* Set the External Memory Page Register
*/
void SetPage(u16 pnum)
{
port2 = pnum & 0x1f;
}
/* Flash memory addressing on the 54x
* Flash is accessed via external memory pages. The STMicro Flash M29W400 is
* programmed 1 word at a time.
*
* External memory pages are 32K words, starting at 0x8000.
* The FBTC Comm Protocol uses a logical address in a "flat" address space.
* The Host also uses 8-bit byte counts instead of word counts, but uses word
* addresses.
* Thus when Host wants 256 bytes (128 words) written at logical address
* 0x20004, we need to convertfrom 0x20004 to page and address within page
* 0x20004 = Page 4, addr 4
*/
/**
* Returns the flash page number that contains the given index into flash
*
* @param [in] index - Zero based index into flash memory
*
* @return - External memory page number corresponding to the index
*
* @pre - InitFlash() has been run
*/
static u16 GetPageNum(FLASH_IMAGE_INDEX_TYPE index)
{
return (u16)(index / FLPAGELEN);
}
/**
* Returns the address within a page for the given index into flash
*
* @param [in] index - Zero based index into flash memory
*
* @return - address within a page corresponding to the given index
*
* @pre - InitFlash() has been run
*/
static FLASH_DATA_TYPE *GetFlashAddr(FLASH_IMAGE_INDEX_TYPE index)
{
return GetFlashBase() + (index % FLPAGELEN);
}
/**
* Returns the number of remaining addressable locations on the current page,
* given an index into the flash
*
* @param [in] index - Zero based index into the flash memory
*
* @return - number of addressable flash locations to the end of the page
* containing the given index
*
* @pre - InitFlash() has been run
*/
static int GetPageMAURemaining(FLASH_IMAGE_INDEX_TYPE index)
{
return FLPAGELEN - (index % FLPAGELEN);
}
/*******************************************************************************
* 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 *)0xffff)
? 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)
{
TARGET_INITIALIZE();
FLASH_WAITS();
SetPage(0);
FlashDisable();
flashnextindex = 0;
}
/**
* Burn data into flash memory 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,
unsigned long nBytes)
{
/* Convenient pointers for sending commands to flash chip */
volatile FLASH_DATA_TYPE* volatile p5 = GetFlashBase()+0x5555;
volatile FLASH_DATA_TYPE* volatile pA = GetFlashBase()+0x2AAA;
volatile u16* volatile pBurn;
u16 page, pagelast;
unsigned long timeoutcount;
int nWords = nBytes >> 1;
if (nWords <= 0)
{
return;
}
flashnextindex = index;
/* Set paging and enable flash access */
page = GetPageNum(flashnextindex);
pagelast = GetPageNum(flashnextindex + nWords - 1);
FlashEnable();
if (pagelast == page)
{
/* All words to be written are within the same page */
pBurn = GetFlashAddr(flashnextindex);
flashnextindex += nWords; /* For next time */
#if DBPRINT
printf("Burning %x bytes of flash at page %x, address %x\n", nBytes,
page, pBurn);
#endif
while (nWords-- > 0)
{
SetPage(0);
*p5 = 0xaa;
*pA = 0x55;
*p5 = 0xa0;
SetPage(page);
*pBurn = *data;
timeoutcount = 0;
do
{
timeoutcount += 1;
} while (*pBurn != *data && timeoutcount < 0xffff);
if (timeoutcount == 0xffff)
{
break;
}
pBurn++;
data++;
}
}
else
{
/* Programming crosses at least one page boundary */
#if DBPRINT
printf("Burning %x bytes of flash starting at page %x, address %x and "
"crossing a page boundary\n", nBytes, GetPageNum(flashnextindex),
GetFlashAddr(flashnextindex));
#endif
while (nWords-- > 0)
{
page = GetPageNum(flashnextindex);
pBurn = GetFlashAddr(flashnextindex);
SetPage(0);
*p5 = 0xaa;
*pA = 0x55;
*p5 = 0xa0;
SetPage(page);
*pBurn = *data;
timeoutcount = 0;
do
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -