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
|
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2003-2004 Theodore A. Roth <troth@openavr.org>
* Copyright (C) 2005 Johnathan Corgan <jcorgan@aeinet.com>
*
* 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., 51 Franklin Street, Boston, MA 02110-1301 USA
*/
/*
* avrdude interface for serial programming via FTDI bit bang mode operation.
*/
#include "ac_cfg.h"
#include "avr.h"
#include "pgm.h"
#include "ftbb.h"
#include <string.h>
#if FTDI_SUPPORT
#define FTBB_DEBUG 0
#if HAVE_LIBFTD2XX
#include <ftd2xx.h>
#elif HAVE_FTDI_H
#include <ftdi.h>
#endif
#define RESET (1<<(pgm->pinno[PIN_AVR_RESET]-1))
#define SCK (1<<(pgm->pinno[PIN_AVR_SCK]-1))
#define MOSI (1<<(pgm->pinno[PIN_AVR_MOSI]-1))
#define MISO (1<<(pgm->pinno[PIN_AVR_MISO]-1))
#define FTDDR (MOSI|SCK|RESET)
#if HAVE_LIBFTD2XX
static FT_STATUS status;
static FT_HANDLE handle;
#elif HAVE_FTDI_H
static struct ftdi_context device;
static int status;
#endif
static unsigned char txbits;
/* Send 8 bits over SPI bus with per-bit read back
*
*/
static unsigned char ftbb_txrx(PROGRAMMER *pgm, unsigned char val)
{
int i;
unsigned char bits;
unsigned char res;
#if HAVE_LIBFTD2XX
DWORD written;
#endif /* HAVE_LIBFTD2XX */
res = 0;
bits = 0;
for (i = 0; i < 8; i++) { // For each bit
// Set up data on low phase of SCK
txbits &= ~SCK;
// Set MOSI to high bit of transmit data
if (val & 0x80)
txbits |= MOSI;
else
txbits &= ~MOSI;
#if HAVE_LIBFTD2XX
FT_Write(handle, &txbits, 1, &written);
// Clock just fell, read previous input bit and shift in
FT_GetBitMode(handle, &bits);
#elif HAVE_FTDI_H
ftdi_write_data(&device, &txbits, 1);
// Clock just fell, read previous input bit and shift in
ftdi_read_pins(&device, &bits);
#endif /* HAVE_LIBFTD2XX */
res = (res << 1) | (bits & MISO);
// Now raise SCK to latch data in AVR
txbits |= SCK;
#if HAVE_LIBFTD2XX
FT_Write(handle, &txbits, 1, &written);
#elif HAVE_FTDI_H
ftdi_write_data(&device, &txbits, 1);
#endif /* HAVE_LIBFTD2XX */
// Move on to next bit in transmit data
val = val << 1;
}
return res;
}
/* Generic programmer command function for pgm->cmd
*
* Sends four bytes of command in sequence and collects responses
*
*/
static int ftbb_cmd(PROGRAMMER *pgm, unsigned char cmd[4], unsigned char res[4])
{
int i;
#if FTBB_DEBUG
printf("CMD: %02X%02X%02X%02X ", cmd[0], cmd[1], cmd[2], cmd[3]);
#endif
for (i = 0; i < 4; i++) {
res[i] = ftbb_txrx(pgm, cmd[i]);
}
#if FTBB_DEBUG
printf("RES: %02X%02X%02X%02X\n", res[0], res[1], res[2], res[3]);
#endif
return 0;
}
/* Programmer initialization command for pgm->initialize
*
* Pulse RESET with SCK low, then send SPI start programming command
*
*/
static int ftbb_initialize(PROGRAMMER *pgm, AVRPART *p)
{
#if HAVE_LIBFTD2XX
DWORD written;
#endif /* HAVE_LIBFTD2XX */
// Atmel documentation says to raise RESET for 2 cpu clocks while sclk is low
// then lower RESET and wait 20 ms.
txbits |= RESET;
#if HAVE_LIBFTD2XX
FT_Write(handle, &txbits, 1, &written);
#elif HAVE_FTDI_H
ftdi_write_data(&device, &txbits, 1);
#endif /* HAVE_LIBFTD2XX */
usleep(1000); // 2 AVR cpu clocks at any realistic clock rate
txbits &= ~RESET;
#if HAVE_LIBFTD2XX
FT_Write(handle, &txbits, 1, &written);
#elif HAVE_FTDI_H
ftdi_write_data(&device, &txbits, 1);
#endif /* HAVE_LIBFTD2XX */
usleep(20000);
pgm->program_enable(pgm, p);
return 0;
}
/* Programmer status display for pgm->display
*
* Not-implemented
*
*/
static void ftbb_display(PROGRAMMER *pgm, char *p)
{
#if FTBB_DEBUG
printf("ftbb: display called with: %s\n", p);
#endif
printf("FTBB: RESET mapped to pinno %d\n", pgm->pinno[PIN_AVR_RESET]);
printf("FTBB: SCK mapped to pinno %d\n", pgm->pinno[PIN_AVR_SCK]);
printf("FTBB: MOSI mapped to pinno %d\n", pgm->pinno[PIN_AVR_MOSI]);
printf("FTBB: MISO mapped to pinno %d\n", pgm->pinno[PIN_AVR_MISO]);
}
/* Programmer enable command for pgm->enable
*
* Lowers SCK and RESET in preparation for serial programming
*
*/
static void ftbb_enable(PROGRAMMER *pgm)
{
#if HAVE_LIBFTD2XX
DWORD written;
#endif /* HAVE_LIBFTD2XX */
// Lower SCK & RESET
txbits &= ~SCK;
txbits &= ~RESET;
#if HAVE_LIBFTD2XX
FT_Write(handle, &txbits, 1, &written);
#elif HAVE_FTDI_H
ftdi_write_data(&device, &txbits, 1);
#endif /* HAVE_LIBFTD2XX */
}
/* Programmer disable command for pgm->disable
*
* Raises RESET to return to normal chip operation
*
*/
static void ftbb_disable(PROGRAMMER *pgm)
{
#if HAVE_LIBFTD2XX
DWORD written;
#endif /* HAVE_LIBFTD2XX */
// Raise RESET to return to normal mode
txbits |= RESET;
#if HAVE_LIBFTD2XX
FT_Write(handle, &txbits, 1, &written);
#elif HAVE_FTDI_H
ftdi_write_data(&device, &txbits, 1);
#endif /* HAVE_LIBFTD2XX */
}
/* Programmer programming mode enable function for pgm->program_enable
*
* Starts SPI programming mode
*
*/
static int ftbb_program_enable(PROGRAMMER *pgm, AVRPART *p)
{
unsigned char cmd[4];
unsigned char res[4];
if (p->op[AVR_OP_PGM_ENABLE] == NULL) {
fprintf(stderr, "program enable instruction not defined for part \"%s\"\n", p->desc);
return -1;
}
memset(cmd, 0, sizeof(cmd));
avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd);
pgm->cmd(pgm, cmd, res);
return 0;
}
/* Progammer erase function for pgm->erase
*
* Sends chip erase command and sleeps the chip erase delay
*
*/
static int ftbb_chip_erase(PROGRAMMER *pgm, AVRPART *p)
{
unsigned char cmd[4];
unsigned char res[4];
if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
fprintf(stderr, "chip erase instruction not defined for part \"%s\"\n", p->desc);
return -1;
}
memset(cmd, 0, sizeof(cmd));
avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd);
pgm->cmd(pgm, cmd, res);
usleep(p->chip_erase_delay);
return 0;
}
/* Parse routine for device name
*
* FFFF:FFFF:0
*
*/
int ftbb_parse_name(char *name, int *vid, int *pid, int *ifc)
{
printf("name=%s\n", name);
return 0;
}
/* Programmer open command for pgm->open
*
* Opens FTD2XX device specified by 'name', performs a chip reset, then
* sets the baudrate and bit bang mode
*
*/
static int ftbb_open(PROGRAMMER *pgm, char *name)
{
int vid, pid, ifc;
ftbb_parse_name(name, &vid, &pid, &ifc);
#if HAVE_LIBFTD2XX
int devnum = 0;
if (strcmp(name, "ft0")) {
fprintf(stderr, "ERROR: FTD2XX device selection not yet implemented!\n");
return -1;
}
// Call FTD2XX library to open device
if ((status = FT_Open(devnum, &handle)) != FT_OK) {
fprintf(stderr, "Failed to open FTD2XX device #%d.\n", devnum);
return -1;
}
// Reset chipset
if ((status = FT_ResetDevice(handle)) != FT_OK) {
fprintf(stderr, "Failed to reset chipset for FTD2XX device #%d.\n", devnum);
return -1;
}
// Set baud rate for bit bang interface
if ((status = FT_SetBaudRate(handle, pgm->baudrate)) != FT_OK) {
fprintf(stderr, "Failed to set baud rate for FTD2XX device #%d.\n", devnum);
return -1;
}
// Set bit bang direction and mode
if ((status = FT_SetBitMode(handle, FTDDR, 1)) != FT_OK) {
fprintf(stderr, "Failed to set bit bang mode for FTD2XX device #%d.\n", devnum);
return -1;
}
#elif HAVE_FTDI_H
// Open device via FTDI library *** FIXME *** hardcoded VID and PID
if (ftdi_usb_open(&device, EZDOP_VENDORID, EZDOP_PRODUCTID)) {
fprintf(stderr, "ftdi_usb_open: %s", device.error_str);
return -1;
}
// Reset FTDI chipset
if (ftdi_usb_reset(&device)) {
fprintf(stderr, "ftdi_usb_reset: %s", device.error_str);
return -1;
}
// Set FTDI chipset baudrate for bitbang
if (ftdi_set_baudrate(&device, pgm->baudrate)) {
fprintf(stderr, "ftdi_set_baudrate: %s", device.error_str);
return -1;
}
// Enable bitbang
if (ftdi_enable_bitbang(&device, FTDDR)) {
fprintf(stderr, "ftdi_enable_bitbang: %s", device.error_str);
return -1;
}
// Minimum chunk size for reads to reduce latency
if (ftdi_read_data_set_chunksize(&device, 256)) {
fprintf(stderr, "ftdi_read_data_set_chunksize: %s", device.error_str);
return -1;
}
#endif /* HAVE_LIBFTD2XX */
return 0;
}
/* Programmer close function for pgm->close
*
* Releases FTD2XX device
*
*/
static void ftbb_close(PROGRAMMER *pgm)
{
#if HAVE_LIBFTD2XX
FT_Close(handle);
#elif HAVE_FTDI_H
ftdi_deinit(&device);
#endif
}
#endif /* FTDI_SUPPORT */
/* Programmer initialization command for startup.
*
* Sets appropriate function pointers in structure
* Tests FTD2XX interface by enumerating number of devices
*
*/
void ftbb_initpgm (PROGRAMMER *pgm)
{
#if FTDI_SUPPORT
#if HAVE_LIBFTD2XX
DWORD num_devices;
#endif
strcpy(pgm->type, "ftbb");
pgm->initialize = ftbb_initialize;
pgm->display = ftbb_display;
pgm->enable = ftbb_enable;
pgm->disable = ftbb_disable;
pgm->program_enable = ftbb_program_enable;
pgm->chip_erase = ftbb_chip_erase;
pgm->cmd = ftbb_cmd;
pgm->open = ftbb_open;
pgm->close = ftbb_close;
#if HAVE_LIBFTD2XX
if ((status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY)) != FT_OK)
fprintf(stderr, "Failed to initialize FTD2XX interface. (%li)\n", status);
else
printf("%lu FTD2XX device(s) found.\n", num_devices);
#elif HAVE_FTDI_H
if ((status = ftdi_init(&device)) < 0)
fprintf(stderr, "Failed to initialize FTDI interface. (%i)\n", status);
#endif
txbits = RESET;
#endif /* FTDI_SUPPORT */
}
|