/************************************************************************/
/* */
/* */
/* ***** ***** */
/* ***** ***** */
/* ***** ***** */
/* ***** ***** */
/* *************** *************** */
/* ***************** ***************** */
/* *************** *************** */
/* ***** ***** TheNetNode - Tools */
/* ***** ***** (PC) */
/* ***** ***** Public Domain */
/* ***** ***** NORD>
#define inportb(adr) inb(adr)
#define outportb(adr, data) outb(data, adr)
#define port_adresse 0x378 /* LPT 1 */
#else /* GO32 */
#include
#define port_adresse _farpeekw(_dos_ds,0x00408)
#endif
char version[] = __NAME__ ", Version " __VER__ __AUTHOR__
" ("__DATE__ ", " __TIME__ ")\r";
/*----------------------------------------------------------------------*/
/* Fehler im Aufruf melden */
/*----------------------------------------------------------------------*/
void usage_error(void)
{
puts(version);
printf("Syntax: OUTPUT [ <0|1>]\n");
printf(" (using io-port 0x%x)\n", port_adresse);
exit(1);
}
/*----------------------------------------------------------------------*/
/* Port Bit setzen */
/*----------------------------------------------------------------------*/
void set_port(int io_port)
{
if (io_port < 8)
{
outportb(port_adresse, (inportb(port_adresse) | (0x01 << io_port)));
/* setzt STROBE-Bit auf LOW-Position (invertiert) */
outportb(port_adresse + 2, (inportb(port_adresse + 2) | (0x01 << 0)));
/* 400ms warten */
usleep(400000);
/* setzt STROBE-Bit auf HIGH-Position (invertiert) */
outportb(port_adresse + 2, (inportb(port_adresse + 2) & ~(0x01 << 0)));
}
else
usage_error();
}
/*----------------------------------------------------------------------*/
/* Port Bit loeschen */
/*----------------------------------------------------------------------*/
void reset_port(int io_port)
{
if (io_port < 8)
{
outportb(port_adresse, (inportb(port_adresse) & ~(0x01 << io_port)));
/* setzt STROBE-Bit auf LOW-Position (invertiert) */
outportb(port_adresse + 2, (inportb(port_adresse + 2) | (0x01 << 0)));
/* 400ms warten */
usleep(400000);
/* setzt STROBE-Bit auf HIGH-Position (invertiert) */
outportb(port_adresse + 2, (inportb(port_adresse + 2) & ~(0x01 << 0)));
}
else
usage_error();
}
/*----------------------------------------------------------------------*/
/* Port Bit lesen */
/*----------------------------------------------------------------------*/
int read_port(int io_port)
{
if (io_port < 8)
return((inportb(port_adresse) >> io_port) & 1);
else
{
usage_error();
return(0);
}
}
/*----------------------------------------------------------------------*/
/* Port Status anzeigen */
/*----------------------------------------------------------------------*/
void show_port(void)
{
int ioport;
printf("\nOutput-Status:\n 7 6 5 4 3 2 1 0\n");
for (ioport = 7; ioport >= 0; ioport--)
printf(" %1.1d", read_port(ioport));
printf("\n");
}
/*----------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
int bit = -1;
#ifdef __LINUX__
/* direkten Portzugriff beim Kernel anfordern */
if (ioperm(port_adresse, 3, 1))
{
perror("Can't get permissions for port-access. (Are you \'root\' ?)");
exit(1);
}
#endif
if (argc == 2) /* Nur Status ausgeben */
{
show_port();
return(0);
}
if (argc == 4) /* Port setzen/loeschen, 4 da TNN Rufzeichen anfuegt */
{
switch (*argv[2])
{
case '0': bit = atoi(argv[1]);
if (bit < 0 || bit > 7 )
{
usage_error();
exit(1);
}
reset_port(bit);
show_port();
break;
case '1': bit = atoi(argv[1]);
if (bit < 0 || bit > 7 )
{
usage_error();
exit(1);
}
set_port(bit);
show_port();
break;
default: usage_error();
exit(1);
}
return(0);
}
usage_error();
#ifdef __LINUX__
/* Port wieder freigeben */
if (ioperm(port_adresse, 3, 0))
{
perror("Can't release io-permissions !");
exit(1);
}
#endif
return(1);
}
/*----------------------------------------------------------------------*/
/*--- Ende von output.c ---*/