-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
323 lines (256 loc) · 8.66 KB
/
main.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
"pic32usb1388 -v1.0- by Michele Costantino - PIC32 programmer for the Microchip AN1388 USB HID Bootloader"
Copyright (C) 2013 Michele Costantino
This program is entirely based on pic32prog by Serge Vakulenko and it represents a modified version of a small part of it
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Release notes:
18/11/2013 - V1.0
- Initial release.
*/
// Linux required packages: libusb-dev, libusb-1.0-0-dev
// Codeblocks settings: Projects->Build Options->Linker Setting-> add link libraries:usb-1.0 and pthread
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "hidapi.h"
#define FRAME_SOH 0x01
#define FRAME_EOT 0x04
#define FRAME_DLE 0x10
#define CMD_READ_VERSION 0x01
#define CMD_ERASE_FLASH 0x02
#define CMD_PROGRAM_FLASH 0x03
#define CMD_READ_CRC 0x04
#define CMD_JUMP_APP 0x05
#define MICROCHIP_VID 0x04d8 /* Microchip Vendor ID */
#define BOOTLOADER_PID 0x003c /* Microchip AN1388 Bootloader */
#define MAXLINETEXT 300 // maximum number of characters per line in the text file
#define debug_level 0 // compile with this to 1 will enable debug messages
// Macros
/* Converting between hex and binary. */
#define NIBBLE(x) (isdigit(x) ? (x)-'0' : tolower(x)+10-'a')
#define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
// Variables
char inputhexfile[MAXLINETEXT], bootloader_rev[3]="000";
// Function prototypes
int parse_hex_file (char *filename);
void bootloader_command (hid_device *hiddev, unsigned char cmd,unsigned char *data, unsigned data_len);
unsigned calculate_crc (unsigned crc, unsigned char *data, unsigned nbytes);
static inline unsigned add_byte (unsigned char c, unsigned char *buf, unsigned indx);
void send_command (hid_device *hiddev, unsigned char *buf, unsigned nbytes);
int recv_command (hid_device *hiddev, unsigned char *buf);
// Main Program
int main (int argc, char **argv)
{
printf("\n");
printf("pic32usb1388 -v1.0- by Michele Costantino - PIC32 programmer for the Microchip AN1388 USB HID Bootloader.\n");
printf("This program is entirely based on pic32prog by Serge Vakulenko and it represents a modified version of a small part of it.\n");
switch (argc)
{
case 1:
printf("Usage: ");
printf("pic32usb1388 [filename.hex]\n");
printf("\nProgram terminated.\n");
return 0;
break;
case 2:
sprintf(inputhexfile,"%s",argv[1]);
parse_hex_file(argv[0]);
return 0;
break;
default:
return 0;
break;
}
}
int parse_hex_file (char *filename)
{
FILE *fd;
unsigned char buf [256], data[64];
int i,j,line;
hid_device *hiddev;
hiddev = hid_open (MICROCHIP_VID, BOOTLOADER_PID, 0);
if (! hiddev)
{
printf ("AN1388 bootloader not found: Is your USB cable connected and your board in bootloader mode?\n");
printf("\nProgram terminated.\n");
return 1;
}
else
printf ("AN1388 bootloader found -");
// Read AN1388 bootloader version
bootloader_command (hiddev, CMD_READ_VERSION, 0, 0);
printf (" version %d.%d\n",bootloader_rev[1], bootloader_rev[2]);
if ((fd = fopen(inputhexfile,"r"))== NULL)
{
printf ("ERROR: Cannot open the input %s file\n",inputhexfile);
printf("\nProgram terminated.\n");
return 1;
}
printf("Erasing...");
fflush (stdout);
bootloader_command (hiddev, CMD_ERASE_FLASH, 0, 0);
printf("done.\n");
fflush (stdout);
line=0;
while (fgets ((char*) buf, sizeof(buf), fd)) {
j=0;
for (i=1; i<10+2*HEX(buf+1); i+=2) {
data[j]=HEX(buf+i);
//printf("%02x",data[j]);
j++;
}
printf("Programming hex file line: %d\r",++line);
fflush (stdout);
bootloader_command (hiddev, CMD_PROGRAM_FLASH, data, j);
}
fclose (fd);
printf("\ndone.\n");
fflush (stdout);
printf("Jumping to the application.\n");
fflush (stdout);
bootloader_command (hiddev, CMD_JUMP_APP, 0, 0);
printf("Please disconnect the USB cable.\nEnd of the process.\n");
fflush (stdout);
hid_close(hiddev);
return 0;
}
void bootloader_command (hid_device *hiddev, unsigned char cmd,unsigned char *data, unsigned data_len)
{
unsigned char buf [64];
unsigned i, n, c, crc;
unsigned char reply [64];
int reply_len;
if (debug_level > 0) {
int k;
fprintf (stderr, "---Cmd%d", cmd);
for (k=0; k<data_len; ++k) {
if (k != 0 && (k & 15) == 0)
fprintf (stderr, "\n ");
fprintf (stderr, " %02x", data[k]);
}
fprintf (stderr, "\n");
}
memset (buf, FRAME_EOT, sizeof(buf));
n = 0;
buf[n++] = FRAME_SOH;
n = add_byte (cmd, buf, n);
crc = calculate_crc (0, &cmd, 1);
if (data_len > 0) {
for (i=0; i<data_len; ++i)
n = add_byte (data[i], buf, n);
crc = calculate_crc (crc, data, data_len);
}
n = add_byte (crc, buf, n);
n = add_byte (crc >> 8, buf, n);
buf[n++] = FRAME_EOT;
send_command (hiddev, buf, n);
if (cmd == CMD_JUMP_APP) {
/* No reply expected. */
return;
}
n = recv_command (hiddev, buf);
c = 0;
for (i=0; i<n; ++i) {
switch (buf[i]) {
default:
reply[c++] = buf[i];
continue;
case FRAME_DLE:
reply[c++] = buf[++i];
continue;
case FRAME_SOH:
c = 0;
continue;
case FRAME_EOT:
reply_len = 0;
if (c > 2) {
unsigned crc = reply[c-2] | (reply[c-1] << 8);
if (crc == calculate_crc (0, reply, c-2))
reply_len = c - 2;
}
if ((reply_len == 3) && (cmd == CMD_READ_VERSION)) {
bootloader_rev[1]=reply[1];
bootloader_rev[2]=reply[2];
}
if (reply_len > 0 && debug_level > 0) {
int k;
fprintf (stderr, "--->>>>");
for (k=0; k<reply_len; ++k) {
if (k != 0 && (k & 15) == 0)
fprintf (stderr, "\n ");
fprintf (stderr, " %02x", reply[k]);
}
fprintf (stderr, "\n");
}
return;
}
}
}
void send_command (hid_device *hiddev, unsigned char *buf, unsigned nbytes)
{
if (debug_level > 0) {
int k;
fprintf (stderr, "---Send");
for (k=0; k<nbytes; ++k) {
if (k != 0 && (k & 15) == 0)
fprintf (stderr, "\n ");
fprintf (stderr, " %02x", buf[k]);
}
fprintf (stderr, "\n");
}
hid_write (hiddev, buf, 64);
}
int recv_command (hid_device *hiddev, unsigned char *buf)
{
int n;
n = hid_read (hiddev, buf, 64);
if (n <= 0) {
fprintf (stderr, "hidboot: error %d receiving packet\n", n);
exit (-1);
}
if (debug_level > 0) {
int k;
fprintf (stderr, "---Recv");
for (k=0; k<n; ++k) {
if (k != 0 && (k & 15) == 0)
fprintf (stderr, "\n ");
fprintf (stderr, " %02x", buf[k]);
}
fprintf (stderr, "\n");
}
return n;
}
inline unsigned add_byte (unsigned char c, unsigned char *buf, unsigned indx)
{
if (c == FRAME_EOT || c == FRAME_SOH || c == FRAME_DLE)
buf[indx++] = FRAME_DLE;
buf[indx++] = c;
return indx;
}
unsigned calculate_crc (unsigned crc, unsigned char *data, unsigned nbytes)
{
static const unsigned short crc_table [16] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
};
unsigned i;
while (nbytes--) {
i = (crc >> 12) ^ (*data >> 4);
crc = crc_table[i & 0x0F] ^ (crc << 4);
i = (crc >> 12) ^ (*data >> 0);
crc = crc_table[i & 0x0F] ^ (crc << 4);
data++;
}
return crc & 0xffff;
}