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

📄 usbio.c

📁 usbio example. bulk transfer. platform bf533+usblan card.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************************

Copyright(c) 2005 Analog Devices, Inc. All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.

*********************************************************************************/

#include <services/services.h>		// system services header
#include <drivers/adi_dev.h>		// device manager header
#include <adi_net2272.h>			// NET2272 device driver includes
#include "ezkitutilities.h"			// EZ-Kit includes
#include "plx\nccommon.h"			// Netchip common includes
#include "usbcmd.h"					// USB commands

#ifndef _DEVICE_H
#include <device.h>					// Blackfin device header
#endif // _DEVICE_H

#include <stdio.h>					// stdio header
#include <string.h>					// string header
#include <device_int.h>				// Blackfin IO device header

static ADI_DEV_DEVICE_HANDLE devhandle;	// driver model device handle
static ADI_DEV_1D_BUFFER UsbcbBuffer;	// 1D buffer for processing usbcb
static ADI_DEV_1D_BUFFER DataBuffer;	// 1D buffer for processing data
static USBCB usbcb;						// USB command block
static char stdiobuff[4096];			// stdio buffer

// USBIO prototypes
int usbio_init(struct DevEntry *dev);
int usbio_open(const char *name, int mode);
int usbio_close(int fd);
int usbio_write(int fd, unsigned char *buf, int size);
int usbio_read(int fd, unsigned char *buf, int size);
long usbio_seek(int fd, long offset, int whence);

// make the buffer big enough to read a large file
#define USBIO_BUFFER_SIZE (MAX_DATA_BYTES_EZEXTENDER*5)

#define USBIO_DEV_ID	123						// USBIO device ID
#define USBIO_STDIN_FILENAME	"usbio_stdin"	// USBIO stdin filename
#define USBIO_STDOUT_FILENAME	"usbio_stdout"	// USBIO stdout filename
#define USBIO_STDERR_FILENAME	"usbio_stderr"	// USBIO stderr filename

// file paths on host, note these paths are relative to the location of the host app
// handling the requests
#define TXT_HOST_PATH "..\\..\\usb software readme.txt"
#define TXT2_HOST_PATH "..\\..\\examples\\usbio\\txt2.txt"
#define JPG_HOST_PATH "..\\..\\common\\bulee.jpg"
#define JPG2_HOST_PATH "..\\..\\examples\\usbio\\jpg2.jpg"

// DevEntry structure for USBIO
DevEntry _usbio_device =
{
	USBIO_DEV_ID,		// DeviceID
	0,					// data
	&usbio_init,		// init
	&usbio_open, 		// open
	&usbio_close, 		// close
	&usbio_write, 		// write
	&usbio_read,		// read
	&usbio_seek,		// seek

	// we will claim these for USBIO later
	dev_not_claimed,	// stdinfd
	dev_not_claimed,	// stdoutfd
	dev_not_claimed,	// stderrfd
};

#pragma align 4
section ("sdram0")
char USBIOBuffer[USBIO_BUFFER_SIZE];	// usbio buffer in SDRAM


/******************************************************************************
Routine Description:

    Perform file I/O and stdio over USB with the host.  This function uses all
    the supported capabilities of file I/O such as open, close, read, write,
    and seek and also uses the three stdio streams (stdin, stdout, and stderr).

Arguments:

	ADI_DEV_DEVICE_HANDLE dh - device handle

Return Value:

    unsigned int - return status

******************************************************************************/

unsigned int PerformIo( ADI_DEV_DEVICE_HANDLE dh )
{
	unsigned int Result = ADI_DEV_RESULT_SUCCESS;	// result
	static int AddEntryResult = 0;					// add entry result
	int i=0, j=0, k=0, m=0;							// temps
	volatile unsigned int v = 0;					// counter
	static FILE *fpin, *fpout, *fperr;				// stdio file ptrs
	FILE *fptxt, *fptxt2, *fpjpg, *fpjpg2;			// file ptrs

	// store the device handle
	devhandle = dh;

	// only setup USBIO if we haven't already
	if ( USBIO_DEV_ID != AddEntryResult )
	{
		// show that printf currently displays in VDSP output window
		printf("This will show up in the VDSP output window\n");

		// add the device to the table
		AddEntryResult = add_devtab_entry( &_usbio_device );

		// make it the default IO device
		set_default_io_device(USBIO_DEV_ID);

		// reopening with USBIO as the default will send stdio to USBIO instead
		fpin = freopen(USBIO_STDIN_FILENAME, "r", stdin);
		fpout = freopen(USBIO_STDOUT_FILENAME, "w", stdout);
		fperr = freopen(USBIO_STDERR_FILENAME, "w", stderr);

		// setup stdout and stderr for line buffering meaning newline will call write()
		setvbuf(fpout, stdiobuff, _IOLBF, sizeof(stdiobuff));
		setvbuf(fperr, stdiobuff, _IOLBF, sizeof(stdiobuff));
	}

	j = fprintf( fperr, "This is not an error, but errors will print in red\n" );

	printf("\nWhat is your name (please don't use spaces)?\n");
	scanf("%s", USBIOBuffer);
	printf("Ok %s, how many times should we blink the LEDs?\n", USBIOBuffer);
 	scanf("%d", &i);

 	// blink the LEDs i times
 	for (j = 0; j < i; j++)
 	{
		ezTurnOnAllLEDs();

		// delay...leave LEDs on
    	for ( v = 0; v < 0x003fffff; v++)
			;

		ezTurnOffAllLEDs();

		// delay...leave LEDs off
    	for ( v = 0; v < 0x003fffff; v++)
			;
 	}

 	printf("\n");

	// open TXT files on the host, one for reading and writing
	fptxt = fopen( TXT_HOST_PATH, "r" );
	fptxt2 = fopen( TXT2_HOST_PATH, "w" );

	// open JPG (binary) files on the host, one for reading and writing
	fpjpg = fopen( JPG_HOST_PATH, "rb" );
	fpjpg2 = fopen( JPG2_HOST_PATH, "wb" );

	// seek to the beginning of the file
	k = fseek( fptxt, 0, SEEK_SET );

	// read the text file
	i = fread( USBIOBuffer, 1, USBIO_BUFFER_SIZE, fptxt );
	printf("Read 0x%x bytes from %s\n", i, TXT_HOST_PATH);

	// write the text file
	j = fwrite( USBIOBuffer, 1, i, fptxt2 );
	printf("Wrote 0x%x bytes to %s\n", j, TXT2_HOST_PATH);

	printf("\n");

	// read the jpg file
	k = fread( USBIOBuffer, 1, USBIO_BUFFER_SIZE, fpjpg );
	printf("Read 0x%x bytes from %s\n", k, JPG_HOST_PATH);
	
	// write the jpg file
	m = fwrite( USBIOBuffer, 1, k, fpjpg2 );
	printf("Wrote 0x%x bytes to %s\n", m, JPG2_HOST_PATH);

	// show the user how to view the jpg with the Image Viewer
	printf("\nIn VisualDSP++ halt the processor, open up the\n");
	printf("\"View->Debug Windows->Image Viewer\" window.  From\n");
	printf("the \"Image Configuration\" dialog set \"Source location\"\n");
	printf("to \"DSP Memory\", \"Source format\" to \"Image stream\"\n");
	printf("\"Size\" to %d and \"Start address\" to 0x%x to\n", k, USBIOBuffer);
	printf("view the image.\n");

	// close the files
	fclose( fptxt );
	fclose( fptxt2 );
	fclose( fpjpg );
	fclose( fpjpg2 );

	//////////////////////////////////////////////////////////
	// tell the host we are done with USBIO to free it up
	//////////////////////////////////////////////////////////

	// init buffer
	UsbcbBuffer.Data = &usbcb;
	UsbcbBuffer.ElementCount = sizeof(usbcb);
	UsbcbBuffer.ElementWidth = 1;
	UsbcbBuffer.CallbackParameter = NULL;
	UsbcbBuffer.pNext = NULL;

	// setup the USBCB
	usbcb.ulCommand = USBIO_STOP;	// command

	// wait for host to take the USBCB
	Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);

	return Result;
}

//////////////////////////////////////////////////////////////////////////////
// int usbio_init(struct DevEntry *dev)
//////////////////////////////////////////////////////////////////////////////

int usbio_init( struct DevEntry *dev )
{
	// do init here, we just return success
	return 1;
}

//////////////////////////////////////////////////////////////////////////////
//	int usbio_open(const char *name, int mode)
//////////////////////////////////////////////////////////////////////////////

int usbio_open( const char *name, int mode )
{
	char *pcInfo;		// data ptr
	size_t sz_name;		// size of filename
	size_t sz_info;		// size of info block
	int fd = -1;		// file descriptor
	int Result;			// result

	// first check for stdio streams, if so return the FD that indicates
	if ( !strcmp(name, USBIO_STDIN_FILENAME) )
		return USBIO_STDIN_FD;
	else if ( !strcmp(name, USBIO_STDOUT_FILENAME) )
		return USBIO_STDOUT_FD;
	else if ( !strcmp(name, USBIO_STDERR_FILENAME) )
		return USBIO_STDERR_FD;

	// get the sizes
	sz_name = strlen(name);
	sz_info = sz_name + 8;

	// if the info block is larger than MAX_DATA_BYTES_EZEXTENDER just fail
	if ( sz_info > MAX_DATA_BYTES_EZEXTENDER)
		return -1;

	// create an info block with mode and name to send to the host, add
	// a little padding for NULL terminator and mode
	pcInfo = (char*) malloc(sz_info);

	// make sure the malloc worked
	if (!pcInfo)
		return -1;

	// fill in the info block, use "sz + 1" to include the NULL terminator
	memcpy( (void*)(pcInfo + FILE_OPEN_MODE_OFFSET), (void*)&mode, sizeof(int) );
	memcpy( (void*)(pcInfo + FILE_OPEN_FILENAME_OFFSET), (void*)name, sz_name + 1 );


	// init buffers
	UsbcbBuffer.Data = &usbcb;
	UsbcbBuffer.ElementCount = sizeof(usbcb);
	UsbcbBuffer.ElementWidth = 1;
	UsbcbBuffer.CallbackParameter = NULL;
	UsbcbBuffer.pNext = NULL;

	DataBuffer.Data = pcInfo;
	DataBuffer.ElementCount = sz_info;
	DataBuffer.ElementWidth = 1;
	DataBuffer.CallbackParameter = NULL;
	DataBuffer.pNext = NULL;

	// setup the USBCB
	usbcb.ulCommand = USBIO_OPEN;	// command
	usbcb.ulCount = sz_info;		// how many data bytes host should expect

	// wait for host to take the USBCB
	Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);

	// then wait for the host to take the info block
	Result = adi_dev_Write(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&DataBuffer);

	// finally wait for the host to send back the file descriptor or negative value
	// which would indicate a failure opening the file on the host
	UsbcbBuffer.Data = &usbcb;
	UsbcbBuffer.ElementCount = sizeof(usbcb);
	UsbcbBuffer.ElementWidth = 1;
	UsbcbBuffer.CallbackParameter = NULL;
	UsbcbBuffer.pNext = NULL;

	// get the data from the host
	Result = adi_dev_Read(devhandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);

	// if we got back NULL, that means the open failed on the host, we must return
	// a negative value to indicate failure
	if ( usbcb.ulData == 0x0 )
		fd = -1;
	else
		fd = usbcb.ulData;

	// free the memory
	free(pcInfo);

	// return the file descriptor
	return fd;
}

//////////////////////////////////////////////////////////////////////////////
//	int usbio_close(int fd)
//////////////////////////////////////////////////////////////////////////////

int usbio_close( int fd )
{
	int Result;			// result

	// init buffer
	UsbcbBuffer.Data = &usbcb;
	UsbcbBuffer.ElementCount = sizeof(usbcb);
	UsbcbBuffer.ElementWidth = 1;
	UsbcbBuffer.CallbackParameter = NULL;
	UsbcbBuffer.pNext = NULL;

⌨️ 快捷键说明

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