forked from whitequark/usbasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisp.c
304 lines (237 loc) · 5.67 KB
/
isp.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
/*
* isp.c - part of USBasp
*
* Autor..........: Thomas Fischl <[email protected]>
* Description....: Provides functions for communication/programming
* over ISP interface
* Licence........: GNU GPL v2 (see Readme.txt)
* Creation Date..: 2005-02-23
* Last change....: 2009-02-28
*/
#include <avr/io.h>
#include "isp.h"
#include "clock.h"
#include "usbasp.h"
#define spiHWdisable() SPCR = 0
uchar sck_sw_delay;
uchar sck_spcr;
uchar sck_spsr;
void spiHWenable() {
SPCR = sck_spcr;
SPSR = sck_spsr;
}
void ispSetSCKOption(uchar option) {
if (option == USBASP_ISP_SCK_AUTO)
option = USBASP_ISP_SCK_375;
if (option >= USBASP_ISP_SCK_93_75) {
ispTransmit = ispTransmit_hw;
sck_spsr = 0;
switch (option) {
case USBASP_ISP_SCK_1500:
/* enable SPI, master, 1.5MHz, XTAL/8 */
sck_spcr = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
sck_spsr = (1 << SPI2X);
case USBASP_ISP_SCK_750:
/* enable SPI, master, 750kHz, XTAL/16 */
sck_spcr = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
break;
case USBASP_ISP_SCK_375:
default:
/* enable SPI, master, 375kHz, XTAL/32 (default) */
sck_spcr = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
sck_spsr = (1 << SPI2X);
break;
case USBASP_ISP_SCK_187_5:
/* enable SPI, master, 187.5kHz XTAL/64 */
sck_spcr = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
break;
case USBASP_ISP_SCK_93_75:
/* enable SPI, master, 93.75kHz XTAL/128 */
sck_spcr = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
break;
}
} else {
ispTransmit = ispTransmit_sw;
switch (option) {
case USBASP_ISP_SCK_32:
sck_sw_delay = 3;
break;
case USBASP_ISP_SCK_16:
sck_sw_delay = 6;
break;
case USBASP_ISP_SCK_8:
sck_sw_delay = 12;
break;
case USBASP_ISP_SCK_4:
sck_sw_delay = 24;
break;
case USBASP_ISP_SCK_2:
sck_sw_delay = 48;
break;
case USBASP_ISP_SCK_1:
sck_sw_delay = 96;
break;
case USBASP_ISP_SCK_0_5:
sck_sw_delay = 192;
break;
}
}
}
void ispDelay() {
uint8_t starttime = TIMERVALUE;
while ((uint8_t) (TIMERVALUE - starttime) < sck_sw_delay) {
}
}
void ispConnect() {
/* all ISP pins are inputs before */
/* now set output pins */
ISP_DDR |= (1 << ISP_RST) | (1 << ISP_SCK) | (1 << ISP_MOSI);
/* reset device */
ISP_OUT &= ~(1 << ISP_RST); /* RST low */
ISP_OUT &= ~(1 << ISP_SCK); /* SCK low */
/* positive reset pulse > 2 SCK (target) */
ispDelay();
ISP_OUT |= (1 << ISP_RST); /* RST high */
ispDelay();
ISP_OUT &= ~(1 << ISP_RST); /* RST low */
if (ispTransmit == ispTransmit_hw) {
spiHWenable();
}
}
void ispDisconnect() {
/* set all ISP pins inputs */
ISP_DDR &= ~((1 << ISP_RST) | (1 << ISP_SCK) | (1 << ISP_MOSI));
/* switch pullups off */
ISP_OUT &= ~((1 << ISP_RST) | (1 << ISP_SCK) | (1 << ISP_MOSI));
/* disable hardware SPI */
spiHWdisable();
}
uchar ispTransmit_sw(uchar send_byte) {
uchar rec_byte = 0;
uchar i;
for (i = 0; i < 8; i++) {
/* set MSB to MOSI-pin */
if ((send_byte & 0x80) != 0) {
ISP_OUT |= (1 << ISP_MOSI); /* MOSI high */
} else {
ISP_OUT &= ~(1 << ISP_MOSI); /* MOSI low */
}
/* shift to next bit */
send_byte = send_byte << 1;
/* receive data */
rec_byte = rec_byte << 1;
if ((ISP_IN & (1 << ISP_MISO)) != 0) {
rec_byte++;
}
/* pulse SCK */
ISP_OUT |= (1 << ISP_SCK); /* SCK high */
ispDelay();
ISP_OUT &= ~(1 << ISP_SCK); /* SCK low */
ispDelay();
}
return rec_byte;
}
uchar ispTransmit_hw(uchar send_byte) {
SPDR = send_byte;
while (!(SPSR & (1 << SPIF)))
;
return SPDR;
}
uchar ispEnterProgrammingMode() {
uchar check;
uchar count = 32;
while (count--) {
ispTransmit(0xAC);
ispTransmit(0x53);
check = ispTransmit(0);
ispTransmit(0);
if (check == 0x53) {
return 0;
}
spiHWdisable();
/* pulse SCK */
ISP_OUT |= (1 << ISP_SCK); /* SCK high */
ispDelay();
ISP_OUT &= ~(1 << ISP_SCK); /* SCK low */
ispDelay();
if (ispTransmit == ispTransmit_hw) {
spiHWenable();
}
}
return 1; /* error: device dosn't answer */
}
uchar ispReadFlash(unsigned long address) {
ispTransmit(0x20 | ((address & 1) << 3));
ispTransmit(address >> 9);
ispTransmit(address >> 1);
return ispTransmit(0);
}
uchar ispWriteFlash(unsigned long address, uchar data, uchar pollmode) {
/* 0xFF is value after chip erase, so skip programming
if (data == 0xFF) {
return 0;
}
*/
ispTransmit(0x40 | ((address & 1) << 3));
ispTransmit(address >> 9);
ispTransmit(address >> 1);
ispTransmit(data);
if (pollmode == 0)
return 0;
if (data == 0x7F) {
clockWait(15); /* wait 4,8 ms */
return 0;
} else {
/* polling flash */
uchar retries = 30;
uint8_t starttime = TIMERVALUE;
while (retries != 0) {
if (ispReadFlash(address) != 0x7F) {
return 0;
};
if ((uint8_t) (TIMERVALUE - starttime) > CLOCK_T_320us) {
starttime = TIMERVALUE;
retries--;
}
}
return 1; /* error */
}
}
uchar ispFlushPage(unsigned long address, uchar pollvalue) {
ispTransmit(0x4C);
ispTransmit(address >> 9);
ispTransmit(address >> 1);
ispTransmit(0);
if (pollvalue == 0xFF) {
clockWait(15);
return 0;
} else {
/* polling flash */
uchar retries = 30;
uint8_t starttime = TIMERVALUE;
while (retries != 0) {
if (ispReadFlash(address) != 0xFF) {
return 0;
};
if ((uint8_t) (TIMERVALUE - starttime) > CLOCK_T_320us) {
starttime = TIMERVALUE;
retries--;
}
}
return 1; /* error */
}
}
uchar ispReadEEPROM(unsigned int address) {
ispTransmit(0xA0);
ispTransmit(address >> 8);
ispTransmit(address);
return ispTransmit(0);
}
uchar ispWriteEEPROM(unsigned int address, uchar data) {
ispTransmit(0xC0);
ispTransmit(address >> 8);
ispTransmit(address);
ispTransmit(data);
clockWait(30); // wait 9,6 ms
return 0;
}