2003-02-20 18:15:50 +00:00
|
|
|
/*
|
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
2006-08-20 06:49:11 +00:00
|
|
|
* Copyright (C) 2003, 2004, 2006
|
|
|
|
* Eric B. Weddington <eweddington@cso.atmel.com>
|
2008-11-05 20:53:51 +00:00
|
|
|
* Copyright 2008, Joerg Wunsch
|
2003-02-20 18:15:50 +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.
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-02-20 18:15:50 +00:00
|
|
|
*/
|
|
|
|
|
2004-07-15 17:29:35 +00:00
|
|
|
/* $Id$ */
|
2003-02-20 18:15:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This is the parallel port interface for Windows built using Cygwin.
|
|
|
|
|
|
|
|
In the ppi_* functions that access the parallel port registers,
|
|
|
|
fd = parallel port address
|
|
|
|
reg = register as defined in an enum in ppi.h. This must be converted
|
|
|
|
to a proper offset of the base address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-08-20 06:49:11 +00:00
|
|
|
#include "ac_cfg.h"
|
2003-02-20 18:15:50 +00:00
|
|
|
|
2020-01-18 17:05:50 +00:00
|
|
|
#if defined (WIN32NATIVE) && defined (HAVE_PARPORT)
|
2003-02-20 18:15:50 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <windows.h>
|
2003-08-20 00:16:27 +00:00
|
|
|
#include <sys/time.h>
|
2004-06-24 11:05:07 +00:00
|
|
|
#include <windows.h>
|
2007-01-24 21:07:54 +00:00
|
|
|
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "avrdude.h"
|
|
|
|
#include "libavrdude.h"
|
|
|
|
|
2003-02-20 18:15:50 +00:00
|
|
|
#include "ppi.h"
|
|
|
|
|
2003-02-24 21:02:01 +00:00
|
|
|
#define DEVICE_LPT1 "lpt1"
|
|
|
|
#define DEVICE_LPT2 "lpt2"
|
|
|
|
#define DEVICE_LPT3 "lpt3"
|
2003-02-20 18:15:50 +00:00
|
|
|
|
|
|
|
#define DEVICE_MAX 3
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
const char *name;
|
|
|
|
int base_address;
|
2003-02-20 18:15:50 +00:00
|
|
|
} winpp;
|
|
|
|
|
|
|
|
static const winpp winports[DEVICE_MAX] =
|
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
{DEVICE_LPT1, 0x378},
|
|
|
|
{DEVICE_LPT2, 0x278},
|
|
|
|
{DEVICE_LPT3, 0x3BC},
|
2003-02-20 18:15:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* FUNCTION PROTOTYPES */
|
|
|
|
static int winnt_pp_open(void);
|
2006-12-11 12:47:35 +00:00
|
|
|
static unsigned short port_get(union filedescriptor *fdp, int reg);
|
2003-02-20 18:15:50 +00:00
|
|
|
static unsigned char reg2offset(int reg);
|
|
|
|
static unsigned char inb(unsigned short port);
|
|
|
|
static void outb(unsigned char value, unsigned short port);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* FUNCTION DEFINITIONS */
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
void ppi_open(char *port, union filedescriptor *fdp)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
|
|
|
unsigned char i;
|
2003-02-24 21:02:01 +00:00
|
|
|
int fd;
|
2003-02-20 18:15:50 +00:00
|
|
|
|
2003-02-24 21:02:01 +00:00
|
|
|
fd = winnt_pp_open();
|
|
|
|
|
|
|
|
if(fd < 0)
|
|
|
|
{
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: can't open device \"giveio\"\n\n", progname);
|
2006-12-11 12:47:35 +00:00
|
|
|
fdp->ifd = -1;
|
|
|
|
return;
|
2003-02-24 21:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search the windows port names for a match */
|
|
|
|
fd = -1;
|
|
|
|
for(i = 0; i < DEVICE_MAX; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(winports[i].name, port) == 0)
|
|
|
|
{
|
|
|
|
/* Set the file descriptor with the Windows parallel port base address. */
|
|
|
|
fd = winports[i].base_address;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-05 20:53:51 +00:00
|
|
|
if(fd == -1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Supplied port name did not match any of the pre-defined
|
|
|
|
* names. Try interpreting it as a numeric
|
|
|
|
* (hexadecimal/decimal/octal) address.
|
|
|
|
*/
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
fd = strtol(port, &cp, 0);
|
|
|
|
if(*port == '\0' || *cp != '\0')
|
|
|
|
{
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: port name \"%s\" is neither lpt1/2/3 nor valid number\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname, port);
|
2008-11-05 20:53:51 +00:00
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
2003-02-24 21:02:01 +00:00
|
|
|
if(fd < 0)
|
|
|
|
{
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: can't open device \"%s\"\n\n", progname, port);
|
2006-12-11 12:47:35 +00:00
|
|
|
fdp->ifd = -1;
|
|
|
|
return;
|
2003-02-24 21:02:01 +00:00
|
|
|
}
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
fdp->ifd = fd;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define DRIVERNAME "\\\\.\\giveio"
|
|
|
|
static int winnt_pp_open(void)
|
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
// Only try to use giveio under Windows NT/2000/XP.
|
|
|
|
OSVERSIONINFO ver_info;
|
|
|
|
|
|
|
|
memset(&ver_info, 0, sizeof(ver_info));
|
|
|
|
|
|
|
|
ver_info.dwOSVersionInfoSize = sizeof(ver_info);
|
|
|
|
|
|
|
|
if(!GetVersionEx(&ver_info))
|
|
|
|
{
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
else if(ver_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
|
|
|
{
|
|
|
|
HANDLE h = CreateFile(DRIVERNAME,
|
|
|
|
GENERIC_READ,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if(h == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close immediately. The process now has the rights it needs. */
|
|
|
|
if(h != NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(h);
|
|
|
|
}
|
|
|
|
}
|
2003-02-20 18:15:50 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
void ppi_close(union filedescriptor *fdp)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
return;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
unsigned char v;
|
|
|
|
unsigned short port;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
port = port_get(fdp, reg);
|
2003-02-24 21:02:01 +00:00
|
|
|
v = inb(port);
|
|
|
|
v |= bit;
|
|
|
|
outb(v, port);
|
|
|
|
return 0;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
unsigned char v;
|
|
|
|
unsigned short port;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
port = port_get(fdp, reg);
|
2003-02-24 21:02:01 +00:00
|
|
|
v = inb(port);
|
|
|
|
v &= ~bit;
|
|
|
|
outb(v, port);
|
|
|
|
|
|
|
|
return 0;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
unsigned char v;
|
2003-02-20 18:15:50 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
v = inb(port_get(fdp, reg));
|
2003-02-24 21:02:01 +00:00
|
|
|
v &= bit;
|
|
|
|
|
|
|
|
return(v);
|
2003-02-20 18:15: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)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
unsigned char v;
|
|
|
|
unsigned short port;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
port = port_get(fdp, reg);
|
2003-02-20 18:15:50 +00:00
|
|
|
|
2003-02-24 21:02:01 +00:00
|
|
|
v = inb(port);
|
|
|
|
v ^= bit;
|
|
|
|
outb(v, port);
|
2003-02-20 18:15:50 +00:00
|
|
|
|
2003-02-24 21:02:01 +00:00
|
|
|
return 0;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get all bits of the specified register.
|
|
|
|
*/
|
2006-12-11 12:47:35 +00:00
|
|
|
int ppi_getall(union filedescriptor *fdp, int reg)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
unsigned char v;
|
2003-02-20 18:15:50 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
v = inb(port_get(fdp, reg));
|
2003-02-24 21:02:01 +00:00
|
|
|
|
|
|
|
return((int)v);
|
2003-02-20 18:15: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)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2006-12-11 12:47:35 +00:00
|
|
|
outb((unsigned char)val, port_get(fdp, reg));
|
2003-02-24 21:02:01 +00:00
|
|
|
return 0;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Calculate port address to access. */
|
2006-12-11 12:47:35 +00:00
|
|
|
static unsigned short port_get(union filedescriptor *fdp, int reg)
|
2003-02-20 18:15:50 +00:00
|
|
|
{
|
2006-12-11 12:47:35 +00:00
|
|
|
return((unsigned short)(fdp->ifd + reg2offset(reg)));
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Convert register enum to offset of base address. */
|
|
|
|
static unsigned char reg2offset(int reg)
|
|
|
|
{
|
2003-02-24 21:02:01 +00:00
|
|
|
unsigned char offset = 0;
|
|
|
|
|
|
|
|
switch(reg)
|
|
|
|
{
|
|
|
|
case PPIDATA:
|
|
|
|
{
|
|
|
|
offset = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPISTATUS:
|
|
|
|
{
|
|
|
|
offset = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPICTRL:
|
|
|
|
{
|
|
|
|
offset = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(offset);
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read in value from port. */
|
|
|
|
static unsigned char inb(unsigned short port)
|
|
|
|
{
|
|
|
|
unsigned char t;
|
2003-02-24 21:02:01 +00:00
|
|
|
|
|
|
|
asm volatile ("in %1, %0"
|
|
|
|
: "=a" (t)
|
|
|
|
: "d" (port));
|
|
|
|
|
|
|
|
return t;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Write value to port. */
|
|
|
|
static void outb(unsigned char value, unsigned short port)
|
|
|
|
{
|
|
|
|
asm volatile ("out %1, %0"
|
2003-02-24 21:02:01 +00:00
|
|
|
:
|
|
|
|
: "d" (port), "a" (value) );
|
|
|
|
|
|
|
|
return;
|
2003-02-20 18:15:50 +00:00
|
|
|
}
|
|
|
|
|
2006-08-20 06:49:11 +00:00
|
|
|
#if !defined(HAVE_GETTIMEOFDAY)
|
2006-12-11 12:47:35 +00:00
|
|
|
struct timezone;
|
2006-08-20 06:49:11 +00:00
|
|
|
int gettimeofday(struct timeval *tv, struct timezone *unused){
|
2004-06-24 11:05:07 +00:00
|
|
|
// i've found only ms resolution, avrdude expects us
|
2003-08-20 00:16:27 +00:00
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
SYSTEMTIME st;
|
|
|
|
GetSystemTime(&st);
|
|
|
|
|
|
|
|
tv->tv_sec=(long)(st.wSecond+st.wMinute*60+st.wHour*3600);
|
|
|
|
tv->tv_usec=(long)(st.wMilliseconds*1000);
|
2006-08-20 06:49:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2003-08-20 00:16:27 +00:00
|
|
|
}
|
2006-08-20 06:49:11 +00:00
|
|
|
#endif /* HAVE_GETTIMEOFDAY */
|
2003-08-20 00:16:27 +00:00
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
// #define W32USLEEPDBG
|
2003-08-20 00:16:27 +00:00
|
|
|
|
2004-07-15 17:29:35 +00:00
|
|
|
#ifdef W32USLEEPDBG
|
|
|
|
|
|
|
|
# define DEBUG_QueryPerformanceCounter(arg) QueryPerformanceCounter(arg)
|
|
|
|
# define DEBUG_DisplayTimingInfo(start, stop, freq, us, has_highperf) \
|
|
|
|
do { \
|
|
|
|
unsigned long dt; \
|
|
|
|
dt = (unsigned long)((stop.QuadPart - start.QuadPart) * 1000 * 1000 \
|
|
|
|
/ freq.QuadPart); \
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, \
|
2004-07-15 17:29:35 +00:00
|
|
|
"hpt:%i usleep usec:%lu sleep msec:%lu timed usec:%lu\n", \
|
|
|
|
has_highperf, us, ((us + 999) / 1000), dt); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# define DEBUG_QueryPerformanceCounter(arg)
|
|
|
|
# define DEBUG_DisplayTimingInfo(start, stop, freq, us, has_highperf)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-01-22 16:40:17 +00:00
|
|
|
#if !defined(HAVE_USLEEP)
|
2008-11-19 23:32:30 +00:00
|
|
|
int usleep(unsigned int us)
|
2003-08-20 00:16:27 +00:00
|
|
|
{
|
2004-06-24 11:05:07 +00:00
|
|
|
int has_highperf;
|
|
|
|
LARGE_INTEGER freq,start,stop,loopend;
|
|
|
|
|
|
|
|
// workaround: although usleep is very precise if using
|
|
|
|
// high-performance-timers there are sometimes problems with
|
|
|
|
// verify - increasing the delay helps sometimes but not
|
|
|
|
// realiably. There must be some other problem. Maybe just
|
|
|
|
// with my test-hardware maybe in the code-base.
|
2010-01-22 16:40:17 +00:00
|
|
|
//// us=(unsigned long) (us*1.5);
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
has_highperf=QueryPerformanceFrequency(&freq);
|
|
|
|
|
|
|
|
//has_highperf=0; // debug
|
|
|
|
|
|
|
|
if (has_highperf) {
|
|
|
|
QueryPerformanceCounter(&start);
|
|
|
|
loopend.QuadPart=start.QuadPart+freq.QuadPart*us/(1000*1000);
|
2010-01-22 16:40:17 +00:00
|
|
|
do {
|
2004-06-24 11:05:07 +00:00
|
|
|
QueryPerformanceCounter(&stop);
|
|
|
|
} while (stop.QuadPart<=loopend.QuadPart);
|
|
|
|
}
|
|
|
|
else {
|
2004-07-15 17:29:35 +00:00
|
|
|
DEBUG_QueryPerformanceCounter(&start);
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
Sleep(1);
|
|
|
|
Sleep( (DWORD)((us+999)/1000) );
|
2004-07-15 17:29:35 +00:00
|
|
|
|
|
|
|
DEBUG_QueryPerformanceCounter(&stop);
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2010-01-22 16:40:17 +00:00
|
|
|
|
2004-07-15 17:29:35 +00:00
|
|
|
DEBUG_DisplayTimingInfo(start, stop, freq, us, has_highperf);
|
2008-11-19 23:32:30 +00:00
|
|
|
|
|
|
|
return 0;
|
2003-08-20 00:16:27 +00:00
|
|
|
}
|
2010-01-22 16:40:17 +00:00
|
|
|
#endif /* !HAVE_USLEEP */
|
2003-02-20 18:15:50 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|