-
Notifications
You must be signed in to change notification settings - Fork 10
/
gpio_tpi.c
356 lines (293 loc) · 7.28 KB
/
gpio_tpi.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
#include "gpio_tpi.h"
#include "gen_ios.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
//Portions of this are roughly based on:
// https://pcm1723.hateblo.jp/entry/20111208/1323351725
void ClockDelay() { int i = 1000; do { asm volatile ("nop"); } while( i-- ); } //700ksps (@i = 500) (slow enough for most systems) ... on further use, i = 1000 seems more reliable.
int did_send_last = 0;
static void SendBit( uint8_t bit )
{
GPIOSet( GPCLK, 0 );
GPIOSet( GPDAT, bit );
ClockDelay();
GPIOSet( GPCLK, 1 );
ClockDelay();
}
static uint8_t Parity( uint8_t val )
{
uint8_t par = val;
par ^= (par >> 4); // b[7:4] (+) b[3:0]
par ^= (par >> 2); // b[3:2] (+) b[1:0]
par ^= (par >> 1); // b[1] (+) b[0]
return par&1;
}
void TPISend( uint8_t tx )
{
uint16_t word;
int i;
GPIODirection( GPDAT, 1 );
word = 0 | (((uint16_t)tx)<<1) | (Parity(tx)<<9) | ( 0b1111 << 10 );
if( !did_send_last )
{
//Insert 2 idle ticks.
for( i = 0; i < 2; i++ )
{
SendBit( 1 );
}
}
for( i = 0; i < 12; i++ )
{
SendBit( (word & (1<<i))?1:0 );
}
did_send_last = 1;
}
int TPIReceive()
{
int i;
uint16_t datl = 0 ;
GPIODirection( GPDAT, 0 );
did_send_last = 0;
for( i = 0; i < 20; i++ )
{
SendBit( 1 );
if( GPIOGet( GPDAT ) == 0 ) break;
}
if( i == 20 ) return -1;
//Includes extra dummy cycle to flush the attiny. It "works" with 11 it seems.
for( i = 0; i < 12; i++ )
{
GPIOSet( GPCLK, 0 );
ClockDelay();
GPIOSet( GPCLK, 1 );
ClockDelay();
if( GPIOGet( GPDAT ) )
{
datl |= 1<<i;
}
}
if( ((datl >> 9)&3) != 3 ) return -1;
if( Parity( datl ) ^ ((datl>>8)&1) ) return -2;
return datl & 0xff;
}
void TPIBreak()
{
int i;
GPIODirection( GPDAT, 1 );
for( i = 0; i < 12; i++ )
{
SendBit( 0 );
}
}
int TPIInit()
{
int i;
int err = InitGenGPIO();
if (err != 0) {
fprintf(stderr, "TPIInit(): Gpio init failed, InitGenGPIO() returned %i", err);
return err;
}
//NOTE: MUST HAVE PULL-UP ON DAT.
GPIODirection( GPRST, 1 );
GPIODirection( GPCLK, 1 );
GPIODirection( GPDAT, 1 );
GPIOSet( GPRST, 1 );
usleep( 1000 );
GPIOSet( GPRST, 0 );
usleep( 10 );
for( i = 0; i < 16; i++ )
{
SendBit( 1 );
}
TPISend( 0xC2 ); //SSTCS 0b0010 0000 0100
TPISend( 0x07 ); //Configures the dead time on bus turn-around.
{
uint64_t nvm_key = 0x1289AB45CDD888FFULL;
TPISend( 0b11100000 ); // SKEY function
while(nvm_key)
{
TPISend(nvm_key & 0xFF);
nvm_key >>= 8;
} // while
}
TPISend( 0b00010000 );
if( TPIReceive() < 0 ) return -1;
uint8_t devid[3];
TPIReadData( 0x3fc0, devid, 3 );
return devid[0]<<16 | devid[1]<<8 | devid[2];
}
void TPIEnd()
{
//Exit NVMEN
TPISend( 0xC0 ); //SSTCS 0b0010 0000 0100
TPISend( 0x00 );
SendBit(1); //Flush.
SendBit(1);
GPIODirection( GPRST, 0 );
GPIODirection( GPCLK, 0 );
GPIODirection( GPDAT, 0 );
}
int TPIReadIO( uint8_t address )
{
int low = address & 0xf;
int high = ( address & 0x30 ) << 1;
TPISend( 0b00010000 | low | high ); // SIN 0aa1 aaaa
return TPIReceive();
}
void TPIWriteIO( uint8_t address, uint8_t value )
{
int low = address & 0xf;
int high = ( address & 0x30 ) << 1;
TPISend( 0b10010000 | low | high ); // SIN 0aa1 aaaa
TPISend( value );
}
void TPISetPR( uint16_t address )
{
//Initialize Memory Pointer Transfer
TPISend(0x68); // SSTPR 0
TPISend(address); //
TPISend(0x69); // SSTPR 1
TPISend(address>>8); //
}
int TPIReadData( uint16_t address, uint8_t * data, int length )
{
TPISetPR( address );
while( length-- )
{
TPISend( 0b00100100 ); // SLD with post-increment.
int r = TPIReceive();
if( r < 0 ) return r;
*(data++) = r;
}
return 0;
}
void TPIWriteData( uint16_t address, const uint8_t * data, int length )
{
TPISetPR( address );
while( length-- )
{
TPISend( 0b01100100 ); // SST with post-increment. Bug in datasheet.
TPISend( *(data++) );
}
return;
}
int TPIEraseSection( uint16_t secadd )
{
int i;
TPIWriteIO( NVMCMD, 0x14 ); //Section erase.
TPIWriteData( secadd|1, "x", 1 ); //Dummy write.
for( i = 0; i < 1000; i++ )
{
int rx = TPIReadIO( NVMCSR );
if( rx < 0 ) return -1;
if( rx == 0 ) return 0;
usleep(100);
}
return -2;
}
int TPIErase()
{
int i;
TPIWriteIO( NVMCMD, 0x10 ); //Chip erase.
TPIWriteData( 0x4001, "x", 1 ); //Dummy write.
for( i = 0; i < 1000; i++ )
{
int rx = TPIReadIO( NVMCSR );
if( rx < 0 ) return -1;
if( rx == 0 ) return 0;
usleep(100);
}
return -2;
}
int TPIWriteFlashWord( uint16_t address, const uint8_t * data )
{
int i;
TPIWriteIO( NVMCMD, 0x1D ); //Write word.
TPIWriteData( address, data, 2 ); //Data write.
for( i = 0; i < 1000; i++ )
{
int rx = TPIReadIO( NVMCSR );
if( rx < 0 ) return -1;
if( rx == 0 ) return 0;
usleep(100);
}
return -2;
}
void TPIDump( int start, int length, const char * name )
{
uint8_t buffer[length];
printf( "%04x - %04x: %s\n", start, start + length, name );
int r = TPIReadData( start, buffer, sizeof( buffer ) );
if( r )
{
fprintf( stderr, "Could not read data space\n" );
}
else
{
int i;
for( i = 0; i < length; i++ )
{
if( ( i & 0x0f ) == 0x00 ) printf( "%04x: ", i + start) ;
printf( "%02x ", buffer[i] );
if( ( i & 0x0f ) == 0x0f ) printf("\n" );
}
}
printf("\n");
}
int TPIEraseAndWriteAllFlash( const uint8_t * data, int length, int quiet )
{
int i;
if( TPIErase() )
{
fprintf( stderr, "Error: Can't erase chip\n" );
return -1;
}
if( !quiet ) fprintf( stderr, "Erased.\n" );
for( i = 0; i < length; i+=2 )
{
TPIWriteFlashWord( i + 0x4000, data + i );
if( !quiet && ( i & 0x20 ) == 0 )
{
fprintf( stderr, "." );
fflush( stderr );
}
}
if( !quiet ) fprintf( stderr, ".\n" );
uint8_t verify[length];
int r = TPIReadData( 0x4000, verify, length );
if( memcmp( verify, data, length ) == 0 )
{
if( !quiet ) fprintf( stderr, "Verified OK.\n" );
return 0;
}
else
{
fprintf( stderr, "Verification failed.\n" );
return -1;
}
}
/*
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.
*/