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

📄 ce_merge.c

📁 TDK 6521 SOC 芯片 DEMO程序
💻 C
字号:
/*
    File: ce_merge.c
    ---- ce_merge ver 1.0 November 05, 2003 ----

    ce_merge ce_lctn old_lapie.hex ce.ce ce.dat new_lapie.hex

    Merges Intel hex files "old_lapie.hex" w/ "ce.ce" and "ce.dat" files.

    Outputs: Merged Intel hex file new_lapie.hex

    Copyright (C) 2005 Teridian SemiConductor, Corp. All Rights Reserved.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/timeb.h>

#define MAX_ROM_KSIZE   32
unsigned char Rom[ MAX_ROM_KSIZE * 1024 ];
char Buf[ 128 ];

#define MAX_CODE_SIZE 0x0C00
#define MAX_DATA_SIZE 0x0080

/*performs fopen on fname with type.  If result is NULL, exits with error*/
FILE *fopenorelse (char *fname, char *type)
{
    FILE *fn;

    fn = fopen (fname, type);

    if (fn == NULL)
    {
       printf ("Could not open '%s'.\n", fname);
       exit(0);
    }

    return(fn);
}

void Error (char *filename, int err, int ln)
{
    if (ln) 
       fprintf (stderr, "\nERROR %d at line %d in %s\n", err, ln, filename);
    else
    {
       fprintf (stderr, "\nUsage: ce_merge ce_code_adr ce_data_adr mpu.hex ce.ce ce.dat new_mpu.hex\n");
       fprintf (stderr, "\nfiles to be intel hex.\n");
    }

    exit(1);
}

int main (int argc, char * argv[])
{
    unsigned int    AddrH, AddrL, RecTyp, Byte;
    unsigned int    Addr, i, ByteCnt, ln, LnCSRd;
    unsigned char   LnCSCal;
      unsigned int  ce_code_lctn, ce_data_lctn;
      unsigned int  ce_code_size, ce_data_size;

    FILE *in_api, *in_code, *in_data;   // Input files.
    FILE *out_api;                      // Output file.
    char *versionstring="ver 1.0";
    char *in_api_name, *in_code_name, *in_data_name, *out_api_name;
    char *cptr;

    if (argc != 7) Error ("Arguments", 1, 0);

    sscanf (argv[1], "%x", &ce_code_lctn);
    sscanf (argv[2], "%x", &ce_data_lctn);
    in_api_name    = argv[3];
    in_code_name   = argv[4];
    in_data_name   = argv[5];
    out_api_name   = argv[6];

    printf ("\n---ce_merge %s%s---\n", versionstring, __DATE__);
    printf ("Beginning merge of MPU code w/ new CE code and data\n");

    cptr = &in_api_name[ strlen (in_api_name) - 4 ];

    if (strcmp (cptr, ".hex"))
    {  
       printf("Error: %s file must end with .hex suffix\n", in_api_name);
       exit(0);
    }

    cptr = &in_code_name[ strlen (in_code_name) - 3 ];

    if (strcmp (cptr, ".ce"))
    {  
       printf("Error: %s file must end with .ce suffix\n", in_code_name);
       exit(0);
    }

    cptr = &in_data_name[ strlen (in_data_name) - 4 ];

    if (strcmp (cptr, ".dat"))
    {  
       printf("Error: %s file must end with .dat suffix\n", in_data_name);
       exit(0);
    }

    cptr = &out_api_name[ strlen (out_api_name) - 4 ];

    if (strcmp (cptr, ".hex"))
    {  
       printf("Error: %s file must end with .hex suffix\n", out_api_name);
       exit(0);
    }

    in_api  = fopenorelse (in_api_name,  "r");
    in_code = fopenorelse (in_code_name, "r");
    in_data = fopenorelse (in_data_name, "r");
    out_api = fopenorelse (out_api_name, "w");

    /* Initially fill Rom with FFs */
    for (i = 0; i < sizeof (Rom); i++)
       Rom[ i ] = 0xFF;

    ln = 1;
    /* Read the input LAPIE hex file */
    do
    {
       i = fscanf (in_api, ":%2x%2x%2x%2x", &ByteCnt, &AddrH, &AddrL, &RecTyp);

       if (i != 4)
          Error (in_api_name, 1, ln);

       if (RecTyp > 1)
          Error (in_api_name, 2, ln);

       Addr = (AddrH << 8) + AddrL;
       LnCSCal = ByteCnt + AddrH + AddrL + RecTyp;

       for (i = 0; i < ByteCnt; i++)
       {
          if (fscanf (in_api, "%2x", &Byte) != 1)
             Error (in_api_name, 4, ln);

          if (Addr < sizeof (Rom))      /* Ignore higher addresses. */
             Rom[ Addr++ ] = Byte;

          LnCSCal += Byte;
       }

       if (fscanf (in_api, "%2x\n", &LnCSRd) != 1)
          Error (in_api_name, 5, ln);

       LnCSCal += LnCSRd;

       if (LnCSCal)
       {
         printf ("%2x %2x %2x %2x %2x %2x\n", ByteCnt, AddrH, AddrL, RecTyp, LnCSRd, LnCSCal);
         Error (in_api_name, 6, ln);
       }

       ln++;
    } while (RecTyp != 1);

    printf ("Read thru line# %d in file %s\n", ln - 1, in_api_name);

    ce_code_size = (unsigned short) ((Rom[ ce_code_lctn - 2 ] << 8) + Rom[ ce_code_lctn - 1 ]);
    ce_data_size = (unsigned short) ((Rom[ ce_data_lctn - 2 ] << 8) + Rom[ ce_data_lctn - 1 ]);

    printf ("\nCode @%4.4Xh Size %d; Data @%4.4Xh Size %d\n", ce_code_lctn, ce_code_size, ce_data_lctn, ce_data_size);

    if ((ce_code_size > MAX_CODE_SIZE) || (ce_data_size > MAX_DATA_SIZE))
    {
        fprintf (stderr, "\nIncompatible file, try different CE location\n");
        Error (in_api_name, 7, ln);
    }

    ln = 1;
    /* Read the input CE code file */
    do
    {
       i = fscanf (in_code, ":%2x%2x%2x%2x", &ByteCnt, &AddrH, &AddrL, &RecTyp);

       if (i != 4) Error (in_code_name, 1, ln);
       if (RecTyp > 1) Error (in_code_name, 2, ln);

       Addr = (AddrH << 8) + AddrL;
       LnCSCal = ByteCnt + AddrH + AddrL + RecTyp;

       for (i = 0; i < ByteCnt; i++)
       {
          if (fscanf (in_code, "%2x", &Byte) != 1) Error (in_code_name, 4, ln);

          if (Addr < (ce_code_size << 1))       /* Ignore higher addresses. */
             Rom[ ce_code_lctn + Addr++ ] = Byte;

          LnCSCal += Byte;
       }

       if (fscanf (in_code, "%2x\n", &LnCSRd) != 1) Error (in_code_name, 5, ln);

       LnCSCal += LnCSRd;

       if (LnCSCal != 0)
       {
          printf ("%2x %2x %2x %2x %2x %2x\n", ByteCnt, AddrH, AddrL, RecTyp, LnCSRd, LnCSCal);
          Error (in_code_name, 6, ln);
       }

       ln++;
    } while (RecTyp != 1);

    printf ("Read thru line# %d in file %s\n", ln - 1, in_code_name);

    ln = 1;
    /* Read the input CE data file */
    do
    {
       i = fscanf (in_data, ":%2x%2x%2x%2x", &ByteCnt, &AddrH, &AddrL, &RecTyp);

       if (i != 4) Error (in_data_name, 1, ln);
       if (RecTyp > 1) Error (in_data_name, 2, ln);

       Addr = (AddrH << 8) + AddrL;
       LnCSCal = ByteCnt + AddrH + AddrL + RecTyp;

       for (i = 0; i < ByteCnt; i++)
       {
          if (fscanf (in_data, "%2x", &Byte) != 1) Error (in_data_name, 4, ln);

          if (Addr < (ce_data_size << 2))      /* Ignore higher addresses. */
             Rom[ ce_data_lctn  + Addr++ ] = Byte;

          LnCSCal += Byte;
       }

       if (fscanf (in_data, "%2x\n", &LnCSRd) != 1) Error (in_data_name, 5, ln);

       LnCSCal += LnCSRd;

       if (LnCSCal != 0)
       {
          printf ("%2x %2x %2x %2x %2x %2x\n", ByteCnt, AddrH, AddrL, RecTyp, LnCSRd, LnCSCal);
          Error (in_data_name, 6, ln);
       }

       ln++;
    } while (RecTyp != 1);

    printf ("Read thru line# %d in file %s\n", ln - 1, in_data_name);

    ln = 1;
    /* Output new hex file for whole Rom */
    for (Addr = 0; Addr < sizeof (Rom);)
    {
       fprintf (out_api, ":20%4.4X00", Addr);
       LnCSCal = 0x20 + (Addr >> 8) + (Addr & 0xFF);

       for (i = 0; i <0x20; i++)
       {
          out_api, fprintf(out_api, "%2.2X", Rom[ Addr ]);
          LnCSCal += Rom[ Addr++ ];
       }

       LnCSCal = 0x100 - LnCSCal;
       fprintf (out_api, "%2.2X\n", LnCSCal);
       ln++;
    }
    fprintf (out_api, ":00000001FF\n");

    printf ("Wrote thru line# %d in file %s\n", ln, out_api_name);

    fclose (in_api);
    fclose (in_code);
    fclose (in_data);
    fclose (out_api);
    printf ("Completed merge of LAPIE w/ new CE code and data\n");

    return (0);
}

⌨️ 快捷键说明

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