1 /*
2 * avrdude - A Downloader/Uploader for AVR device programmers
3 * Copyright (C) 2000, 2001, 2002, 2003 Brian S. Dean <bsd@bsdhome.com>
4 * Copyright (C) 2005 Michael Holzt <kju-avr@fqdn.org>
5 * Copyright (C) 2011 Darell Tan <darell.tan@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 /* $Id: bitbang.c 1321 2014-06-13 20:07:40Z awachtler $ */
21
22 #include "ac_cfg.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #if !defined(WIN32NATIVE)
32 # include <signal.h>
33 # include <sys/time.h>
34 #endif
35
36 #include "avrdude.h"
37 #include "libavrdude.h"
38
39 #include "par.h"
40 #include "serbb.h"
41 #include "tpi.h"
42 #include "bitbang.h"
43
44 static int delay_decrement;
45
46 #if defined(WIN32NATIVE)
47 static int has_perfcount;
48 static LARGE_INTEGER freq;
49 #else
50 static volatile int done;
51
52 typedef void (*mysighandler_t)(int);
53 static mysighandler_t saved_alarmhandler;
54
alarmhandler(int signo)55 static void alarmhandler(int signo)
56 {
57 done = 1;
58 signal(SIGALRM, saved_alarmhandler);
59 }
60 #endif /* WIN32NATIVE */
61
62 /*
63 * Calibrate the microsecond delay loop below.
64 */
bitbang_calibrate_delay(void)65 static void bitbang_calibrate_delay(void)
66 {
67 #if defined(WIN32NATIVE)
68 /*
69 * If the hardware supports a high-resolution performance counter,
70 * we ultimately prefer that one, as it gives quite accurate delays
71 * on modern high-speed CPUs.
72 */
73 if (QueryPerformanceFrequency(&freq))
74 {
75 has_perfcount = 1;
76 avrdude_message(MSG_NOTICE2, "%s: Using performance counter for bitbang delays\n",
77 progname);
78 }
79 else
80 {
81 /*
82 * If a high-resolution performance counter is not available, we
83 * don't have any Win32 implementation for setting up the
84 * per-microsecond delay count, so we can only run on a
85 * preconfigured delay stepping there. The figure below should at
86 * least be correct within an order of magnitude, judging from the
87 * auto-calibration figures seen on various Unix systems on
88 * comparable hardware.
89 */
90 avrdude_message(MSG_NOTICE2, "%s: Using guessed per-microsecond delay count for bitbang delays\n",
91 progname);
92 delay_decrement = 100;
93 }
94 #else /* !WIN32NATIVE */
95 struct itimerval itv;
96 volatile int i;
97
98 avrdude_message(MSG_NOTICE2, "%s: Calibrating delay loop...",
99 progname);
100 i = 0;
101 done = 0;
102 saved_alarmhandler = signal(SIGALRM, alarmhandler);
103 /*
104 * Set ITIMER_REAL to 100 ms. All known systems have a timer
105 * granularity of 10 ms or better, so counting the delay cycles
106 * accumulating over 100 ms should give us a rather realistic
107 * picture, without annoying the user by a lengthy startup time (as
108 * an alarm(1) would do). Of course, if heavy system activity
109 * happens just during calibration but stops before the remaining
110 * part of AVRDUDE runs, this will yield wrong values. There's not
111 * much we can do about this.
112 */
113 itv.it_value.tv_sec = 0;
114 itv.it_value.tv_usec = 100000;
115 itv.it_interval.tv_sec = itv.it_interval.tv_usec = 0;
116 setitimer(ITIMER_REAL, &itv, 0);
117 while (!done)
118 i--;
119 itv.it_value.tv_sec = itv.it_value.tv_usec = 0;
120 setitimer(ITIMER_REAL, &itv, 0);
121 /*
122 * Calculate back from 100 ms to 1 us.
123 */
124 delay_decrement = -i / 100000;
125 avrdude_message(MSG_NOTICE2, " calibrated to %d cycles per us\n",
126 delay_decrement);
127 #endif /* WIN32NATIVE */
128 }
129
130 /*
131 * Delay for approximately the number of microseconds specified.
132 * usleep()'s granularity is usually like 1 ms or 10 ms, so it's not
133 * really suitable for short delays in bit-bang algorithms.
134 */
bitbang_delay(unsigned int us)135 void bitbang_delay(unsigned int us)
136 {
137 #if defined(WIN32NATIVE)
138 LARGE_INTEGER countNow, countEnd;
139
140 if (has_perfcount)
141 {
142 QueryPerformanceCounter(&countNow);
143 countEnd.QuadPart = countNow.QuadPart + freq.QuadPart * us / 1000000ll;
144
145 while (countNow.QuadPart < countEnd.QuadPart)
146 QueryPerformanceCounter(&countNow);
147 }
148 else /* no performance counters -- run normal uncalibrated delay */
149 {
150 #endif /* WIN32NATIVE */
151 volatile unsigned int del = us * delay_decrement;
152
153 while (del > 0)
154 del--;
155 #if defined(WIN32NATIVE)
156 }
157 #endif /* WIN32NATIVE */
158 }
159
160 /*
161 * transmit and receive a byte of data to/from the AVR device
162 */
bitbang_txrx(PROGRAMMER * pgm,unsigned char byte)163 static unsigned char bitbang_txrx(PROGRAMMER * pgm, unsigned char byte)
164 {
165 int i;
166 unsigned char r, b, rbyte;
167
168 rbyte = 0;
169 for (i=7; i>=0; i--) {
170 /*
171 * Write and read one bit on SPI.
172 * Some notes on timing: Let T be the time it takes to do
173 * one pgm->setpin()-call resp. par clrpin()-call, then
174 * - SCK is high for 2T
175 * - SCK is low for 2T
176 * - MOSI setuptime is 1T
177 * - MOSI holdtime is 3T
178 * - SCK low to MISO read is 2T to 3T
179 * So we are within programming specs (expect for AT90S1200),
180 * if and only if T>t_CLCL (t_CLCL=clock period of target system).
181 *
182 * Due to the delay introduced by "IN" and "OUT"-commands,
183 * T is greater than 1us (more like 2us) on x86-architectures.
184 * So programming works safely down to 1MHz target clock.
185 */
186
187 b = (byte >> i) & 0x01;
188
189 /* set the data input line as desired */
190 pgm->setpin(pgm, PIN_AVR_MOSI, b);
191
192 pgm->setpin(pgm, PIN_AVR_SCK, 1);
193
194 /*
195 * read the result bit (it is either valid from a previous falling
196 * edge or it is ignored in the current context)
197 */
198 r = pgm->getpin(pgm, PIN_AVR_MISO);
199
200 pgm->setpin(pgm, PIN_AVR_SCK, 0);
201
202 rbyte |= r << i;
203 }
204
205 return rbyte;
206 }
207
bitbang_tpi_clk(PROGRAMMER * pgm)208 static int bitbang_tpi_clk(PROGRAMMER * pgm)
209 {
210 unsigned char r = 0;
211 pgm->setpin(pgm, PIN_AVR_SCK, 1);
212
213 r = pgm->getpin(pgm, PIN_AVR_MISO);
214
215 pgm->setpin(pgm, PIN_AVR_SCK, 0);
216
217 return r;
218 }
219
bitbang_tpi_tx(PROGRAMMER * pgm,unsigned char byte)220 void bitbang_tpi_tx(PROGRAMMER * pgm, unsigned char byte)
221 {
222 int i;
223 unsigned char b, parity;
224
225 /* start bit */
226 pgm->setpin(pgm, PIN_AVR_MOSI, 0);
227 bitbang_tpi_clk(pgm);
228
229 parity = 0;
230 for (i = 0; i <= 7; i++) {
231 b = (byte >> i) & 0x01;
232 parity ^= b;
233
234 /* set the data input line as desired */
235 pgm->setpin(pgm, PIN_AVR_MOSI, b);
236 bitbang_tpi_clk(pgm);
237 }
238
239 /* parity bit */
240 pgm->setpin(pgm, PIN_AVR_MOSI, parity);
241 bitbang_tpi_clk(pgm);
242
243 /* 2 stop bits */
244 pgm->setpin(pgm, PIN_AVR_MOSI, 1);
245 bitbang_tpi_clk(pgm);
246 bitbang_tpi_clk(pgm);
247 }
248
bitbang_tpi_rx(PROGRAMMER * pgm)249 int bitbang_tpi_rx(PROGRAMMER * pgm)
250 {
251 int i;
252 unsigned char b, rbyte, parity;
253
254 /* make sure pin is on for "pullup" */
255 pgm->setpin(pgm, PIN_AVR_MOSI, 1);
256
257 /* wait for start bit (up to 10 bits) */
258 b = 1;
259 for (i = 0; i < 10; i++) {
260 b = bitbang_tpi_clk(pgm);
261 if (b == 0)
262 break;
263 }
264 if (b != 0) {
265 avrdude_message(MSG_INFO, "bitbang_tpi_rx: start bit not received correctly\n");
266 return -1;
267 }
268
269 rbyte = 0;
270 parity = 0;
271 for (i=0; i<=7; i++) {
272 b = bitbang_tpi_clk(pgm);
273 parity ^= b;
274
275 rbyte |= b << i;
276 }
277
278 /* parity bit */
279 if (bitbang_tpi_clk(pgm) != parity) {
280 avrdude_message(MSG_INFO, "bitbang_tpi_rx: parity bit is wrong\n");
281 return -1;
282 }
283
284 /* 2 stop bits */
285 b = 1;
286 b &= bitbang_tpi_clk(pgm);
287 b &= bitbang_tpi_clk(pgm);
288 if (b != 1) {
289 avrdude_message(MSG_INFO, "bitbang_tpi_rx: stop bits not received correctly\n");
290 return -1;
291 }
292
293 return rbyte;
294 }
295
bitbang_rdy_led(PROGRAMMER * pgm,int value)296 int bitbang_rdy_led(PROGRAMMER * pgm, int value)
297 {
298 pgm->setpin(pgm, PIN_LED_RDY, !value);
299 return 0;
300 }
301
bitbang_err_led(PROGRAMMER * pgm,int value)302 int bitbang_err_led(PROGRAMMER * pgm, int value)
303 {
304 pgm->setpin(pgm, PIN_LED_ERR, !value);
305 return 0;
306 }
307
bitbang_pgm_led(PROGRAMMER * pgm,int value)308 int bitbang_pgm_led(PROGRAMMER * pgm, int value)
309 {
310 pgm->setpin(pgm, PIN_LED_PGM, !value);
311 return 0;
312 }
313
bitbang_vfy_led(PROGRAMMER * pgm,int value)314 int bitbang_vfy_led(PROGRAMMER * pgm, int value)
315 {
316 pgm->setpin(pgm, PIN_LED_VFY, !value);
317 return 0;
318 }
319
320
321 /*
322 * transmit an AVR device command and return the results; 'cmd' and
323 * 'res' must point to at least a 4 byte data buffer
324 */
bitbang_cmd(PROGRAMMER * pgm,const unsigned char * cmd,unsigned char * res)325 int bitbang_cmd(PROGRAMMER * pgm, const unsigned char *cmd,
326 unsigned char *res)
327 {
328 int i;
329
330 for (i=0; i<4; i++) {
331 res[i] = bitbang_txrx(pgm, cmd[i]);
332 }
333
334 if(verbose > 4)
335 {
336 avrdude_message(MSG_NOTICE2, "bitbang_cmd(): [ ");
337 for(i = 0; i < 4; i++)
338 avrdude_message(MSG_NOTICE2, "%02X ", cmd[i]);
339 avrdude_message(MSG_NOTICE2, "] [ ");
340 for(i = 0; i < 4; i++)
341 {
342 avrdude_message(MSG_NOTICE2, "%02X ", res[i]);
343 }
344 avrdude_message(MSG_NOTICE2, "]\n");
345 }
346
347 return 0;
348 }
349
bitbang_cmd_tpi(PROGRAMMER * pgm,const unsigned char * cmd,int cmd_len,unsigned char * res,int res_len)350 int bitbang_cmd_tpi(PROGRAMMER * pgm, const unsigned char *cmd,
351 int cmd_len, unsigned char *res, int res_len)
352 {
353 int i, r;
354
355 pgm->pgm_led(pgm, ON);
356
357 for (i=0; i<cmd_len; i++) {
358 bitbang_tpi_tx(pgm, cmd[i]);
359 }
360
361 r = 0;
362 for (i=0; i<res_len; i++) {
363 r = bitbang_tpi_rx(pgm);
364 if (r == -1)
365 break;
366 res[i] = r;
367 }
368
369 if(verbose >= 2)
370 {
371 avrdude_message(MSG_NOTICE2, "bitbang_cmd_tpi(): [ ");
372 for(i = 0; i < cmd_len; i++)
373 avrdude_message(MSG_NOTICE2, "%02X ", cmd[i]);
374 avrdude_message(MSG_NOTICE2, "] [ ");
375 for(i = 0; i < res_len; i++)
376 {
377 avrdude_message(MSG_NOTICE2, "%02X ", res[i]);
378 }
379 avrdude_message(MSG_NOTICE2, "]\n");
380 }
381
382 pgm->pgm_led(pgm, OFF);
383 if (r == -1)
384 return -1;
385 return 0;
386 }
387
388 /*
389 * transmit bytes via SPI and return the results; 'cmd' and
390 * 'res' must point to data buffers
391 */
bitbang_spi(PROGRAMMER * pgm,const unsigned char * cmd,unsigned char * res,int count)392 int bitbang_spi(PROGRAMMER * pgm, const unsigned char *cmd,
393 unsigned char *res, int count)
394 {
395 int i;
396
397 pgm->setpin(pgm, PIN_LED_PGM, 0);
398
399 for (i=0; i<count; i++) {
400 res[i] = bitbang_txrx(pgm, cmd[i]);
401 }
402
403 pgm->setpin(pgm, PIN_LED_PGM, 1);
404
405 if(verbose >= 2)
406 {
407 avrdude_message(MSG_NOTICE2, "bitbang_cmd(): [ ");
408 for(i = 0; i < count; i++)
409 avrdude_message(MSG_NOTICE2, "%02X ", cmd[i]);
410 avrdude_message(MSG_NOTICE2, "] [ ");
411 for(i = 0; i < count; i++)
412 {
413 avrdude_message(MSG_NOTICE2, "%02X ", res[i]);
414 }
415 avrdude_message(MSG_NOTICE2, "]\n");
416 }
417
418 return 0;
419 }
420
421
422 /*
423 * issue the 'chip erase' command to the AVR device
424 */
bitbang_chip_erase(PROGRAMMER * pgm,AVRPART * p)425 int bitbang_chip_erase(PROGRAMMER * pgm, AVRPART * p)
426 {
427 unsigned char cmd[4];
428 unsigned char res[4];
429 AVRMEM *mem;
430
431 if (p->flags & AVRPART_HAS_TPI) {
432 pgm->pgm_led(pgm, ON);
433
434 while (avr_tpi_poll_nvmbsy(pgm));
435
436 /* NVMCMD <- CHIP_ERASE */
437 bitbang_tpi_tx(pgm, TPI_CMD_SOUT | TPI_SIO_ADDR(TPI_IOREG_NVMCMD));
438 bitbang_tpi_tx(pgm, TPI_NVMCMD_CHIP_ERASE); /* CHIP_ERASE */
439
440 /* Set Pointer Register */
441 mem = avr_locate_mem(p, "flash");
442 if (mem == NULL) {
443 avrdude_message(MSG_INFO, "No flash memory to erase for part %s\n",
444 p->desc);
445 return -1;
446 }
447 bitbang_tpi_tx(pgm, TPI_CMD_SSTPR | 0);
448 bitbang_tpi_tx(pgm, (mem->offset & 0xFF) | 1); /* high byte */
449 bitbang_tpi_tx(pgm, TPI_CMD_SSTPR | 1);
450 bitbang_tpi_tx(pgm, (mem->offset >> 8) & 0xFF);
451
452 /* write dummy value to start erase */
453 bitbang_tpi_tx(pgm, TPI_CMD_SST);
454 bitbang_tpi_tx(pgm, 0xFF);
455
456 while (avr_tpi_poll_nvmbsy(pgm));
457
458 pgm->pgm_led(pgm, OFF);
459
460 return 0;
461 }
462
463 if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
464 avrdude_message(MSG_INFO, "chip erase instruction not defined for part \"%s\"\n",
465 p->desc);
466 return -1;
467 }
468
469 pgm->pgm_led(pgm, ON);
470
471 memset(cmd, 0, sizeof(cmd));
472
473 avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd);
474 pgm->cmd(pgm, cmd, res);
475 usleep(p->chip_erase_delay);
476 pgm->initialize(pgm, p);
477
478 pgm->pgm_led(pgm, OFF);
479
480 return 0;
481 }
482
483 /*
484 * issue the 'program enable' command to the AVR device
485 */
bitbang_program_enable(PROGRAMMER * pgm,AVRPART * p)486 int bitbang_program_enable(PROGRAMMER * pgm, AVRPART * p)
487 {
488 unsigned char cmd[4];
489 unsigned char res[4];
490 int i;
491
492 if (p->flags & AVRPART_HAS_TPI) {
493 /* enable NVM programming */
494 bitbang_tpi_tx(pgm, TPI_CMD_SKEY);
495 for (i = sizeof(tpi_skey) - 1; i >= 0; i--)
496 bitbang_tpi_tx(pgm, tpi_skey[i]);
497
498 /* check NVMEN bit */
499 bitbang_tpi_tx(pgm, TPI_CMD_SLDCS | TPI_REG_TPISR);
500 i = bitbang_tpi_rx(pgm);
501 return (i != -1 && (i & TPI_REG_TPISR_NVMEN)) ? 0 : -2;
502 }
503
504 if (p->op[AVR_OP_PGM_ENABLE] == NULL) {
505 avrdude_message(MSG_INFO, "program enable instruction not defined for part \"%s\"\n",
506 p->desc);
507 return -1;
508 }
509
510 memset(cmd, 0, sizeof(cmd));
511 avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd);
512 pgm->cmd(pgm, cmd, res);
513
514 if (res[2] != cmd[1])
515 return -2;
516
517 return 0;
518 }
519
520 /*
521 * initialize the AVR device and prepare it to accept commands
522 */
bitbang_initialize(PROGRAMMER * pgm,AVRPART * p)523 int bitbang_initialize(PROGRAMMER * pgm, AVRPART * p)
524 {
525 int rc;
526 int tries;
527 int i;
528
529 bitbang_calibrate_delay();
530
531 pgm->powerup(pgm);
532 usleep(20000);
533
534 /* TPIDATA is a single line, so MISO & MOSI should be connected */
535 if (p->flags & AVRPART_HAS_TPI) {
536 /* make sure cmd_tpi() is defined */
537 if (pgm->cmd_tpi == NULL) {
538 avrdude_message(MSG_INFO, "%s: Error: %s programmer does not support TPI\n",
539 progname, pgm->type);
540 return -1;
541 }
542
543 /* bring RESET high first */
544 pgm->setpin(pgm, PIN_AVR_RESET, 1);
545 usleep(1000);
546
547 avrdude_message(MSG_NOTICE2, "doing MOSI-MISO link check\n");
548
549 pgm->setpin(pgm, PIN_AVR_MOSI, 0);
550 if (pgm->getpin(pgm, PIN_AVR_MISO) != 0) {
551 avrdude_message(MSG_INFO, "MOSI->MISO 0 failed\n");
552 return -1;
553 }
554 pgm->setpin(pgm, PIN_AVR_MOSI, 1);
555 if (pgm->getpin(pgm, PIN_AVR_MISO) != 1) {
556 avrdude_message(MSG_INFO, "MOSI->MISO 1 failed\n");
557 return -1;
558 }
559
560 avrdude_message(MSG_NOTICE2, "MOSI-MISO link present\n");
561 }
562
563 pgm->setpin(pgm, PIN_AVR_SCK, 0);
564 pgm->setpin(pgm, PIN_AVR_RESET, 0);
565 usleep(20000);
566
567 if (p->flags & AVRPART_HAS_TPI) {
568 /* keep TPIDATA high for 16 clock cycles */
569 pgm->setpin(pgm, PIN_AVR_MOSI, 1);
570 for (i = 0; i < 16; i++)
571 pgm->highpulsepin(pgm, PIN_AVR_SCK);
572
573 /* remove extra guard timing bits */
574 bitbang_tpi_tx(pgm, TPI_CMD_SSTCS | TPI_REG_TPIPCR);
575 bitbang_tpi_tx(pgm, 0x7);
576
577 /* read TPI ident reg */
578 bitbang_tpi_tx(pgm, TPI_CMD_SLDCS | TPI_REG_TPIIR);
579 rc = bitbang_tpi_rx(pgm);
580 if (rc != 0x80) {
581 avrdude_message(MSG_INFO, "TPIIR not correct\n");
582 return -1;
583 }
584 } else {
585 pgm->highpulsepin(pgm, PIN_AVR_RESET);
586 }
587
588 usleep(20000); /* 20 ms XXX should be a per-chip parameter */
589
590 /*
591 * Enable programming mode. If we are programming an AT90S1200, we
592 * can only issue the command and hope it worked. If we are using
593 * one of the other chips, the chip will echo 0x53 when issuing the
594 * third byte of the command. In this case, try up to 32 times in
595 * order to possibly get back into sync with the chip if we are out
596 * of sync.
597 */
598 if (p->flags & AVRPART_IS_AT90S1200) {
599 pgm->program_enable(pgm, p);
600 }
601 else {
602 tries = 0;
603 do {
604 rc = pgm->program_enable(pgm, p);
605 if ((rc == 0)||(rc == -1))
606 break;
607 pgm->highpulsepin(pgm, p->retry_pulse/*PIN_AVR_SCK*/);
608 tries++;
609 } while (tries < 65);
610
611 /*
612 * can't sync with the device, maybe it's not attached?
613 */
614 if (rc) {
615 avrdude_message(MSG_INFO, "%s: AVR device not responding\n", progname);
616 return -1;
617 }
618 }
619
620 return 0;
621 }
622
verify_pin_assigned(PROGRAMMER * pgm,int pin,char * desc)623 static int verify_pin_assigned(PROGRAMMER * pgm, int pin, char * desc)
624 {
625 if (pgm->pinno[pin] == 0) {
626 avrdude_message(MSG_INFO, "%s: error: no pin has been assigned for %s\n",
627 progname, desc);
628 return -1;
629 }
630 return 0;
631 }
632
633
634 /*
635 * Verify all prerequisites for a bit-bang programmer are present.
636 */
bitbang_check_prerequisites(PROGRAMMER * pgm)637 int bitbang_check_prerequisites(PROGRAMMER *pgm)
638 {
639
640 if (verify_pin_assigned(pgm, PIN_AVR_RESET, "AVR RESET") < 0)
641 return -1;
642 if (verify_pin_assigned(pgm, PIN_AVR_SCK, "AVR SCK") < 0)
643 return -1;
644 if (verify_pin_assigned(pgm, PIN_AVR_MISO, "AVR MISO") < 0)
645 return -1;
646 if (verify_pin_assigned(pgm, PIN_AVR_MOSI, "AVR MOSI") < 0)
647 return -1;
648
649 if (pgm->cmd == NULL) {
650 avrdude_message(MSG_INFO, "%s: error: no cmd() method defined for bitbang programmer\n",
651 progname);
652 return -1;
653 }
654 return 0;
655 }
656