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

📄 lpc21isp.c

📁 LPC2000系列化的ISP下载源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
            }            Pos++;            RecordLength   = Ascii2Hex(FileContent[Pos++]);            RecordLength <<= 4;            RecordLength  |= Ascii2Hex(FileContent[Pos++]);            DebugPrintf( 4, "RecordLength = %02X\n", RecordLength);            RecordAddress   = Ascii2Hex(FileContent[Pos++]);            RecordAddress <<= 4;            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);            RecordAddress <<= 4;            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);            RecordAddress <<= 4;            RecordAddress  |= Ascii2Hex(FileContent[Pos++]);            DebugPrintf( 4, "RecordAddress = %04X\n", RecordAddress);            RealAddress = RealAddress - (RealAddress & 0xffff) + RecordAddress;            DebugPrintf( 4, "RealAddress = %08lX\n", RealAddress);            RecordType      = Ascii2Hex(FileContent[Pos++]);            RecordType    <<= 4;            RecordType     |= Ascii2Hex(FileContent[Pos++]);            DebugPrintf( 4, "RecordType = %02X\n", RecordType);            if(RecordType == 0x00)          // 00 - Data record            {                    // Memory for binary file big enough ?                while(RealAddress + RecordLength > BinaryMemSize)                {                    BinaryMemSize <<= 1;                    IspEnvironment->BinaryContent = realloc(IspEnvironment->BinaryContent, BinaryMemSize);                }                    // We need to know, what the highest address is,                    // how many bytes / sectors we must flash                if(RealAddress + RecordLength > IspEnvironment->BinaryLength)                {                    IspEnvironment->BinaryLength = RealAddress + RecordLength;                    DebugPrintf( 3, "Image size now: %ld\n", IspEnvironment->BinaryLength);                }                for(i = 0; i < RecordLength; i++)                {                    Hexvalue        = Ascii2Hex(FileContent[Pos++]);                    Hexvalue      <<= 4;                    Hexvalue       |= Ascii2Hex(FileContent[Pos++]);                    IspEnvironment->BinaryContent[RealAddress + i] = Hexvalue;                }            }            else if(RecordType == 0x01)     // 01 - End of file record            {                break;            }            else if(RecordType == 0x02)     // 02 - Extended segment address record            {                for(i = 0; i < RecordLength * 2; i++)   // double amount of nibbles                {                    RealAddress <<= 4;                    if(i == 0)                    {                        RealAddress  = Ascii2Hex(FileContent[Pos++]);                    }                    else                    {                        RealAddress |= Ascii2Hex(FileContent[Pos++]);                    }                }                RealAddress <<= 4;            }            else if(RecordType == 0x03)     // 03 - Start segment address record            {                for(i = 0; i < RecordLength * 2; i++)   // double amount of nibbles                {                    RealAddress <<= 4;                    if(i == 0)                    {                        RealAddress  = Ascii2Hex(FileContent[Pos++]);                    }                    else                    {                        RealAddress |= Ascii2Hex(FileContent[Pos++]);                    }                }                RealAddress <<= 8;            }            else if(RecordType == 0x04)     // 04 - Extended linear address record, used by IAR            {                DebugPrintf( 1, "RecordType %02X not yet implemented - ignore ?\n", RecordType);                // exit(1);            }            else if(RecordType == 0x05)     // 05 - Start linear address record            {                DebugPrintf( 1, "RecordType %02X not yet implemented\n", RecordType);                exit(1);            }            while(FileContent[Pos++] != 0x0a)      // Search till line end            {            }        }        DebugPrintf( 2, "File %s converted to binary format...\n", IspEnvironment->input_file);            // When debugging is switched on, output result of conversion to file debugout.bin        if(debug_level >= 4)        {            int fdout;            fdout = open("debugout.bin", O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0777);            write(fdout, IspEnvironment->BinaryContent, IspEnvironment->BinaryLength);            close(fdout);        }    }    else    {        memcpy(IspEnvironment->BinaryContent, FileContent, FileLength);        IspEnvironment->BinaryLength = FileLength;    }    DebugPrintf( 2, "Image size : %ld\n", IspEnvironment->BinaryLength);        // check length to flash for correct alignment, can happen with broken ld-scripts    if (IspEnvironment->BinaryLength % 4 != 0)    {        unsigned long NewBinaryLength = ((IspEnvironment->BinaryLength + 3)/4) * 4;        DebugPrintf( 2, "Warning:  data not aligned to 32 bits, padded (length was %lX, now %lX)\n", IspEnvironment->BinaryLength, NewBinaryLength);        IspEnvironment->BinaryLength = NewBinaryLength;    }}#define ANALOG_DEVICES_SYNC_CHAR        ((BINARY)0x08)#define ANALOG_DEVICES_SYNC_RESPONSE    ("ADuC")#define ANALOG_DEVICES_SYNC_SIZE        (strlen( ANALOG_DEVICES_SYNC_RESPONSE))typedef struct {    BINARY product_id[15];    BINARY version[3];    BINARY reserved[4];    BINARY terminator[2];    } AD_SYNC_RESPONSE;/***************************** AnalogDevicesSync ************************//**  Attempt to synchronize with an Analog Device ARM micro.  Sends abackspace and reads back the microcontrollers response.  Performsmultiple retries. Exits the program on error, returns to caller in thecase of success.*/static void AnalogDevicesSync(ISP_ENVIRONMENT *IspEnvironment){    BINARY sync;                        /* Holds sync command.          */    AD_SYNC_RESPONSE response;          /* Response from micro.         */    int sync_attempts;                  /* Number of retries.           */        /*  Make sure we don't read garbage later instead of the        */        /* response we expect from the micro.                           */    ClearSerialPortBuffers(IspEnvironment);    DebugPrintf( 2, "Synchronizing\n"); /* Progress report.             */    sync = ANALOG_DEVICES_SYNC_CHAR;    /* Build up sync command.       */        /*  Perform the actual sync attempt.  First send the sync       */        /* character, the attempt to read back the response.  For the   */        /* AD ARM micro this is a fixed length block.  If response is   */        /* received attempt to validate it by comparing the first       */        /* characters to those expected.  If the received block does    */        /* not validate or is incomplete empty the serial buffer and    */        /* retry.                                                       */    for(sync_attempts = 0; sync_attempts < 5; sync_attempts++)    {        SendComPortBlock( IspEnvironment, &sync, 1);        if( ReceiveComPortBlockComplete( IspEnvironment, &response, sizeof( response),            500) == 0)        {            if( memcmp( response.product_id, ANALOG_DEVICES_SYNC_RESPONSE,                ANALOG_DEVICES_SYNC_SIZE) == 0)            {                return;            }            else            {                DumpString( 3, &response, sizeof(response),                    "Unexpected response to sync attempt ");            }        }        else        {            DebugPrintf( 3, "No (or incomplete) answer on sync attempt\n");        }        ClearSerialPortBuffers(IspEnvironment);    }    DebugPrintf( 1, "No (or unacceptable) answer on sync attempt\n");    exit(4);}typedef struct {    char start1;    char start2;    BINARY bytes;    char cmd;    BINARY address_h;    BINARY address_u;    BINARY address_m;    BINARY address_l;    BINARY data[251];    } AD_PACKET;/***************************** AnalogDevicesFormPacket ******************//**  Create an Analog Devices communication packet from the constituentelements.\param [in] cmd The command being sent, one of 'E' for erase, 'W' forwrite, 'V' for verify or 'R' for run..\param [in] no_bytes the number of data bytes to send with the command inthe packet.\param [in] address the address to apply the command to.\param [in] data the data to send with the packet, may be null if no_bytesis zero.\param[out] packet that will be filled.*/static void AnalogDevicesFormPacket( ISP_ENVIRONMENT *IspEnvironment,                                     char cmd, int no_bytes, unsigned int address,                                     const void *data, AD_PACKET *packet){    BINARY checksum;    const BINARY *data_in;    int i;    (void)IspEnvironment; /* never used in this function */        /*  Some sanity checking on the arguments.  These should only   */        /* fail if there is a bug in the caller.                        */        /*  Check 1) that the number of data bytes is in an acceptable  */        /* range, 2) that we have a non-null pointer if data is being   */        /* put in the packet and 3) that we have a non-null pointer to  */        /* the packet to be filled. We just exit with an error message  */        /* if any of these tests fail.                                  */    if( (no_bytes < 0) || (no_bytes > 250))    {        DebugPrintf( 1,            "The number of bytes (%d) passed to FormPacket is invalid.\n",            no_bytes);        exit( -1);    }    if( (data == 0) && (no_bytes != 0))    {        DebugPrintf( 1,            "A null pointer to data paased to FormPacket when data was expected.\n");        exit( -1);    }    if( packet == 0)    {        DebugPrintf( 1,            "A null packet pointer was passed to FormPacket.\n");        exit( -1);    }    checksum = 0;               /*  Checksum starts at zero.            */    data_in = data;             /*  Pointer pun so we can walk through  */                                /* the data.                            */    packet->start1 = 0x7;       /*  The start of the packet is constant.*/    packet->start2 = 0xE;        /*  Fill in the rest of the packet and calculate the checksum   */        /* as we go.                                                    */        /* The number of bytes is the number of data bytes + the        */        /* address bytes + the command byte.                            */    packet->bytes = no_bytes + 5;    checksum += packet->bytes;        /*  The command for the packet being sent.  No error checking   */        /* done on this.                                                */    packet->cmd = cmd;    checksum += cmd;        /*  Now break up the address and place in the proper packet     */        /* locations.                                                   */    packet->address_l = address & 0xFF;    packet->address_m = (address >> 8) & 0xFF;    packet->address_u = (address >> 16) & 0xFF;    packet->address_h = (address >> 24) & 0xFF;    checksum += packet->address_l;    checksum += packet->address_m;    checksum += packet->address_u;    checksum += packet->address_h;        /*  Copy the data bytes into the packet.  We could use memcpy   */        /* but we have to calculate the checksum anyway.                */    for( i = 0; i < no_bytes; i++)    {        packet->data[i] = data_in[i];        checksum += data_in[i];    }        /*  Finally, add the checksum to the end of the packet.         */    packet->data[i] = -checksum;}#define ANALOG_DEVICES_ACK      0x6#define ANALOG_DEVICES_NAK      0x7/***************************** AnalogDevicesSendPacket ******************//**  Send a previously form Analog Devices communication.  Retry acouple of times if needed but fail by exiting the program if no ACK isforthcoming.\param [in] packet the packet to send.*/static void AnalogDevicesSendPacket( ISP_ENVIRONMENT *IspEnvironment,                                     const AD_PACKET * packet){    BINARY response;    int retry = 0;    do {        retry++;                /*  Make sure we don't read garbage later instead of    */                /* the response we expect from the micro.               */        ClearSerialPortBuffers(IspEnvironment);                /*  Send the packet, the size is the number of data     */                /* bytes in the packet plus 3 bytes worth of header     */                /* plus checksum.                                       */        SendComPortBlock( IspEnvironment, packet, packet->bytes + 4);                /*  Receive the response and check, return to caller    */                /* if successful.                                       */        if( ReceiveComPortBlockComplete( IspEnvironment, &response, 1, 500) == 0)        {            if( response == ANALOG_DEVICES_ACK)            {                DebugPrintf( 3, "Packet Sent\n");                return;            }            if( response != ANALOG_DEVICES_NAK)            {                DebugPrintf( 3, "Unexpected response to packet (%x)\n", (int)response);            }            DebugPrintf( 2, "*");        }    } while( retry < 3);    DebugPrintf( 1, "Send packet failed\n");    exit( -1);}/***************************** AnalogDevicesErase ***********************/

⌨️ 快捷键说明

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