📄 usb.c
字号:
#include <stdio.h>
#include "uart.h"
#include "usb.h"
#include "usblib.h"
#include "usb_dsc.h"
#include "timer.h"
#include "intc.h"
#include "regio.h"
#include "gpio.h"
#include "oem_gpio.h"
#include "mm.h"
#include "Romdemon.h"
#include "Flash_Def.h"
#include "v2cfg.h"
#define RESET_INT BIT16
extern void ExecutionApp(U32 AppStart);
/******* for USB download *******/
volatile U32 USBdownloadAddress;
volatile U8 *downPt;
volatile U32 downloadFileSize;
volatile U16 checkSum;
volatile U32 totalDLCnt, download_run = 0;
volatile int in_USB_Download;
void dn_usb_start(int dn_flag);
//void (*run)(void);
void __IsrUsbd(int eVec);
void USB_Download(void);
void dn_only(void){
dn_usb_start(0);
}
void dn_and_run(void){
dn_usb_start(1);
}
void dn_and_flash(void){
dn_usb_start(2);
}
void makeSE0(void){
GPIO_SetPinLevel(GPIO_PIN_USB_RESET, GPIO_PIN_LEVEL__HIGH);
MS_DELAY(10);
GPIO_SetPinLevel(GPIO_PIN_USB_RESET, GPIO_PIN_LEVEL__LOW);
}
void dn_usb_start(int dn_flag){
MM_DisableCache(MM_TCM_CLASS__DATA);
if(dn_flag) uprintf("Download file and run the file...\n");
else uprintf("Download file only...\n");
in_USB_Download = 1;
download_run = dn_flag;
while(in_USB_Download == 1) USB_Download();
MM_EnableCache(MM_TCM_CLASS__DATA);
}
void USB_Download(void){
int i, j = 0, first = 1;
U16 cs, dnCS, checkSum = 0;
U32 temp;
U8 tempMem[16];
USBdownloadAddress = (U32)tempMem;
downPt = (unsigned char *)USBdownloadAddress;
downloadFileSize = 0;
if(isUsbdSetConfiguration == 0){
uprintf("\nUSB host is not connected yet.\n");
uprintf("Connect USB cable to Visor Board USB slave port\n");
}
totalDLCnt = 0;
makeSE0();
while(downloadFileSize == 0){
if(first && isUsbdSetConfiguration){
uprintf("\n\nUSB host is connected\n");
uprintf("Input download addrss in 'Configuration->Option' menu of DNW\n");
uprintf("and then select the file you wanna download in 'USB Port->Transmit' menu of DNW\n");
uprintf("Waiting a download... \n");
first = 0;
}
//mDelay(150);
if(Uart_GetKey() && isUsbdSetConfiguration == 0){
uprintf("USB host is not connected yet.\n");
uprintf("Connect USB cable to Visor Board USB slave port\n");
}
}
uprintf("Now, Downloading [ADDRESS:0x%x ,TOTAL:%d bytes]\n",
USBdownloadAddress, downloadFileSize - 10);
while(1){
if(totalDLCnt >= downloadFileSize) break;
}
uprintf("RECEIVED FILE SIZE: %8d bytes\n", totalDLCnt - 10);
uprintf("Now, Checksum calculation\n");
cs = 0;
i = USBdownloadAddress;
j = (USBdownloadAddress + downloadFileSize - 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 = (USBdownloadAddress + downloadFileSize - 10) & 0xfffffffc;
j = (USBdownloadAddress + downloadFileSize - 10);
while(i < j) cs += *((U8 *)i++);
checkSum = cs;
dnCS = *((unsigned char *)(USBdownloadAddress + downloadFileSize -8 -2))+
(*((unsigned char *)(USBdownloadAddress + downloadFileSize -8 -1)) << 8);
if(checkSum != dnCS){
uprintf("Checksum Error!!! MEM:%x DN:%x\n",checkSum,dnCS);
return;
}
uprintf("Download O.K.\n\n");
if(download_run == 1){
ARM_DisableInterrupts();
ExecutionApp(0); // never return. Launch() ?
}
else if(download_run == 2)
UpgradeImage();
in_USB_Download = 0;
}
void __IsrUsbd(int eVec)
{
volatile U32 ep_sts_L1;
ep_sts_L1 = mdw_MajorIntStsRd();
if (ep_sts_L1 & BIT2) ep2_Rx_handler();
else if (ep_sts_L1 & BIT1) ep1_Tx_handler();
else if (ep_sts_L1 & BIT0) ep0_handler();
else if (ep_sts_L1 & RESET_INT) usb_reset();
mdw_MajorIntStsSet(ep_sts_L1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -