⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jb_io.c

📁 software for report builder
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************/
/*                                                                */
/* Module:       jb_io.c                                          */
/*                                                                */
/* Descriptions: Manages I/O related routines, file and string    */
/*               processing functions.                            */
/*                                                                */
/* Revisions:    1.0 02/22/02                                     */
/*                                                                */
/******************************************************************/

#include <stdio.h>
#include "jb_io.h"

#if PORT == WINDOWS_NT
#include <windows.h>

#elif PORT == LINUX
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#ifndef LINUX_PARPORT
 #include <sys/io.h>
#else
 #include <linux/ppdev.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <unistd.h>
#endif

#else if PORT == EMBEDDED
/* include your library here */
#endif

/* global definitions */

#if PORT==WINDOWS_NT
#define	PGDC_IOCTL_GET_DEVICE_INFO_PP   0x00166A00L
#define PGDC_IOCTL_READ_PORT_PP         0x00166A04L
#define PGDC_IOCTL_WRITE_PORT_PP        0x0016AA08L
#define PGDC_IOCTL_PROCESS_LIST_PP      0x0016AA1CL
#define PGDC_WRITE_PORT                 0x0a82
#define PGDC_HDLC_NTDRIVER_VERSION      2
#define PORT_IO_BUFFER_SIZE             256
#endif /* PORT==WINDOWS_NT */

#if PORT == LINUX
#define PORT_IO_BUFFER_SIZE		256
#endif /*PORT == LINUX*/

#define MAX_FILE_LINE_LENGTH            160


/* global Variables */

#if PORT==WINDOWS_NT
HANDLE  nt_device_handle     = INVALID_HANDLE_VALUE;
int     port_io_buffer_count = 0;

struct PORT_IO_LIST_STRUCT
{
	USHORT command;
	USHORT data;
} port_io_buffer[PORT_IO_BUFFER_SIZE];

#elif PORT == LINUX

#ifndef LINUX_PARPORT
unsigned short	 lpt_addr;
#else
static int	 ppfp			= 0;
char 	devicepath[256];
#endif
int		 port_io_buffer_count	= 0;
int		 port_io_buffer[PORT_IO_BUFFER_SIZE];
int		 totalrd		= 0;
int		 totalwr		= 0;

#else if PORT == EMBEDDED
/* define your global variables here */
#endif /*PORT*/



#if PORT==WINDOWS_NT
/******************************************************************/
/* Name:         InitNtDriver                                     */
/*                                                                */
/* Parameters:   None.                                            */
/*                                                                */
/* Return Value: None.                                            */
/*               		                                          */
/* Descriptions: Initialize Windows NT Driver for ByteBlasterMV.  */
/*                                                                */
/******************************************************************/
void InitNtDriver()
{
	int init_ok = 0;	/* Initialization OK */

	ULONG buffer[1];
	ULONG returned_length = 0;
	char nt_lpt_str[] = { '\\', '\\', '.', '\\', 'A', 'L', 'T', 'L', 'P', 'T', '1', '\0' }; 

	nt_device_handle = CreateFile( 
			nt_lpt_str,
			GENERIC_READ | GENERIC_WRITE,
			0,
			NULL,
			OPEN_EXISTING,
			FILE_ATTRIBUTE_NORMAL,
			NULL );

	if ( nt_device_handle == INVALID_HANDLE_VALUE )
		fprintf( stderr, "I/O Error: Cannot open device \"%s\"\n", nt_lpt_str );
	else
	{
		if ( DeviceIoControl(
				nt_device_handle,
				PGDC_IOCTL_GET_DEVICE_INFO_PP,
				(ULONG *) NULL,
				0,
				&buffer,
				sizeof(ULONG),
				&returned_length,
				NULL ))
		{
			if ( returned_length == sizeof( ULONG ) )
			{
				if (buffer[0] == PGDC_HDLC_NTDRIVER_VERSION)
				{
					init_ok = 1;
				}
				else
				{
					fprintf(stderr,
						"I/O Error:  device driver %s is not compatible\n(Driver version is %lu, expected version %lu.\n",
						nt_lpt_str,
						(unsigned long) buffer[0],
						(unsigned long) PGDC_HDLC_NTDRIVER_VERSION );
				}
			}	
			else
				fprintf(stderr, "I/O Error:  device driver %s is not compatible.\n", nt_lpt_str);		
		}

		if ( !init_ok )
		{
			fprintf( stderr, "I/O Error: DeviceIoControl not successful" );
			CloseHandle( nt_device_handle );
			nt_device_handle = INVALID_HANDLE_VALUE;
		}
	}

	if ( !init_ok )
	{
		fprintf( stderr, "Error: Driver initialization failed... Exiting...\n" );
		CloseNtDriver();
		exit(1);
	}
}

/******************************************************************/
/* Name:         CloseNtDriver                                    */
/*                                                                */
/* Parameters:   None.                                            */
/*                                                                */
/* Return Value: None.                                            */
/*               		                                          */
/* Descriptions: Close Windows NT Driver.                         */
/*                                                                */
/******************************************************************/
void CloseNtDriver()
{
	CloseHandle( nt_device_handle );
	nt_device_handle = INVALID_HANDLE_VALUE;
}

/******************************************************************/
/* Name:         flush_ports                                      */
/*                                                                */
/* Parameters:   None.                                            */
/*                                                                */
/* Return Value: None.                                            */
/*               		                                          */
/* Descriptions: Flush processes in port_io_buffer and reset      */
/*               buffer size to 0.                                */
/*                                                                */
/******************************************************************/
void flush_ports(void)
{
	ULONG n_writes = 0L;
	BOOL status;

	status = DeviceIoControl(
		nt_device_handle,			/* handle to device */
		PGDC_IOCTL_PROCESS_LIST_PP,	/* IO control code */
		(LPVOID)port_io_buffer,		/* IN buffer (list buffer) */
		port_io_buffer_count * sizeof(struct PORT_IO_LIST_STRUCT),/* length of IN buffer in bytes */
		(LPVOID)port_io_buffer,	/* OUT buffer (list buffer) */
		port_io_buffer_count * sizeof(struct PORT_IO_LIST_STRUCT),/* length of OUT buffer in bytes */
		&n_writes,					/* number of writes performed */
		0);							/* wait for operation to complete */

	if ((!status) || ((port_io_buffer_count * sizeof(struct PORT_IO_LIST_STRUCT)) != n_writes))
	{
		fprintf(stderr, "I/O Error:  Cannot access ByteBlaster hardware\n");
		CloseHandle(nt_device_handle);
		exit(1);
	}

	port_io_buffer_count = 0;
}

/******************************************************************/
/* Name:         VerifyHardware (ByteBlasterMV)                   */
/*                                                                */
/* Parameters:   None.                                            */
/*                                                                */
/* Return Value: '0' if verification is successful;'1' if not.    */
/*               		                                          */
/* Descriptions: Verify if hardware is properly attached to the   */
/*               parallel port.                                   */
/*                                                                */
/******************************************************************/
int VerifyHardware()
{
	int error = 0;
	int test_count = 0;
	int read_data = 0;
	
	for ( test_count = 0; test_count < 2; test_count++ )
	{
		/* Write '0' to Pin 7 and Pin 9 (Data5,7) for the first test and '1' for the second test */
		int vector = (test_count) ? 0xA0 : 0x0;/* 1010 0000:0000 0000... drive to Port0 */
		int expect = (test_count) ? 0x60 : 0x0;/* X11X XXXX:X00X XXXX... expect from Port1 */

		WritePort( 0, vector, 1 );
		
		/* Expect '0' at Pin 10 and Pin 12 (Ack and Paper End) for the first test and '1' for the second test */
		read_data = ReadPort( 1 ) & 0x60;

		error = error | ( read_data == expect ? 0:1 );
	}

	if ( !error ) 
		fprintf( stdout, "Info: Verifying hardware: Hardware found...\n" );
	else
		fprintf( stderr, "Error: Verifying hardware: Hardware not found or not installed properly...\n" );

	return error;
}
#elif PORT == LINUX
void InitLinuxDriver()
{
#ifndef LINUX_PARPORT
                printf("Attempting to gain IO permissions on 0x%02x to 0x%02x\n",lpt_addr,lpt_addr+3); /* DEBUG */
                if (ioperm(lpt_addr, 3, 1)) {
                  perror("initialize_jtag_hardware failed");
                  exit(errno);
                }
#else

	int ret = 0;
	ppfp = open (devicepath, O_RDWR);
	if (!ppfp) {
		fprintf( stderr, "Error: Device open failed. exiting.\n" );
		exit(1);
	}
	ret = ioctl (ppfp, PPCLAIM);
	fprintf (stderr, "Note: ioctl claim returned stat: %i (err: %s)\n", ret, strerror (errno));
	if (ret != 0) {
		fprintf( stderr, "Error: Driver initialization failed. check permissions. exiting.\n" );
		close (ppfp);
		exit(1);
	}
#ifdef PARPORT_EXCLUSIVE
  	ret = ioctl (ppfp, PPEXCL);
	fprintf (stderr, "Note: ioctl excl returned stat: %i (err: %s)\n", ret, strerror (errno));
	if (ret != 0) {
		fprintf (stderr, "Warning: Device exclusive access failed.\n");
	}
#endif /* EXCLUSIVE */
#endif /* PARPORT*/
}
void CloseLinuxDriver()
{
#ifndef LINUX_PARPORT
	ioperm(lpt_addr, 3, 0); 
#else
	if (ppfp) close(ppfp);
	fprintf(stderr,"Total bytes transfered [r:%i|w:%i]\n", totalrd,totalwr);
#endif /* PARPORT*/
}

void flush_ports(void)
{	
#ifndef LINUX_PARPORT
	;;
#else
	int i,ret=0;
//	ret=write(ppfp,&port_io_buffer,port_io_buffer_count*sizeof(int));
	for (i=0; ret == 0 && i < port_io_buffer_count ; i++) {
		ret = ioctl (ppfp, PPWDATA, &(port_io_buffer[i]));
		totalwr++;
		printf("%i - %i\n",ret,port_io_buffer[i]);
	}

	if ( ret<0) 
	{
		fprintf(stderr, "I/O Error:  Cannot flush buffers to ByteBlaster hardware (%i,%i)\n",ret,errno);
		CloseLinuxDriver();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -