-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
276 lines (215 loc) · 6.63 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
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
/*
USBasp - USB in-circuit programmer for Atmel AVR controllers
Thomas Fischl <[email protected]>
License:
The project is built with AVR USB driver by Objective Development, which is
published under an own licence based on the GNU General Public License (GPL).
USBasp is also distributed under this enhanced licence. See Documentation.
Target.........: ATMega8 at 12 MHz
Creation Date..: 2005-02-20
Last change....: 2006-12-29
PC2 SCK speed option. GND -> slow (8khz SCK),
open -> fast (375kHz SCK)
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include "usbdrv.h"
#include "isp.h"
#include "clock.h"
#define USBASP_FUNC_CONNECT 1
#define USBASP_FUNC_DISCONNECT 2
#define USBASP_FUNC_TRANSMIT 3
#define USBASP_FUNC_READFLASH 4
#define USBASP_FUNC_ENABLEPROG 5
#define USBASP_FUNC_WRITEFLASH 6
#define USBASP_FUNC_READEEPROM 7
#define USBASP_FUNC_WRITEEEPROM 8
#define PROG_STATE_IDLE 0
#define PROG_STATE_WRITEFLASH 1
#define PROG_STATE_READFLASH 2
#define PROG_STATE_READEEPROM 3
#define PROG_STATE_WRITEEEPROM 4
#define PROG_BLOCKFLAG_FIRST 1
#define PROG_BLOCKFLAG_LAST 2
#define ledRedOn() PORTC &= ~(1 << PC1)
#define ledRedOff() PORTC |= (1 << PC1)
#define ledGreenOn() PORTC &= ~(1 << PC0)
#define ledGreenOff() PORTC |= (1 << PC0)
static uchar replyBuffer[8];
static uchar prog_state = PROG_STATE_IDLE;
static unsigned int prog_address;
static unsigned int prog_nbytes = 0;
static unsigned int prog_pagesize; //TP: Mega128 fix
static uchar prog_blockflags;
static uchar prog_pagecounter;
uchar usbFunctionSetup(uchar data[8]) {
uchar len = 0;
if(data[1] == USBASP_FUNC_CONNECT){
/* set SCK speed */
if ((PINC & (1 << PC2)) == 0) {
ispSetSCKOption(ISP_SCK_SLOW);
} else {
ispSetSCKOption(ISP_SCK_FAST);
}
ispConnect();
ledRedOn();
} else if (data[1] == USBASP_FUNC_DISCONNECT) {
ispDisconnect();
ledRedOff();
} else if (data[1] == USBASP_FUNC_TRANSMIT) {
if(chip==ATM){
replyBuffer[0] = ispTransmit(data[2]);
replyBuffer[1] = ispTransmit(data[3]);
replyBuffer[2] = ispTransmit(data[4]);
replyBuffer[3] = ispTransmit(data[5]);
}else{
if(data[2]==0x24){
replyBuffer[0] = ispTransmit(data[2]);
replyBuffer[1] = ispTransmit(data[3]);
replyBuffer[2] = ispTransmit(data[4]);
switch(ispTransmit(data[5])&0x1C){
case(0x00):replyBuffer[3]=0xE0;break;
case(0x04):replyBuffer[3]=0xE5;break;
case(0x0C):replyBuffer[3]=0xEE;break;
case(0x1C):replyBuffer[3]=0xFF;break;
}
}
else if(data[2]==0x30){
replyBuffer[0] = ispTransmit(0x28);
replyBuffer[1] = ispTransmit(data[3]);
replyBuffer[2] = ispTransmit(data[4]);
replyBuffer[3] = ispTransmit(data[5]);
}
else{
replyBuffer[0] = ispTransmit(data[2]);
replyBuffer[1] = ispTransmit(data[3]);
replyBuffer[2] = ispTransmit(data[4]);
replyBuffer[3] = ispTransmit(data[5]);
}
}
len = 4;
} else if (data[1] == USBASP_FUNC_READFLASH) {
prog_address = (data[3] << 8) | data[2];
prog_nbytes = (data[7] << 8) | data[6];
prog_state = PROG_STATE_READFLASH;
len = 0xff; /* multiple in */
} else if (data[1] == USBASP_FUNC_READEEPROM) {
prog_address = (data[3] << 8) | data[2];
prog_nbytes = (data[7] << 8) | data[6];
prog_state = PROG_STATE_READEEPROM;
len = 0xff; /* multiple in */
} else if (data[1] == USBASP_FUNC_ENABLEPROG) {
replyBuffer[0] = ispEnterProgrammingMode();;
len = 1;
} else if (data[1] == USBASP_FUNC_WRITEFLASH) {
prog_address = (data[3] << 8) | data[2];
prog_pagesize = data[4];
prog_blockflags = data[5] & 0x0F;
prog_pagesize += (((unsigned int)data[5] & 0xF0)<<4); //TP: Mega128 fix
if (prog_blockflags & PROG_BLOCKFLAG_FIRST) {
prog_pagecounter = prog_pagesize;
}
prog_nbytes = (data[7] << 8) | data[6];
prog_state = PROG_STATE_WRITEFLASH;
len = 0xff; /* multiple out */
} else if (data[1] == USBASP_FUNC_WRITEEEPROM) {
prog_address = (data[3] << 8) | data[2];
prog_pagesize = 0;
prog_blockflags = 0;
prog_nbytes = (data[7] << 8) | data[6];
prog_state = PROG_STATE_WRITEEEPROM;
len = 0xff; /* multiple out */
}
usbMsgPtr = replyBuffer;
return len;
}
uchar usbFunctionRead(uchar *data, uchar len) {
uchar i;
/* check if programmer is in correct read state */
if ((prog_state != PROG_STATE_READFLASH) &&
(prog_state != PROG_STATE_READEEPROM)) {
return 0xff;
}
/* fill packet */
for (i = 0; i < len; i++) {
if (prog_state == PROG_STATE_READFLASH) {
data[i] = ispReadFlash(prog_address);
} else {
data[i] = ispReadEEPROM(prog_address);
}
prog_address++;
}
/* last packet? */
if (len < 8) {
prog_state = PROG_STATE_IDLE;
}
return len;
}
uchar usbFunctionWrite(uchar *data, uchar len) {
uchar retVal = 0;
uchar i;
/* check if programmer is in correct write state */
if ((prog_state != PROG_STATE_WRITEFLASH) &&
(prog_state != PROG_STATE_WRITEEEPROM)) {
return 0xff;
}
for (i = 0; i < len; i++) {
if (prog_state == PROG_STATE_WRITEFLASH) {
/* Flash */
if (prog_pagesize == 0) {
/* not paged */
ispWriteFlash(prog_address, data[i], 1);
} else {
/* paged */
ispWriteFlash(prog_address, data[i], 0);
prog_pagecounter --;
if (prog_pagecounter == 0) {
ispFlushPage(prog_address, data[i]);
prog_pagecounter = prog_pagesize;
}
}
} else {
/* EEPROM */
ispWriteEEPROM(prog_address, data[i]);
}
prog_nbytes --;
if (prog_nbytes == 0) {
prog_state = PROG_STATE_IDLE;
if ((prog_blockflags & PROG_BLOCKFLAG_LAST) &&
(prog_pagecounter != prog_pagesize)) {
/* last block and page flush pending, so flush it now */
ispFlushPage(prog_address, data[i]);
}
retVal = 1; // Need to return 1 when no more data is to be received
}
prog_address ++;
}
return retVal;
}
int main(void)
{
uchar i, j;
PORTD = 0;
PORTB = 0; /* no pullups on USB and ISP pins */
DDRD = ~(1 << 2); /* all outputs except PD2 = INT0 */
DDRB = ~0; /* output SE0 for USB reset */
j = 0;
while(--j){ /* USB Reset by device only required on Watchdog Reset */
i = 0;
while(--i); /* delay >10ms for USB reset */
}
DDRB = 0; /* all USB and ISP pins inputs */
DDRC = 0x03; /* all inputs except PC0, PC1 */
PORTC = 0xfe;
chip=ATM;
clockInit(); /* init timer */
ispSetSCKOption(ISP_SCK_FAST);
usbInit();
sei();
for(;;){ /* main event loop */
usbPoll();
}
return 0;
}