-
Notifications
You must be signed in to change notification settings - Fork 10
/
tpiflash.c
360 lines (316 loc) · 8.48 KB
/
tpiflash.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gpio_tpi.h"
#include "gen_ios.h"
#include <sys/time.h>
double GTime()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
int FlashFile( const char * filename, int silent )
{
FILE * f = fopen( filename, "rb" );
if( !f )
{
fprintf( stderr, "Error: can't open bin file: %s\n", filename );
return -6;
}
fseek( f, 0, SEEK_END );
int len = ftell( f );
fseek( f, 0, SEEK_SET );
uint8_t buffer[len+1];
if( fread( buffer, len, 1, f ) != 1 )
{
fprintf( stderr, "Error: can't read contents of binary\n" );
return -7;
}
fclose( f );
if( len & 1 ) len++; //Make sure we have full words.
if( TPIEraseAndWriteAllFlash( buffer, len, silent ) )
{
if( !silent )
{
TPIDump( 0x4000, len, "FLASH" );
printf( "\n" );
}
return -1;
}
return 0;
}
int Init( const char * code )
{
int initcode = TPIInit();
if( initcode < 0 )
{
fprintf( stderr, "Error: Cannot connect to ATTiny (Error %d)\n", initcode );
exit( -3 );
}
char initcodesat[10];
sprintf( initcodesat, "%06x", initcode );
if( memcmp( code, initcodesat, 6 ) != 0 )
{
fprintf( stderr, "Error: Device code mismatch. Expected: \"%s\", got \"%s\"\n", code, initcodesat );
exit( -5 );
}
}
//This assumes you are NOT in programming mode.
double GetMHzOfOSCCAL( uint8_t osccal, const char * code )
{
int i;
Init( code );
if( FlashFile( "osccal.bin", 1 ) ) { TPIEnd(); return -11; }
uint8_t wordset[2] = { osccal, 0xff };
TPIWriteFlashWord( 0x40fe, wordset );
TPIEnd();
//wait for line to go low.
double Last = GTime();
double Now = GTime();
int tries = 5;
double Times[tries];
double TotalTimes = 0;
for( i = 0; i < tries; i++ )
{
Last = GTime();
while( GPIOGet( GPDAT ) && ( (Now = GTime()) - Last ) < 1.0 );
if( Now-Last >= 1.0 ) goto timeout;
Last = GTime();
while( !GPIOGet( GPDAT ) && ( (Now = GTime()) - Last ) < 1.0 );
if( Now-Last >= 1.0 ) goto timeout;
Times[i] = Now-Last;
TotalTimes += Now - Last;
}
TotalTimes /= tries;
double mostaccuratediff = 1e20;
double mostaccurate = 0;
//Find time closest to average.
for( i = 0; i < tries; i++ )
{
double diff = Times[i] - TotalTimes;
if( diff < 0 ) diff = -diff;
if( diff < mostaccuratediff )
{
mostaccuratediff = diff;
mostaccurate = Times[i];
}
}
double MHz = 1.0/(mostaccurate/50000.0);
return MHz;
timeout:
fprintf( stderr, "Error: Timeout on timer\n" );
return -5;
}
int ReadEasy( const char * readdata )
{
if( readdata[0] == '0' )
{
if( readdata[1] == 'x' || readdata[1] == 'X' )
{
return strtol( readdata+2, NULL, 16 );
}
else
return strtol( readdata+1, NULL, 8 );
}
else
return strtol(readdata, NULL, 10);
}
int main( int argc, char ** argv )
{
int i;
if( argc < 3 || strlen( argv[2] ) != 6 )
{
fprintf( stderr, "Error: Usage: tpiflash [w/e/c/r/o/p] [device id]\n\
1e8f0a - ATTiny4\n\
1e8f09 - ATTiny5\n\
1e9008 - ATTiny9\n\
1e9003 - ATTiny10\n\n\
w: write flash. Extra parameter [binary file to flash]\n\
e: erase chip.\n\
c: config chip. Extra parameter [config byte, default 0 (inverted)]\n\
r: dump chip memories\n\
o: oscillator calibration. Extra par [target MHz, optional or 'all']\n\
p: poke. Extra: [address in flash] [value]\n");
return -1;
}
Init( argv[2] );
if( argv[1][0] == 'E' || argv[1][0] == 'e' )
{
if( TPIErase() )
{
fprintf( stderr, "Error: Can't erase chip\n" );
return -1;
}
printf( "Erased.\n" );
}
else if( argv[1][0] == 'C' || argv[1][0] == 'c' )
{
if( argc < 4 )
{
fprintf( stderr, "Error: Need more flags.\n" );
return -4;
}
int word = ~atoi( argv[3] );
if( TPIEraseSection( 0x3f41 ) )
{
fprintf( stderr, "Error: Can't erase config.\n" );
return -14;
}
uint8_t wordset[2] = { word, 0xff };
TPIWriteFlashWord( 0x3f40, wordset );
printf( "Set.\n" );
TPIDump( 0x3f40, 0x02, "CONFIG" );
}
else if( argv[1][0] == 'P' || argv[1][0] == 'p' )
{
if( argc != 5 )
{
TPIEnd();
fprintf( stderr, "Usage: p [dev id] [address, START AT 0x4000 for flash, must be word aligned] [value (word)]\n" );
return -47;
}
uint16_t addy = ReadEasy( argv[3] );
uint16_t val = ReadEasy( argv[4] );
uint8_t matching[2];
int r = TPIReadData( addy, matching, 2 );
if( r < 0 )
{
TPIEnd();
fprintf( stderr, "Can't read specified value.\n" );
return -46;
}
if( matching[0] != 0xff || matching[1] != 0xff )
{
TPIEnd();
fprintf( stderr, "Can't write to unerased flash.\n" );
return -45;
}
matching[0] = val&0xff;
matching[1] = val>>8;
TPIWriteFlashWord( addy, matching );
uint8_t verify[2] = { 0xff, 0xff };
r = TPIReadData( addy, verify, 2 );
if( r == 0 && matching[0] == verify[0] && matching[1] == verify[1] )
{
printf( "[0x%04x] = 0x%02x%02x\n", addy, verify[0], verify[1] );
}
else
{
TPIEnd();
printf( "%d: [0x%04x] = 0x%02x%02x\n", r , addy, verify[0], verify[1] );
fprintf( stderr, "Verification failed.\n" );
return -44;
}
}
else if( argv[1][0] == 'R' || argv[1][0] == 'r' )
{
TPIDump( 0x00, 0x60, "IO + RAM" );
TPIDump( 0x3f00, 0x02, "NVM" );
TPIDump( 0x3f40, 0x02, "CONFIG" );
TPIDump( 0x3f80, 0x02, "CALIB" );
TPIDump( 0x3fC0, 0x04, "DEVID" );
TPIDump( 0x4000, 0x400, "FLASH" );
}
else if( argv[1][0] == 'W' || argv[1][0] == 'w' )
{
if( argc < 4 )
{
fprintf( stderr, "Error: Need a binary file\n" );
return -4;
}
FlashFile( argv[3], 0 );
}
else if( argv[1][0] == 'O' || argv[1][0] == 'o' )
{
if( argc < 4 )
{
fprintf( stderr, "No target parameter for speed set. Printing speed at default cal.\n" );
uint8_t osccal[2];
int r = TPIReadData( 0x3f80, osccal, 2 );
if( r < 0 )
{
fprintf( stderr, "Error: can't read initial osccal\n" );
TPIEnd();
return -5;
}
TPIEnd();
double MHz = GetMHzOfOSCCAL( osccal[0], argv[2] );
printf( "%d, %f\n", osccal[0], MHz );
}
else if( strcmp( argv[3], "all" ) == 0 )
{
int cal = 0;
TPIEnd();
for( ; cal < 256; cal++ )
{
double MHz = GetMHzOfOSCCAL( cal, argv[2] );
printf( "%d, %f\n", cal, MHz );
fflush( stdout );
}
}
else
{
double target = atof( argv[3] );
int osccal = 0x80;
int jump = 0x40;
int tries = 0;
int best_osc = 0x80;
double best_cal = 1e20;
double freqat = 1e20;
TPIEnd();
for( tries = 0; tries < 16; tries++ )
{
double MHz = GetMHzOfOSCCAL( osccal, argv[2] );
if( MHz < 0 ) return -98;
jump = jump * .8;
fprintf( stderr, "%d, %f\n", osccal, MHz );
double diff = MHz - target;
if( diff < 0 ) diff = -diff;
if( diff < best_cal ) {
best_osc = osccal;
best_cal = diff;
freqat = MHz;
}
if( MHz < target ) osccal += jump;
else osccal -= jump;
if( osccal < 0 ) osccal = 0;
if( osccal > 0xff ) osccal = 0xff;
}
printf( "%d, %f, %f, %3.4f%%\n", best_osc, freqat, best_cal, best_cal/freqat*100. );
}
return 0;
timeout:
fprintf( stderr, "Error: Timed out on calibration tool\n" );
return 0;
}
else
{
printf( "Unknown flash command\n" );
}
TPIEnd();
}
/*
Copyright 2018 <>< Charles Lohr / Unit-E Technologies
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/