forked from setarcos/ch341prog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
178 lines (172 loc) · 5.23 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
/*
* This file is part of the ch341prog project.
*
* Copyright (C) 2014 Pluto Yang (yangyj.ee@gmail.com)
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include "ch341a.h"
int main(int argc, char* argv[])
{
int32_t ret;
uint8_t *buf;
FILE *fp;
char *filename;
int cap;
int length = 0;
char op = 0;
uint32_t speed = CH341A_STM_I2C_20K;
int8_t c;
const char usage[] =
"\nUsage:\n"\
" -h, --help display this message\n"\
" -i, --info read the chip ID info\n"\
" -e, --erase erase the entire chip\n"\
" -l, --length <bytes> manually set length\n"\
" -w, --write <filename> write chip with data from filename\n"\
" -r, --read <filename> read chip and save data to filename\n"\
" -t, --turbo increase the i2c bus speed (-tt to use much faster speed)\n"\
" -d, --double double the spi bus speed\n";
const struct option options[] = {
{"help", no_argument, 0, 'h'},
{"erase", no_argument, 0, 'e'},
{"write", required_argument, 0, 'w'},
{"length", required_argument, 0, 'l'},
{"read", required_argument, 0, 'r'},
{"turbo", no_argument, 0, 't'},
{"double", no_argument, 0, 'd'},
{0, 0, 0, 0}};
int32_t optidx = 0;
while ((c = getopt_long(argc, argv, "hiew:r:l:td", options, &optidx)) != -1){
switch (c) {
case 'i':
case 'e':
if (!op)
op = c;
else
op = 'x';
break;
case 'w':
case 'r':
if (!op) {
op = c;
filename = (char*) malloc(strlen(optarg) + 1);
strcpy(filename, optarg);
} else
op = 'x';
break;
case 'l':
length = atoi(optarg);
break;
case 't':
if ((speed & 3) < 3) {
speed++;
}
break;
case 'd':
speed |= CH341A_STM_SPI_DBL;
break;
default:
printf("%s\n", usage);
return 0;
}
}
if (op == 0) {
fprintf(stderr, "%s\n", usage);
return 0;
}
if (op == 'x') {
fprintf(stderr, "Conflicting options, only one option at a time.\n");
return -1;
}
ret = ch341Configure(CH341A_USB_VENDOR, CH341A_USB_PRODUCT);
if (ret < 0)
return -1;
ret = ch341SetStream(speed);
if (ret < 0) goto out;
ret = ch341SpiCapacity();
if (ret < 0) goto out;
cap = 1 << ret;
printf("Chip capacity is %d\n", cap);
if (length != 0){
cap = length;
}
if (op == 'i') goto out;
if (op == 'e') {
uint8_t timeout = 0;
ret = ch341EraseChip();
if (ret < 0) goto out;
do {
sleep(1);
ret = ch341ReadStatus();
if (ret < 0) goto out;
printf(".");
fflush(stdout);
timeout++;
if (timeout == 100) break;
} while(ret != 0);
if (timeout == 100)
fprintf(stderr, "Chip erase timeout.\n");
else
printf("Chip erase done!\n");
}
if ((op == 'r') || (op == 'w')) {
buf = (uint8_t *)malloc(cap);
if (!buf) {
fprintf(stderr, "Malloc failed for read buffer.\n");
goto out;
}
}
if (op == 'r') {
ret = ch341SpiRead(buf, 0, cap);
if (ret < 0)
goto out;
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Couldn't open file %s for writing.\n", filename);
goto out;
}
fwrite(buf, 1, cap, fp);
if (ferror(fp))
fprintf(stderr, "Error writing file [%s]\n", filename);
fclose(fp);
}
if (op == 'w') {
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Couldn't open file %s for reading.\n", filename);
goto out;
}
ret = fread(buf, 1, cap, fp);
if (ferror(fp)) {
fprintf(stderr, "Error reading file [%s]\n", filename);
if (fp)
fclose(fp);
goto out;
}
fprintf(stderr, "File Size is [%d]\n", ret);
fclose(fp);
ret = ch341SpiWrite(buf, 0, ret);
}
out:
ch341Release();
return 0;
}