📄 unformat.c
字号:
// // 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.////****************************************************************************voidWaitTillEmpty(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.////****************************************************************************voidWaitFor(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.////****************************************************************************voidUsage(void){ fprintf(stderr, "Usage: unformat {-h} {-v} {-p<port>}\n\n"); fprintf(stderr, "Unformats the NAND FLASH on a Cirrus Logic " BOARD ".\n\n"); fprintf(stderr, " -p <port> Use the specified serial port " "(default is \"1\"). Valid\n");#ifdef _WIN32 fprintf(stderr, " values are 1 (COM1), 2 (COM2), 3 " "(COM3), and 4 (COM4).\n\n");#endif#ifdef __linux__ fprintf(stderr, " values are 1 (/dev/ttyS0), 2 " "(/dev/ttyS1), 3\n"); fprintf(stderr, " (/dev/ttyS2), and 4 " "(/dev/ttyS3).\n\n");#endif fprintf(stderr, " -v Prints the version of this " "program.\n\n"); fprintf(stderr, " -h Prints this usage message.\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.////****************************************************************************intmain(int argc, char *argv[]){ long lPort = 1, lFileSize, lIdx, lLoop, lSum; char cChar, cFirstChar, cRateChar, cBuffer[1024]; int bError = 0; FILE *pFile; // // First, set stdout to be unbuffered, so that our status messages are // always displayed immediately. // setbuf(stdout, NULL); // // See if there are any flags specified on the command line. // while(1) { // // If we are out of arguments, or this argument does not start with a // '-', then stop looking at command line arguments. // if((argc == 1) || (argv[1][0] != '-')) { break; } // // See if this argument is "-p". // if(argv[1][1] == 'p') { // // Get the port number from the command line. // lPort = atoi(argv[1] + 2); // // Make sure that the specified port number is valid. // if((lPort != 1) && (lPort != 2) && (lPort != 3) && (lPort != 4)) { // // Tell the user that the port number is invalid. // fprintf(stderr, "Invalid serial port '%s'.\n\n", argv[1] + 2); // // Print the usage message. // Usage(); // // We're done. // return(1); } } // // See if this argument is "-h". // else if(argv[1][1] == 'h') { // // Print the usage message. // Usage(); // // We're done. // return(1); } // // See if this argument is "-v". // else if(argv[1][1] == 'v') { // // Print the version of this program. // printf(BOARD " Unformat Version 2.00 for ");#ifdef _WIN32 printf("Win32.\n");#endif#ifdef __linux__ printf("Linux.\n");#endif printf("Copyright (c) 1999-2001 Cirrus Logic, Inc.\n\n"); printf("Report bugs to <epdapps@crystal.cirrus.com>.\n"); // // We're done. // return(0); } // // An unrecognized flag was specifed. // else { // // Tell the user that the specified flag is invalid. // fprintf(stderr, "Invalid flag '%s'.\n\n", argv[1]); // // Print the usage message. // Usage(); // // We're done. // return(1); } // // Skip to the next argument on the command line. // argv++; argc--; } // // Make sure that there were no additional arguments specified. // if(argc != 1) { // // Print the usage message. // Usage(); // // We're done. // return(1); } // // Open the serial port to be used. // if(OpenPort(lPort) != 1) { return(1); } // // Program a baud rate of 9600, 8 data bits, and no parity. // SetBaud(9600); // // Tell the user to reset the board. // printf("Waiting for the board to wakeup..."); // // Wait until we read a '<' from the comm port. // WaitFor('<'); // // Tell the user that we are downloading the boot code. // printf("\r \r"); printf("Downloading unformat code...( 0%%)"); // // Write the boot code to the comm port. // for(lIdx = 0; lIdx < 2048; lIdx++) { // // Write this character. // SendChar(pcBoot[lIdx]); // // Periodically print out our progress. // if((lIdx & 127) == 127) { // // Wait until the transmit buffer is empty. // WaitTillEmpty(); // // Print the updated status. // printf("\b\b\b\b\b%3d%%)", ((lIdx + 1) * 100) / 2048); } } // // Wait until we read a '>' from the comm port. // WaitFor('>'); // // Indicate that the unformat is proceeding. // printf("\r \r"); printf("Unformatting..."); // // Wait until we read a '!' from the comm port. // WaitFor('!'); // // Indicate that the unformat succeeded. // printf("\r \r"); printf("Successfully unformatted the NAND FLASH.\n"); // // Success. // return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -