summaryrefslogtreecommitdiff
path: root/digital/avr/modules/isp/isp.c
blob: 36d904fe3c76f123e1bf45059eddb91eb7b8fb7d (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* isp.c - ISP AVR programming. */
/* avr.isp - Serial programming AVR module. {{{
 *
 * Copyright (C) 2008 Nicolas Schodet
 *
 * Inspired by STK500 programmer by Guido Socher.
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"

#include "modules/utils/utils.h"

#include "isp.h"

/** ISP context. */
struct isp_t
{
    /** Current address. */
    uint16_t addr;
    /** Current extended address. */
    uint8_t ext_addr;
    /** Extended address loaded in device. */
    uint8_t last_ext_addr;
    /** Whether the device needs extended addressing. */
    uint8_t larger_than_64k;
    /** Programming start address (used for page program command). */
    uint16_t start_addr;
    /** Number of bytes left to process. */
    uint16_t bytes_left;
    /** Programming mode. */
    uint8_t mode;
    /** Programming delay if no other poll method. */
    uint8_t delay_ms;
    /** Write Program Memory or Load Page. */
    uint8_t cmd_write_mem;
    /** Write Page. */
    uint8_t cmd_write_page;
    /** Read Program Memory. */
    uint8_t cmd_read_mem;
    /** Value read until memory is programmed for value polling. */
    uint8_t poll[2];
    /** Whether the page can be polled (if almost one byte is not poll[0]).
     * This is non zero if true, with LSB = 1 for high byte. */
    uint8_t pollable;
    /** Address to use for polling. */
    uint16_t poll_addr;
    /** Whether this is a flash memory. */
    uint8_t flash;
    /** Whether addresses are words addresses. */
#define word_addr flash
};

/** Command used to load extended address. */
#define ISP_CMD_LOAD_EXTENDED_ADDRESS 0x4d
/** Bit to address high byte. */
#define ISP_CMD_HIGH_BYTE (1 << 3)
/** Extra EEPROM programming delay. */
#define ISP_EEPROM_DELAY_MS 2
/** Miscellaneous read delay. */
#define ISP_MISC_READ_DELAY_MS 5

/** Global context. */
static struct isp_t isp_global;

#define isp_spi_enable AC_ISP_SPI_ENABLE
#define isp_spi_disable AC_ISP_SPI_DISABLE
#define isp_spi_sck_pulse AC_ISP_SPI_SCK_PULSE
#define isp_spi_tx AC_ISP_SPI_TX

/** Transmit a 16 bit data. */
static void
isp_spi_tx_16 (uint16_t data)
{
    isp_spi_tx ((data >> 8) & 0xff);
    isp_spi_tx ((data >> 0) & 0xff);
}

/** Transmit a 32 bit data and return last byte received.  This is used for
 * RDY/BSY polling. */
static uint8_t
isp_spi_tx_32 (uint32_t data)
{
    isp_spi_tx ((data >> 24) & 0xff);
    isp_spi_tx ((data >> 16) & 0xff);
    isp_spi_tx ((data >> 8) & 0xff);
    return isp_spi_tx ((data >> 0) & 0xff);
}

/** Wait for the specified delay, which can be a variable (it must be a
 * constant for utils_delay_ms. */
static void
isp_delay_ms (uint8_t delay_ms)
{
    while (delay_ms--)
	utils_delay_ms (1);
}

/** Enable SPI port and enter programing mode.
 * - timeout_ms: command time-out, unused.
 * - stab_delay_ms: stabilisation delay once device is reseted and SPI
 *   initialised.
 * - cmd_exe_delay_ms: delay for the command execution.
 * - synch_loops: number of synchronisation loops (there is no start of frame
 *   in SPI).
 * - byte_delay_ms: delay between each byte.
 * - poll_value: value to read to consider the sequence is correct.
 * - poll_index: transfer index at which the poll_value must be read.
 * - cmd: command bytes to be transfered.
 * - returns: ISP_OK or ISP_FAILED if no synchronisation. */
uint8_t
isp_enter_progmode (uint8_t timeout_ms, uint8_t stab_delay_ms,
		    uint8_t cmd_exe_delay_ms, uint8_t synch_loops,
		    uint8_t byte_delay_ms, uint8_t poll_value,
		    uint8_t poll_index, const uint8_t cmd[4])
{
    uint8_t i, tmp1, tmp2;
    /* Reset context. */
    isp_global.ext_addr = 0;
    isp_global.last_ext_addr = 0xff;
    isp_global.larger_than_64k = 0;
    isp_global.bytes_left = 0;
    /* Enable SPI. */
    isp_spi_enable ();
    isp_delay_ms (stab_delay_ms);
    /* Limit the number of loops. */
    if (synch_loops > 48)
	synch_loops = 48;
    /* Minimum byte delay. */
    if (byte_delay_ms < 1)
	byte_delay_ms = 1;
    /* Synchronisation loops. */
    for (i = 0; i < synch_loops; i++)
      {
	isp_delay_ms (cmd_exe_delay_ms);
	isp_spi_tx (cmd[0]);
	isp_delay_ms (byte_delay_ms);
	isp_spi_tx (cmd[1]);
	isp_delay_ms (byte_delay_ms);
	tmp1 = isp_spi_tx (cmd[2]);
	isp_delay_ms (byte_delay_ms);
	tmp2 = isp_spi_tx (cmd[3]);
	/* Test success. */
	if (poll_index == 0
	    || (poll_index == 3 && tmp1 == poll_value)
	    || (poll_index != 3 && tmp2 == poll_value))
	    return ISP_OK;
	/* Else, new try. */
	isp_spi_sck_pulse ();
	isp_delay_ms (20);
      }
    return ISP_FAILED;
}

/** Leave programming mode and disable SPI port.
 * - pre_delay_ms: delay before disabling.
 * - post_delay_ms: delay after disabling. */
void
isp_leave_progmode (uint8_t pre_delay_ms, uint8_t post_delay_ms)
{
    isp_delay_ms (pre_delay_ms);
    isp_spi_disable ();
    isp_delay_ms (post_delay_ms);
}

/** Load programing address.
 * - addr: address to load, bit 31 means the device has more than 64k words
 *   and extended addressing should be used. */
void
isp_load_address (uint32_t addr)
{
    isp_global.addr = addr & 0xffff;
    isp_global.ext_addr = (addr >> 16) & 0xff;
    isp_global.last_ext_addr = 0xff;
    isp_global.larger_than_64k = (addr & 0x80000000) ? 1 : 0;
}

/** Chip full erase.
 * - erase_delay_ms: delay to ensure the erase is finished.
 * - poll_method: use delay (0) or RDY/BSY polling (1).
 * - cmd: chip erase command.
 * - returns: ISP_OK or ISP_TIMEOUT if using polling and it timed-out. */
uint8_t
isp_chip_erase (uint8_t erase_delay_ms, uint8_t poll_method,
		const uint8_t cmd[4])
{
    uint8_t tries;
    isp_spi_tx (cmd[0]);
    isp_spi_tx (cmd[1]);
    isp_spi_tx (cmd[2]);
    isp_spi_tx (cmd[3]);
    if (poll_method == 0)
      {
	/* Use delay. */
	isp_delay_ms (erase_delay_ms);
      }
    else
      {
	/* Use RDY/BSY command. */
	tries = 150;
	while ((isp_spi_tx_32 (0xf0000000) & 1) && tries)
	    tries--;
	if (tries == 0)
	    return ISP_TIMEOUT;
      }
    return ISP_OK;
}

/** Start a program command.  See context for parameters.
 * - num_bytes: total number of bytes to program.
 * - returns: ISP_OK or ISP_FAILED for bad parameters.
 * See context for other parameters meaning. */
uint8_t
isp_program_begin (uint16_t num_bytes, uint8_t mode, uint8_t delay_ms,
		   uint8_t cmd_write_mem, uint8_t cmd_write_page,
		   uint8_t cmd_read_mem, const uint8_t poll[2], uint8_t flash)
{
    /* Set delay bounds. */
    if (delay_ms < 4)
	delay_ms = 4;
    if (delay_ms > 32)
	delay_ms = 32;
    /* Check data size. */
    if (word_addr && (num_bytes & 1))
	return ISP_FAILED;
    /* Store parameters. */
    isp_global.start_addr = isp_global.addr;
    isp_global.bytes_left = num_bytes;
    isp_global.mode = mode;
    isp_global.delay_ms = delay_ms;
    isp_global.cmd_write_mem = cmd_write_mem;
    isp_global.cmd_write_page = cmd_write_page;
    isp_global.cmd_read_mem = cmd_read_mem;
    isp_global.poll[0] = poll[0];
    isp_global.poll[1] = poll[1];
    isp_global.pollable = 0;
    isp_global.flash = flash;
    return ISP_OK;
}

/** Start a flash memory program command.
 * - num_bytes: total number of bytes to program.
 * - returns: ISP_OK or ISP_FAILED for bad parameters.
 * See context for other parameters meaning. */
uint8_t
isp_program_flash_begin (uint16_t num_bytes, uint8_t mode, uint8_t delay_ms,
			 uint8_t cmd_write_mem, uint8_t cmd_write_page,
			 uint8_t cmd_read_mem, const uint8_t poll[2])
{
    return isp_program_begin (num_bytes, mode, delay_ms, cmd_write_mem,
			      cmd_write_page, cmd_read_mem, poll, 1);
}

/** Start a EEPROM memory program command.
 * - num_bytes: total number of bytes to program.
 * - returns: ISP_OK or ISP_FAILED for bad parameters.
 * See context for other parameters meaning. */
uint8_t
isp_program_eeprom_begin (uint16_t num_bytes, uint8_t mode, uint8_t delay_ms,
			  uint8_t cmd_write_mem, uint8_t cmd_write_page,
			  uint8_t cmd_read_mem, const uint8_t poll[2])
{
    return isp_program_begin (num_bytes, mode, delay_ms, cmd_write_mem,
			      cmd_write_page, cmd_read_mem, poll, 0);
}

/** Provide data for memory programming.  Data should be given by even sized
 * packs or loading of word addressed data will fail.
 * - returns: ISP_OK, ISP_FAILED for bad parameters or ISP_TIMEOUT if using
 *   polling and it timed-out. */
uint8_t
isp_program_continue (const uint8_t *data, uint16_t size)
{
    uint16_t i;
    uint8_t tries, read;
    /* Check size. */
    if (size > isp_global.bytes_left
	|| (isp_global.word_addr && (size & 1)))
	return ISP_FAILED;
    isp_global.bytes_left -= size;
    /* Loop on each input bytes. */
    for (i = 0; i < size; i++)
      {
	/* Check for Load Extended Address. */
	if (isp_global.larger_than_64k
	    && isp_global.last_ext_addr != isp_global.ext_addr)
	  {
	    isp_spi_tx (ISP_CMD_LOAD_EXTENDED_ADDRESS);
	    isp_spi_tx (0x00);
	    isp_spi_tx (isp_global.ext_addr);
	    isp_spi_tx (0x00);
	    isp_global.last_ext_addr = isp_global.ext_addr;
	  }
	/* Write memory/load page, using bit 3 as byte selector. */
	if (isp_global.word_addr && (i & 1))
	    isp_spi_tx (isp_global.cmd_write_mem | ISP_CMD_HIGH_BYTE);
	else
	    isp_spi_tx (isp_global.cmd_write_mem);
	isp_spi_tx_16 (isp_global.addr);
	isp_spi_tx (data[i]);
	/* EEPROM needs more delay. */
	if (!isp_global.flash)
	    isp_delay_ms (ISP_EEPROM_DELAY_MS);
	/* Poll. */
	if (!(isp_global.mode & ISP_MODE_PAGE))
	  {
	    /* Poll right now. */
	    tries = 150;
	    read = isp_global.poll[0];
	    if ((isp_global.mode & ISP_MODE_WORD_VALUE)
		&& data[i] != read)
	      {
		/* Poll by reading byte. */
		while (read != data[i] && tries)
		  {
		    if (isp_global.word_addr && (i & 1))
			isp_spi_tx (isp_global.cmd_read_mem
				    | ISP_CMD_HIGH_BYTE);
		    else
			isp_spi_tx (isp_global.cmd_read_mem);
		    isp_spi_tx_16 (isp_global.addr);
		    read = isp_spi_tx (0x00);
		    tries--;
		  }
	      }
	    else if (isp_global.mode & ISP_MODE_WORD_RDY_BSY)
	      {
		/* RDY/BSY polling. */
		while ((isp_spi_tx_32 (0xf0000000) & 1) && tries)
		    tries--;
	      }
	    else
		/* Simple delay. */
		isp_delay_ms (isp_global.delay_ms);
	    if (tries == 0)
		return ISP_TIMEOUT;
	  }
	else
	  {
	    /* Check for poll method. */
	    if (!isp_global.pollable && data[i] != isp_global.poll[0])
	      {
		isp_global.pollable = 0x02 | (i & 1);
		isp_global.poll_addr = isp_global.addr;
	      }
	  }
	/* Increment address. */
	if (!isp_global.word_addr || (i & 1))
	  {
	    isp_global.addr++;
	    if (isp_global.addr == 0)
		isp_global.ext_addr++;
	  }
      }
    return ISP_OK;
}

/** End program command.
 * - returns: ISP_OK, ISP_FAILED if too early or ISP_TIMEOUT if using polling
 *   and it timed-out. */
uint8_t
isp_program_end (void)
{
    uint8_t tries, read;
    /* Check size. */
    if (isp_global.bytes_left != 0)
	return ISP_FAILED;
    /* Write page if requested. */
    if ((isp_global.mode & (ISP_MODE_PAGE | ISP_MODE_PAGE_WRITE))
	== (ISP_MODE_PAGE | ISP_MODE_PAGE_WRITE))
      {
	/* Write page. */
	isp_spi_tx (isp_global.cmd_write_page);
	isp_spi_tx_16 (isp_global.start_addr);
	isp_spi_tx (0x00);
	/* EEPROM needs more delay. */
	if (!isp_global.flash)
	    isp_delay_ms (ISP_EEPROM_DELAY_MS);
	/* Poll. */
	tries = 150;
	read = isp_global.poll[0];
	if ((isp_global.mode & ISP_MODE_PAGE_VALUE)
	    && isp_global.pollable)
	  {
	    /* Poll by reading byte. */
	    while (read == isp_global.poll[0] && tries)
	      {
		if (isp_global.word_addr && (isp_global.pollable & 1))
		    isp_spi_tx (isp_global.cmd_read_mem
				| ISP_CMD_HIGH_BYTE);
		else
		    isp_spi_tx (isp_global.cmd_read_mem);
		isp_spi_tx_16 (isp_global.poll_addr);
		read = isp_spi_tx (0x00);
		tries--;
	      }
	  }
	else if (isp_global.mode & ISP_MODE_PAGE_RDY_BSY)
	  {
	    /* RDY/BSY polling. */
	    while ((isp_spi_tx_32 (0xf0000000) & 1) && tries)
		tries--;
	  }
	else
	    /* Simple delay. */
	    isp_delay_ms (isp_global.delay_ms);
	if (tries == 0)
	    return ISP_TIMEOUT;
      }
    return ISP_OK;
}

/** Start a read command.
 * - num_bytes: total number of bytes to program.
 * - returns: ISP_OK or ISP_FAILED for bad parameters.
 * See context for other parameters meaning. */
uint8_t
isp_read_begin (uint16_t num_bytes, uint8_t cmd_read_mem, uint8_t flash)
{
    /* Check data size. */
    if (word_addr && (num_bytes & 1))
	return ISP_FAILED;
    /* Store parameters. */
    isp_global.bytes_left = num_bytes;
    isp_global.cmd_read_mem = cmd_read_mem;
    isp_global.flash = flash;
    return ISP_OK;
}

/** Start a flash memory read command.
 * - num_bytes: total number of bytes to program.
 * - returns: ISP_OK or ISP_FAILED for bad parameters.
 * See context for other parameters meaning. */
uint8_t
isp_read_flash_begin (uint16_t num_bytes, uint8_t cmd_read_mem)
{
    return isp_read_begin (num_bytes, cmd_read_mem, 1);
}

/** Start a EEPROM memory read command.
 * - num_bytes: total number of bytes to program.
 * - returns: ISP_OK or ISP_FAILED for bad parameters.
 * See context for other parameters meaning. */
uint8_t
isp_read_eeprom_begin (uint16_t num_bytes, uint8_t cmd_read_mem)
{
    return isp_read_begin (num_bytes, cmd_read_mem, 0);
}

/** Get data from read memory.  Data should be read by even sized packs or
 * loading of word addressed data will fail.
 * - returns: ISP_OK or ISP_FAILED for bad parameters. */
uint8_t
isp_read_continue (uint8_t *data, uint16_t size)
{
    uint16_t i;
    /* Check size. */
    if (size > isp_global.bytes_left
	|| (isp_global.word_addr && (size & 1)))
	return ISP_FAILED;
    isp_global.bytes_left -= size;
    /* Loop on each bytes. */
    for (i = 0; i < size; i++)
      {
	/* Check for Load Extended Address. */
	if (isp_global.larger_than_64k
	    && isp_global.last_ext_addr != isp_global.ext_addr)
	  {
	    isp_spi_tx (ISP_CMD_LOAD_EXTENDED_ADDRESS);
	    isp_spi_tx (0x00);
	    isp_spi_tx (isp_global.ext_addr);
	    isp_spi_tx (0x00);
	    isp_global.last_ext_addr = isp_global.ext_addr;
	  }
	/* Read memory, using bit 3 as byte selector. */
	if (isp_global.word_addr && (i & 1))
	    isp_spi_tx (isp_global.cmd_read_mem | ISP_CMD_HIGH_BYTE);
	else
	    isp_spi_tx (isp_global.cmd_read_mem);
	isp_spi_tx_16 (isp_global.addr);
	data[i] = isp_spi_tx (0x00);
	/* Increment address. */
	if (!isp_global.word_addr || (i & 1))
	  {
	    isp_global.addr++;
	    if (isp_global.addr == 0)
		isp_global.ext_addr++;
	  }
      }
    return ISP_OK;
}

/** End read command.
 * - returns: ISP_OK or ISP_FAILED if too early. */
uint8_t
isp_read_end (void)
{
    /* Check size. */
    if (isp_global.bytes_left != 0)
	return ISP_FAILED;
    return ISP_OK;
}

/** Program miscellaneous memory (fuse, lock).
 * - cmd: program command. */
void
isp_program_misc (const uint8_t cmd[4])
{
    uint8_t i;
    for (i = 0; i < 4; i++)
	isp_spi_tx (cmd[i]);
}

/** Read miscellaneous memory (fuse, lock, signature, osccal).
 * - ret_addr: transfer index at which the return value must be read.
 * - cmd: read command. */
uint8_t
isp_read_misc (uint8_t ret_addr, const uint8_t cmd[4])
{
    uint8_t i, read = 0, tmp;
    for (i = 0; i < 4; i++)
      {
	tmp = isp_spi_tx (cmd[i]);
	if (i == ret_addr)
	    read = tmp;
	isp_delay_ms (ISP_MISC_READ_DELAY_MS);
      }
    return read;
}

/** Generic SPI access.
 * - num_tx: number of bytes to transmit.
 * - num_rx: number of bytes to receive.
 * - rx_start: start reception after this number of transmitted bytes.
 * - dout: buffer to read sent bytes.
 * - din: buffer to write received bytes.
 * Limitation: no support for doing this in several chunks as memory
 * programing and reading. */
void
isp_multi (uint8_t num_tx, uint8_t num_rx, uint8_t rx_start,
	   const uint8_t *dout, uint8_t *din)
{
    uint8_t in, out;
    while (num_tx || num_rx)
      {
	out = 0;
	if (num_tx)
	  {
	    out = *dout++;
	    num_tx--;
	  }
	in = isp_spi_tx (out);
	if (rx_start)
	    rx_start--;
	else if (num_rx)
	  {
	    *din++ = in;
	    num_rx--;
	  }
      }
}