📄 uxmodem.c
字号:
/******************************************************************************
*
* Copyright (c) 2008 Nuvoton Electronics Corp.
* All rights reserved.
*
* File name: uxmodem.c
*
*
******************************************************************************/
#include <string.h>
#include "platform.h"
#include "usbd.h"
#include "uprintf.h"
extern UINT32 volatile Bulk_Out_Transfer_Size;
#define NON_CACHE 0x80000000
#define ALIGN 0x04
extern void usb_isr(void);
extern int usb_recv(UINT8* buf,UINT32 len);
extern int usb_send(UINT8* buf,UINT32 len);
int loop=1;
void usleep(int count)
{
int volatile i=0;
for(i=0;i<count;i++);
}
int Disable_USB(void)
{
outpw(REG_USBD_PHY_CTL, inpw(REG_USBD_PHY_CTL) & (~0x200)); // offset 0x704
outpw(REG_CLKEN, inpw(REG_CLKEN) & (~0x100));
outpw(REG_PWRON, inpw(REG_PWRON) & (~0x400));//D+ low power off usb
return 0;
}
int Enable_USB(void)
{
outpw(REG_CLKEN, inpw(REG_CLKEN) | 0x100);
outpw(REG_USBD_PHY_CTL, inpw(REG_USBD_PHY_CTL) | 0x200); // offset 0x704
/* wait PHY clock ready */
while(1)
{
outpw(REG_USBD_EPA_MPS, 0x20);
if (inpw(REG_USBD_EPA_MPS) == 0x20)
break;
}
USB_Initialize();
USB_Int_Init();
usleep(0xf000);
outpw(REG_PWRON, inpw(REG_PWRON) | 0x400);
return 0;
}
void usb_down(UINT32 _base,unsigned int* fileSize)
{
int len;
unsigned char* ptr;
unsigned int file_len=0;
unsigned char* _ch;
unsigned int* _ack;
_ch=(unsigned char*)(_base);
//_ch=((unsigned char*)(((unsigned int)_ch)/*|NON_CACHE*/));
usleep(10000);//give usb device init time
ptr=_ch;
file_len=0;
while(1)
{
if(Bulk_Out_Transfer_Size>0)
{
usb_recv((unsigned char*)ptr,4);
file_len=*((unsigned int*)ptr);
break;
}
}
if(((UINT32)_ch+file_len)%ALIGN!=0)
_ack=(unsigned int*)(_ch+file_len+(ALIGN-((UINT32)_ch+file_len)%ALIGN));
else
_ack=(unsigned int*)(_ch+file_len);
_ack=((unsigned int*)(((unsigned int)_ack)/*|NON_CACHE*/));
*fileSize=file_len;
//uprintf("file size %d\n", file_len);
//*_ack=4;
// usb_send((unsigned char*)_ack,4);//send ack to PC
do
{
if(Bulk_Out_Transfer_Size>0)
{
len=Bulk_Out_Transfer_Size;
usb_recv(ptr,len);//recv data from PC
ptr+=len;
*_ack=len;
usb_send((unsigned char*)_ack,4);//send ack to PC
}
}while((ptr-_ch)<file_len);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -