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
|
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2000-2006 Brian S. Dean <bsd@bsdhome.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: par.c,v 1.19 2006/08/29 23:12:15 joerg_wunsch Exp $ */
#include "ac_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#if defined(__FreeBSD__)
# include "freebsd_ppi.h"
#elif defined(__linux__)
# include "linux_ppdev.h"
#elif defined(__sun__) && defined(__svr4__) /* Solaris */
# include "solaris_ecpp.h"
#endif
#include "avr.h"
#include "pindefs.h"
#include "pgm.h"
#include "ppi.h"
#include "bitbang.h"
extern char * progname;
extern int do_cycles;
extern int verbose;
#if HAVE_PARPORT
struct ppipins_t {
int pin;
int reg;
int bit;
int inverted;
};
static struct ppipins_t ppipins[] = {
{ 1, PPICTRL, 0x01, 1 },
{ 2, PPIDATA, 0x01, 0 },
{ 3, PPIDATA, 0x02, 0 },
{ 4, PPIDATA, 0x04, 0 },
{ 5, PPIDATA, 0x08, 0 },
{ 6, PPIDATA, 0x10, 0 },
{ 7, PPIDATA, 0x20, 0 },
{ 8, PPIDATA, 0x40, 0 },
{ 9, PPIDATA, 0x80, 0 },
{ 10, PPISTATUS, 0x40, 0 },
{ 11, PPISTATUS, 0x80, 1 },
{ 12, PPISTATUS, 0x20, 0 },
{ 13, PPISTATUS, 0x10, 0 },
{ 14, PPICTRL, 0x02, 1 },
{ 15, PPISTATUS, 0x08, 0 },
{ 16, PPICTRL, 0x04, 0 },
{ 17, PPICTRL, 0x08, 1 }
};
#define NPINS (sizeof(ppipins)/sizeof(struct ppipins_t))
static int par_setpin(PROGRAMMER * pgm, int pin, int value)
{
int inverted;
inverted = pin & PIN_INVERSE;
pin &= PIN_MASK;
if (pin < 1 || pin > 17)
return -1;
pin--;
if (ppipins[pin].inverted)
inverted = !inverted;
if (inverted)
value = !value;
if (value)
ppi_set(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
else
ppi_clr(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
if (pgm->ispdelay > 1)
bitbang_delay(pgm->ispdelay);
return 0;
}
static void par_setmany(PROGRAMMER * pgm, unsigned int pinset, int value)
{
int pin;
for (pin = 1; pin <= 17; pin++) {
if (pinset & (1 << pin))
par_setpin(pgm, pin, value);
}
}
static int par_getpin(PROGRAMMER * pgm, int pin)
{
int value;
int inverted;
inverted = pin & PIN_INVERSE;
pin &= PIN_MASK;
if (pin < 1 || pin > 17)
return -1;
pin--;
value = ppi_get(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
if (value)
value = 1;
if (ppipins[pin].inverted)
inverted = !inverted;
if (inverted)
value = !value;
return value;
}
static int par_highpulsepin(PROGRAMMER * pgm, int pin)
{
int inverted;
inverted = pin & PIN_INVERSE;
pin &= PIN_MASK;
if (pin < 1 || pin > 17)
return -1;
pin--;
if (ppipins[pin].inverted)
inverted = !inverted;
if (inverted) {
ppi_clr(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
if (pgm->ispdelay > 1)
bitbang_delay(pgm->ispdelay);
ppi_set(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
if (pgm->ispdelay > 1)
bitbang_delay(pgm->ispdelay);
} else {
ppi_set(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
if (pgm->ispdelay > 1)
bitbang_delay(pgm->ispdelay);
ppi_clr(pgm->fd, ppipins[pin].reg, ppipins[pin].bit);
if (pgm->ispdelay > 1)
bitbang_delay(pgm->ispdelay);
}
return 0;
}
static char * pins_to_str(unsigned int pmask)
{
static char buf[64];
int pin;
char b2[8];
buf[0] = 0;
for (pin = 1; pin <= 17; pin++) {
if (pmask & (1 << pin)) {
sprintf(b2, "%d", pin);
if (buf[0] != 0)
strcat(buf, ",");
strcat(buf, b2);
}
}
return buf;
}
/*
* apply power to the AVR processor
*/
static void par_powerup(PROGRAMMER * pgm)
{
par_setmany(pgm, pgm->pinno[PPI_AVR_VCC], 1); /* power up */
usleep(100000);
}
/*
* remove power from the AVR processor
*/
static void par_powerdown(PROGRAMMER * pgm)
{
par_setmany(pgm, pgm->pinno[PPI_AVR_VCC], 0); /* power down */
}
static void par_disable(PROGRAMMER * pgm)
{
par_setmany(pgm, pgm->pinno[PPI_AVR_BUFF], 1); /* turn off */
}
static void par_enable(PROGRAMMER * pgm)
{
/*
* Prepare to start talking to the connected device - pull reset low
* first, delay a few milliseconds, then enable the buffer. This
* sequence allows the AVR to be reset before the buffer is enabled
* to avoid a short period of time where the AVR may be driving the
* programming lines at the same time the programmer tries to. Of
* course, if a buffer is being used, then the /RESET line from the
* programmer needs to be directly connected to the AVR /RESET line
* and not via the buffer chip.
*/
par_setpin(pgm, pgm->pinno[PIN_AVR_RESET], 0);
usleep(1);
/*
* enable the 74367 buffer, if connected; this signal is active low
*/
par_setmany(pgm, pgm->pinno[PPI_AVR_BUFF], 0);
}
static int par_open(PROGRAMMER * pgm, char * port)
{
int rc;
bitbang_check_prerequisites(pgm);
pgm->fd = ppi_open(port);
if (pgm->fd < 0) {
fprintf(stderr, "%s: failed to open parallel port \"%s\"\n\n",
progname, port);
exit(1);
}
/*
* save pin values, so they can be restored when device is closed
*/
rc = ppi_getall(pgm->fd, PPIDATA);
if (rc < 0) {
fprintf(stderr, "%s: error reading status of ppi data port\n", progname);
return -1;
}
pgm->ppidata = rc;
rc = ppi_getall(pgm->fd, PPICTRL);
if (rc < 0) {
fprintf(stderr, "%s: error reading status of ppi ctrl port\n", progname);
return -1;
}
pgm->ppictrl = rc;
return 0;
}
static void par_close(PROGRAMMER * pgm)
{
/*
* Restore pin values before closing,
* but ensure that buffers are turned off.
*/
ppi_setall(pgm->fd, PPIDATA, pgm->ppidata);
ppi_setall(pgm->fd, PPICTRL, pgm->ppictrl);
par_setpin(pgm, pgm->pinno[PPI_AVR_BUFF], 1);
/*
* Handle exit specs.
*/
switch (pgm->exit_reset) {
case EXIT_RESET_ENABLED:
par_setpin(pgm, pgm->pinno[PIN_AVR_RESET], 0);
break;
case EXIT_RESET_DISABLED:
par_setpin(pgm, pgm->pinno[PIN_AVR_RESET], 1);
break;
case EXIT_RESET_UNSPEC:
/* Leave it alone. */
break;
}
switch (pgm->exit_vcc) {
case EXIT_VCC_ENABLED:
par_setmany(pgm, pgm->pinno[PPI_AVR_VCC], 1);
break;
case EXIT_VCC_DISABLED:
par_setmany(pgm, pgm->pinno[PPI_AVR_VCC], 0);
break;
case EXIT_VCC_UNSPEC:
/* Leave it alone. */
break;
}
ppi_close(pgm->fd);
pgm->fd = -1;
}
static void par_display(PROGRAMMER * pgm, char * p)
{
char vccpins[64];
char buffpins[64];
if (pgm->pinno[PPI_AVR_VCC]) {
snprintf(vccpins, sizeof(vccpins), "%s",
pins_to_str(pgm->pinno[PPI_AVR_VCC]));
}
else {
strcpy(vccpins, " (not used)");
}
if (pgm->pinno[PPI_AVR_BUFF]) {
snprintf(buffpins, sizeof(buffpins), "%s",
pins_to_str(pgm->pinno[PPI_AVR_BUFF]));
}
else {
strcpy(buffpins, " (not used)");
}
fprintf(stderr,
"%s VCC = %s\n"
"%s BUFF = %s\n"
"%s RESET = %d\n"
"%s SCK = %d\n"
"%s MOSI = %d\n"
"%s MISO = %d\n"
"%s ERR LED = %d\n"
"%s RDY LED = %d\n"
"%s PGM LED = %d\n"
"%s VFY LED = %d\n",
p, vccpins,
p, buffpins,
p, pgm->pinno[PIN_AVR_RESET],
p, pgm->pinno[PIN_AVR_SCK],
p, pgm->pinno[PIN_AVR_MOSI],
p, pgm->pinno[PIN_AVR_MISO],
p, pgm->pinno[PIN_LED_ERR],
p, pgm->pinno[PIN_LED_RDY],
p, pgm->pinno[PIN_LED_PGM],
p, pgm->pinno[PIN_LED_VFY]);
}
/*
* parse the -E string
*/
static int par_parseexitspecs(PROGRAMMER * pgm, char *s)
{
char *cp;
while ((cp = strtok(s, ","))) {
if (strcmp(cp, "reset") == 0) {
pgm->exit_reset = EXIT_RESET_ENABLED;
}
else if (strcmp(cp, "noreset") == 0) {
pgm->exit_reset = EXIT_RESET_DISABLED;
}
else if (strcmp(cp, "vcc") == 0) {
pgm->exit_vcc = EXIT_VCC_ENABLED;
}
else if (strcmp(cp, "novcc") == 0) {
pgm->exit_vcc = EXIT_VCC_DISABLED;
}
else {
return -1;
}
s = 0; /* strtok() should be called with the actual string only once */
}
return 0;
}
void par_initpgm(PROGRAMMER * pgm)
{
strcpy(pgm->type, "PPI");
pgm->exit_vcc = EXIT_VCC_UNSPEC;
pgm->exit_reset = EXIT_RESET_UNSPEC;
pgm->rdy_led = bitbang_rdy_led;
pgm->err_led = bitbang_err_led;
pgm->pgm_led = bitbang_pgm_led;
pgm->vfy_led = bitbang_vfy_led;
pgm->initialize = bitbang_initialize;
pgm->display = par_display;
pgm->enable = par_enable;
pgm->disable = par_disable;
pgm->powerup = par_powerup;
pgm->powerdown = par_powerdown;
pgm->program_enable = bitbang_program_enable;
pgm->chip_erase = bitbang_chip_erase;
pgm->cmd = bitbang_cmd;
pgm->open = par_open;
pgm->close = par_close;
pgm->setpin = par_setpin;
pgm->getpin = par_getpin;
pgm->highpulsepin = par_highpulsepin;
pgm->parseexitspecs = par_parseexitspecs;
}
#else /* !HAVE_PARPORT */
void par_initpgm(PROGRAMMER * pgm)
{
fprintf(stderr,
"%s: parallel port access not available in this configuration\n",
progname);
}
#endif /* HAVE_PARPORT */
|