📄 usb_boot.c
字号:
/**********************************
*
* nor flash
***********************************/
struct Partition{
U32 offset;
U32 size;
char *name;
};
static struct Partition NorPart[] = {
{0, 0x00040000, "bootloader"}, //256K
{0x00040000, 0x001c0000, "zImage"}, //2m-256k
{0,0,0}
};
static int NorSelPart(char *info)
{
unsigned short i, max_sel;
struct Partition *ptr = NorPart;
uart_printf("Please select which region to %s : Esc to abort\n", info);
for(i=0; ptr->size!=0; i++, ptr++)
uart_printf("%d : offset 0x%-8x, size 0x%-8x [%s]\n", i, ptr->offset, ptr->size, ptr->name);
max_sel = i;
while(1) {
i = uart_getch();
if(i==0x1b)
return -1;
if((i>='0')&&(i<(max_sel+'0'))) {
i -= '0';
// StartPage = NorPart[i].offset>>9;
// BlockCnt = NorPart[i].size>>14;
return i;
}
}
}
static void NorRunSystem(void)
{
FlashRead(0x40000, (unsigned short *) 0x31000000, 0x1c0000);
}
static void ProgNorFlash(void)
{
//if(NorFlashSupport) {
int i;
i=NorSelPart("write");
// uart_printf("i=%d\n",i);
if(i==0)
SectorProg(0, (unsigned short *)downloadAddress, downloadFileSize);
else
SectorProg(0x40000, (unsigned short *)downloadAddress, downloadFileSize);
// }
}
/***********************************************************************************
*
*uart download
*
************************************************************************************/
void comdownload(void)
{
U32 size;
U8 * buf;
U8 * temp;
U8 tmp_data;
U16 checksum;
uart_printf("\nNow download file from uart0...\n");
downloadAddress = tempDownloadAddress;
buf = (U8 *)downloadAddress;
temp = buf-4;
// pISR_UART0 = (U32)Uart0RxInt; //串口接收数据中断
// ClearSubPending(BIT_SUB_RXD0);
// ClearPending(BIT_UART0);
// EnableSubIrq(BIT_SUB_RXD0);
// EnableIrq(BIT_UART0);
while((U32)temp<(U32)buf)
{
// Led_Display(0);
// Delay(1000);
// Led_Display(15);
// Delay(1000);
tmp_data=(U8)uart_getch();
*temp ++ = tmp_data;
}
//接收文件长度,4 bytes
size = *(U32 *)(buf-4);
downloadFileSize = size-6;
// uart_printf("Download File Size = %d\n", size);
// uart_printf("tmp=%x size=%x\n",temp,size);
while(((U32)temp-(U32)buf)<(size-4))
{
// Led_Display(0);
// Delay(8000);
// Led_Display(15);
// Delay(8000);
// *temp ++
tmp_data=(U8)uart_getch();
*temp ++ = tmp_data;
// uart_printf("temp=%x\n",temp);
}
// DisableSubIrq(BIT_SUB_RXD0);
// DisableIrq(BIT_UART0);
uart_printf("Download File = %d\n", size);
checksum = 0;
for(size=0; size<downloadFileSize; size++)
checksum += buf[size];
if(checksum!=(buf[size]|(buf[size+1]<<8))) {
uart_printf("Checksum fail!\n");
return;
}
}
/*********************************************************************************************
* name: WaitDownload
* func: usb downloader
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void WaitDownload(void)
{
UINT32T i;
UINT32T j;
UINT16T cs;
UINT32T temp;
UINT16T dnCS;
int first=1;
float time;
UINT8T tempMem[16];
UINT8T key;
checkSum=0;
// uart_printf("tempMem=%x\n",(UINT32T)tempMem);
downloadAddress=(UINT32T)tempMem; //_RAM_STARTADDRESS;
downPt=(unsigned char *)downloadAddress;
//This address is used for receiving first 8 byte.
downloadFileSize=0;
#if 1
// MMU_DisableICache();
//For multi-ICE.
//If ICache is not turned-off, debugging is started with ICache-on.
#endif
/*******************************/
/* Test program download */
/*******************************/
if(isUsbdSetConfiguration==0)
{
uart_printf("USB host is not connected yet.\n");
}
while(downloadFileSize==0)
{
if(first==1 && isUsbdSetConfiguration!=0)
{
uart_printf("USB host is connected. Waiting a download.\n");
first=0;
}
key=uart_getkey();
if(key!=0)
{
Menu();
first=1; //To display the message,"USB host ...."
}
}
// uart_printf("1\n");
timer_initex();
timer_startex();
#if USBDMA
rINTMSK &= ~(BIT_DMA2);
uart_printf("downloadFileSize=%d\n",downloadFileSize);
ClearEp3OutPktReady();
// indicate the first packit is processed.
// has been delayed for DMA2 cofiguration.
// uart_printf("23 downloadFileSize=%d\n",downloadFileSize);
if(downloadFileSize>EP3_PKT_SIZE)
{
if(downloadFileSize<=(0x80000))
{
ConfigEp3DmaMode(downloadAddress+EP3_PKT_SIZE-8,downloadFileSize-EP3_PKT_SIZE);
//wait until DMA reload occurs.
while((rDSTAT2&0xfffff)==0);
//will not be used.
rDIDST2=(downloadAddress+downloadFileSize-EP3_PKT_SIZE);
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=rDCON2&~(0xfffff)|(0);
}
else
{
ConfigEp3DmaMode(downloadAddress+EP3_PKT_SIZE-8,0x80000-EP3_PKT_SIZE);
//wait until DMA reload occurs.
while((rDSTAT2&0xfffff)==0);
if(downloadFileSize>(0x80000*2))//for 1st autoreload
{
rDIDST2=(downloadAddress+0x80000-8); //for 1st autoreload.
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=rDCON2&~(0xfffff)|(0x80000);
while(rEP3_DMA_TTC<0xfffff)
{
rEP3_DMA_TTC_L=0xff;
rEP3_DMA_TTC_M=0xff;
rEP3_DMA_TTC_H=0xf;
}
}
else
{
rDIDST2=(downloadAddress+0x80000-8); //for 1st autoreload.
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=rDCON2&~(0xfffff)|(downloadFileSize-0x80000);
while(rEP3_DMA_TTC<0xfffff)
{
rEP3_DMA_TTC_L=0xff;
rEP3_DMA_TTC_M=0xff;
rEP3_DMA_TTC_H=0xf;
}
}
}
totalDmaCount=0;
}
else
{
totalDmaCount=downloadFileSize;
}
#endif
uart_printf("\nNow, Downloading [ADDRESS:%xh,TOTAL:%d]\n",
downloadAddress,downloadFileSize);
uart_printf("RECEIVED FILE SIZE:%8d",0);
#if USBDMA
j=0x10000;
while(1)
{
// uart_printf("fdf=%x\n",rOUT_FIFO_CNT1_REG);
if( (rDCDST2-(UINT32T)downloadAddress+8)>=j)
{
uart_printf("\b\b\b\b\b\b\b\b%8d",j);
j+=0x10000;
}
if(totalDmaCount>=downloadFileSize)break;
}
#else
j=0x10000;
while(((UINT32T)downPt-downloadAddress)<(downloadFileSize-8))
{
if( ((UINT32T)downPt-downloadAddress)>=j)
{
uart_printf("\b\b\b\b\b\b\b\b%8d",j);
j+=0x10000;
}
}
#endif
time=timer_stopex();
uart_printf("\b\b\b\b\b\b\b\b%8d",downloadFileSize);
uart_printf("(%5.1fKB/S,%3.1fS)\n",(float)(downloadFileSize/time/1000.),time);
// uart_printf("rEP3_DMA_TTC_L=%x\n",rEP3_DMA_TTC_L);
// uart_printf("rEP3_DMA_TTC_M=%x\n",rEP3_DMA_TTC_M);
// uart_printf("rEP3_DMA_TTC_H=%x\n",rEP3_DMA_TTC_H);
#if USBDMA
/*******************************/
/* Verify check sum */
/*******************************/
uart_printf("Now, Checksum calculation\n");
cs=0;
i=(downloadAddress);
j=(downloadAddress+downloadFileSize-10)&0xfffffffc;
while(i<j)
{
temp=*((UINT32T *)i);
i+=4;
cs+=(UINT16T)(temp&0xff);
cs+=(UINT16T)((temp&0xff00)>>8);
cs+=(UINT16T)((temp&0xff0000)>>16);
cs+=(UINT16T)((temp&0xff000000)>>24);
}
i=(downloadAddress+downloadFileSize-10)&0xfffffffc;
j=(downloadAddress+downloadFileSize-10);
while(i<j)
{
cs+=*((UINT8T *)i++);
}
checkSum=cs;
#else
//checkSum was calculated including dnCS. So, dnCS should be subtracted.
checkSum=checkSum - *((unsigned char *)(downloadAddress+downloadFileSize-8-2))
- *( (unsigned char *)(downloadAddress+downloadFileSize-8-1) );
#endif
dnCS=*((unsigned char *)(downloadAddress+downloadFileSize-8-2))+
(*( (unsigned char *)(downloadAddress+downloadFileSize-8-1) )<<8);
if(downloadFileSize<1024*4)
for (i=0;i<downloadFileSize;i++)
uart_printf("%c",*(unsigned char *)(downloadAddress+i));
if(checkSum!=dnCS)
{
uart_printf(" Checksum Error!!! MEM:%x DN:%x\n",checkSum,dnCS);
return;
}
uart_printf("\n Download O.K.\n\n");
uart_txempty(consoleNum);
if(download_run==1)
{
rINTMSK = BIT_ALLMSK;
run=(void (*)(void))downloadAddress;
run();
}
}
/*********************************************************************************************
* name: Isr_Init
* func: Initialization of Isr
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void Isr_Init(void)
{
pISR_UNDEF=(unsigned)debug_undef;
pISR_SWI =(unsigned)debug_swi;
pISR_PABORT=(unsigned)debug_abort_d;
pISR_DABORT=(unsigned)debug_abort_d;
rINTMOD=0x0; // All=IRQ mode
rSRCPND = rSRCPND; // clear all interrupt
rINTPND = rINTPND; // clear all interrupt
rINTMSK = BIT_ALLMSK; // All interrupt is masked.
rINTSUBMSK = BIT_SUB_ALLMSK; // All sub-interrupt is masked.
//pISR_URXD0=(unsigned)Uart0_RxInt;
//rINTMSK=~(BIT_URXD0); //enable UART0 RX Default value=0xffffffff
#if 1
pISR_USBD =(unsigned)IsrUsbd;
pISR_DMA2 =(unsigned)IsrDma2;
#else
pISR_IRQ =(unsigned)IsrUsbd;
//Why doesn't it receive the big file if use this. (???)
//It always stops when 327680 bytes are received.
#endif
ClearPending(BIT_DMA2);
ClearPending(BIT_USBD);
//rINTMSK&=~(BIT_USBD);
//pISR_FIQ,pISR_IRQ must be initialized
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -