📄 out2boot.c
字号:
//=============================================================================
// Filename: out2boot.c
//
// Description: Program to convert a coff file (.out) into a header file
// for the mcu function that initializes the DSP.
//
// This program calls the hex500 utility which converts a
// coff file (filename.out) into two files containing the
// program and data memory.
//
//
//
// Copyright (c) 2002 Texas Instruments Incorporated
//
// Revision History:
// 12/18/00 EGO Started file.
// 02/06/01 EGO Fixed array notation in output file.
// 02/07/01 EGO Fixed data array too.
// 02/07/01 EGO Added #include of "swtype.h" for u16 definition.
// 03/14/01 EGO Two output files at Jim's request. One for prog, one for
// data.
// 03/16/01 EGO Make arrays const.
//=============================================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
void write_header(FILE *file, char *filename)
{
fprintf(file, "//===================================================="
"=====================\n");
fprintf(file, "// Filename: %s\n", filename);
fprintf(file, "//\n");
fprintf(file, "// Description:\n");
fprintf(file, "//\n");
fprintf(file, "// DSP code to be initalized by the bootloader.\n");
fprintf(file, "// This file was created automatically by out2boot.exe\n");
fprintf(file, "// DO NOT MODIFY IT BY HAND; it will be overwritten\n");
fprintf(file, "//\n");
fprintf(file, "// The format of each line is as follows: \n");
fprintf(file, "// - the number of data words to follow\n");
fprintf(file, "// - the starting destination address\n");
fprintf(file, "// - the data itself\n");
fprintf(file,
"// The final line with the length set to zero marks the end "
"of the data.\n");
fprintf(file, "//\n");
fprintf(file, "// Copyright (c) 2002 Texas Instruments Incorporated\n");
fprintf(file, "//===================================================="
"=====================\n");
}
int process(int doing_code, char *root_name)
{
#define LINE_LENGTH 200
char inFileName[_MAX_PATH]; // Filename of input file.
FILE *input; // Handle to coff file - serves as intput.
FILE *output; // Handle to output file
char buffer[LINE_LENGTH]; // Used to read lines out of input file
char output_fn[_MAX_PATH]; // Name of the output file
int numWords; // Number of words in the data line.
int done; // Flag to break out of loop.
int firstPass; // Flag signals first time through loop.
int i; // loop index
char temp[7]; // hold hex digits before conversion
// Open the input file
if (doing_code) {
sprintf(inFileName, "%s.i00", root_name);
}
else {
sprintf(inFileName, "%s.i10", root_name);
}
if ((input = fopen(inFileName, "r")) == NULL) {
// Input file does not exist -- nothing to do
return 1;
}
// Create the output file (<input file>_code.h or <input file>_data.h)
strcpy(output_fn, root_name);
if (doing_code) {
strcat(output_fn, "_code.h");
}
else {
strcat(output_fn, "_data.h");
}
if ((output = fopen(output_fn, "w")) == NULL) {
printf("unable to open output file %s\n", output_fn);
return 1;
}
// This section sets up the beginning of the MCU file.
write_header(output, output_fn);
if (doing_code) {
fprintf(output, "const unsigned short %s_program[] = {\n", root_name);}
else {
fprintf(output, "const unsigned short %s_data[] = {\n", root_name);
}
done = 0;
firstPass = 1;
while((!feof(input)) & (done == 0)) {
fgets(buffer,LINE_LENGTH,input); // get next line from input file
temp[0] = '0';
temp[1] = 'x';
temp[2] = '0';
temp[3] = '0';
temp[4] = buffer[1]; temp[5] = buffer[2]; // the byte count
temp[6] = '\0';
// Convert bytes to words.
numWords = strtoul(temp, 0, 16) / 2;
if(numWords > 0) {
if (firstPass == 1) {
fprintf(output, " %d, ", numWords);
firstPass = 0;
}
else {
fprintf(output, ",\n %d, ", numWords);
}
// the destination address
fprintf(output, "0x%c%c%c%c", buffer[3],
buffer[4], buffer[5], buffer[6]);
// sanity check the record of the Intel-format hex file;
// only prepared to deal with data record (tag '00')
if ((buffer[7] != '0') || (buffer[8] != '0')) {
printf("invalid record type seen in the Intel hex file\n");
fclose(input);
fclose(output);
return 1;
}
// the data itself
for (i = 0; i < numWords; i++) {
fprintf(output, ", 0x%c%c%c%c", buffer[9 + i*4],
buffer[10 + i*4],
buffer[11 + i*4],
buffer[12 + i*4]);
}
}
else {
fprintf(output,",\n %d };\n\n", numWords);
done = 1;
}
}
fclose(input);
fclose(output);
(void) unlink(inFileName);
return 0;
}
void main(int argc, char *argv[])
{
char dosString[100]; // Used to issue hex500 command.
if(argc != 1+1) {
printf("Usage: out2boot <input file>\n\n");
printf("Enter the input file name without an extension.\n");
printf("This tool converts an input coff file into header files\n");
printf("<input file>_code.h and <input file>_data.h, for use by "
"the MCU bootloader.\n\n");
return;
}
// hex500 options: -q = quiet, -i = intel format, -romwidth = word size
sprintf(dosString, "hex500 -q -i -romwidth 16 %s.out", argv[1]);
if(system(dosString) != 0) {
printf("hex500 failed.\n");
return;
}
// Output the DSP code sections
(void) process(TRUE, argv[1]);
// Output the DSP data sections
(void) process(FALSE, argv[1]);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -