-
Notifications
You must be signed in to change notification settings - Fork 0
/
attiny.spin
342 lines (268 loc) · 6.56 KB
/
attiny.spin
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
CON
_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x
CLK = 8
MISO = 9
MOSI = 10
RESET = 11
OBJ
pst: "Unix Serial Terminal"
VAR
word pageData[32]
word buffer[4096]
byte input[256]
PUB start | o, i, j, nc
pst.start(115200)
pst.str(string("ATTiny Programmer"))
pst.newline
outa[CLK]~
outa[MOSI]~
outa[RESET]~~
dira[CLK]~~
dira[MOSI]~~
dira[RESET]~~
outa[RESET]~
waitcnt(cnt + (clkfreq >> 5))
repeat
pst.StrInMax(@input,255)
pst.str(@input)
pst.str(string("<<"))
pst.newline
if strcomp(@input,string("HEX"))
i:=readHEX
if i>0
writeBuffer(i)
elseif strcomp(@input,string("TEST"))
runAndTest
elseif strcomp(@input,string("ENABLE"))
pst.str(string(">>ENABLE"))
pst.newline
programEnable
elseif strcomp(@input,string("ERASE"))
pst.str(string(">>ERASE"))
chipErase
{{
elseif strcomp(@input,string("EEPROM")
readEEPROM
writeEEPROM
elseif strcomp(@input,string("FUSE")
loadFUSE
elseif strcomp(@input,string("FUSEEXT")
loadFUSEEXT
elseif strcomp(@input,string("FUSEHIGH")
loadFUSEHIGH
}}
PUB writeBuffer(addr)
writeProgMem(0,@buffer,(addr>>5)+1)
PUB readHEX | byteCount, addr, recType, checksum, b, maxAddr
pst.str(string(">>HEX"))
pst.newline
wordfill(@buffer,$ffff,4096)
maxAddr:=0
repeat
if not pst.charIn==":"
pst.str(string("INVALID"))
return -1
byteCount:=pst.hexInCount(2)
addr:=pst.hexInCount(4)
maxAddr #>= addr
recType:=pst.hexInCount(2)
checksum:=byteCount+(addr >> 8) + (addr & $ff) +recType
pst.newline
if recType==0
repeat byteCount
b:=pst.hexInCount(2)
if addr & 1 == 0
buffer[addr>>1]:=b
else
buffer[addr>>1]|=b<<8
checksum+=b
addr++
checksum+=pst.hexInCount(2)
if (checksum & $ff) <> 0 or (pst.charIn <> pst#NL)
pst.str(string("BAD CHECKSUM: "))
pst.hex(checksum,2)
pst.newline
return -1
pst.hex(addr,4)
pst.char(":")
pst.hex(byteCount,2)
pst.newline
elseif recType==1
pst.strInMax(@input,255)
pst.str(string(">>HEXDONE"))
return maxAddr
PUB runAndTest
waitcnt(cnt+clkfreq>>2)
dira[MOSI]~
outa[RESET]~~
repeat
if ina[MOSI]
pst.str(string("ON"))
else
pst.str(string("OFF"))
pst.newline
waitcnt(cnt+clkfreq>>2)
PUB dumpState | i, j
pst.str(string("Program Memory"))
pst.newline
readProgMem(0,@buffer,4)
repeat i from 0 to 3
repeat j from 0 to 31
pst.hex(buffer[i<<5 | j] & $ff,2)
pst.hex(buffer[i<<5 | j] >> 8,2)
if (j==15)
pst.newline
pst.newline
pst.str(string("EEPROM"))
pst.newline
repeat i from 0 to 128
pst.hex(readEEPROMPage(i<<2),8)
if i & 7 == 7
pst.newline
pst.newline
pst.str(string("Signature: "))
pst.hex(readSignature,6)
pst.newline
pst.str(string("Lock: "))
pst.hex(readLock, 2)
pst.newline
pst.str(string("Fuse: "))
pst.hex(readFuse,2)
pst.newline
pst.str(string("Fuse High: "))
pst.hex(readFuseHigh,2)
pst.newline
pst.str(string("Fuse Extended: "))
pst.hex(readFuseExtended,2)
pst.newline
pst.str(string("Calibration: "))
pst.hex(readCalibration,2)
pst.newline
{{repeat i from 0 to 64 step 32
wordmove(@pageData,@program + (i<<1),32)
writeProgMemPage(i)
repeat i from 0 to 64 step 32
readProgMemPage(i)
pst.str(string(">>"))
repeat j from 0 to 31
pst.hex(pageData[j],4)
pst.char(" ")
pst.newline
waitcnt(cnt+clkfreq>>2)
dira[MOSI]~
outa[RESET]~~
repeat
if ina[MOSI]
pst.str(string("ON"))
else
pst.str(string("OFF"))
pst.newline
waitcnt(cnt+clkfreq>>2)
}}
PUB programEnable
logAndWrite($ac53 << 16)
PUB readProgMem(addr,buf,pageCount)
repeat pageCount
readProgMemPage(addr)
wordmove(buf,@pageData,32)
addr+=32
buf+=64
PUB writeProgMem(addr,buf,pageCount)
repeat pageCount
wordmove(@pageData,buf,32)
writeProgMemPage(addr)
addr+=32
buf+=64
PUB readProgMemPage(addr) | i
repeat i from 0 to 31
pageData[i]:=logAndWrite($20 << 24 | (addr+i) << 8) & $ff
pageData[i]|=((logAndWrite($28 << 24 | (addr+i) << 8) & $ff) << 8)
PUB writeProgMemPage(addr) | i
repeat i from 0 to 31
logAndWrite($40 << 24 | (addr+i) << 8 | (pageData[i] & $ff))
logAndWrite($48 << 24 | (addr+i) << 8 | pageData[i] >> 8)
logAndWrite($4c << 24 | (addr & $3f) << 8)
waitReady
PUB chipErase
logAndWrite($ac80 << 16)
waitReady
PUB logAndWrite(d)
'pst.hex(d,8)
'pst.str(string(": "))
result:=write(d)
'pst.hex(result,8)
'pst.newline
PUB write(d) | in
in:=0
repeat 32
d<-=1
outa[MOSI]:=d&1
outa[CLK]~~
in:=(in<<1)|ina[MISO]
outa[CLK]~
return in
PUB writeEEPROMPage(addr,val) | i
repeat i from 0 to 3
logAndWrite($c1 << 24 | i << 8 | (val & $ff))
val >>= 8
logAndWrite($c2 << 24 | (addr & $3c) << 8)
waitReady
PUB readEEPROMPage(addr) | val, i
val:=0
addr&=$fc
repeat i from 0 to 3
val<<=8
val|=readEEPROMByte(addr+i)
return val
PUB writeEEPROMByte(addr,val)
logAndWrite($c0 << 24 | addr << 8 | (val & $ff))
waitReady
PUB readEEPROMByte(addr)
return logAndWrite($a0 << 24 | addr << 8) & $ff
PUB readLock
return logAndWrite($58 << 24) & $ff
PUB writeLock(val)
logAndWrite($ace0 << 16 | (val & $ff))
waitReady
PUB readSignature | i, val
val:=0
repeat i from 0 to 2
val<<=8
val|=logAndWrite($30 << 24 | i << 8) & $ff
return val
PUB readFuse
return logAndWrite($50 << 24) & $ff
PUB writeFuse(val)
logAndWrite($aca0 << 16 | (val & $ff))
waitReady
PUB readFuseHigh
return logAndWrite($5808 << 16) & $ff
PUB writeFuseHigh(val)
logAndWrite($aca8 << 16 | (val & $ff))
waitReady
PUB readFuseExtended
return logAndWrite($5008 << 16) & $ff
PUB writeFuseExtended(val)
logAndWrite($aca4 << 16 | (val & $ff))
waitReady
PUB readCalibration
return logAndWrite($38 << 24) & $ff
PUB waitReady | status
status:=1
repeat until status==0
'waitcnt(cnt + clkfreq >> 6)
status:=logAndwrite($f0 << 24)&1
DAT
word
program byte $0E, $C0, $15, $C0, $14, $C0, $13, $C0, $12, $C0, $11, $C0, $10, $C0, $0F, $C0
byte $0E, $C0, $0D, $C0, $0C, $C0, $0B, $C0, $0A, $C0, $09, $C0, $08, $C0, $11, $24
byte $1F, $BE, $CF, $E5, $D2, $E0, $DE, $BF, $CD, $BF, $1E, $D0, $31, $C0, $E8, $CF
byte $CF, $93, $DF, $93, $00, $D0, $CD, $B7, $DE, $B7, $0F, $C0, $1A, $82, $19, $82
byte $06, $C0, $29, $81, $3A, $81, $2F, $5F, $3F, $4F, $3A, $83, $29, $83, $29, $81
byte $3A, $81, $29, $33, $31, $05, $A8, $F3, $81, $50, $81, $11, $EF, $CF, $0F, $90
byte $0F, $90, $DF, $91, $CF, $91, $08, $95, $B8, $9A, $C0, $9A, $8A, $EF, $E0, $DF
byte $8A, $EF, $DE, $DF, $8A, $EF, $DC, $DF, $8A, $EF, $DA, $DF, $18, $BA, $8A, $EF
byte $D7, $DF, $8A, $EF, $D5, $DF, $8A, $EF, $D3, $DF, $8A, $EF, $D1, $DF, $ED, $CF
byte $F8, $94, $FF, $CF
long 0, 0, 0, 0,0,0,0, 0,0,0,0