|
|
/* RS-232 example */
|
/* RS-232 example */
|
/* Compiles with Microsoft Visual C++ 5.0/6.0 */
|
/* Compiles with Microsoft Visual C++ 5.0/6.0 */
|
/* (c) fpga4fun.com KNJN LLC - 2003, 2004, 2005, 2006 */
|
/* (c) fpga4fun.com KNJN LLC - 2003, 2004, 2005, 2006 */
|
/* modified by Liwei 2007-9-2 */
|
/* modified by Liwei 2007-9-2 */
|
|
|
#include "stdio.h"
|
#include "stdio.h"
|
#include "windows.h"
|
#include "windows.h"
|
#include "conio.h"
|
#include "conio.h"
|
#define MAX_PRG_SIZE 0x1d00
|
#define MAX_PRG_SIZE 0x1d00
|
|
|
#define DEFAULT_BAUD_RATE 38400
|
#define DEFAULT_BAUD_RATE 38400
|
/*the two functions listed between are added by Liwei 2007-8-29*/
|
/*the two functions listed between are added by Liwei 2007-8-29*/
|
char HEX[]="0123456789ABCDEF" ;
|
char HEX[]="0123456789ABCDEF" ;
|
char hex[]="0123456789abcdef" ;
|
char hex[]="0123456789abcdef" ;
|
unsigned char hex2byte(char hex_char)
|
unsigned char hex2byte(char hex_char)
|
{
|
{
|
unsigned char i ;
|
unsigned char i ;
|
for(i=0;i<16;++i)if(HEX[i]==hex_char)return i ;
|
for(i=0;i<16;++i)if(HEX[i]==hex_char)return i ;
|
for(i=0;i<16;++i)if(hex[i]==hex_char)return i ;
|
for(i=0;i<16;++i)if(hex[i]==hex_char)return i ;
|
return 0 ;
|
return 0 ;
|
}
|
}
|
unsigned char hex2u8(char*par)
|
unsigned char hex2u8(char*par)
|
{
|
{
|
return(hex2byte(par[0])*16+hex2byte(par[1]));
|
return(hex2byte(par[0])*16+hex2byte(par[1]));
|
}
|
}
|
unsigned int par2u32(char*par)
|
unsigned int par2u32(char*par)
|
{
|
{
|
unsigned int i,ret=0 ;
|
unsigned int i,ret=0 ;
|
if(par==NULL)return ;
|
if(par==NULL)return ;
|
if((0==strncmp(par,"0x",2))||(0==strncmp(par,"0X",2)))
|
if((0==strncmp(par,"0x",2))||(0==strncmp(par,"0X",2)))
|
for(i=2;;++i)
|
for(i=2;;++i)
|
{
|
{
|
if(par[i]=='\0')return ret ;if(par[i]==' ')return ret ;
|
if(par[i]=='\0')return ret ;if(par[i]==' ')return ret ;
|
ret=ret*16+hex2byte(par[i]);
|
ret=ret*16+hex2byte(par[i]);
|
}
|
}
|
else
|
else
|
for(i=0;;++i)
|
for(i=0;;++i)
|
{
|
{
|
if(par[i]=='\0')return ret ;if(par[i]==' ')return ret ;
|
if(par[i]=='\0')return ret ;if(par[i]==' ')return ret ;
|
ret=ret*10+hex2byte(par[i]);
|
ret=ret*10+hex2byte(par[i]);
|
}
|
}
|
return 0 ;
|
return 0 ;
|
}
|
}
|
/****************************/
|
/****************************/
|
HANDLE hCom ;
|
HANDLE hCom ;
|
|
|
void OpenCom(unsigned int baudrate,char*comno)
|
void OpenCom(unsigned int baudrate,char*comno)
|
{
|
{
|
DCB dcb ;
|
DCB dcb ;
|
COMMTIMEOUTS ct ;
|
COMMTIMEOUTS ct ;
|
|
|
hCom=CreateFile(comno,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
|
hCom=CreateFile(comno,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
|
if(hCom==INVALID_HANDLE_VALUE)exit(1);
|
if(hCom==INVALID_HANDLE_VALUE)exit(1);
|
if(!SetupComm(hCom,4096,4096))exit(1);
|
if(!SetupComm(hCom,4096,4096))exit(1);
|
|
|
if(!GetCommState(hCom,&dcb))exit(1);
|
if(!GetCommState(hCom,&dcb))exit(1);
|
dcb.BaudRate=baudrate ;
|
dcb.BaudRate=baudrate ;
|
//modified by `Liwei 2007-9-4
|
//modified by `Liwei 2007-9-4
|
((DWORD*)(&dcb))[2]=0x1001 ;
|
((DWORD*)(&dcb))[2]=0x1001 ;
|
/* set port properties for TXDI + no flow-control */
|
/* set port properties for TXDI + no flow-control */
|
dcb.ByteSize=8 ;
|
dcb.ByteSize=8 ;
|
dcb.Parity=NOPARITY ;
|
dcb.Parity=NOPARITY ;
|
dcb.StopBits=2 ;
|
dcb.StopBits=2 ;
|
if(!SetCommState(hCom,&dcb))exit(1);
|
if(!SetCommState(hCom,&dcb))exit(1);
|
|
|
/* set the timeouts to 0 */
|
/* set the timeouts to 0 */
|
ct.ReadIntervalTimeout=MAXDWORD ;
|
ct.ReadIntervalTimeout=MAXDWORD ;
|
ct.ReadTotalTimeoutMultiplier=0 ;
|
ct.ReadTotalTimeoutMultiplier=0 ;
|
ct.ReadTotalTimeoutConstant=0 ;
|
ct.ReadTotalTimeoutConstant=0 ;
|
ct.WriteTotalTimeoutMultiplier=0 ;
|
ct.WriteTotalTimeoutMultiplier=0 ;
|
ct.WriteTotalTimeoutConstant=0 ;
|
ct.WriteTotalTimeoutConstant=0 ;
|
if(!SetCommTimeouts(hCom,&ct))exit(1);
|
if(!SetCommTimeouts(hCom,&ct))exit(1);
|
}
|
}
|
|
|
void CloseCom()
|
void CloseCom()
|
{
|
{
|
CloseHandle(hCom);
|
CloseHandle(hCom);
|
}
|
}
|
|
|
DWORD WriteCom(char*buf,int len)
|
DWORD WriteCom(char*buf,int len)
|
{
|
{
|
DWORD nSend ;
|
DWORD nSend ;
|
if(!WriteFile(hCom,buf,len,&nSend,NULL))exit(1);
|
if(!WriteFile(hCom,buf,len,&nSend,NULL))exit(1);
|
|
|
return nSend ;
|
return nSend ;
|
}
|
}
|
|
|
void WriteComChar(char b)
|
void WriteComChar(char b)
|
{
|
{
|
WriteCom(&b,1);
|
WriteCom(&b,1);
|
}
|
}
|
|
|
int ReadCom(char*buf,int len)
|
int ReadCom(char*buf,int len)
|
{
|
{
|
DWORD nRec ;
|
DWORD nRec ;
|
if(!ReadFile(hCom,buf,len,&nRec,NULL))exit(1);
|
if(!ReadFile(hCom,buf,len,&nRec,NULL))exit(1);
|
|
|
return(int)nRec ;
|
return(int)nRec ;
|
}
|
}
|
|
|
char ReadComChar()
|
char ReadComChar()
|
{
|
{
|
DWORD nRec ;
|
DWORD nRec ;
|
char c ;
|
char c ;
|
if(!ReadFile(hCom,&c,1,&nRec,NULL))exit(1);
|
if(!ReadFile(hCom,&c,1,&nRec,NULL))exit(1);
|
|
|
return nRec?c:0 ;
|
return nRec?c:0 ;
|
}
|
}
|
//ser_dld 19200 COM1 N
|
//ser_dld 19200 COM1 N
|
void main(int argc,char*argv[])
|
void main(int argc,char*argv[])
|
{
|
{
|
char c,s[11];
|
char c,s[11];
|
int i,baud_rate ;
|
int i,baud_rate ;
|
|
|
FILE*ff=fopen("code.txt","r");
|
FILE*ff=fopen("code.txt","r");
|
baud_rate=(NULL!=argv[1])?par2u32(argv[1]):DEFAULT_BAUD_RATE ;
|
baud_rate=(NULL!=argv[1])?par2u32(argv[1]):DEFAULT_BAUD_RATE ;
|
//baud_rate=DEFAULT_BAUD_RATE;
|
//baud_rate=DEFAULT_BAUD_RATE;
|
OpenCom(baud_rate,argv[2]);
|
OpenCom(baud_rate,argv[2]);
|
WriteComChar('?');
|
WriteComChar('?');
|
Sleep(1);
|
Sleep(1);
|
i=100 ;
|
i=100 ;
|
while(i--);
|
while(i--);
|
c=ReadComChar();
|
c=ReadComChar();
|
WriteComChar('!');
|
WriteComChar('!');
|
Sleep(10);
|
Sleep(10);
|
c=ReadComChar();
|
c=ReadComChar();
|
if((argv[3]!=NULL)&&(argv[3][0]=='N'))
|
if((argv[3]!=NULL)&&(argv[3][0]=='N'))
|
{
|
{
|
|
|
}
|
}
|
else
|
else
|
{
|
{
|
if(c!='O')
|
if(c!='O')
|
{
|
{
|
//printf("%c",c);
|
//printf("%c",c);
|
printf("1,Powerup you board.\n2,Chech cable.\nany key to exit...\n");
|
printf("1,Powerup you board.\n2,Chech cable.\nany key to exit...\n");
|
getchar();
|
getchar();
|
return ;
|
return ;
|
}
|
}
|
else
|
else
|
{
|
{
|
printf("Downloading,wait...\n");
|
printf("Downloading,wait...\n");
|
}
|
}
|
}
|
}
|
|
|
|
|
rewind(ff);
|
rewind(ff);
|
while(fgets(s,10,ff))
|
while(fgets(s,10,ff))
|
{
|
{
|
WriteComChar(hex2u8((char*)(s+0)));
|
WriteComChar(hex2u8((char*)(s+0)));
|
printf("%2.2x ",hex2u8((char*)(s+0)));
|
printf("%2.2x ",hex2u8((char*)(s+0)));
|
Sleep(1);
|
Sleep(1);
|
|
|
WriteComChar(hex2u8((char*)(s+2)));
|
WriteComChar(hex2u8((char*)(s+2)));
|
printf("%2.2x ",hex2u8((char*)(s+2)));
|
printf("%2.2x ",hex2u8((char*)(s+2)));
|
Sleep(1);
|
Sleep(1);
|
|
|
WriteComChar(hex2u8((char*)(s+4)));
|
WriteComChar(hex2u8((char*)(s+4)));
|
printf("%2.2x ",hex2u8((char*)(s+4)));
|
printf("%2.2x ",hex2u8((char*)(s+4)));
|
Sleep(1);
|
Sleep(1);
|
|
|
WriteComChar(hex2u8((char*)(s+6)));
|
WriteComChar(hex2u8((char*)(s+6)));
|
printf("%2.2x \n",hex2u8((char*)(s+6)));
|
printf("%2.2x \n",hex2u8((char*)(s+6)));
|
Sleep(1);
|
Sleep(1);
|
|
|
}
|
}
|
Sleep(1);
|
Sleep(1);
|
printf("Download to MIPS789 OK!\nPress RESET botton to RUN the MIPS789 program...");
|
printf("Download to MIPS789 OK!\nPress RESET botton to RUN the MIPS789 program...");
|
WriteComChar(0x88);
|
WriteComChar(0x88);
|
getch();
|
getch();
|
CloseCom();
|
CloseCom();
|
}
|
}
|
|
|