-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
380 lines (289 loc) · 9.36 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
//in this project build options linker settings have been modified
//to link to libusb.a
/* Program Author : N.Srinivas a.k.a. NeutroN StrikeR
Date: 15-10-2014
email id: striker.dbz[at]hotmail.com
The code is provided with: The MIT License (MIT)
Copyright (c) 2014 N.Srinivas a.k.a. NeutroN StrikeR (striker.dbz[at]hotmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include"libusb\include\lusb0_usb.h"
#include <stdio.h>
#include <stdlib.h>
#include<stdint.h> //for typedefs of uint8_t and uint16_t etc
#include<ctype.h> //for tolower();
extern usb_dev_handle * usbOpenDevice(int VID, char * vendor, int PID, char * product);
extern void list_all_usb_devs();
void usage(char * filename)
{
printf("\n\tUsage :\n");
printf("\t%s read <address> : Read word from specified Address\n",filename);
printf("\t%s read all : Read the whole chip\n",filename);
printf("\t%s read2f <file> : Read data to a .bin file from chip\n",filename);
printf("\t%s write <a> <w> : Write a word; 'w' is word, 'a' is address \n",filename);
printf("\t%s writef <file> : Write Data from a .bin to chip\n",filename);
printf("\t%s ON : Turn On Test LED\n",filename);
printf("\t%s OFF : Turn Off Test LED\n",filename);
}
#define USB_LED_OFF 0
#define USB_LED_ON 1
#define READ_WORD 2
#define WRITE_WORD 3
#define READ_BYTE 4
#define WRITE_BYTE 5
usb_dev_handle * handle = NULL;
uint16_t soft_clk_flag = 0;
int read_word(uint16_t *word,uint8_t address)
{
//remember to use this function usb_dev_handle * variable must
//be declared a global variable and don't use this fucntion without
//opening the usb device
if(handle == NULL)
{
fprintf(stderr,"\nError! Programmer Not Found\n");
return -2;
}
char buffer[2];
int flag;
flag = usb_control_msg(handle, USB_TYPE_VENDOR|USB_RECIP_DEVICE
|USB_ENDPOINT_IN,READ_WORD,soft_clk_flag,address,buffer,sizeof(buffer),5000);
*word = (uint8_t)buffer[0];
*word = *word<<8;
*word |= (uint8_t)buffer[1];
return flag;
}
int write_word(uint16_t word,uint8_t address)
{
//remember to use this function usb_dev_handle * variable must
//be declared a global variable and don't use this fucntion without
//opening the usb device
if(handle == NULL)
{
fprintf(stderr,"\nError! Programmer Not Found\n");
return -2;
}
int flag;
char buffer[2];
flag = usb_control_msg(handle,USB_TYPE_VENDOR|USB_RECIP_DEVICE
|USB_ENDPOINT_IN,WRITE_WORD,word,address,buffer,sizeof(buffer),5000);
return flag;
}
int led(uint8_t state)
{
if(state >= USB_LED_ON)
state = USB_LED_ON;
return usb_control_msg(handle,USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_IN,
state,0,0,0,0,5000);
}
int check_type()
{
uint16_t w_SoftClk, wo_SoftClk;
uint16_t sig_word = 0x5555;
uint16_t data_wclk,data_woclk;
//save original chip data
soft_clk_flag = 0;
read_word(&wo_SoftClk,0);//read word from address 0 without using SOFTCLK
soft_clk_flag = 1;
read_word(&w_SoftClk,0);//read word from address 0 using softclk
//now lets check
write_word(sig_word,0);//write signature word to address 0
soft_clk_flag = 0;
read_word(&data_woclk,0);
soft_clk_flag = 1;
read_word(&data_wclk,0);
if(sig_word == data_wclk)
{
soft_clk_flag = 1;
write_word(w_SoftClk,0);
printf("This chip requires Soft CLk\n");
}
else if(sig_word == data_woclk)
{
soft_clk_flag = 0;
write_word(wo_SoftClk,0);
printf("This chip does not require Soft CLk\n");
}
else
{
fprintf(stderr,"Chip not detected\n");
return -1;
}
return soft_clk_flag;
}
int main(int argc, char *argv[])
{
int nBytes = 0;
int address = 0;
uint16_t data;
if(argc<2)
{
usage(argv[0]);
return -1;
}
handle = usbOpenDevice(0x16c0,"NeuTroN",0x7755,"93CXXProg");
if(handle == NULL)
{
printf("\nError! Programmer Not found.\n");
return 0;
}
//this checking here detects whether the chip requires soft_clk or not
//and also determines if any chip exists or not
if(check_type() == -1)
{
usb_close(handle);
return 0;
}
if(stricmp(argv[1],"read")==0)
{
if(argc==3 && stricmp(argv[2],"all")==0)
{
uint8_t k;
for(k=0;k<64;k++)
{
nBytes = read_word(&data,k);
//blink led while transmission
led(k%2);
if(nBytes<0)
fprintf(stderr,"Error!\n");
else
printf("%04x\t",data);
}
}
else
{
address = atoi(argv[2]);
nBytes = read_word(&data,address);
if(nBytes < 0)
{
fprintf(stderr,"Error!");
usb_close(handle);
return -1;
}
printf("\n%04x is data\n",data);
}
//turn off led
led(USB_LED_OFF);
}
else if (argc == 4 && stricmp(argv[1],"write")==0)
{
uint16_t address = atoi(argv[2]);
uint16_t word = atoi(argv[3]);
nBytes = write_word(word,address);
if(nBytes<0)
fprintf(stderr,"Error!\n");
else
fprintf(stdout,"\nData written successfully.\n");
}
else if(argc > 2 && stricmp(argv[1],"read2f")==0)
{
char c;
printf("\nCaution! if file \"%s\" exists its contents will be overwritten.\
\nTo Continue press 'y' any other key to abort: ",argv[2]);
c = getchar();
c = tolower(c); //convert if upper case to lower
if(c!='y')
return 0;
FILE *fp = fopen(argv[2],"wb");
if(!fp)
{
fprintf(stderr,"\nCannot create file. Check filepath and name properly.\n");
return -1;
}
uint16_t fdata;
uint8_t pointer=0;
for(pointer=0;pointer<64;pointer++)
{
nBytes = read_word(&fdata,pointer);
//blink led while transmission
led(pointer%2);
fwrite(&fdata,2,1,fp);
//printf("%04x",fdata);
if(nBytes<0)
{
fprintf(stderr,"Error! Try Again\n");
fclose(fp);
return -1;
}
}
fclose(fp);
fprintf(stdout,"\nData written to file \"%s\" successfully\n",argv[2]);
//turn off led
led(USB_LED_OFF);
}
else if(argc == 3 && stricmp(argv[1],"writef")==0)
{
FILE *fp = fopen(argv[2],"rb");
if(!fp)
{
fprintf(stderr,"\nCannot open file. Check filepath and name properly.\n");
return -1;
}
uint16_t fdata,pointer=0;
printf("\nWriting:");//console progress indication
for(pointer=0;pointer<64;pointer++)
{
fread(&fdata,2,1,fp);
nBytes = write_word(fdata,pointer);
printf("#");//console progress indication
//blink led while transmission
led(pointer%2);
if(nBytes<0)
{
fprintf(stderr,"Error! Try Again\n");
fclose(fp);
return -1;
}
}
printf("\nWriting Complete Now Lets Verify Data.");
printf("\n\nVerifying:");
fseek(fp,0,SEEK_SET);
uint8_t count =0;
for(pointer=0;pointer<64;pointer++)
{
printf("#");
fread(&data,2,1,fp);
read_word(&fdata,pointer);
if(data != fdata)
count++;
led(pointer%2);
}
if(count>0)
printf("\n%d Error's occurred during verifying.\n",count);
else
{
printf("\nVerifying Complete.");
printf("\nData in file \"%s\" written Successfully.\n",argv[2]);
}
//turn off led
led(USB_LED_OFF);
}
else if(stricmp(argv[1],"on")==0)
{
led(USB_LED_ON);
}
else if(stricmp(argv[1],"off")==0)
{
led(USB_LED_OFF);
}
else if(stricmp(argv[1],"list")==0)
{
list_all_usb_devs();
}
else usage(argv[0]);
usb_close(handle);
return 0;
}