srec2data.cpp
00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <ctype.h>
00011 #include <fcntl.h>
00012 #include "sformatCtrl.h"
00013
00014
00015
00016 int main(int argc, char *argv[]) {
00017
00018
00019 FILE *fd = NULL;
00020 if (argc > 1) {
00021 fd = fopen(argv[1], "r");
00022 if (fd == NULL) {
00023 perror("fopen");
00024 exit(1);
00025 }
00026 } else {
00027 fprintf(stderr, "usage : %s <file_name>\n", argv[0]);
00028 exit(1);
00029 }
00030
00031
00032
00033 char line[49];
00034 SFormat_Ctrl::srec_t srec;
00035 int line_cnt = 0;
00036 int index;
00037 char code[4096];
00038 memset(code, 4096, 0);
00039 int last_index = 0;
00040 while (1) {
00041
00042 if (fscanf(fd, "%s\r\n", line) <= 0) {
00043 ++line_cnt;
00044 break;
00045 }
00046
00047 if (SFormat_Ctrl::parseSFormat(&srec, line) < 0) {
00048 printf("line: %s\n", line);
00049 fprintf(stderr, "sformat error at %d\n", line_cnt);
00050 exit(1);
00051 }
00052 if (srec.type < 1 || srec.type > 3) {
00053 continue;
00054 }
00055
00056 index = srec.address - 0xfffff000;
00057 for (int i = 0; i < srec.data_size; ++i) {
00058 code[index + i] = srec.byte_data[i];
00059 if (index + i > last_index) {
00060 last_index = index + i;
00061 }
00062 }
00063 }
00064 fclose(fd);
00065
00066
00067 char ch;
00068 printf("static char write_program[] = \n\"");
00069 for (int i = 0; i <= last_index; ++i) {
00070 ch = code[i];
00071 if (i % 16 == 0 && i != 0) {
00072 printf("\"\n\"");
00073 }
00074 printf("\\x%02x", (unsigned char)ch);
00075 }
00076 printf("\";\n");
00077
00078 return 0;
00079 }
00080