-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_parser.cpp
162 lines (135 loc) · 4.32 KB
/
hex_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QFile>
#include <QDebug>
#include "hex_parser.h"
#define MAX_DATA 32
//#define DEBUG_PARSER
/* debugging with global verbose */
#ifdef DEBUG_PARSER
#define DBG(format, arg...) \
printf("parser:" format, ##arg);
#else
#define DBG(format, arg...) {}
#endif
hex_parser::hex_parser()
{
app_eeprom_size = 0;
usr_eeprom_size = 0;
flash_size = 0;
}
hex_parser::~hex_parser()
{
}
/*
setup hex format
return: true if setup was OK
false if setup was NOK
*/
bool hex_parser::set_hex_format(hex_format format)
{
if (format == MCU_16F88) {
USR_EEPROM_MAX_SIZE = 160*2;
APP_EEPROM_MAX_SIZE = 32*2;
FLASH_MAX_SIZE = 1024;
FLASH_START_ADR = 0x1c00;
FLASH_END_ADR = FLASH_START_ADR+FLASH_MAX_SIZE;
USR_EEPROM_START_ADR = 0X4200;
USR_EEPROM_END_ADR = USR_EEPROM_START_ADR+USR_EEPROM_MAX_SIZE;
APP_EEPROM_START_ADR = 0x4340;
APP_EEPROM_END_ADR = APP_EEPROM_START_ADR+APP_EEPROM_MAX_SIZE;
} else if (format == MCU_16F886) {
USR_EEPROM_MAX_SIZE = 160*2;
APP_EEPROM_MAX_SIZE = 32*2;
FLASH_MAX_SIZE = 2048;
FLASH_START_ADR = 0x3800;
FLASH_END_ADR = FLASH_START_ADR+FLASH_MAX_SIZE;
USR_EEPROM_START_ADR = 0X4200;
USR_EEPROM_END_ADR = USR_EEPROM_START_ADR+USR_EEPROM_MAX_SIZE;
APP_EEPROM_START_ADR = 0x4340;
APP_EEPROM_END_ADR = APP_EEPROM_START_ADR+APP_EEPROM_MAX_SIZE;
} else
//unsupported hex format
return false;
return true;
}
/*
read and parse data from hex file
parameter: format of hex file
return: true if read was OK
false if read was NOK
*/
bool hex_parser::read_file(hex_format format)
{
FILE *hex;
unsigned int len, adr, type;
unsigned int data; // temporary buffer for data
unsigned int i;
// reset parser
app_eeprom_size = 0;
usr_eeprom_size = 0;
flash_size = 0;
// open hex file
hex = fopen(hexfile.toLatin1(), "r");
if (hex == NULL) {
qDebug() <<"Cannot open file" << hexfile.toLatin1();
return false;
}
// setup hex format
if (!this->set_hex_format(format))
return false;
// read hex file
while (1) {
if (fscanf(hex, ":%2x%4x%2x", &len, &adr, &type) != 3) {
goto read_err;
}
DBG("lenght is %d, address is %d, type is %d\n", len, adr, type);
switch (type) {
case DATA:
// read data to buffer
for (i=0; i<len; i++) {
if (fscanf(hex, "%2x", &data) != 1){
goto read_err;
}
if ((adr >= FLASH_START_ADR) && (adr < FLASH_END_ADR)) {
flash[flash_size] = data;
DBG("flash[%d] = 0x%x\n", flash_size,
flash[flash_size]);
flash_size++;
} else if ((adr >= USR_EEPROM_START_ADR) &&
(adr < USR_EEPROM_END_ADR)) {
usr_eeprom[usr_eeprom_size] = data;
DBG("usr_eeprom[%d] = 0x%x\n", usr_eeprom_size, usr_eeprom[usr_eeprom_size]);
usr_eeprom_size++;
fscanf(hex,"%2x", &data); // skip every second byte (=0x00)
i++;
} else if ((adr >= APP_EEPROM_START_ADR) &&
(adr < APP_EEPROM_END_ADR)) {
app_eeprom[app_eeprom_size] = data;
DBG("app_eeprom[%d] = 0x%x\n", app_eeprom_size, app_eeprom[app_eeprom_size]);
app_eeprom_size++;
fscanf(hex,"%2x", &data); // skip every second byte (=0x00)
i++;
}
// other addresses are ignored
}
DBG("\n");
break;
case END_OF_FILE:
DBG("EEPROM size: %d\n", app_eeprom_size + usr_eeprom_size);
DBG("FLASH size: %d\n", flash_size);
fclose(hex);
return true;
default:
fprintf(stderr, "Unsupported type of hex format.\n");
goto read_err;
}
// go to end of line
while (getc(hex) != '\n') // end of file check?
;
}
read_err:
fclose(hex);
return false;
}