📄 usb.c
字号:
#include "def.h"
#include "board.h"
#include "2410addr.h"
#include "2410usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"
#include "usbin.h"
#ifdef USB_DOWNLOAD_SUPPORT
//unsigned long download_len;
//unsigned long download_addr;
extern void LedSet(U32 led);
int LoadFileFromUsb(U32 a1, U32 a2, U32 a3, U32 a4);
//======================================================
extern volatile U32 download_addr;
extern volatile U32 download_len;
volatile U8 *downPt;
volatile U32 totalDmaCount;
volatile U16 checkSum;
volatile int isUsbdSetConfiguration;
/***********************************************/
/* USBD interrupt handler */
/* USB中断服务程序 */
/***********************************************/
static void __irq IsrUsbd(void)
{
U8 usbdIntpnd, epIntpnd;
U8 saveIndexReg = rINDEX_REG; //保存索引寄存器值(现场保护)
usbdIntpnd = rUSB_INT_REG; //保存中断标志寄存器
epIntpnd = rEP_INT_REG;
if(usbdIntpnd&SUSPEND_INT) //总线上超过3ms没有活动信号引起中断
{
rUSB_INT_REG = SUSPEND_INT;//清除中断标志位
}
if(usbdIntpnd&RESUME_INT)
{
rUSB_INT_REG = RESUME_INT; //恢复引起的中断
}
if(usbdIntpnd&RESET_INT) //接收到复位信号引起的中断
{
//ResetUsbd();
ReconfigUsbd(); //重新配置USB device
rUSB_INT_REG = RESET_INT; //RESET_INT should be cleared after ResetUsbd().
PrepareEp1Fifo();
}
if(epIntpnd&EP0_INT) //端点0中断
{
rEP_INT_REG = EP0_INT;
Ep0Handler(); //进入控制传输中断处理程序
}
if(epIntpnd&EP1_INT)
{
rEP_INT_REG=EP1_INT;
Ep1Handler(); //进入Ep1中断处理程序(BULK IN)
}
if(epIntpnd&EP2_INT)
{
rEP_INT_REG = EP2_INT;
//Ep2Handler();
}
if(epIntpnd&EP3_INT)
{
rEP_INT_REG = EP3_INT;
Ep3Handler(); //进入Ep3中断处理程序(BULK OUT)
}
if(epIntpnd&EP4_INT)
{
rEP_INT_REG = EP4_INT;
//Ep4Handler();
}
ClearPending(BIT_USBD); //USB设备中断处理完毕,清除中断标志位
rINDEX_REG = saveIndexReg; //恢复索引寄存器原来的值(恢复现场)
}
U32 UsbState;
U32 UsbInLength;
U8 *UsbTxAddr;
void UsbdInit(U8 fun)
{
isUsbdSetConfiguration = 0;
//MDIV=40,PDIV=4,SDIV=1
//Upll = (m * Fin) / (p * 2**s)
//m = (MDIV + 8), p = (PDIV + 2), s = SDIV
//Upll=48MHz
rUPLLCON = (40<<12) | (4<<4) | 1;
//初始化描述符
InitDescriptorTable(fun);
ReconfigUsbd();
UsbState = 0;
}
//======================================================
static __inline void cpu_arm920_cache_clean_invalidate_all(void)
{
__asm{
mov r1, #0
mov r1, #7 << 5 /* 8 segments */
cache_clean_loop1:
orr r3, r1, #63UL << 26 /* 64 entries */
cache_clean_loop2:
mcr p15, 0, r3, c7, c14, 2 /* clean & invalidate D index */
subs r3, r3, #1 << 26
bcs cache_clean_loop2 /* entries 64 to 0 */
subs r1, r1, #1 << 5
bcs cache_clean_loop1 /* segments 7 to 0 */
mcr p15, 0, r1, c7, c5, 0 /* invalidate I cache */
mcr p15, 0, r1, c7, c10, 4 /* drain WB */
}
}
void cache_clean_invalidate(void)
{
cpu_arm920_cache_clean_invalidate_all();
}
static __inline void cpu_arm920_tlb_invalidate_all(void)
{
__asm{
mov r0, #0
mcr p15, 0, r0, c7, c10, 4 /* drain WB */
mcr p15, 0, r0, c8, c7, 0 /* invalidate I & D TLBs */
}
}
void tlb_invalidate(void)
{
cpu_arm920_tlb_invalidate_all();
}
void call_linux(U32 a0, U32 a1, U32 a2)
{
void (*goto_start)(U32, U32);
cache_clean_invalidate();
tlb_invalidate();
__asm{
// mov r0, a0//%0
// mov r1, a1//%1
// mov r2, a2//%2
mov ip, #0
mcr p15, 0, ip, c13, c0, 0 /* zero PID */
mcr p15, 0, ip, c7, c7, 0 /* invalidate I,D caches */
mcr p15, 0, ip, c7, c10, 4 /* drain write buffer */
mcr p15, 0, ip, c8, c7, 0 /* invalidate I,D TLBs */
mrc p15, 0, ip, c1, c0, 0 /* get control register */
bic ip, ip, #0x0001 /* disable MMU */
mcr p15, 0, ip, c1, c0, 0 /* write control register */
//mov pc, r2
//nop
//nop
/* no outpus */
//: "r" (a0), "r" (a1), "r" (a2)
}
// SetClockDivider(1, 1);
// SetSysFclk(FCLK_200M); //start kernel, use 200M
Delay(100);
goto_start = (void (*)(U32, U32))a2;
(*goto_start)(a0, a1);
}
static int WaitDownload(U32 addr);
static int BoardUsbDownload(U32 addr, U32 run)
{
U8 fun;
int len;
//GPH9-->output GPH10-->output
rGPHCON = rGPHCON&~(0xf<<18)|(0x5<<18);
//GPG9-->input
rGPGCON &= 0xfff3ffff;
fun = 1;
//USB Device初始化
UsbdInit(fun);
Delay(100);
//GPG9-->output
rGPGCON |= 0x00040000;
//GPG9 ouput 1
rGPGDAT |= 0x0200;
//中断服务程序
pISR_USBD =(unsigned)IsrUsbd;
ClearPending(BIT_USBD);
EnableIrq(BIT_USBD);
len = WaitDownload(addr);
DisableIrq(BIT_USBD);
rGPGCON &= 0xfff3ffff; //GPG9 input
if(len>0) {
printf("\nUSB download finished,press any key to exit\n");
// getch();
// if(run) {//getyorn()) {
// //void (*fun)(void) = (void (*)(void))download_addr;
// //CacheDisable();
// //CacheFlush();
// rINTMSK = BIT_ALLMSK;
// Delay(100);
// //(*fun)();
// call_linux(0, 193, download_addr);
// }
}
return len;
}
void Test_usbdevice(void)
{
printf("FFT-S3C2410 USB DEVICE Download File Test,please Enter 'ESC' to exit\n");
LoadFileFromUsb(0,0,0,0);//USB DEVICE下载函数
printf("FFT-S3C2410 USB DEVICE Test Finished\n");
}
int LoadFileFromUsb(U32 a1, U32 a2, U32 a3, U32 a4)
{
puts("USB download file\n");
return BoardUsbDownload(0, 0);
}
/*************************************************************/
U32 UsbPoll(void);
//等待下载数据,addr--下载地址
static int WaitDownload(U32 addr)
{
U32 UsbConnected = 0;
U32 i, j;
U32 temp;
U8 tempMem[16];
U16 cs, dnCS;
#if 0
MMU_DisableICache();
//For multi-ICE.
//If ICache is not turned-off, debugging is started with ICache-on.
#endif
/*******************************/
/* Test program download */
/*******************************/
checkSum = 0;
downPt = tempMem; //This address is used for receiving first 8 byte.
download_addr = addr; //_RAM_STARTADDRESS;
download_len = 0;
puts("Wait for usb line,Press Esc key to exit \n");
while(!isUsbdSetConfiguration)
{
if(getkey()==ESC_KEY)
return -1;
}
puts("USB connected\n");
/* DisableIrq(BIT_USBD);
DisableIrq(BIT_DMA2);
while(!UsbPoll())
{
if(getkey()==ESC_KEY)
{
puts("USB download aborted.\n");
return -1;
}
}
DbgOut("End, Received %dBytes\n",(U32)downPt-download_addr);
checkSum=checkSum - *((unsigned char *)(download_addr+download_len-8-2))
- *( (unsigned char *)(download_addr+download_len-8-1) );
goto PollRxEnd;
*/
j=0;
while(download_len==0)
{
if(j%0x50000==0)
LedSet(0xf);//led4 on
if(j%0x50000==0x28000)
LedSet(0x0);
j++;
if(getkey()==ESC_KEY)
{
puts("USB download aborted.\n");
return -1;
}
if(isUsbdSetConfiguration)
{
if(!UsbConnected)
puts("Now, USB is connected,you may load file with usb");
UsbConnected = 1;
}
}
#if USBDMA //USBDMA = TRUE
ClearEp3OutPktReady(); //中断处理读完第一个包后未清OutPktReady位,在此清掉
if(download_len>EP3_PKT_SIZE)
{
if(download_len<=(0x80000))
ConfigEp3DmaMode(download_addr+EP3_PKT_SIZE-8,download_len-EP3_PKT_SIZE);
else
ConfigEp3DmaMode(download_addr+EP3_PKT_SIZE-8,0x80000-EP3_PKT_SIZE);
totalDmaCount=0;
}
else // download_len < EP3_PKT_SIZE
totalDmaCount=download_len;
#endif
printf("\nNow, Downloading [ADDRESS:%xh,TOTAL:%d]\n",
download_addr, download_len);
j=0x80000;
#if USBDMA
while(totalDmaCount<download_len)
{
if((rDCDST2-(U32)download_addr+8)>=j)
{
/* static int led = 0x0;
led ^=2;
Led_Display(led);*/
putch('d');
j+=0x80000;
}
if(getkey()==ESC_KEY)
{
int i;
ConfigEp3IntMode();
DisableIrq(BIT_DMA2);
DbgOut("\nReceive process aborted.\n");
DbgOut("Received %d bytes\n", totalDmaCount);
for(i=0; i<32;i++)
{
printf("0x%x = %x\n", download_addr+i*0x100000, *(U32 *)(download_addr+i*0x100000));
}
return -1;
}
}
#else
while(((U32)downPt-download_addr)<(download_len-8))
{
if(((U32)downPt-download_addr)>=j)
{
static int led = 0x0;
led ^= 2;
Led_Display(led);
putch('i');
j+=0x80000;
}
if(getkey()==ESC_KEY)
{
int i;
DbgOut("\nReceive process aborted.\n");
DbgOut("Received %d bytes\n", (U32)downPt-download_addr);
for(i=0;i<48;i++)
{
printf("0x%x = %x\n", download_addr+i*0x100000, *(U32 *)(download_addr+i*0x100000));
}
return -1;
}
}
#endif
#if USBDMA
/*******************************/
/* Verify check sum */
/*******************************/
printf("Now, Checksum calculation\n");
cs=0;
i=(download_addr);
j=(download_addr+download_len-10)&0xfffffffc;
while(i<j)
{
temp=*((U32 *)i);
i+=4;
cs+=(U16)(temp&0xff);
cs+=(U16)((temp&0xff00)>>8);
cs+=(U16)((temp&0xff0000)>>16);
cs+=(U16)((temp&0xff000000)>>24);
}
i=(download_addr+download_len-10)&0xfffffffc;
j=(download_addr+download_len-10);
while(i<j)
{
cs+=*((U8 *)i++);
}
checkSum = cs;
#else
//checkSum was calculated including dnCS. So, dnCS should be subtracted.
checkSum=checkSum - *((unsigned char *)(download_addr+download_len-8-2))
- *( (unsigned char *)(download_addr+download_len-8-1) );
#endif
//PollRxEnd:
dnCS=*((unsigned char *)(download_addr+download_len-8-2))+
(*((unsigned char *)(download_addr+download_len-8-1))<<8);
if(checkSum!=dnCS)
{
printf("Error!!! MEM:%x DN:%x\n", checkSum, dnCS);
return -1;
}
printf("Ok\n");
download_len-=10;
return download_len;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -