2001-01-19 02:46:50 +00:00
|
|
|
/*
|
2003-02-08 04:17:25 +00:00
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
2004-12-22 01:52:45 +00:00
|
|
|
* Copyright (C) 2000-2004 Brian S. Dean <bsd@bsdhome.com>
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* 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.
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* 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.
|
2001-01-19 02:46:50 +00:00
|
|
|
*
|
2003-02-06 19:08:33 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2001-01-19 02:46:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2005-11-25 14:46:43 +00:00
|
|
|
|
2022-01-07 12:15:55 +00:00
|
|
|
#if !defined(WIN32)
|
2005-11-25 14:46:43 +00:00
|
|
|
|
2003-02-14 20:34:03 +00:00
|
|
|
#include "ac_cfg.h"
|
|
|
|
|
2005-11-03 22:37:37 +00:00
|
|
|
#if HAVE_PARPORT
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
#include <stdio.h>
|
2003-02-11 21:58:07 +00:00
|
|
|
#include <stdlib.h>
|
2002-11-30 14:09:12 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2001-01-19 02:46:50 +00:00
|
|
|
#include <unistd.h>
|
2002-11-30 14:09:12 +00:00
|
|
|
#include <errno.h>
|
2003-02-11 23:50:31 +00:00
|
|
|
|
2010-01-17 16:58:06 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2005-11-03 22:37:37 +00:00
|
|
|
# include "freebsd_ppi.h"
|
2003-02-11 23:50:31 +00:00
|
|
|
#elif defined(__linux__)
|
2005-11-03 22:37:37 +00:00
|
|
|
# include "linux_ppdev.h"
|
2009-02-23 22:04:57 +00:00
|
|
|
#elif defined(__sun__) || defined(__sun) /* Solaris */
|
2005-11-03 22:37:37 +00:00
|
|
|
# include "solaris_ecpp.h"
|
2003-02-11 23:50:31 +00:00
|
|
|
#endif
|
2001-01-19 02:46:50 +00:00
|
|
|
|
2007-01-24 21:07:54 +00:00
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "libavrdude.h"
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
#include "ppi.h"
|
|
|
|
|
2003-11-22 03:31:27 +00:00
|
|
|
enum {
|
|
|
|
PPI_READ,
|
|
|
|
PPI_WRITE,
|
|
|
|
PPI_SHADOWREAD
|
|
|
|
};
|
|
|
|
|
2007-01-24 22:43:46 +00:00
|
|
|
static int ppi_shadow_access(union filedescriptor *fdp, int reg,
|
|
|
|
unsigned char *v, unsigned char action)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
2003-11-22 03:31:27 +00:00
|
|
|
static unsigned char shadow[3];
|
|
|
|
int shadow_num;
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
switch (reg) {
|
|
|
|
case PPIDATA:
|
2003-11-22 03:31:27 +00:00
|
|
|
shadow_num = 0;
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
case PPICTRL:
|
2003-11-22 03:31:27 +00:00
|
|
|
shadow_num = 1;
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
case PPISTATUS:
|
2003-11-22 03:31:27 +00:00
|
|
|
shadow_num = 2;
|
2001-01-19 02:46:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: avr_set(): invalid register=%d\n",
|
2001-10-13 03:13:13 +00:00
|
|
|
progname, reg);
|
2001-01-19 02:46:50 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-22 03:31:27 +00:00
|
|
|
switch (action) {
|
|
|
|
case PPI_SHADOWREAD:
|
|
|
|
*v = shadow[shadow_num];
|
|
|
|
break;
|
|
|
|
case PPI_READ:
|
2006-12-11 12:47:35 +00:00
|
|
|
DO_PPI_READ(fdp->ifd, reg, v);
|
2003-11-22 03:31:27 +00:00
|
|
|
shadow[shadow_num]=*v;
|
|
|
|
break;
|
|
|
|
case PPI_WRITE:
|
|
|
|
shadow[shadow_num]=*v;
|
2006-12-11 12:47:35 +00:00
|
|
|
DO_PPI_WRITE(fdp->ifd, reg, v);
|
2003-11-22 03:31:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-01-19 02:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set the indicated bit of the specified register.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_set(union filedescriptor *fdp, int reg, int bit)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
int rc;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
rc = ppi_shadow_access(fdp, reg, &v, PPI_SHADOWREAD);
|
2003-11-22 03:31:27 +00:00
|
|
|
v |= bit;
|
2006-12-11 12:47:35 +00:00
|
|
|
rc |= ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
|
2003-11-22 03:31:27 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clear the indicated bit of the specified register.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_clr(union filedescriptor *fdp, int reg, int bit)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
int rc;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
rc = ppi_shadow_access(fdp, reg, &v, PPI_SHADOWREAD);
|
2003-11-22 03:31:27 +00:00
|
|
|
v &= ~bit;
|
2006-12-11 12:47:35 +00:00
|
|
|
rc |= ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
|
2003-11-22 03:31:27 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the indicated bit of the specified register.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_get(union filedescriptor *fdp, int reg, int bit)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
int rc;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
rc = ppi_shadow_access(fdp, reg, &v, PPI_READ);
|
2003-11-22 03:31:27 +00:00
|
|
|
v &= bit;
|
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
2001-01-24 18:45:58 +00:00
|
|
|
return v; /* v == bit */
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* toggle the indicated bit of the specified register.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_toggle(union filedescriptor *fdp, int reg, int bit)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
int rc;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
rc = ppi_shadow_access(fdp, reg, &v, PPI_SHADOWREAD);
|
2003-11-22 03:31:27 +00:00
|
|
|
v ^= bit;
|
2006-12-11 12:47:35 +00:00
|
|
|
rc |= ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
|
2003-11-22 03:31:27 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get all bits of the specified register.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_getall(union filedescriptor *fdp, int reg)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
int rc;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
rc = ppi_shadow_access(fdp, reg, &v, PPI_READ);
|
2003-11-22 03:31:27 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
2003-11-22 03:31:27 +00:00
|
|
|
return v; /* v == bit */
|
2001-01-19 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set all bits of the specified register to val.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_setall(union filedescriptor *fdp, int reg, int val)
|
2001-01-19 02:46:50 +00:00
|
|
|
{
|
|
|
|
unsigned char v;
|
|
|
|
int rc;
|
|
|
|
|
2003-11-22 03:31:27 +00:00
|
|
|
v = val;
|
2006-12-11 12:47:35 +00:00
|
|
|
rc = ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
|
2003-11-22 03:31:27 +00:00
|
|
|
|
2001-01-19 02:46:50 +00:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
void ppi_open(char * port, union filedescriptor *fdp)
|
2001-01-24 18:45:58 +00:00
|
|
|
{
|
2003-02-13 19:27:50 +00:00
|
|
|
int fd;
|
2003-11-22 03:31:27 +00:00
|
|
|
unsigned char v;
|
2001-01-24 18:45:58 +00:00
|
|
|
|
2003-02-13 19:27:50 +00:00
|
|
|
fd = open(port, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: can't open device \"%s\": %s\n",
|
2003-02-13 19:27:50 +00:00
|
|
|
progname, port, strerror(errno));
|
2006-12-11 12:47:35 +00:00
|
|
|
fdp->ifd = -1;
|
|
|
|
return;
|
2002-11-30 14:09:12 +00:00
|
|
|
}
|
|
|
|
|
2005-11-29 20:20:22 +00:00
|
|
|
ppi_claim (fd);
|
|
|
|
|
2003-11-22 03:31:27 +00:00
|
|
|
/*
|
|
|
|
* Initialize shadow registers
|
|
|
|
*/
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
ppi_shadow_access (fdp, PPIDATA, &v, PPI_READ);
|
|
|
|
ppi_shadow_access (fdp, PPICTRL, &v, PPI_READ);
|
|
|
|
ppi_shadow_access (fdp, PPISTATUS, &v, PPI_READ);
|
2003-11-22 03:31:27 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
fdp->ifd = fd;
|
2002-11-30 14:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
void ppi_close(union filedescriptor *fdp)
|
2002-11-30 14:09:12 +00:00
|
|
|
{
|
2006-12-11 12:47:35 +00:00
|
|
|
ppi_release (fdp->ifd);
|
|
|
|
close(fdp->ifd);
|
2002-11-30 14:09:12 +00:00
|
|
|
}
|
|
|
|
|
2005-11-03 22:37:37 +00:00
|
|
|
#endif /* HAVE_PARPORT */
|
2004-05-19 21:02:25 +00:00
|
|
|
|
2022-01-07 12:15:55 +00:00
|
|
|
#endif /* !WIN32 */
|