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

📄 isp1581.c

📁 DSP通过双口RAM和ISP1581实现下位机的USB固件程序
💻 C
字号:
#include "SMPro.h"
#include "HAL.h"
#include "USB.H"
#include "Isp1581.h"
extern ZBOARDFLAGS bZBoardFlags;

void outport(unsigned short nPort, unsigned short nVal)
{
	unsigned short idata temp;
	
	unsigned char xdata *exAddress;
	exAddress = nPort;
	temp=nVal;
	ISP1581_CS=0;//xxg
	*exAddress=temp;
	temp=temp>>8;
	*(exAddress+1)=temp;
	ISP1581_CS=1;//xxg

}

void outportb(unsigned short nPort, unsigned char nVal)
{
	unsigned char xdata *exAddress;
	exAddress = nPort;
	
	ISP1581_CS=0;//xxg
	*exAddress = nVal;
	ISP1581_CS=1;//xxg
}

unsigned short inport(unsigned short nPort)
{
	unsigned short idata nVal;
	unsigned char idata temp;
	unsigned char xdata *exAddress;
	exAddress = nPort;
	
	ISP1581_CS=0;//xxg
	
	temp=*exAddress;
	nVal=*(exAddress+1);
	ISP1581_CS=1;//xxg
	nVal=nVal<<8;
	nVal=nVal+temp;
	return nVal;
	
}

unsigned char inportb(unsigned short nPort)
{
	//unsigned short idata nVal;
	unsigned char nVal;
	unsigned char xdata *exAddress;
	exAddress = nPort;
	ISP1581_CS=0;//xxg
	nVal = *exAddress;
	ISP1581_CS=1;//xxg
	return nVal;

}
//////////////////////////////////////////////////////////
void ISP1581_ResetDevice(void)
{
	unsigned char i=0,j;

	ISP1581_SetMode(mode_reset);
	ISP1581_SetMode(0x0);
	for(i = 0; i <15; i++)
	{
		for(j = 0; j < 255; j++)
		{
		}
	
	}
	
}
void ISP1581_SendResume(void)
{
	UCHAR c;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	ISP1581_ReadChipID();; // 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(bZBoardFlags.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_SetEPIndex(UCHAR bEPIndex)  //OK
{
	outportb(rega_epindex, bEPIndex);
}

UCHAR ISP1581_GetEndpointStatus(UCHAR bEPIndex) //OK
{
	UCHAR c;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();
	ISP1581_SetEPIndex(bEPIndex);
	c = inportb(rega_epctlfc);

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

void ISP1581_SetEndpointStatus(UCHAR bEPIndex, UCHAR bStalled) //OK
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	ISP1581_SetEPIndex(bEPIndex);
	outportb(rega_epctlfc, bStalled);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}
void ISP1581_ControlReadHandshake(void)
{
	ISP1581_SetEndpointStatus(EPINDEX4CONTROL_IN, epctlfc_status|epctlfc_stall);
}
void ISP1581_ControlWriteHandshake(void)
{
	ISP1581_SetEndpointStatus(EPINDEX4CONTROL_OUT, epctlfc_status|epctlfc_stall);
}

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

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

	outportb(rega_addren, bAddress); // set new address enable);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}
USHORT ISP1581_ReadControlEndpointSetup(UCHAR * buf)
{
	USHORT i,c;

	ISP1581_SetEPIndex(EPINDEX4CONTROL_SETUP);
	c = inport(rega_dcount);
	for(i=0; i<c; i++)
	{
		*buf = inportb(rega_dport);
		buf ++;
	}

	return c;
}

USHORT ISP1581_WriteControlEndpoint(UCHAR * buf, USHORT len)
{
	USHORT i;
	UCHAR  c;

	ISP1581_SetEPIndex(EPINDEX4EP07OUT);// work around for es4, write in endpoint corrupts out endpoint data
	ISP1581_SetEPIndex(EPINDEX4CONTROL_IN);

	outport(rega_dcount, len);
	for(i=0;i<len;i++,buf++)
	{
	  c=*buf;
	  outportb(rega_dport,c);
	}
	return len;
}

USHORT ISP1581_ReadControlEndpoint(UCHAR * buf, unsigned short len)
{
	USHORT i, j, RX_LEN;
	
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	ISP1581_SetEPIndex(EPINDEX4CONTROL_OUT);
	// get pkt length
	RX_LEN = inport(rega_dcount);

	if(RX_LEN <= EP0_PACKET_SIZE)
	{
		j = RX_LEN;
	}
	else
	{
		j = EP0_PACKET_SIZE;
	}

	for(i=0; i< j; i++) // Maxium data length allowed.
	{
		* buf = inportb(rega_dport);
		buf ++;
	}

	for(i = j; i <RX_LEN; i++)   // strip invalid data other than setup has set
		inportb(rega_dport);

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

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

	return j;
}
USHORT ISP1581_ReadBulkEndpoint(UCHAR bEPIndex, unsigned char * buf, USHORT len)
{
	USHORT i, j, c;

	ISP1581_SetEPIndex(bEPIndex);
	j = inport(rega_dcount);

	if(j<len)
		len=j;
	for(i=0; i<len; i=i+1, buf++ )
	{
		c = inportb(rega_dport);
		*buf = c;
	}
	for(i=len; i<j; i=i+1)
		inportb(rega_dport);                  // data received is more than count setup got, discards the extra data.
	
	return len;
}
void ISP1581_WriteBulkEndpoint(UCHAR bEPIndex, unsigned char * buf, USHORT len)
{
	USHORT  i;

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

	ISP1581_SetEPIndex(EPINDEX4EP07OUT);// work around for es4, write in endpoint corrupts out endpoint data
	ISP1581_SetEPIndex(bEPIndex);
		outport(rega_dcount, len);


	for(i=0; i<len; i++, buf++ )
	{
		outportb(rega_dport,*buf);
	}

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}
/*
USHORT ISP1581_ReadISOEndpoint(UCHAR bEPIndex, unsigned short * buf, USHORT len)
{
	USHORT i, j;

	ISP1581_SetEPIndex(bEPIndex);
	//outport(aport, rega_dcount);
	j = inport(rega_dcount);
	if(j<len)
		len=j;
	for(i=0; i<len; i=i++, buf++ )
	{
		*buf = inportb(rega_dport);
	}
	
	for(i=len; i<j; i=i+1)
		inportb(rega_dport);                  // data received is more than count setup got, discards the extra data.

	return len;
}
*/
/*void ISP1581_WriteISOEndpoint(UCHAR bEPIndex, unsigned short * buf, USHORT len)
{
	USHORT  i;

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

	ISP1581_SetEPIndex(EPINDEX4EP07OUT);// work around for es4, write in endpoint corrupts out endpoint data
	ISP1581_SetEPIndex(bEPIndex);

	if (len < 256)
	{
        	outport(rega_dcount, len);
	}

	for(i=0; i<len; i ++, buf++ )
	{
		outportb(rega_dport,*buf);
	}

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

*/
void ISP1581_StallEP0InControlWrite(void)
{
	ISP1581_SetEndpointStatus(EPINDEX4CONTROL_IN, epctlfc_stall);
	ISP1581_SetEndpointStatus(EPINDEX4CONTROL_OUT, epctlfc_stall);
}

void ISP1581_StallEP0InControlRead(void)
{
	ISP1581_SetEndpointStatus(EPINDEX4CONTROL_OUT, epctlfc_stall);
	ISP1581_SetEndpointStatus(EPINDEX4CONTROL_IN, epctlfc_stall);
}

USHORT ISP1581_ReadInterruptRegisterLow(void) 
{
	USHORT idata i = 0;
		
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();
	
	i = inport(rega_interrupt_low);
	
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();

	return i;
}

USHORT ISP1581_ReadInterruptRegisterHigh(void) 
{
	USHORT idata i = 0;
	
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	i = inport(rega_interrupt_high);
	

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

	return i;
}

void ISP1581_IntClearl(USHORT wIntlow) //OK
{
	outport(rega_interrupt_low,wIntlow);
}

void ISP1581_IntClearh(USHORT wInthigh) //OK
{
	outport(rega_interrupt_high,wInthigh);
	
}
UCHAR ISP1581_GetMode(void)
{
	UCHAR c;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	//outport(aport, rega_mode);
	c = inportb(rega_mode);

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

void ISP1581_SetMode(UCHAR bMode)  //OK
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outportb(rega_mode, bMode);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}
void ISP1581_SetTestMode(unsigned char testmode) //OK
{
	outportb(rega_testmode,testmode);
}

void ISP1581_SetIntConfig(unsigned char IntConfig) //OK
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outportb(rega_intcfg,IntConfig);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}
USHORT ISP1581_GetIntEnableLow(void)
{
	USHORT i;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	i = inport(rega_intmask_low);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return i;
}
void ISP1581_SetIntEnableLow(USHORT int_vector)//OK
{
	//unsigned short temp2;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(rega_intmask_low,int_vector);
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

USHORT ISP1581_GetIntEnableHigh(void)	//OK
{
	USHORT i;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	//outport(aport, rega_intmask_high); // assigned interrupt register address;
	i = inport(rega_intmask_high);
	
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return i;
}

void ISP1581_SetIntEnableHigh(USHORT int_vector)	//OK
{

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

	outport(rega_intmask_high,int_vector);

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

void ISP1581_SetEndpointConfig(UCHAR bEPIndex, UCHAR bEPConfig) //OK
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	ISP1581_SetEPIndex(bEPIndex);

	outportb(rega_eptype,bEPConfig);

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

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

    /* Select Endpoint */
	ISP1581_SetEPIndex(bEPIndex);
	outportb(rega_epctlfc, epctlfc_clbuf);
   
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}

void ISP1581_SetEPMAXSize(unsigned char bEPIndex, unsigned short epmaxsize) //OK
{
	ISP1581_SetEPIndex(bEPIndex);
	outport(rega_epmaxsize,epmaxsize);
	
}


void ISP1581_UnlockDevice(void)
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(rega_unlock, 0xaa37);

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

unsigned short ISP1581_ReadChipID(void)
{
	unsigned short i;

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

	i = inport(rega_chipid+1);
	
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();

	return i;
}


/////////////////////////////////////////////////////
void ISP1581_SetDMAConfig(USHORT wDMACnfg)
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	//outport(aport, rega_dmacfg);
	outport(rega_dmacfg, wDMACnfg);

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

/*USHORT ISP1581_GetDMAConfig(void)
{
	USHORT i;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	//outport(aport, rega_dmacfg);
	i = inport(rega_dmacfg);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return i;
}
*/
void ISP1581_SetDMAHDCfg(unsigned char dmahdcfg)
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outportb(rega_dmahdcfg,dmahdcfg);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
}
/*
unsigned char ISP1581_GetDMAHDCfg(void)
{
	unsigned char i;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	i = inportb(rega_dmahdcfg);
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return i;
}
*/
/*
void ISP1581_SetDMACMD(unsigned char bCMD)
{
	outportb(rega_dmacmd, bCMD);
}

void ISP1581_SetDMACounterLow(USHORT wDMACounter)
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(rega_dmacount, wDMACounter);

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

void ISP1581_SetDMACounterHigh(USHORT wDMACounter)
{
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	outport(rega_dmacount+0x2, wDMACounter);

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

USHORT ISP1581_GetDMACounterLow(void)
{
	USHORT i;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	i = inport(rega_dmacount);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return i;
}

USHORT ISP1581_GetDMACounterHigh(void)
{
	USHORT i;
	if(bZBoardFlags.bits.At_IRQL1 == 0)
		RaiseIRQL();

	i = inport(rega_dmacount+0x2);

	if(bZBoardFlags.bits.At_IRQL1 == 0)
		LowerIRQL();
	return i;
}
*/
/*void ISP1581_SetDMAIntMask(unsigned short dmaintmask)
{
	//outport(aport, rega_dmaintmask);
	outport(rega_dmaintmask, dmaintmask);
}
unsigned short ISP1581_GetDMAIntMask(void)
{
	return inport(rega_dmaintmask);
}*/
USHORT ISP1581_GetDMAInt(void)
{
	return inport(rega_dmaint);
}

void ISP1581_SetDMAInt(USHORT dma_int)
{
	outport(rega_dmaint, dma_int);
}


//void ISP1581_SetDMAEP(unsigned char dmaep)
//{
//	outportb(rega_dmaep,dmaep);
//}

⌨️ 快捷键说明

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