📄 bootloader.c
字号:
#include "bootloader.h"
extern const unsigned int M29W160B_Blk_Off[];
extern char file_name[FILE_NAME_LENGTH];
unsigned int Download_Address = 0x0d000000;
void SetDownloadAddress (int argc, const char **argv)
{
unsigned int u32Addr;
DM_Printf("Download Address (Currently %08xh): ", Download_Address);
if (InputHex(psUart, &u32Addr) != 1) return;
u32Addr &= 0xfffffffe; /* 16-bit align */
if ((u32Addr >= CS6_BASE) && (u32Addr < CS6_BASE+CS6_SIZE)) Download_Address = u32Addr;
else DM_Message("Illegal Address!");
return;
}
void YmodemDownload (int argc, const char **argv)
{
int i32Size;
DM_Message("Please Send File by Ymodem Protocol ... (press 'a' to abort)");
rINTMSK |= BIT_GLOBAL;
i32Size = ymodem_receive((char *)Download_Address);
if (i32Size > 0)
{
DM_Printf("\n\r%s Received, %d Bytes.", file_name, i32Size);
}
else if (i32Size < 0)
{
DM_Message("\n\rAborted by user.");
}
else
{
DM_Message("\n\rFailed to receive file!");
}
return;
}
void ProgramBootFlash (int argc, const char **argv)
{
int i;
unsigned int u32IntMask;
unsigned int u32FlashAddr, u32BufAddr, u32Offset, u32LastOff, u32Size;
unsigned int u32FirstBlk, u32LastBlk, u32CurrBlk;
/* get the start address in boot flash to program to */
DM_Message("Destination Address (HEX): ");
if (InputHex(psUart, &u32FlashAddr) != 1) return;
u32FlashAddr &= 0xfffffffe; /* 16-bit align */
u32Offset = u32FlashAddr-(unsigned int)BFLASH_BASE; /* get offset into flash memory */
u32Offset >>= 1; /* 16-bit align */
if (u32Offset >= BFLASH_SIZE)
{
DM_Message("Not within Boot Flash Memory Space!");
return;
}
/* get the data size to program into boot flash */
DM_Message("Bytes to Program (Decimal): ");
if (InputDec(psUart, &u32Size) != 1) return;
if ((u32Size % 2) != 0) u32Size ++; /* 16-bit align */
u32Size >>= 1; /* 16-bit align */
if (u32Size > (BFLASH_SIZE - u32Offset))
{
DM_Message("Not within Boot Flash Memory Space!");
return;
}
/* get the start address of ram buffer */
DM_Message("Source Address (HEX): ");
if (InputHex(psUart, &u32BufAddr) != 1) return;
u32BufAddr &= 0xfffffffe; /* 16-bit align */
u32LastOff = u32Offset + u32Size - 1;
/* compute the block number where the program starts */
for (u32FirstBlk=0; u32FirstBlk<BFLASH_BLKNUM-1; u32FirstBlk++)
{
if (u32Offset < M29W160B_Blk_Off[u32FirstBlk+1])
{
break;
}
}
/* compute the block number where the program ends */
for (u32LastBlk=u32FirstBlk; u32LastBlk<BFLASH_BLKNUM-1; u32LastBlk++)
{
if (u32LastOff < M29W160B_Blk_Off[u32LastBlk+1])
{
break;
}
}
/* disable interrupt */
u32IntMask = rINTMSK;
rINTMSK |= BIT_GLOBAL;
/* erase blocks */
for (u32CurrBlk=u32FirstBlk; u32CurrBlk<=u32LastBlk; u32CurrBlk++)
{
DM_Printf("Erasing Block %d ...", u32CurrBlk);
if (BfBlockErase(u32CurrBlk) == 0)
{
DM_Message("Done.");
}
else
{
DM_Message("Failed!");
rINTMSK = u32IntMask;
return;
}
}
/* program data */
DM_Message("Programming ... ");
if (BfProgram(u32Offset, u32Size, (unsigned short *)u32BufAddr) == 0)
{
DM_Message("Done.");
}
else
{
DM_Message("Failed!");
rINTMSK = u32IntMask;
return;
}
/* restore interrupt mask word */
rINTMSK = u32IntMask;
/* verify */
DM_Message("Verifying ... ");
for (i=0; i<u32Size; i++)
{
if ((*(unsigned short *)u32FlashAddr) != (*(unsigned short *)u32BufAddr))
{
DM_Message("Failed!");
return;
}
u32FlashAddr += 2;
u32BufAddr +=2;
}
DM_Message("Passed.");
return;
}
void LoadToRam (int argc, const char **argv)
{
int i;
unsigned int u32FlashAddr, u32RamAddr, u32Size;
unsigned char *pu8Src, *pu8Dest;
/* get the start address in boot flash to load from */
DM_Message("Source Address (HEX): ");
if (InputHex(psUart, &u32FlashAddr) != 1) return;
/* get the data size to load */
DM_Message("Bytes to Load (Decimal): ");
if (InputDec(psUart, &u32Size) != 1) return;
/* get the start address of ram area to load to */
DM_Message("Destination Address (HEX): ");
if (InputHex(psUart, &u32RamAddr) != 1) return;
/* copy data */
pu8Src = (unsigned char *)u32FlashAddr;
pu8Dest = (unsigned char *)u32RamAddr;
DM_Message("Loading ... ");
for (i=0; i<u32Size; i++)
{
pu8Dest[i] = pu8Src[i];
}
DM_Message("Done.");
/* verify */
pu8Src = (unsigned char *)u32FlashAddr;
pu8Dest = (unsigned char *)u32RamAddr;
DM_Message("Verifying ... ");
for (i=0; i<u32Size; i++)
{
if(pu8Dest[i] != pu8Src[i])
{
DM_Message("Failed!");
return;
}
}
DM_Message("Passed.");
return;
}
void JumpTo (int argc, const char **argv)
{
unsigned int u32Addr;
void (*imgp)(void);
DM_Message("Destination Address: ");
if (InputHex(psUart, &u32Addr) != 1) return;
rINTMSK |= BIT_GLOBAL;
imgp = (void (*)(void))u32Addr;
(*imgp)();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -