📄 usbtxrx.cpp
字号:
//Create the download progress dialogbox.
CreateDialog(_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DownloadProgressProc); //modaless
//ShowWindow(_hDlgDownloadProgress,SW_SHOW);
//isn't needed because the dialog box already has WS_VISIBLE attribute.
}
else
{
EB_Printf("[ERROR:Can't creat a thread. Memory is not sufficient]\n");
}
}
else if ( !strncmp(szTempFileName,_T("bin"),3) )
{
hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
EB_Printf(TEXT("[ERROR:File Open](%s)\n"), szFileName );
return;
}
fileSize=GetFileSize(hFile,NULL);
txBuf=(char *)malloc(fileSize);
if(txBuf==0)
{
EB_Printf(TEXT("[ERROR:Memory Allocation Fail:(%d)]\n"),fileSize);
return;
}
// Read ubootimag.ubi file
ReadFile(hFile,(void *)(txBuf),fileSize,&txBufSize,NULL);
if(txBufSize!=fileSize)
{
EB_Printf(TEXT("[ERROR:File Size Error]\n") );
return;
}
CloseHandle(hFile);
iTxBuf=0;
threadResult=_beginthread( (void (*)(void *))UsbTxFile,0x2000,(void *)0);
if(threadResult!=-1)
{
//Create the download progress dialogbox.
CreateDialog(_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DownloadProgressProc); //modaless
//ShowWindow(_hDlgDownloadProgress,SW_SHOW);
//isn't needed because the dialog box already has WS_VISIBLE attribute.
}
else
{
EB_Printf("[ERROR:Can't creat a thread. Memory is not sufficient]\n");
}
}
else if ( !strncmp(szTempFileName,_T("nb0"),3) )
{
nFileNumber = 1;
hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
EB_Printf(TEXT("[ERROR:File Open](%s)\n"), szFileName );
return;
}
fileSize=GetFileSize(hFile,NULL);
txBuf=(char *)malloc(fileSize+7+4+4+4+4+MAX_PATH);
if(txBuf==0)
{
EB_Printf(TEXT("[ERROR:Memory Allocation Fail:(%d)]\n"),fileSize);
return;
}
memset((void *)txBuf, 0, fileSize+7+4+4+4+4+MAX_PATH);
// Read nb0 file
ReadFile(hFile,(void *)(txBuf+7+4+4+4+4+MAX_PATH),fileSize,&txBufSize,NULL);
if(txBufSize!=fileSize)
{
EB_Printf(TEXT("[ERROR:File Size Error]\n") );
return;
}
CloseHandle(hFile);
ptxBuf = txBuf+7+4+4+4+4+MAX_PATH;
iTxBuf=0;
*((BYTE *)txBuf+0)=0x4E;
*((BYTE *)txBuf+1)=0x30;
*((BYTE *)txBuf+2)=0x30;
*((BYTE *)txBuf+3)=0x30;
*((BYTE *)txBuf+4)=0x46;
*((BYTE *)txBuf+5)=0x46;
*((BYTE *)txBuf+6)=0xa;
ptxBuf = txBuf + 7 + 4 + 4;
*((DWORD *)ptxBuf+0)=0; //nb0 start address == 0
*((DWORD *)ptxBuf+1)=fileSize; //nb0 filesize
strcpy((char *)(ptxBuf+8), szTitleName);
ptxBuf = txBuf+7;
nCheckSum = 0;
for ( i = 0; i < 4+4+MAX_PATH; i++ )
{
nCheckSum += (unsigned char)(*(ptxBuf+i));
}
*((DWORD *)ptxBuf+0)=nCheckSum; //checksum
*((DWORD *)ptxBuf+1)=nFileNumber; //file number = 1
szOutFileName = (char *)malloc(255);
strcpy(szOutFileName, szDirName);
strcat(szOutFileName, TEXT("ubootimage.ubi"));
hOutFile = CreateFile(szOutFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,0,NULL);
WriteFile(hOutFile, (void *)(txBuf), txBufSize, &fileBufSize,NULL);
CloseHandle(hOutFile);
threadResult=_beginthread( (void (*)(void *))UsbTxFile,0x2000,(void *)0);
if(threadResult!=-1)
{
//Create the download progress dialogbox.
CreateDialog(_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DownloadProgressProc); //modaless
//ShowWindow(_hDlgDownloadProgress,SW_SHOW);
//isn't needed because the dialog box already has WS_VISIBLE attribute.
}
else
{
EB_Printf("[ERROR:Can't creat a thread. Memory is not sufficient]\n");
}
}
}
#define USB_OVERLAPPED_IO FALSE //should be FALSE becasue USB overlapped IO doesn't work well.
#define TX_SIZE (4096*4)
VOID CALLBACK UsbWriteCompleteRoutine(DWORD dwErrorCode,DWORD dwNumberOfBytesTransfered,
LPOVERLAPPED lpOverlapped)
{
//nothing to do
}
void UsbTxFile(void *args)
{
void *txBlk;
ULONG txBlkSize;
DWORD nBytesWrite;
DWORD txBufSize100;
#if USB_OVERLAPPED_IO
//NOTE: hWrite should have FILE_FLAG_OVERLAPPED attribute.
OVERLAPPED os;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if(os.hEvent==NULL)
{
EB_Printf(TEXT("[ERROR:DoRxTx os.hEvent]\n"));
free((void *)txBuf);
CloseHandle(hWrite);
_endthread();
}
#endif
InitDownloadProgress();
txBufSize100=txBufSize/100;
if(txBufSize100==0)txBufSize100=1;
while(1)
{
if((txBufSize-iTxBuf) > TX_SIZE)
txBlkSize=TX_SIZE;
else
txBlkSize=txBufSize-iTxBuf;
txBlk=(void *)(txBuf+iTxBuf);
#if USB_OVERLAPPED_IO
WriteFile(hWrite,txBlk,txBlkSize,&nBytesWrite,&os);
//...
GetOverlappedResult(hWrite,&os,&temp,FALSE);
//...
//Why doesn't work GetOverlappedResult().
//It may seem that secbulk.sys doesn't support OverlappedIO.
#else
WriteFile(hWrite,txBlk,txBlkSize,&nBytesWrite,NULL);
#endif
//assert(nBytesWrite == WriteLen);
iTxBuf+=TX_SIZE;
if( ((iTxBuf/txBufSize100)%2)==1 )
DisplayDownloadProgress(iTxBuf/txBufSize100);
if(downloadCanceled==1)break; //download is canceled by user.
if(iTxBuf>=txBufSize)break;
}
CloseDownloadProgress();
free((void *)txBuf);
CloseHandle(hWrite);
#if USB_OVERLAPPED_IO
CloseHandle(os);
#endif
_endthread();
}
void MenuUsbReceive(HWND hwnd)
{
/* int nBytesRead, nBytes;
ULONG i, j;
UINT success;
if (fRead)
{
//ReadLen=64;
hRead = open_file( inPipe);
pinBuf = malloc(ReadLen);
if (pinBuf)
{
success = ReadFile(hRead,
pinBuf,
ReadLen,
&nBytesRead,
NULL);
printf("<%s> R (%04.4d) : request %06.6d bytes -- %06.6d bytes read\n",
inPipe, i, ReadLen, nBytesRead);
}
if (pinBuf)
{
free(pinBuf);
}
// close devices if needed
if(hRead != INVALID_HANDLE_VALUE)
CloseHandle(hRead);
}
*/
ULONG i;
UINT success;
UINT rxLength=128;
ULONG nBytesRead;
char *rxBuf;
rxBuf = (char *)malloc(rxLength);
if(rxBuf==NULL)return;
EB_Printf("=============== USB Receive Test ==============\n");
hRead = open_file(inPipe);
success = ReadFile(hRead,
rxBuf,
rxLength,
&nBytesRead,
NULL);
if(success)
{
for(i=0;i<rxLength;i++)
{
EB_Printf("%2x ",rxBuf[i]);
if(i%16==15)EB_Printf("\n");
}
}
else
{
EB_Printf("Error: can't receive data from the USBD\n");
}
#if 0
//This part is only for S3C2410 USB signal quality test
EB_Printf("============ 4096*128byte Read Test ===========\n");
for(i=0;i<4096;i++)
{
success = ReadFile(hRead,
rxBuf,
rxLength,
&nBytesRead,
NULL);
if(success)
{
EB_Printf("%2x,",(unsigned char)rxBuf[0]);
}
else
{
EB_Printf("Er,");
}
if(i%16==15)EB_Printf("\n");
}
#endif
EB_Printf("===============================================\n");
if (rxBuf)free(rxBuf);
// close devices if needed
if(hRead != INVALID_HANDLE_VALUE)
CloseHandle(hRead);
}
int IsUsbConnected(void)
{
HANDLE hDEV = open_dev();
if ( hDEV!=INVALID_HANDLE_VALUE )
{
CloseHandle(hDEV);
return 1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -