-
Notifications
You must be signed in to change notification settings - Fork 170
/
main.c
454 lines (398 loc) · 11.5 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
int RS232_OpenComport(int, int);
int RS232_PollComport(int, unsigned char *, int);
int RS232_SendByte(int, unsigned char);
int RS232_SendBuf(int, unsigned char *, int);
void RS232_CloseComport(int);
void RS232_cputs(int, const char *);
int RS232_IsCTSEnabled(int);
int RS232_IsDSREnabled(int);
void RS232_enableDTR(int);
void RS232_disableDTR(int);
void RS232_enableRTS(int);
void RS232_disableRTS(int);
#define SBEGIN 0x01
#define SDATA 0x02
#define SRSP 0x03
#define SEND 0x04
#define ERRO 0x05
FILE *pfile = NULL;
long fsize = 0;
int BlkTot = 0;
int Remain = 0;
int BlkNum = 0;
int DownloadProgress = 0;
int com;
int end = 0;
HANDLE Cport[16];
char comports[16][10]={"\\\\.\\COM1", "\\\\.\\COM2", "\\\\.\\COM3", "\\\\.\\COM4",
"\\\\.\\COM5", "\\\\.\\COM6", "\\\\.\\COM7", "\\\\.\\COM8",
"\\\\.\\COM9", "\\\\.\\COM10", "\\\\.\\COM11", "\\\\.\\COM12",
"\\\\.\\COM13", "\\\\.\\COM14", "\\\\.\\COM15", "\\\\.\\COM16"};
char baudr[64];
void ProcessProgram(void);
/*
* argv[0]----.exe file name
* argv[1]----ComPort number
* argv[2]----file path
* argv[3]----Device
*/
int main(int arg, char *argv[])
{
int fLen = 0;
int device = 0;
printf("Copyright (c) 2013 RedBearLab.com\n");
printf("%s version 0.5\n", argv[0]);
if(arg < 4)
{
printf("Invalid parameters.\n");
printf("Usage: %s <com number> <bin file> <device>\n", argv[0]);
printf("Example: %s 2 abc.bin 0\n", argv[0]);
printf(" <device>: 0 -- Default (e.g. UNO)\n");
printf(" 1 -- Leonardo\n");
return 0;
}
char form[5] = ".bin";
char format[5] = " ";
fLen = strlen(argv[2]);
if(fLen < 5)
{
printf("The .bin file name is invalid!\n\n");
return 0; // file path is not valid
}
format[3] = argv[2][fLen-1];
format[2] = argv[2][fLen-2];
format[1] = argv[2][fLen-3];
format[0] = argv[2][fLen-4];
if(0 != strcmp(form, format))
{
printf("File format must be .bin\n\n");
return 0;
}
com = atoi(argv[1]);
device = atoi(argv[3]);
printf("Comport : COM%d\n", com);
printf("Bin file: %s\n", argv[2]);
if(device == 0)
{
printf("Device : Default (e.g. UNO)\n\n");
}
else
{
printf("Device: Leonardo\n\n");
}
com = atoi(argv[1]) - 1;
if(1 == RS232_OpenComport(com, 115200))
{
return 0; // Open comprt error
}
printf("Comport open!\n");
if(device == 0)
{
RS232_disableDTR(com);
printf("<Baud:115200> <data:8> <parity:none> <stopbit:1> <DTR:off> <RTS:off>\n\n");
}
else
{
RS232_enableDTR(com);
printf("<Baud:115200> <data:8> <parity:none> <stopbit:1> <DTR:on> <RTS:off>\n\n");
}
RS232_disableRTS(com);
pfile = fopen(argv[2], "rb"); // read only
if(NULL == pfile)
{
printf("The file doesn't exist or is occupied!\n");
RS232_CloseComport(com);
printf("Comport closed!\n\n");
return 0;
}
printf("File open!\n");
fseek(pfile,0,SEEK_SET);
fseek(pfile,0,SEEK_END);
fsize = ftell(pfile);
fseek(pfile,0,SEEK_SET);
Remain = fsize % 512;
if(Remain != 0)
{
BlkTot = fsize / 512 + 1;
printf("!!WARNING: File's size isn't the integer multiples of 512 bytes, and \n");
printf(" the last block will be filled in up to 512 bytes with 0xFF! \n");
}
else
{
BlkTot = fsize / 512;
}
printf("Block total: %d\n\n", BlkTot);
BlkNum = 0;
printf("Enable transmission...\n");
unsigned char buf[2] = {SBEGIN, 0}; // Enable transmission, do not verify
if(RS232_SendBuf(com, buf, 2) != 2)
{
printf("Enable failed!\n");
fclose(pfile);
printf("File closed!\n");
RS232_CloseComport(com);
printf("Comport closed!\n\n");
return 0;
}
else
{
printf("Request sent already!\n");
}
printf("/********************************************************************/\n");
printf("* If there is no respond last for 3s, please press \"Ctrl+C\" to exit!\n");
printf("* And pay attention to :\n* 1. The connection between computer and Arduino;\n");
printf("* 2. The connection between Arduino and CC2540;\n");
printf("* 3. Whether the device you using is Leonardo or not;\n");
printf("* 4. Other unexpected errors.\n");
printf("/********************************************************************/\n\n");
printf("Waiting for respond from Arduino...\n\n");
while(!end)
{
ProcessProgram();
}
if(end == 2)
{
printf("Upload successfully!\n");
}
else
{
printf("Upload Failed!\n");
}
BlkNum = 0;
DownloadProgress = 0;
fclose(pfile);
printf("File closed!\n");
RS232_CloseComport(com);
printf("Comport closed!\n\n");
return 0;
}
void ProcessProgram()
{
int len;
unsigned char rx;
len = RS232_PollComport(com, &rx, 1);
if(len > 0)
{
switch(rx)
{
case SRSP:
{
if(BlkNum == BlkTot)
{
unsigned char temp = SEND;
RS232_SendByte(com, temp);
end = 2;
}
else
{
if(BlkNum == 0)
{
printf("Uploading firmware...\n\n");
}
DownloadProgress = 1;
unsigned char buf[515];
buf[0] = SDATA;
if((BlkNum == (BlkTot-1)) && (Remain != 0))
{
fread(buf+1, Remain, 1, pfile);
int filled = 512 - Remain;
int i = 0;
for(i; i<filled; i++)
{
buf[Remain+1+i] = 0xFF;
}
}
else
{
fread(buf+1, 512, 1, pfile);
}
unsigned short CheckSum = 0x0000;
for(unsigned int i=0; i<512; i++)
{
CheckSum += (unsigned char)buf[i+1];
}
buf[513] = (CheckSum >> 8) & 0x00FF;
buf[514] = CheckSum & 0x00FF;
RS232_SendBuf(com, buf, 515);
BlkNum++;
printf("%d ", BlkNum);
}
break;
}
case ERRO:
{
if(DownloadProgress == 1)
{
end = 1;
printf("Verify failed!\n");
}
else
{
end = 1;
printf("No chip detected!\n");
}
break;
}
default:
break;
}
len = 0;
}
}
int RS232_OpenComport(int comport_number, int baudrate)
{
if((comport_number>15)||(comport_number<0))
{
printf("illegal comport number\n\n");
return(1);
}
switch(baudrate)
{
case 110 : strcpy(baudr, "baud=110 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 300 : strcpy(baudr, "baud=300 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 600 : strcpy(baudr, "baud=600 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 1200 : strcpy(baudr, "baud=1200 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 2400 : strcpy(baudr, "baud=2400 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 4800 : strcpy(baudr, "baud=4800 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 9600 : strcpy(baudr, "baud=9600 data=8 parity=N stop=1 dtr=off rts=off");
break;
case 19200 : strcpy(baudr, "baud=19200 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 38400 : strcpy(baudr, "baud=38400 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 57600 : strcpy(baudr, "baud=57600 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 115200 : strcpy(baudr, "baud=115200 data=8 parity=N stop=1 dtr=off rts=off");
break;
case 128000 : strcpy(baudr, "baud=128000 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 256000 : strcpy(baudr, "baud=256000 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 500000 : strcpy(baudr, "baud=500000 data=8 parity=N stop=1 dtr=on rts=on");
break;
case 1000000 : strcpy(baudr, "baud=1000000 data=8 parity=N stop=1 dtr=on rts=on");
break;
default : printf("invalid baudrate\n");
return(1);
break;
}
Cport[comport_number] = CreateFileA(comports[comport_number],
GENERIC_READ|GENERIC_WRITE,
0, /* no share */
NULL, /* no security */
OPEN_EXISTING,
0, /* no threads */
NULL); /* no templates */
if(Cport[comport_number]==INVALID_HANDLE_VALUE)
{
printf("unable to open comport!\n\n");
return(1);
}
DCB port_settings;
memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */
port_settings.DCBlength = sizeof(port_settings);
if(!BuildCommDCBA(baudr, &port_settings))
{
printf("unable to set comport dcb settings\n\n");
CloseHandle(Cport[comport_number]);
return(1);
}
if(!SetCommState(Cport[comport_number], &port_settings))
{
printf("unable to set comport cfg settings\n\n");
CloseHandle(Cport[comport_number]);
return(1);
}
COMMTIMEOUTS Cptimeouts;
Cptimeouts.ReadIntervalTimeout = MAXDWORD;
Cptimeouts.ReadTotalTimeoutMultiplier = 0;
Cptimeouts.ReadTotalTimeoutConstant = 0;
Cptimeouts.WriteTotalTimeoutMultiplier = 0;
Cptimeouts.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts(Cport[comport_number], &Cptimeouts))
{
printf("unable to set comport time-out settings\n\n");
CloseHandle(Cport[comport_number]);
return(1);
}
return(0);
}
int RS232_PollComport(int comport_number, unsigned char *buf, int size)
{
int n;
if(size>4096) size = 4096;
/* added the void pointer cast, otherwise gcc will complain about */
/* "warning: dereferencing type-punned pointer will break strict aliasing rules" */
ReadFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL);
return(n);
}
int RS232_SendByte(int comport_number, unsigned char byte)
{
int n;
WriteFile(Cport[comport_number], &byte, 1, (LPDWORD)((void *)&n), NULL);
if(n<0) return(1);
return(0);
}
int RS232_SendBuf(int comport_number, unsigned char *buf, int size)
{
int n;
if(WriteFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL))
{
return(n);
}
return(-1);
}
void RS232_CloseComport(int comport_number)
{
CloseHandle(Cport[comport_number]);
}
int RS232_IsCTSEnabled(int comport_number)
{
int status;
GetCommModemStatus(Cport[comport_number], (LPDWORD)((void *)&status));
if(status&MS_CTS_ON) return(1);
else return(0);
}
int RS232_IsDSREnabled(int comport_number)
{
int status;
GetCommModemStatus(Cport[comport_number], (LPDWORD)((void *)&status));
if(status&MS_DSR_ON) return(1);
else return(0);
}
void RS232_enableDTR(int comport_number)
{
EscapeCommFunction(Cport[comport_number], SETDTR);
}
void RS232_disableDTR(int comport_number)
{
EscapeCommFunction(Cport[comport_number], CLRDTR);
}
void RS232_enableRTS(int comport_number)
{
EscapeCommFunction(Cport[comport_number], SETRTS);
}
void RS232_disableRTS(int comport_number)
{
EscapeCommFunction(Cport[comport_number], CLRRTS);
}
void RS232_cputs(int comport_number, const char *text) /* sends a string to serial port */
{
while(*text != 0) RS232_SendByte(comport_number, *(text++));
}
#ifdef __cplusplus
} /* extern "C" */
#endif