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

📄 dn_srec.c

📁 motorola 针对coldfire 5275 评估板的Dbug bootloader源程序
💻 C
字号:
/*
 * File:        srec.c
 * Purpose:     Routines to download Motorola S-Records.
 *
 * Notes:
 */
#include "src/include/dbug.h"
#include "src/uif/uif.h"
#include "src/uif/net/net.h"

#ifdef DBUG_NETWORK

#ifdef CPU_MCF5208
    extern ADDRESS prog_start_address;
    extern ADDRESS prog_end_address;
#endif  /* CPU_MCF5208 */

/********************************************************************/
static int
dn_gethexvalue (int uint8)
{
    switch (uint8)
    {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            return (uint8 - '0');
        case 'A':
        case 'a':
            return 10;
        case 'B':
        case 'b':
            return 11;
        case 'C':
        case 'c':
            return 12;
        case 'D':
        case 'd':
            return 13;
        case 'E':
        case 'e':
            return 14;
        case 'F':
        case 'f':
            return 15;
        default:
            return 0;
    }
}

/********************************************************************/
static int
dn_getS (void)
{
    /*
     * This routine skips all characters until an `S' is received.
     * It then returns the next character.
     * This routine is here in order to not echo characters back
     * out as they are received during a download.
     *
     * This code ignores NL,CR-LF end of line issues by waiting
     * for 'S'.
     */
    int c;

    while (TRUE)
    {
        c = tftp_in_char();
        if (c == -1)
            return -1;
        if (c == 'S')
            break;
    }

    /* Get type of S-record */
    return tftp_in_char();
}

/********************************************************************/
static int
dn_getSpair (void)
{
    int ch;
    int uint81, uint82;

    ch = tftp_in_char();
    if (ch == -1)
        return -1;

    uint81 = dn_gethexvalue(ch);
    uint81 = uint81 << 4;
    ch = tftp_in_char();
    if (ch == -1)
        return -1;
    uint82 = uint81 | dn_gethexvalue(ch);
    return uint82;
}

/********************************************************************/
int
dn_download_srecord (int offset_to_addr)
{
    int checksum, type, length, data, i;
    int done;
    ADDRESS address;
    
    #ifdef CPU_MCF5208
        unsigned char first_pass = 1; /* get the program start address first 
                                       * time through the loop */
    #endif  /* CPU_MCF5208 */

    done = FALSE;
    while (!done)
    {
        /* Get start of S-record */
        type = dn_getS();
        if (type == -1)
            return FALSE;

        /* Get record length */
        length = dn_getSpair();
        checksum = length;
        if (length == -1)
            return FALSE;

        /* Take appropriate action */
        switch (type)
        {
            case '1':
            case '2':
            case '3':
                address = NULL;
                type -= '0';
                for (i = 0; i <= type ; i++)
                {
                    /* formulate address */
                    data = dn_getSpair();
                    if (data == -1)
                        return FALSE;
                    address = (address << 8) | data;
                    /* maintain 8-bit checksum */
                    checksum = (data + checksum) & 0x00FF;
                    length--;
                }
                /* adjust for user specified offset */
                address = (ADDRESS)((int)address + offset_to_addr);
                #ifdef CPU_MCF5208
				    if( first_pass ) {
				        prog_start_address = address;
				        first_pass = 0;
				    }
				#endif  /* CPU_MCF5208 */

                if (board_dlio_vda((ADDRESS)address))
                {
                    /* Get data and put into memory */
                    for (i = 0; i < (length -1); i++)
                    {
                        data = dn_getSpair();
                        if (data == -1)
                            return FALSE;
                        *(uint8 *)address = (uint8)data;
                        /* checksum is computed from data written to RAM */
                        checksum = (*(uint8 *)address + checksum) & 0x00FF;
                        ++address;
                    }
                    #ifdef CPU_MCF5208
                        prog_end_address = address;
                    #endif  /* CPU_MCF5208 */

                    /* verify checksum */
                    data = dn_getSpair();
                    if (data == -1)
                        return FALSE;
                    if (((data - ~checksum) & 0x00FF) != 0)
                    {
                        printf("\bError:  S-record checksum error!\n");
                        printf("Expected:  %2X,  Received: %2X\n",
                            data,checksum);
                    }
                }
                else
                {
                    printf("\bError: Invalid download address: %#08X\n",address);
                    return FALSE;
                }
                break;
            case '7':
            case '8':
            case '9':
                type = type - '0';
                type = 10 - type;
                address = 0;
                while (type-- >= 0)
                {
                    data = dn_getSpair();
                    if (data == -1)
                        return FALSE;
                    checksum = (data + checksum) & 0x00FF;
                    address = (address << 8) | data;
                    length--;
                }
                address = address + offset_to_addr;
                while (length-- > 1)
                {
                    data = dn_getSpair();
                    if (data == -1)
                        return FALSE;
                    checksum = (data + checksum) & 0x00FF;
                }

                data = dn_getSpair();
                if (data == -1)
                    return FALSE;

                if (((data - ~checksum) & 0x00FF) != 0)
                {
                    printf("\bError:  S-record checksum error!\n");
                    return FALSE;
                }
                else
                {
                    printf("\bS-record download successful!\n");
                    cpu_pc_modify(address);
                }
                done = TRUE;
                break;
            case '0':
            case '4':
            case '5':
            case '6':
            default:
                break;
        }
    }
    return TRUE;
}

/********************************************************************/

#endif /* #ifdef DBUG_NETWORK */

⌨️ 快捷键说明

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