📄 download.c
字号:
#ifdef _WIN32
COMMTIMEOUTS sTimeouts;
char cChar;
DWORD dwLen;
//
// Fill in the timeout structure based on the timeout requested for this
// read.
//
sTimeouts.ReadIntervalTimeout = 0;
sTimeouts.ReadTotalTimeoutMultiplier = 0;
sTimeouts.ReadTotalTimeoutConstant = lTimeout;
sTimeouts.WriteTotalTimeoutMultiplier = 0;
sTimeouts.WriteTotalTimeoutConstant = 0;
//
// Set the timeout for this read.
//
SetCommTimeouts(hSerialPort, &sTimeouts);
//
// Read a character.
//
if(!ReadFile(hSerialPort, &cChar, 1, &dwLen, NULL))
{
//
// The read failed, so set the read character to a NULL.
//
cChar = 0;
}
//
// If we did not read a character, then set the character to NULL.
//
if(dwLen != 1)
{
cChar = 0;
}
//
// Return the character we read.
//
return(cChar);
#endif
#ifdef __linux__
struct timeb sTime;
char cChar;
int res;
time_t tStart, tNow;
//
// Get the current time.
//
ftime(&sTime);
tStart = (sTime.time * 1000) + sTime.millitm;
//
// Read the next character from the serial port.
//
while(1)
{
//
// Try to read a character from the serial port.
//
res = read(lSerialPort, &cChar, 1);
if(res == 1)
{
//
// We read a character, so break out of the loop.
//
break;
}
//
// Get the current time.
//
ftime(&sTime);
tNow = (sTime.time * 1000) + sTime.millitm;
//
// See if the timeout has expired.
//
if(lTimeout && ((tStart + lTimeout) < tNow))
{
cChar = 0;
break;
}
}
//
// Return the character read from the serial port.
//
return(cChar);
#endif
}
//****************************************************************************
//
// WaitTillEmpty waits until the serial port's output buffer is empty.
//
//****************************************************************************
void
WaitTillEmpty(void)
{
#ifdef _WIN32
//
// Wait for 10ms so the output buffer can drain.
//
Sleep(10);
#endif
#ifdef __linux__
//
// Wait until the output buffer is empty.
//
tcdrain(lSerialPort);
#endif
}
//****************************************************************************
//
// WaitFor waits until a specific character is read from the serial port.
//
//****************************************************************************
void
WaitFor(char cWaitChar)
{
char cChar;
//
// Wait until we read a specific character from the serial port.
//
while(1)
{
//
// Read a character.
//
cChar = ReceiveChar(0);
//
// Stop waiting if we received the character.
//
if(cChar == cWaitChar)
{
break;
}
}
}
//****************************************************************************
//
// Prints out a usage message for the program.
//
//****************************************************************************
void
Usage(void)
{
fprintf(stdout, "\
Usage: download {-b <baud>} {-h} {-n <MacAd> } {-o <offset>} {-p <port>}\n\
{-s <device> } {-v} <filename>\n\
\n\
Downloads a program image into either the NOR flash or the SPI EEPROM \n\
connected to a EP93xx board. It supports all sizes of Intel B3, C3, J3 and P30 \n\
flashes and AM29LV320D flash in either 16-bit or 32-bit configuration.\n\
For SPI EEPROM,it supports the Atmel 25F1024 EEPROM and SST \n\
SST25VF020,SST25VF040B.\n\
\n\
-b <baud> Use the specified baud rate (default is \"115200\"). Valid \n\
values are 9600, 19200, 38400, 57600, and 115200.\n\
\n\
-h Prints this usage message.\n\
\n\
-n [MacAd] Program the specified 6-byte-long Ethernet MAC address into\n\
EEPROM. The format of MAC address is XxXxXxXxXxXx,\n\
where Xx is a hex number of a byte. If MacAd is not specified,\n\
it prints the MAC address stored in EEPROM. This implies the\n\
argument \"-s 2\".\n\
\n\
-o <offset> Start programming at the given offset into the FLASH or EEPROM.\n\
It takes numbers, Num*K (2^10), and Num*M (2^20).\n\
This value must correspond to a block address boundary.\n\
If not, it will print an error message and exit. \n\
Default is \"0\".\n\
\n\
-p <port> Use the specified serial port.\n\
Append the specified port string to the serial port prefix,\n\
/dev/tty for Linux and COM for Windows.\n");
#ifdef _WIN32
fprintf(stdout, "\
For Windows, default is \"1\" which implies COM1.\n\
Valid values are 1, 2, 3, 4, 5, 6,...etc.\n");
#endif
#ifdef __linux__
fprintf(stdout, "\
For Linux, default is \"S0\" which implies /dev/ttyS0.\n\
Valid values are S0, S1,...USB0,USB1,...etc.\n");
#endif
fprintf(stdout, "\
\n\
-s <device> Program the specified image into storage device, the EEPROM \n\
or the NOR FLASH. (default is \"1\"). Valid\n\
values are 1 (NOR FLASH) and 2 (EEPROM).\n\
\n\
-v Prints the version of this program.\n\
\n\
filename The name of the file to be programmed into NOR flash or EEPROM.\n\
This argument is optional if \"-n\" is specified. \n\
\n\
The information of MAC address is placed at offset 0x1000 in the EEPROM,\n\
The 32 bytes of data at that position will be overwritten, and the\n\
first 0x1000 bytes of the EEPROM will be filled with zeros if a file name\n\
(filename) is not specified. If a file name is specified when -n is used, \n\
the combination of the file size and the -o offset should not overlap with\n\
the MAC address. In case overlap occurs, the program will print an error\n\
message and exit.\n\
\n");
}
//****************************************************************************
//
// This program waits for the '<' character from the boot ROM, sends the boot
// code, waits for the '>' from the boot ROM, waits for the '?' from the boot
// code, changes the serial port rate (preferably to 115200), downloads the
// user data file, and then prints out progress status as the boot code writes
// the user data file to the NOR FLASH.
//
//****************************************************************************
int
main(int argc, char *argv[])
{
long lPort = 1,lUSBPort=10, lRate = 115200, lDevice = 1, lStorage = 1;
long lFileSize, mFileSize, lIdx, codesize, lLoop, lSum, lOffset = 0,mOffset = 0;
long lType = 9312, lNumber;
char cChar, cFirstChar, cRateChar,cBuffer[1024], *pcString,PortString[30],MACString[30];
int bError = 0, bEEPROM = 0, bMAC = 0, bHaveFile = 0,bFlash = 0,bReadEEPROM=0;
int bUSBPort = 0;
unsigned long lNumber1=0,lNumber2=0;
unsigned char bBuffer[1024];
int iEEPORMType=0;
unsigned short ManufactureId=0,DeviceId=0;
FILE *pFile;
//
// First, set stdout to be unbuffered, so that our status messages are
// always displayed immediately.
//
setbuf(stdout, NULL);
//
// Prevent getopt from printing out any errors.
//
opterr = 0;
//
// See if there are any flags specified on the command line.
//
while((cChar = getopt(argc, argv, "b:d:hn:o:p:s:v")) != -1)
{
//
// Determine which flag was specified.
//
switch(cChar)
{
//
// See if this argument is "-b".
//
case 'b':
{
//
// Get the baud rate from the command line.
//
lRate = atoi(optarg);
//
// Make sure that the specified baud rate is valid.
//
if((lRate != 9600) && (lRate != 19200) && (lRate != 38400) &&
(lRate != 57600) && (lRate != 115200))
{
//
// Tell the user that the baud rate is invalid.
//
fprintf(stderr, "Invalid baud rate '%s'.\n\n", optarg);
//
// Print the usage message.
//
Usage();
//
// We're done.
//
return(1);
}
//
// We're done handling this argument.
//
break;
}
//
// See if this argument is "-d".
//
case 'd':
{
//
// Get the download channel from the command line.
//
lDevice = atoi(optarg);
//
// Make sure that the specified device is valid.
//
if((lDevice != 1) && (lDevice != 2) && (lDevice != 3))
{
//
// Tell the user that the specified device is invalid.
//
fprintf(stderr, "Invalid download device '%s'.\n\n", optarg);
//
// Print the usage message.
//
Usage();
//
// We're done.
//
return(1);
}
//
// We're done handling this argument.
//
break;
}
//
// See if this argument is "-h".
//
case 'h':
{
//
// Print the usage message.
//
Usage();
//
// We're done.
//
return(1);
}
//
// See if this argument is "-n".
//
case 'n':
{
int i;
//
// Get the board number from the command line.
//
//lNumber = atoi(optarg);
//lNumber=strtol(optarg,&MACString,1);
strcpy(MACString,optarg);
//fprintf(stderr, "n -- %x -- %s --len=%x.\n\n", lNumber,MACString,strlen(MACString));
if((strlen(MACString)>0)&&(MACString[0]=='-'))
{
if((MACString[1]=='b')||(MACString[1]=='d')||(MACString[1]=='h')||
(MACString[1]=='n')||(MACString[1]=='o')||(MACString[1]=='p')||
(MACString[1]=='s')||(MACString[1]=='t')||(MACString[1]=='v')
)
{
if(bFlash==1)
{
//
// Tell the user that the storage device is invalid.
//
fprintf(stderr, "the -s and -n options conflict.\n\n");
fprintf(stderr, "The storage device should flash.\n\n");
//
// Print the usage message.
//
//Usage();
//
// We're done.
//
return(1);
}
//fprintf(stderr, "Read MAC Address.\n\n");
//
// Indicate that the EEPROM is to be programmed, and that we
// have a MAC address.
//
bReadEEPROM = 1;
lStorage = 2;
bEEPROM = 1;
bMAC = 1;
optind = optind -1;
//
// We're done handling this argument.
//
break;
}
}
if((strlen(MACString)<13)&&(strlen(MACString)>0))
{
for(i=0;i<strlen(MACString);i++)
{
if(((MACString[i]>='0')&&(MACString[i]<='9'))||
((MACString[i]>='A')&&(MACString[i]<='F'))||
((MACString[i]>='a')&&(MACString[i]<='f'))
)
{
lNumber=1;
}
else
{
lNumber=0;
break;
}
}
}
else
{
lNumber=0;
}
//
// Make sure that the specified board number is valid.
//
if((lNumber < 1) || (lNumber > 65535))
{
//
// Tell the user that the board number is invalid.
//
fprintf(stderr, "Invalid board number '%s'.\n\n", optarg);
//
// Print the usage message.
//
Usage();
//
// We're done.
//
return(1);
}
if(bFlash==1)
{
//
// Tell the user that the storage device is invalid.
//
fprintf(stderr, "the -s and -n options conflict.\n\n");
fprintf(stderr, "The storage device should flash.\n\n");
//
// Print the usage message.
//
//Usage();
//
// We're done.
//
return(1);
}
//
// Indicate that the EEPROM is to be programmed, and that we
// have a MAC address.
//
lStorage = 2;
bEEPROM = 1;
bMAC = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -