⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 download.c

📁 EP9315的wince下载程序。download.exe
💻 C
📖 第 1 页 / 共 5 页
字号:

    //
    // Wait until we read a '?' from the serial port.
    //
    while(1)
    {
        //
        // 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 no SDRAM.\n");
            ClosePort(lPort,bUSBPort);
            return(1);
        }
    
        //
        // 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 copy SDRAM command.
    //
    SendChar('C');

    //
    // Wait until we read a '<' from the serial port.
    //
    WaitFor('<');
    
    //
    // Send the size of second boot code.
    //
    SendChar((char)SECEND_BOOT_CODE_SIZE);
    SendChar((char)(SECEND_BOOT_CODE_SIZE>>8));
    SendChar((char)(SECEND_BOOT_CODE_SIZE>>16));
    SendChar((char)(SECEND_BOOT_CODE_SIZE>>24));

    //
    // Wait until we read a '>' from the serial port.
    //
    WaitFor('D');

    //
    // Tell the user that we are downloading the second boot code.
    //
    printf("\r                                  \r");
    printf("Downloading second boot code...(  0%%)");

    //
    // Write the second boot code to the serial port.
    //
    codesize = (SECEND_BOOT_CODE_SIZE+3)&(~3);
    for(lIdx = 0; lIdx < codesize; lIdx++)
    {
        //
        // Write this character.
        //
        if(lIdx < SECEND_BOOT_CODE_SIZE)
            SendChar(pcInit[lIdx]);
        else
            SendChar(0);

        //
        // Periodically print out our progress.
        //
        //if((lIdx & 1023) == 1023)
        //if((lIdx & 127) == 127)
        if((lIdx & 15) == 15)
        {
            //
            // Wait until the transmit buffer is empty.
            //
            WaitTillEmpty();

            //
            // Print the updated status.
            //
            printf("\b\b\b\b\b%3d%%)", ((lIdx + 1) * 100) / codesize);
        }
    }
    printf("\r                                      \r");

    //
    // Wait until we read a '>' from the serial port.
    //
    WaitFor('>');

    //
    // Send a '-' character until we receive back a '?' character.
    // Reconstruct the serial communication between PC and second boot code.
    //
    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)
    {
    }

    if(bHaveFile)
    {
    	//printf("\rProgram File into the FLASH or EEPROM.");
    //
    // Send the program FLASH command.
    //
    switch (lStorage)
    {
    	case 1:
    	    SendChar('F');
            printf("\rProgram the nor FLASH.");
            break;
        case 2:
        {	
            SendChar('S');
	    //printf("\rProgram the spi EEPROM.Base=%x,Len=%x",lOffset,lFileSize);
         }
         	break;
        default:
            printf("\rNo storage device was specified and flashing G&R LED.");       
    	    break;
    }
    
    //
    // Read the next character from the serial port.
    //
    cChar = ReceiveChar(0);

    //
    // See if this is a 'X'.
    //
    if(cChar == 'A')
    {
        //printf("\rFind an AMD nor flash.");
        cChar = ReceiveChar(0);
        ManufactureId = cChar&0xff;
        cChar = ReceiveChar(0);
        ManufactureId = ManufactureId +(cChar&0xff)*256;
        printf("\rFlash Manufacture ID:%04x,",ManufactureId);
        
        cChar = ReceiveChar(0);
        DeviceId = cChar&0xff;
        cChar = ReceiveChar(0);
        DeviceId = DeviceId +(cChar&0xff)*256;
        printf(" Device ID:%04x\n",DeviceId);
    } 
    else if(cChar == 'I')
    {
        //printf("\rFind an Intel nor flash.");
        cChar = ReceiveChar(0);
        ManufactureId = cChar&0xff;
        cChar = ReceiveChar(0);
        ManufactureId = ManufactureId +(cChar&0xff)*256;
        printf("\rFlash Manufacture ID:%04x,",ManufactureId);
        
        cChar = ReceiveChar(0);
        DeviceId = cChar&0xff;
        cChar = ReceiveChar(0);
        DeviceId = DeviceId +(cChar&0xff)*256;
        printf(" Device ID:%04x\n",DeviceId);
    } 
    else if(cChar == 'S')
    {
        printf("\rFind an SST nor flash."); 
    } 
    else if(cChar == 'X')
    {
        fprintf(stderr, "The board contains an unknown %s.\n",
                bEEPROM ? "EEPROM" : "FLASH");
        ClosePort(lPort,bUSBPort);
        return(1);
    } 
    else
    {
    }
        
    //
    // 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.
    //
    SendChar('R');
    //printf("\r The %s need to be erased .Base=%x,Len=%x.",(bEEPROM ? "EEPROM" : "FLASH"),lOffset,lFileSize);
    //printf("\r                                                                          \r");


    
    //
    // 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,bUSBPort);
            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,bUSBPort);
            return(1);
        }
        
        //
        // See if this is a '='.
        //
        if(cChar == '=')
        {
            //printf("\r                     \r");
            printf("The Erase failed at offset %x len =%x.\n",lOffset,lFileSize);
            ClosePort(lPort,bUSBPort);
            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;
        }

        //
        // 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]);
        	
        	if(bUSBPort==1)
        	{
                    if((lLoop & 15) == 15)
        	    {
            	    //
            	    // Wait until the transmit buffer is empty.
            	    //
            	    WaitTillEmpty();
            	    }
        	}	
            }

            //
            // 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)
            {
                //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -