📄 download.c
字号:
// Tell the user to reset the board. // printf("Waiting for the board to wakeup..."); // // Wait until we read a '<' from the serial port. // WaitFor('<'); // // Tell the user that we are downloading the boot code. // printf("\r \r"); printf("Downloading boot code...( 0%%)"); // // Write the boot code to the serial 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); } } printf("\r \r"); // // Wait until we read a '>' from the serial port. // WaitFor('>'); // // Wait until we read a '?' from the serial port. // while(1) { // // Read the next character from the serial port. // cChar = ReceiveChar(0); // // Quit waiting if this is a '?'. // if(cChar == '?') { break; } } // // Get the baud rate character for the given baud rate. // switch(lRate) { case 9600: { cRateChar = '0'; break; } case 19200: { cRateChar = '1'; break; } case 38400: { cRateChar = '2'; break; } case 57600: { cRateChar = '3'; break; } case 115200: { cRateChar = '4'; break; } } // // Tell the boot code to switch to the desired baud rate. // SendChar('B'); SendChar(cRateChar); // // Wait until the output buffer is empty. // WaitTillEmpty(); // // Switch our baud rate to the desired rate. // SetBaud(lRate); // // Send a '-' character until we receive back a '?' character. // while(1) { // // Send a '-' character. // SendChar('-'); // // Read the character. // cChar = ReceiveChar(10); // // Quit waiting if this is a '?'. // if(cChar == '?') { break; } } // // Empty out the input queue. // while((cChar = ReceiveChar(10)) != 0) { } // // Send the program FLASH command. // if(bEEPROM) { SendChar('S'); } else { SendChar('F'); } // // Read the next character from the serial port. // cChar = ReceiveChar(0); // // See if this is a 'X'. // if(cChar == 'X') { fprintf(stderr, "The board contains an unknown %s.\n", bEEPROM ? "EEPROM" : "FLASH"); ClosePort(lPort); return(1); } // // Send the starting offset. // SendChar((char)(lOffset & 0xFF)); SendChar((char)((lOffset >> 8) & 0xFF)); SendChar((char)((lOffset >> 16) & 0xFF)); SendChar((char)((lOffset >> 24) & 0xFF)); // // Send the length of the data file. // SendChar((char)(lFileSize & 0xFF)); SendChar((char)((lFileSize >> 8) & 0xFF)); SendChar((char)((lFileSize >> 16) & 0xFF)); SendChar((char)((lFileSize >> 24) & 0xFF)); // // Tell the user that we are erasing the FLASH. // printf("Erasing the %s...", bEEPROM ? "EEPROM" : "FLASH"); // // Wait until we receive a '!' indicating that the FLASH has been erased. // while(1) { // // Read the next character from the serial port. // cChar = ReceiveChar(0); // // Quit waiting if this is a '!'. // if(cChar == '!') { break; } // // See if this is a '&'. // if(cChar == '&') { printf("\r \r"); fprintf(stderr, "The image is too large for the %s.\n", bEEPROM ? "EEPROM" : "FLASH"); ClosePort(lPort); return(1); } // // See if this is a '%'. // if(cChar == '%') { printf("\r \r"); fprintf(stderr, "The offset '%ld' is not valid for the %s.\n", lOffset, bEEPROM ? "EEPROM" : "FLASH"); ClosePort(lPort); return(1); } } // // Tell the user that we are downloading the file data. // printf("\rProgramming the %s...( 0%%)", bEEPROM ? "EEPROM" : "FLASH"); // // Send the actual data in the file. // for(lIdx = 0; lIdx < lFileSize; lIdx += 1024) { // // See if the data should come from a file. // if(bHaveFile) { // // Read the next 1K block from the file. // lLoop = fread(cBuffer, 1, 1024, pFile); } else { // // Clear the data buffer. // memset(cBuffer, 0, 1024); lLoop = 1024; } // // See if the Ethernet MAC address is to be programmed, and the correct // memory location has been reached. // if(bMAC && (lIdx == 0x1000)) { // // The first four bytes should be "EMAC", the tag to indicate a // Ethernet MAC block. // cBuffer[0] = 'E'; cBuffer[1] = 'M'; cBuffer[2] = 'A'; cBuffer[3] = 'C'; // // The next six bytes are the Ethernet MAC address. // cBuffer[4] = 0x00; cBuffer[5] = 0xdc; cBuffer[6] = 0x6c; cBuffer[7] = lType - 9300; cBuffer[8] = lNumber / 256; cBuffer[9] = lNumber & 255; // // The next six bytes are reserved. // cBuffer[10] = 0x00; cBuffer[11] = 0x00; cBuffer[12] = 0x00; cBuffer[13] = 0x00; cBuffer[14] = 0x00; cBuffer[15] = 0x00; // // The next 16 bytes are the string name for this board. // cBuffer[16] = 'e'; cBuffer[17] = 'd'; cBuffer[18] = 'b'; cBuffer[19] = '0' + (lType / 1000); cBuffer[20] = '0' + ((lType / 100) % 10); cBuffer[21] = '0' + ((lType / 10) % 10); cBuffer[22] = '0' + (lType % 10); cBuffer[23] = '-'; cBuffer[24] = '0' + (lNumber / 10000); cBuffer[25] = '0' + ((lNumber / 1000) % 10); cBuffer[26] = '0' + ((lNumber / 100) % 10); cBuffer[27] = '0' + ((lNumber / 10) % 10); cBuffer[28] = '0' + (lNumber % 10); cBuffer[29] = 0x00; cBuffer[30] = 0x00; cBuffer[31] = 0x00; } // // If we could not read 1K from the file, then fill the remainder of // the buffer with zeros. // for(; lLoop < 1024; lLoop++) { cBuffer[lLoop] = 0; } // // Send this block of data until it is correctly received and // programmed into the FLASH. // do { // // Send the data for this block. // for(lLoop = 0; lLoop < 1024; lLoop++) { SendChar(cBuffer[lLoop]); } // // Compute the checksum for this block. // for(lLoop = 0, lSum = 0; lLoop < 1024; lLoop++) { lSum += (long)(unsigned char)cBuffer[lLoop]; } // // Send the checksum for this block. // SendChar((char)(lSum & 0xFF)); SendChar((char)((lSum >> 8) & 0xFF)); SendChar((char)((lSum >> 16) & 0xFF)); SendChar((char)((lSum >> 24) & 0xFF)); // // We now need to wait to see what the target does with this block // of data. Several things could happen: // 1) Everything is OK and it sends a '#' character. // 2) The block checksum was bad and it sends a '@' character. // 3) Some bytes were lost in the transfer and it does nothing. // To handle all these cases, we wait for a while to receive a // character. If we never receive a character, we assume that a // byte was lost and start sending extra bytes, up to 16 extra // bytes. The extra bytes are always 0xFF, so that the checksum // will always fail and the target will request a block resend. If // we send 1028 extra bytes and still do not receive a reply, then // assume that the target died and abort the download. // for(lLoop = 0; lLoop < 16; lLoop++) { // // Read the character from the serial port. // cChar = ReceiveChar(4000); if(cChar != 0) { break; } // // Send a 0xFF character. // SendChar(0xFF); } // // If we could not get a response from the target, then indicate // an error and quit trying to send this and further blocks. // if(lLoop == 16) { // // Indicate that there was an error. // bError = 1; // // Stop trying to send this block. // break; } } while(cChar != '#'); // // If there was an error, then quit sending data. // if(bError) { break; } // // Print out our progress. // printf("\b\b\b\b\b%3d%%)", (((lIdx + 1024) / 1024) * 100) / (lFileSize / 1024)); } printf("\r \r"); // // If there has not been an error thus far, read the final status from the // target. // if(!bError) { // // Read characters from the serial port until we receive a '*'. // while(1) { // // Read a character from the serial port. // cChar = ReceiveChar(0); // // If the character is a '^', then there was an error programming // the FLASH. // if(cChar == '^') { bError = 1; } // // If the character is a '*', then we are done. // if(cChar == '*') { break; } } } // // Close the file. // if(bHaveFile) { fclose(pFile); } // // Close the serial port. // ClosePort(lPort); // // Tell the user we are done. // if(!bError) { if(bHaveFile) { printf("Successfully programmed '%s'.\n", argv[argc - 1]); } else { printf("Successfully programmed the Ethernet MAC address.\n"); } return(0); } else { if(bHaveFile) { printf("Failed to program '%s'.\n", argv[argc - 1]); } else { printf("Failed to program the Ethernet MAC address.\n"); } return(1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -