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

📄 isp1581.c

📁 飞利浦公司usb2.0芯片isp1581的通用驱动代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
   //*************************************************************************
   //
   //                  P H I L I P S   P R O P R I E T A R Y
   //
   //           COPYRIGHT (c)   2000 BY PHILIPS SINGAPORE.
   //                     --  ALL RIGHTS RESERVED  --
   //
   // Project:          ISP1581 EVA board
   // File Name:        ISP1581.C
   // Author:           Qian Jiang
   // Created:          Sept. 1, 2000
   // Modified:
   // Revision:         0.0
   //
   //*************************************************************************
*/
#include <dos.h>
#include <conio.h>

#include "BasicTyp.h"
#include "common.h"

#include "usb200.h"

#include "HAL4Sys.h"
#include "ISP1581.h"
#include "isr.h"
#include "mainloop.h"
#include "verify.h"

/*
//*************************************************************************
//  Public static data
//*************************************************************************
*/
extern ISP1581FLAGS bISP1581flags;
extern CONTROL_XFER ControlData;
extern GLOBE_VARIABLE globe_variable;
//#define test_datatogglebitofstall
/*
//*************************************************************************
//  Private static data
//*************************************************************************
*/

void interrupt (*ISP1581_OldIsr4ISP1581)(void);

/*
//*************************************************************************
// Subroutines -- 1
//*************************************************************************
*/
void ISP1581_Acquire(void)
{
	UCHAR c, eo;

	RaiseIRQL();
	bISP1581flags.bits.At_IRQL1 = 1;

	Hal4Sys_ControlISP1581Interrupt(FALSE);

	if(bISP1581flags.bits.dbg )
		ISP1581_RegAccess();

    ISP1581_OldIsr4ISP1581 = getvect(0x0D);
	setvect(0x0D, usb_isr);

	c = inportb(0x21);
	c &= 0xDF;
	outportb(0x21, c);


	Hal4Sys_ControlISP1581Interrupt(TRUE);


	bISP1581flags.bits.At_IRQL1 = 0;
	LowerIRQL();

}

void ISP1581_Release(void)
{
	UCHAR c;

	RaiseIRQL();
	bISP1581flags.bits.At_IRQL1 = 1;

	Hal4Sys_ControlISP1581Interrupt(FALSE);

	c = inportb(0x21);
	c |= 0x20;
	outportb(0x21, c);

	setvect(0xD, ISP1581_OldIsr4ISP1581);

	Hal4Sys_ControlISP1581Interrupt(TRUE);
	bISP1581flags.bits.At_IRQL1 = 0;
	LowerIRQL();


}
/*
//*************************************************************************
//  Subroutines -- 2
//*************************************************************************
*/
void ISP1581_ResetDevice(void)
{
	unsigned char i=0,j,k;

	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	cprintf("SOFT RESET ISP1581, *&%$#@!_+(*");
	ISP1581_SetMode(mode_reset);
	ISP1581_SetMode(0x0);
	for(i = 0; i <15; i++)
	{
		for(j = 0; j < 255; j++)
		{
			for(k = 0; k < 255; k++)
			{}
		}
		cprintf(".");
	}
	cprintf("RESET OK!\r\n");
    
    if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

void ISP1581_SetMode(UCHAR bMode)
{
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(aport, rega_mode);
	outport(dport, (unsigned short)bMode);

    if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

UCHAR ISP1581_GetMode(void)
{
	UCHAR c;
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(aport, rega_mode);
	c = (unsigned char)inport(dport);

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return c;
}

void ISP1581_SetEPIndex(UCHAR bEPIndex)
{
	outport(aport, rega_epindex);
	outport(dport, (bEPIndex|0xff00));
}

UCHAR ISP1581_GetEPIndex(void)
{
	outport(aport, rega_epindex);
	return (unsigned char)inport(dport);
}

UCHAR ISP1581_GetAddress(void)
{
	UCHAR c;
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(aport, rega_addren);
	c = (unsigned char)inport(dport);       // mask address enable bit;
	c &= addren_addrmask;
	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return c;
}

void ISP1581_SetAddressEnable(UCHAR bAddress, UCHAR bEnable)
{
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	if(bEnable)
		bAddress |= addren_enable;
	else
		bAddress &= addren_addrmask;

	outport(aport, rega_addren);
	outport(dport, (unsigned short)bAddress | 0xff00); // set new address enable);

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

void ISP1581_SendResume(void)
{
	UCHAR c;
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	ISP1581_GetDataFromChipRam(); // wake up device first;
	// need to add some delay for 3 ms to let the device get out of suspend, (crystal to be stable);
	// srcatch is used to store user information for wake up;
	ISP1581_UnlockDevice();
	c = ISP1581_GetMode();
	ISP1581_SetMode(c|mode_sndrsu);
	ISP1581_SetMode(c);

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

void ISP1581_GoSuspend(void)
{
	unsigned char c;
	c = ISP1581_GetMode();
	ISP1581_SetMode(c|mode_gosusp);
	ISP1581_SetMode(c);
	return;
}


void ISP1581_SoftConnect(UCHAR bEnable)
{
	UCHAR c;
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(aport, rega_mode);
	c = (unsigned char)inport(dport);
    if(bEnable)
		c |= mode_softct;
	else
		c &= ~mode_softct;

    outport(dport, c);

    if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

void ISP1581_ClearBuffer(UCHAR bEPIndex)
{
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

    /* Select Endpoint */
	ISP1581_SetEPIndex(bEPIndex);
	outport(aport, rega_epctlfc);
	outport(dport, epctlfc_clbuf | 0xff00);
   
	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

void ISP1581_ValidBuffer(UCHAR bEPIndex)
{
	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	ISP1581_SetEPIndex(bEPIndex);
	outport(aport, rega_epctlfc);
	outport(dport, epctlfc_venp | 0xff00);

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

USHORT ISP1581_CheckShortPKT(void)
{
	unsigned unsigned c;

	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(aport, rega_shtpkt);
	c =  inport(dport);

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return c;
}

USHORT ISP1581_GetPktLength(void)
{
	unsigned short c;

	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(aport, rega_dcount);
	c = inport(dport);

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();

	return c;
}

USHORT ISP1581_ReadControlEndpointSetup(UCHAR * buf)
{
	// characterize for set up command.
	USHORT i, c;

	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();
	/* read Buffer */   //JQ read LSB first then MSB of length.

	ISP1581_SetEPIndex(EPINDEX4CONTROL_SETUP);

	outport(aport, rega_dcount);
	c = inport(dport);
#ifdef debug
	if(c != 0x8)
	{
		textattr(03);
		cprintf("L>%x", c);
		textattr(07);
	}
	else
	{
		textattr(0x03);
		cprintf("L8#");
		textattr(07);
	}
#endif

	outport(aport,rega_dport);
	for(i=0; i<4; i ++, buf++)
	{
		c = inport(dport);
#ifdef debug
		textattr(0x07);
		cprintf("%02x",(unsigned char)c);
		textattr(0x03);
		cprintf("%02x", (unsigned char)(c >> 8));
		textattr(0x07);
#endif
		* buf = (UCHAR)c;
		buf ++;
		c >>= 8;
		* buf = (UCHAR) c;
	}
	/* Clear Buffer */

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();

	return 0x08;
}

USHORT ISP1581_ReadControlEndpoint(UCHAR * buf, unsigned short len)
{
	// characterize for set up command.
	USHORT i, j, RX_LEN, c;

	if(bISP1581flags.bits.At_IRQL1 == 0)
		RaiseIRQL();
	/* read Buffer */   //JQ read LSB first then MSB of length.

	ISP1581_SetEPIndex(EPINDEX4CONTROL_OUT);

	// get pkt length
	outport(aport, rega_dcount);
	RX_LEN = inport(dport);
	if(RX_LEN <= EP0_PACKET_SIZE)
	{
		j = RX_LEN;
#ifdef debug
		textattr(0x05);
		cprintf("DOL%x#",RX_LEN);
#endif
	}
	else
	{
		j = EP0_PACKET_SIZE;
#ifdef debug
		cprintf("Data out is longer than expected!\n");
#endif
	}

	outport(aport, rega_dport);

	for(i=0; i< j; i += 2, buf++ ) // Maxium data length allowed.
	{
		c = inport(dport);

#ifdef debug
		cprintf("%04x",c);
#endif
		* buf = (UCHAR)c;
		buf ++;
		c >>= 8;
		* buf = (UCHAR)c;
	}
#ifdef debug
	textattr(0x07);
#endif
	for(i = j; i <RX_LEN; i += 2)   // strip invalid data other than setup has set
		inport(dport);

	if(RX_LEN > len)
		j = EP0_PACKET_SIZE;    // return max length informing more data received than expected.

	if(bISP1581flags.bits.At_IRQL1 == 0)
		LowerIRQL();

	return j;
}

USHORT ISP1581_ReadBulkEndpoint(UCHAR bEPIndex, unsigned short far * buf, USHORT len)
{
	USHORT i, j, c;
	unsigned char key, d, e;
	static unsigned short pktno=0, pktno1;

#ifdef test_datatogglebitofstall
	static unsigned char f;
	static unsigned short wtogglestall=0x0008;
#endif

//debug, data corrupt, with buffer or read out wrong only?
/*	ISP1581_SetEPIndex(bEPIndex);
	outport(aport, rega_dcount); // read out length;
	inport(dport);
	outport(aport, rega_dport); // point to data port;
	for(i = 0; i <16; i++)
	{
		inport(dport); // read out 8 words (16 bytes);
	}
	ISP1581_SetEPIndex(0x0a);
	// read out 8 words;
*/
	ISP1581_SetEPIndex(bEPIndex);
	outport(aport, rega_dcount);
	j = inport(dport);

	outport(aport, rega_dport);
	pktno1 = inport(dport);
	*buf = pktno1;
//stall
#ifdef test_datatogglebitofstall
	if(*buf == wtogglestall)
	{
		ISP1581_SetEndpointStatus(bEPIndex, epctlfc_stall);
//		ISP1581_SetEndpointStatus(bEPIndex, epctlfc_stall|epctlfc_clbuf); //double buffer, 1st;
//		ISP1581_IntClearl(int_ep1rx);
//		ISP1581_SetEndpointStatus(bEPIndex, epctlfc_stall|epctlfc_clbuf); //dobule buffer, 2nd.
		switch(bISP1581flags.bits.ConnectSpeed)
		{
			case HighSpeed:
			{
				if(wtogglestall == 0x0020)
					wtogglestall = 0x0028;
				else
					wtogglestall = 0x0020;
				return;
			}
			case FullSpeed:
			{
				if(wtogglestall == 0x0002)
					wtogglestall = 0x0003;
				else
					wtogglestall = 0x0002;
				return;
			}
		}
	}
#endif
	buf++;
	for(i=2; i<len; i=i+2, buf++ )
	{
		c = inport(dport);
		*buf = c;
//below to check if data received is corrupted.
/*		d = (unsigned char)c;
		e = (unsigned char)(c>>8);
#ifdef debug
		if(((d==f+1)&&(e==d+1))||(i<=4))
			cprintf("%02x%02x",d,e);
		else
		{
			textattr(0x87);
			cprintf("%02x%02x",d,e);
			textattr(0x07);
		}
#endif
		f = e;
*/	}

	for(i=len; i<j; i=i+2)
		inport(dport);                  // data received is more than count setup got, discards the extra data.

⌨️ 快捷键说明

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