summaryrefslogtreecommitdiff
path: root/examples/Ports/isp_flash/isp_flash.ino
blob: a695bf4d9675ea2068225e8bcb6761c7e3ba101a (plain) (blame)
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
359
360
361
/// @dir isp_flash
/// Derived from http://arduino.cc/en/Tutorial/ArduinoISP
// - 2010-04-17, jcw
//
// see http://jeelabs.org/2010/04/24/isp-plug/
// and http://jeelabs.org/2011/05/29/summary-of-isp-options/

// pin definitions
#define PIN_SCK     14
#define PIN_MISO    4
#define PIN_MOSI    17
#define PIN_RESET   7
#define LED_PMODE   15  // on while programming (LED to VCC via resistor)

// original comments:
//
// October 2009 by David A. Mellis
// - Added support for the read signature command
// 
// February 2009 by Randall Bohn
// - Added support for writing to EEPROM (what took so long?)
// Windows users should consider WinAVR's avrdude instead of the
// avrdude included with Arduino software.
//
// January 2008 by Randall Bohn
// - Thanks to Amplificar for helping me with the STK500 protocol
// - The AVRISP/STK500 (mk I) protocol is used in the arduino bootloader
// - The SPI functions herein were developed for the AVR910_ARD programmer 
// - More information at http://code.google.com/p/mega-isp

#define HWVER 2
#define SWMAJ 1
#define SWMIN 18

// STK Definitions
#define STK_OK      '\x10'
#define STK_FAILED  '\x11'
#define STK_UNKNOWN '\x12'
#define STK_INSYNC  '\x14'
#define STK_NOSYNC  '\x15'
#define CRC_EOP     '\x20' //ok it is a space...

int here;           // address for reading and writing, set by 'U' command
byte data[256];     // global block storage

struct {
    byte devicecode;
    byte revision;
    byte progtype;
    byte parmode;
    byte polling;
    byte selftimed;
    byte lockbytes;
    byte fusebytes;
    int flashpoll;
    int eeprompoll;
    int pagesize;
    // not used in this code:
    // int eepromsize;
    // long flashsize;
} param;

static byte getch() {
    while (!Serial.available())
        ;
    return Serial.read();
}

static void putch(char c) {
    Serial.print(c);
}

static void readbytes(int n) {
    for (byte x = 0; x < n; x++)
        data[x] = getch();
}

static void spi_init() {
    digitalWrite(PIN_SCK, 1);
    digitalWrite(PIN_MISO, 1);
    digitalWrite(PIN_MOSI, 1);
    digitalWrite(PIN_RESET, 1);

    pinMode(PIN_SCK, OUTPUT);
    pinMode(PIN_MISO, INPUT);
    pinMode(PIN_MOSI, OUTPUT);
    pinMode(PIN_RESET, OUTPUT);
}

static byte spi_send(byte b) {
    byte reply = 0;
    for (byte i = 0; i < 8; ++i) {
        digitalWrite(PIN_MOSI, b & 0x80);
        digitalWrite(PIN_SCK, 0); // slow pulse, max 60KHz
        digitalWrite(PIN_SCK, 1);
        b <<= 1;
        reply = (reply << 1) | digitalRead(PIN_MISO);
    }
    return reply;
}

static byte spi_transaction(byte a, byte b, byte c, byte d) {
    spi_send(a); 
    spi_send(b);
    spi_send(c);
    return spi_send(d);
}

static byte spi_transaction_wait(byte a, byte b, byte c, byte d) {
    byte reply = spi_transaction(a, b, c, d);
    while (spi_transaction(0xF0, 0, 0, 0) & 1)
        ;
    return reply;
}

static void empty_reply() {
    if (getch() == CRC_EOP) {
        putch(STK_INSYNC);
        putch(STK_OK);
    } else
        putch(STK_NOSYNC);
}

static void breply(byte b) {
    if (getch() == CRC_EOP) {
        putch(STK_INSYNC);
        putch(b);
        putch(STK_OK);
    } else
        putch(STK_NOSYNC);
}

static void get_version(byte c) {
    switch(c) {
        case 0x80:  breply(HWVER); break;
        case 0x81:  breply(SWMAJ); break;
        case 0x82:  breply(SWMIN); break;
        case 0x93:  breply('S'); break;
        default:    breply(0);
    }
}

static void set_parameters() {
    // call this after reading paramter packet into data[]
    memcpy(&param, data, 9);
    // following fields are big endian
    param.eeprompoll = data[10] * 0x0100 + data[11];
    param.pagesize = data[12] * 0x0100 + data[13];
    // not used in this code:
    // param.eepromsize = data[14] * 0x0100 + data[15];
    // param.flashsize = data[16] * 0x01000000L + data[17] * 0x00010000L +
    //                     data[18] * 0x00000100 + data[19];
}

static void start_pmode() {
    spi_init();
    // following delays may not work on all targets...
    pinMode(PIN_RESET, OUTPUT);
    digitalWrite(PIN_RESET, HIGH);
    pinMode(PIN_SCK, OUTPUT);
    digitalWrite(PIN_SCK, LOW);
    delay(50);
    digitalWrite(PIN_RESET, LOW);
    delay(50);
    pinMode(PIN_MISO, INPUT);
    pinMode(PIN_MOSI, OUTPUT);
    spi_transaction_wait(0xAC, 0x53, 0x00, 0x00);
    digitalWrite(LED_PMODE, 0); 
}

static void end_pmode() {
    pinMode(PIN_MISO, INPUT);
    pinMode(PIN_MOSI, INPUT);
    pinMode(PIN_SCK, INPUT);
    pinMode(PIN_RESET, INPUT);
    digitalWrite(LED_PMODE, 1); 
}

static void universal() {
    readbytes(4);
    breply(spi_transaction_wait(data[0], data[1], data[2], data[3]));
}

static void flash(byte hilo, int addr, byte value) {
    spi_transaction_wait(0x40+8*hilo, addr >> 8, addr, value);
}

static void commit(int addr) {
    spi_transaction_wait(0x4C, addr >> 8, addr, 0);
}

static int current_page(int addr) {
    return here & ~(param.pagesize/2-1);
}

static byte write_flash(int length) {
    if (param.pagesize < 1) return STK_FAILED;
    int page = current_page(here);
    int x = 0;
    while (x < length) {
        if (page != current_page(here)) {
            commit(page);
            page = current_page(here);
        }
        flash(LOW, here, data[x++]);
        flash(HIGH, here, data[x++]);
        here++;
    }
    commit(page);
    return STK_OK;
}

static byte write_eeprom(int length) {
    // this writes byte-by-byte, page writing may be faster (4 bytes at a time)
    for (int x = 0; x < length; x++)
        spi_transaction_wait(0xC0, 0x00, here*2+x, data[x]);
    return STK_OK;
}

static void program_page() {
    char result = STK_FAILED;
    int length = 256 * getch();
    length += getch();
    if (length > 256) {
        putch(STK_FAILED);
        return;
    }
    char memtype = getch();
    readbytes(length);
    if (getch() == CRC_EOP) {
        putch(STK_INSYNC);
        if (memtype == 'F') result = write_flash(length);
        if (memtype == 'E') result = write_eeprom(length);
        putch(result);
    } else
        putch(STK_NOSYNC);
}

static byte flash_read(byte hilo, int addr) {
    return spi_transaction(0x20 + hilo * 8, addr >> 8, addr, 0);
}

static char flash_read_page(int length) {
    for (int x = 0; x < length; x+=2) {
        putch(flash_read(LOW, here));
        putch(flash_read(HIGH, here));
        here++;
    }
    return STK_OK;
}

static char eeprom_read_page(int length) {
    for (int x = 0; x < length; x++)
        putch(spi_transaction(0xA0, 0x00, here*2+x, 0xFF));
    return STK_OK;
}

static void read_page() {
    int length = 256 * getch();
    length += getch();
    char memtype = getch();
    if (getch() == CRC_EOP) {
        putch(STK_INSYNC);
        char result = STK_FAILED;
        if (memtype == 'F') result = flash_read_page(length);
        if (memtype == 'E') result = eeprom_read_page(length);
        putch(result);
    } else
        putch(STK_NOSYNC);
}

static void read_signature() {
    if (getch() == CRC_EOP) {
        putch(STK_INSYNC);
        putch(spi_transaction(0x30, 0x00, 0x00, 0x00));
        putch(spi_transaction(0x30, 0x00, 0x01, 0x00));
        putch(spi_transaction(0x30, 0x00, 0x02, 0x00));
        putch(STK_OK);
    } else
        putch(STK_NOSYNC);
}

static int avrisp() { 
    switch (getch()) {
        case '0': // signon
            empty_reply();
            break;
        case '1':
            if (getch() == CRC_EOP) {
                putch(STK_INSYNC);
                for (const char* p = "AVR ISP"; *p != 0; ++p)
                    putch(*p);
                putch(STK_OK);
            }
            break;
        case 'A':
            get_version(getch());
            break;
        case 'B':
            readbytes(20);
            set_parameters();
            empty_reply();
            break;
        case 'E': // extended parameters - ignore for now
            readbytes(5);
            empty_reply();
            break;
        case 'P':
            start_pmode();
            empty_reply();
            break;
        case 'U':
            here = getch();
            here += 256 * getch();
            empty_reply();
            break;
        case '`': //STK_PROG_FLASH
            getch();
            getch();
            empty_reply();
            break;
        case 'a': //STK_PROG_DATA
            getch();
            empty_reply();
            break;
        case 'd': //STK_PROG_PAGE
            program_page();
            break;
        case 't': //STK_READ_PAGE
            read_page();    
            break;
        case 'V':
            universal();
            break;
        case 'Q':
            end_pmode();
            empty_reply();
            break;
        case 'u': //STK_READ_SIGN
            read_signature();
            break;
            // expecting a command, not CRC_EOP
            // this is how we can get back in sync
        case CRC_EOP:
            putch(STK_NOSYNC);
            break;
            // anything else we will return STK_UNKNOWN
        default:
            putch(getch() == CRC_EOP ? STK_UNKNOWN : STK_NOSYNC);
    }
}

void setup() {
    Serial.begin(19200);
    pinMode(LED_PMODE, OUTPUT);
    digitalWrite(LED_PMODE, 1);
}

void loop(void) {
    avrisp();
}