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

📄 usb.c

📁 utisoftware c code for linux,机卡分离方案。
💻 C
字号:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/timer.h>

#include <asm/irq.h>
#include "libassp.h"
#include "def596.h"
#include "defusr.h"
#include "extern.h"
#include "datatbl.h"
#include "macusr.h"

extern void usbint(int irq, void * meaningless, struct pt_regs * none);		// gengyue
extern U16	BchgFlg;
extern void TSPIP_Config_SD(void);

MODULE_AUTHOR("Gengyue");					// gengyue
MODULE_DESCRIPTION("USB Device driver for Linux");
MODULE_LICENSE("GPL");


#define FPGA0_MODE	0xf032//0xF03A
/*#define FPGA0_MODE	0x005c*/

U8   flag=0;
U8   flag1=0;
U32  translen=2048;
U32  translen1=64;
U8	 UsbDeviceTS[4];
U16  USBerror=0;
U16  USBerrorPre=0 ;

const int  USB_PROCESS_WORKSPACE                       = 2048;
int  intcount = 0;
U32 usb_write(U8 *SendBuf, U32 Size);

struct timer_list usb_timer;

static void write_uint32(unsigned long byte_address, unsigned long data)
{
        *((volatile unsigned long *) byte_address) = data;
}

static unsigned long read_uint32(unsigned long byte_address)
{
        return *((volatile unsigned long *) byte_address);
}

static void USBProcess(unsigned long data)				// test device on or off
{
	U16	buffer;
	int full_cnt=0;
	U16	buff;
	
	if (USBerror>USBerrorPre)
	{
		USB_MODULE_PRINT("USB error = %d\n", USBerror);
		USBerrorPre=USBerror;
	}

	/* zkt print */
	/* if put this print code in the block of "if( ConfigNum != 0 )",
	   and put two vaialble define in the block. Re-run this program, 
	   audio is ok, but no video. Why? */
	USBRD(FPGA0, buff);
	if((buff & 0x0400) != 0x0000)
	{
		full_cnt++;
		USBWR(FPGA0, FPGA0_MODE); 
		USB_MODULE_PRINT("TSI FULL CNT = %d\n", full_cnt);
	}

	if(BchgFlg == YES)
	{
		if( ChkAttach() == YES )			/* Attach? */
		{
			usb_attach();						/* Attach processing */
			usb_enumerate();
			TSPIP_Config_SD();
		} 
		else 
		{
			usb_detach();
			BchgFlg = YES;
		}
	} 
	else 
		USBRD(SYSCFG, buffer);					/* USB active? */
	init_timer(&usb_timer);
	usb_timer.function = USBProcess;
	usb_timer.data = (unsigned long)0;
	usb_timer.expires = jiffies + 20;
	add_timer(&usb_timer);
}
/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: Main
 *-----------------------------------------------------------------------------
 * Declaration	: void main(void)
 *-----------------------------------------------------------------------------
 * Argument		: void
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
int init_module(void)
{
	U32 translenTemp=1024*3;
	unsigned long buffer;
		
	USBWR(FPGA0, FPGA0_MODE);
	USBWR(DMA0CFG, 0xffff); 
		
	UsbDeviceTS[0]=(U8)(translenTemp&0x00FF);
	UsbDeviceTS[1]=(U8)((translenTemp>>8)&0x00FF);
	UsbDeviceTS[2]=(U8)((translenTemp>>16)&0x00FF);
	UsbDeviceTS[3]=(U8)((translenTemp>>24)&0x00FF);

	{
		register U16	buf;
		USBRD(SYSCFG, buf);
		USBWR(SYSCFG, USBE);
		USBRD(SYSCFG, buf);
		USBRD(DVSTCTR, buf);
		USBWR(DVSTCTR, WKUP);
		USBRD(DVSTCTR, buf);
	}
	waitAssp();				// wait for enable chip  read-write
	initAssp();				// initial register
	usb_detach();			// clear usb device bit
	BchgFlg = YES;
				
	cli();							// gengyue
	if (request_irq(USB_INT_LEVEL, usbint, SA_INTERRUPT, "usb chip device", 0) == 0)	// gengyue
		enable_irq((unsigned int)USB_INT_LEVEL);
	else
		USB_MODULE_PRINT("interrupt_install failed\n");
	sti();							// gengyue

	init_timer(&usb_timer);
	usb_timer.function = USBProcess;
	usb_timer.data = (unsigned long)0;
	usb_timer.expires = jiffies + 100;
	add_timer(&usb_timer);
	
	return 0;												// gengyue
}

void cleanup_module(void)									// gengyue
{
	del_timer(&usb_timer);
	free_irq(USB_INT_LEVEL, NULL);
}

/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: Delay 1milli-second
 *-----------------------------------------------------------------------------
 * Declaration	: void delay_1ms(void)
 *-----------------------------------------------------------------------------
 * Argument		: void
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			: Please change for your MCU
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
void delay_1ms(void)
{
	mdelay(1);
}


/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: Delay n micro-second
 *-----------------------------------------------------------------------------
 * Declaration	: void delay_xms(U16 Time)
 *-----------------------------------------------------------------------------
 * Argument		: U16 Time			; Wait Time
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
void delay_xms(U16 Time)
{
	volatile register U16	i;

	for( i = 0; i < Time; ++i ) {
		delay_1ms();
	}
}


/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: Delay 10micro-second
 *-----------------------------------------------------------------------------
 * Declaration	: void delay_10us(U16 Time)
 *-----------------------------------------------------------------------------
 * Argument		: U16 Time			; Wait Time
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			: Please change for your MCU
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
void delay_10us(U16 Time)
{
	udelay(10);
}


/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: User application
 *-----------------------------------------------------------------------------
 * Declaration	: void User_Application(void)
 *-----------------------------------------------------------------------------
 * Argument		: void
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
void User_Application(void)
{
	if (flag1==0){
	flag1=1;
	User_Data_Out(PIPE6, (U32)translen1, (U8 *)P6_Buff);
	}
	if( Buffer_Write_Data_Flag[PIPE7] == DATA_NONE) 
		if (flag1==1) {
			User_Data_In(PIPE7, (U32)translen1, (U8 *)P7_Buff);
			flag1=2;		
		}	
}


/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: User data in
 *-----------------------------------------------------------------------------
 * Declaration	: void User_Data_In(U16 Pipe, U32 Size, U8 *Table)
 *-----------------------------------------------------------------------------
 * Argument		: U16 Pipe			; Pipe Number
 *				: U16 Size			; Data Size
 *				: U8  *Table		; Data Table Address
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
void User_Data_In(U16 Pipe, U32 Size, U8 *Table)
{
	if( Buffer_Read_Data_Flag[Pipe] == DATA_NONE ) {
		DI_Start(Pipe, Size, Table);
	}
	else if( Buffer_Read_Data_Flag[Pipe] == DATA_OK ) {
		if( Read_Data(Pipe, Buffer_Read_Data_Size[Pipe], Table) == BUF_READY ) {
			Buffer_Read_Data_Flag[Pipe] = DATA_NONE;
		}
	}
}


/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: User data out
 *-----------------------------------------------------------------------------
 * Declaration	: void User_Data_Out(U16 Pipe, U32 Size, U8 *Table)
 *-----------------------------------------------------------------------------
 * Argument		: U16 Pipe			; Pipe Number
 *				: U16 Size			; Data Size
 *				: U8  *Table		; Data Table Address
 *-----------------------------------------------------------------------------
 * Return		: void
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
void User_Data_Out(U16 Pipe, U32 Size, U8 *Table)
{
	if( Buffer_Write_Data_Flag[Pipe] == DATA_NONE ) {
		if( Create_Data(Pipe) != DATA_NONE ) {
	delay_xms(200);
			DO_Start(Pipe, Size, Table);
			
	}
	}
}
void DMA_RW(U16 Pipe)
{
	U16	buf;

	
	SetEpPid(Pipe, PID_BUF);				/* Set BUF */
	
	
	/* USBWR(PIPE2CTR, 0x0001); */

	USBRD(PIPE2CTR, buf);
	udelay(10);
	USB_MODULE_PRINT("PIPE2CTR register's value = %x\n", buf);

	USBRD(BRDYENB, buf);
	udelay(10);
	USB_MODULE_PRINT("BRDYENB register's value = %x\n", buf);

	USBRD(NRDYENB, buf);
	udelay(10);
	USB_MODULE_PRINT("NRDYENB register's value = %x\n", buf);

	USBRD(BEMPENB, buf);
	udelay(10);
	USB_MODULE_PRINT("BEMPENB register's value = %x\n", buf);

/*
	USBWR(PIPESEL, 0x0002);
	USBWR(PIPECFG, 0x4302);
	USBWR(PIPEBUF, 0x3c36);
	USBWR(PIPEMAXP, 0x4040);
	USBWR(PIPESEL, 0x0000); */

}

/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: Create send(OUT) data
 *-----------------------------------------------------------------------------
 * Declaration	: U16 Create_Data(U16 Pipe)
 *-----------------------------------------------------------------------------
 * Argument		: U16 Pipe			; Pipe Number
 *-----------------------------------------------------------------------------
 * Return		: U16				; DATA_WAIT : Enable  Send Data
 *				:					; DATA_NONE : Disable Send Data
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
U16 Create_Data(U16 Pipe)
{
	/*
	 * Create send(OUT) data routine
	 */

	/* Enable send(OUT) data */
	return DATA_WAIT;

	/* Disable send(OUT) data */
	/* return DATA_NONE; */
}


/*""FUNC COMMENT""*************************************************************
 *-----------------------------------------------------------------------------
 * Function		: Read receive(IN) data
 *-----------------------------------------------------------------------------
 * Declaration	: U16 Read_Data(U16 Pipe, U16 Bsize, U8 *Table)
 *-----------------------------------------------------------------------------
 * Argument		: U16 Pipe			; Pipe Number
 *				: U16 Bsize			; Data Size
 *				: U8  *Table		; Data Table Address
 *-----------------------------------------------------------------------------
 * Return		: U16				; BUF_READY : Processing Of Receive Data Is Completed
 *				:					; BUF_BUSY  : Receive Data Is Under Processing
 *-----------------------------------------------------------------------------
 * Note			:
 *-----------------------------------------------------------------------------
 *""FUNC COMMENT END""********************************************************/
U16 Read_Data(U16 Pipe, U16 Bsize, U8 *Table)
{
	/*
	 * Read receive(IN) data routine
	 */

	/* Processing of receive data is completed */
	return BUF_READY;

	/* Received data is under processing */
	/* return BUF_BUSY; */
}

#if 0
U16 usb_read(U8 *ReceiveBuf, U32 Size)
{	
	if( Buffer_Read_Data_Flag[PIPE7] == DATA_NONE ) 
	{
		DI_Start(PIPE7, Size, ReceiveBuf);

		while(1)
		{
			USB_TASK_DELAY(1);
			if (Buffer_Read_Data_Flag[PIPE7] == DATA_NONE)
				break;

		}
		USB_MODULE_PRINT(("recieve bytes value = %x\n", Buffer_Read_Data_Size[PIPE7]));
		return Buffer_Read_Data_Size[PIPE7];
	}
	else
	{
		return 0;
	}
}
#endif

#if 1
U16 usb_read(U8 *ReceiveBuf, U32 Size)
{	

	U16 temp; 
	if( Buffer_Read_Data_Flag[PIPE7] == DATA_NONE ) 
	{
		DI_Start(PIPE7, Size, ReceiveBuf);
	}
	if (Buffer_Read_Data_Flag[PIPE7] == DATA_OK)
	{
		/*
		USB_MODULE_PRINT(("recieve bytes value = %x\n", Buffer_Read_Data_Size[PIPE7]));
		*/
		Buffer_Read_Data_Flag[PIPE7] = DATA_NONE;
		temp = Buffer_Read_Data_Size[PIPE7];
		DI_Start(PIPE7, Size, ReceiveBuf);
		
		return temp;
	}

	return 0;
}
#endif

U32 usb_write(U8 *SendBuf, U32 Size)
{
	 U32 ErrorCode;

	if( Buffer_Write_Data_Flag[PIPE6] == DATA_NONE )
	{
		DO_Start(PIPE6, Size, SendBuf);

		while(1)
		{
			udelay(10);
			if (Buffer_Write_Data_Flag[PIPE6] == DATA_NONE)
				break;
		}
		/*USB_MODULE_PRINT(("send success\n"));*/
		
		ErrorCode =  0; //NO_ERROR
	}
	else
	{
		ErrorCode = 12; //ERROR_DEVICE_BUSY
	}

	return ErrorCode;
}

⌨️ 快捷键说明

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