-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
74 lines (66 loc) · 1.57 KB
/
main.c
File metadata and controls
74 lines (66 loc) · 1.57 KB
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ft232h.h"
#define ROM_SIZE 0x10000
int main(int argc, char **argv) {
int ret;
char *filename = NULL;
char op = ' ';
struct ft232h_context *context;
while((ret = getopt(argc, argv, "hr:w:v:p:")) != -1) {
switch(ret) {
case 'r':
filename = optarg;
op = 'r';
break;
case 'w':
filename = optarg;
op = 'w';
break;
case 'h':
printf("Usage ft232h-eeprom -r <file> -w <file>\n");
printf(" -r <file>: Read eeprom contents to file\n");
printf(" -w <file>: Write file contents to eeprom\n");
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
context = (struct ft232h_context *)malloc(sizeof(struct ft232h_context));
ft232h_init(context);
char *buf;
FILE *f;
int size;
switch(op) {
case 'r':
printf("Reading from chip to %s...\n", filename);
buf = (char *)malloc(ROM_SIZE);
for(int i = 0; i < ROM_SIZE; i++) {
ft232h_read(context, i);
buf[i] = context->data;
}
f = fopen(filename, "w");
fwrite(buf, sizeof(char), ROM_SIZE -1, f);
fclose(f);
free(buf);
break;
case 'w':
printf("Writing %s to chip...\n", filename);
f = fopen(filename, "r");
fseek(f , 0 , SEEK_END);
size = ftell(f);
rewind(f);
buf = (char *)malloc(size);
fread(buf, 1, size, f);
fclose(f);
for(int i = 0; i < size; i++) {
ft232h_write(context, i, buf[i]);
}
free(buf);
break;
}
ft232h_free(context);
return EXIT_SUCCESS;
}