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

📄 usb.c

📁 image capture billing control system
💻 C
字号:
/* EP(End Point)
0 : Command In
1 : Bulk Out
2 : Bulk In
3 : Interrupt Out
*/
	
void Init_USB(void)
{
	USB_Reset = 0;
	Delay_100ms(1);
/*
	USBI_VID1 = 0x88;	// Vender ID = 8788
	USBI_VID2 = 0x87;
	USBI_PID1 = 0x04;	// Product ID = 0104
	USBI_PID2 = 0x01;
*/
	USBI_VID1 = 0x0A;	// Vender ID = 090A
	USBI_VID2 = 0x09;
	USBI_PID1 = 0xC0;	// Product ID = 12C0
	USBI_PID2 = 0x12;
/*
	USBI_DEV1 = 0x04;	// Device release number = 0403
	USBI_DEV2 = 0x03;
*/
	USBI_DEV1 = 0x00;	// Device release number = 0403
	USBI_DEV2 = 0x00;

	USBI_INTR = 0x01;	// Interrupt endpoint data rate
	USBI_PWR  = 0xA0;	// Power source configuration : BusPower, RemoteWakeup
//	USBI_MPW  = 0xFA;	// Maximum power source consumption. 80H = 128 --> 256 mA
	USBI_MPW  = 100;	// Maximum power source consumption. 100 --> 200 mA
//	USBI_INT  = 0x0F;	// PowerSave, CtrlEP, IntEp, Reset
	USBI_INT  = 0x06;	// Control EndPoint & Interrupt EndPoint interrupt enable
		
	USBI_GEN  = 0x10;	// System Ready

	UsbInterruptData = USB_READY;
	BIM_Refreshed=0;
	SMC_Refreshed=0;
	
	USBI_CR = 2;	// FIFO = 2Byte
	USBI_CRD1 = UsbInterruptData;
	USBI_CRD2 = ~UsbInterruptData;

	Delay_100ms(1);
	P3CfgA	= 0xFD;
	P3CfgB	= 0x02;
	Delay_100ms(1);
//	P3CfgA	= 0xef;		// P36 : High-Impedance	-> USB Control open
//	P3CfgB	= 0x10;
}


#define POWER_SAVE_IRQ		UsbIntBackup & 0x80
#define CONTROL_EP_IRQ		UsbIntBackup & 0x40
#define INTERRUPT_EP_IRQ	UsbIntBackup & 0x20
#define USB_RESET_IRQ		UsbIntBackup & 0x10

IntExt0()       interrupt	0             // 0003H USB & P3.2(INT0)
{
	Byte UsbIntBackup, i;

	UsbIntBackup = USBI_INT;
	USBI_INT &= 0x0F;	// All USB IRQ = 0

	if (INTERRUPT_EP_IRQ)
	{
		USBI_CR = 2;	// FIFO = 2Byte
		USBI_CRD1 = UsbInterruptData;
		USBI_CRD2 = ~UsbInterruptData;
		UID_Transfered = 1;
	}
	else if (CONTROL_EP_IRQ)
	{
		for( i=0 ; i<8 ; i++)
		{
			USB_Command_Temp[i] = USBI_CMD;
		}
/*
		USB_Command_Temp[0] = USBI_CMD;	// Device Code
		USB_Command_Temp[1] = USBI_CMD;	// High:Command, Low:Option(BIM/SMG, FAT/File)
		USB_Command_Temp[2] = USBI_CMD;	//
		USB_Command_Temp[3] = USBI_CMD;	//
		USB_Command_Temp[4] = USBI_CMD;	//
		USB_Command_Temp[5] = USBI_CMD;	//
		USB_Command_Temp[6] = USBI_CMD;
		USB_Command_Temp[7] = USBI_CMD;
*/
		UsbCommandIn = TRUE;
	}
}

void Poll_USB()
{
	Byte USB_command;
	Byte i;
	Word parameter;
	Bit refreshed;
//	Word page, count;

	if (!UsbCommandIn) return;

	EA = FALSE;	// Interrupt Disable

	for( i=0 ; i<8 ; i++ )
	{
		USB_Command[i] = USB_Command_Temp[i];
	}

	UsbCommandIn = FALSE;

	EA = TRUE;	// Interrupt enable

	if (USB_Command[0] != 0x40) return;	// Host to device, Vendor command

	Volume_Mute();
	Yellow_On();

	Freq_24M();
	Delay_100ms(1);

//	Green_Light(3,3);

	if(!(USB_Command[1]&0x01))
	{
		Mem_Select=BIM;
		refreshed = BIM_Refreshed;
		Current_Flash_Mem_Blocks = Mem1_Blocks + Mem2_Blocks;
		Current_Flash_Mem_Files = BIM_Files;
	}
	else
	{
		Mem_Select=SMC;
		refreshed = SMC_Refreshed;
		Current_Flash_Mem_Blocks = SMC_Blocks;
		Current_Flash_Mem_Files = SMC_Files;
	}

	USB_command = USB_Command[1]&0xf0;

	SectorBufferForUSB();

/*
	Upload_Byte(Download_Byte());
	Upload_Byte(Download_Byte());
	goto USB_EXIT;
*/
	if(USB_command!=usbMEM_INFO)
	{
		if(refreshed)
		{
			Upload_Byte(REFRESHED);
		}
		else
		{
			Upload_Byte(0xff);
			USB_command = usbMEM_INFO;
//			Upload_Mem_Info();
//			goto USB_EXIT;
		}
	}

//	Red_Light(3,3);
	if(Download_Word()!=START) goto USB_EXIT;

	switch (USB_command)
	{
		case usbRESET:
			if(!Wait_UID_Transfered(START)) return;
			if(!Wait_UID_Transfered(END)) return;
			break;

		case usbMEM_INFO:
			Upload_Mem_Info();

			if(Download_Word() == START)
			{
				Flash_Mem_Format();
				Upload_Mem_Info();
			}

			break;

		case usbFORMAT:	// Buli-in Mem All Erase
			if( !Flash_Mem_Format() ) goto USB_EXIT;
			Upload_Mem_Info();
			break;

		case usbDOWNLOAD:

//			parameter = Download_Word();
//			if(parameter == 0xffff) goto USB_EXIT;

			Upload_RD();

			if(Download_Word() != START) break;

			while( (parameter = Download_Word() ) != 0xffff )	// parameter = Amount of Blocks
			{
				if( !Check_Unused_Mem(parameter) ) break;	// REMAINED_MEM_ERROR
				if( !Download_1File(parameter) ) break;		// REMAINED_MEM_ERROR because bad block
				Upload_Word(File_Start_Block);
				Upload_Word(File_Last_Block);
//				if( !Download_File_Info() ) goto USB_EXIT;	// SYSTEM_AREA_ERROR
				Current_Flash_Mem_Files ++;
			}
			Download_RD();

			Upload_Mem_Info();

			break;

		case usbUPLOAD:
			while( (parameter = Download_Word() ) != 0xffff )	// parameter = File's number
			{
				Upload_1File(parameter);
			}
			break;

		case usbDELETE:
			Upload_RD();

			while( (parameter = Download_Word() ) != 0xffff )	// parameter = File's number
			{
				Delete_File(parameter);
				Req(END);
			}

			Download_RD();

			Upload_Mem_Info();
			break;

		case usbCHANGE_ORDER:
//			Upload_RD();
			Download_RD();
			Upload_Mem_Info();
			break;
/*
		case usbREFRESH:
			SmartMediaReadID();
			UsbInterruptData = SMI_DeviceCode;
			break;

		case usbEXIT:
			Mem_Reset();
			break;
*/
		default:
			break;
//			break;
	}

USB_EXIT:
//	Red_Light(1,2);
	Freq_12M();
	Delay_100ms(1);
//	Download(1);
	Status_Change(STATUS_STOP);
	Init_Key();
}

⌨️ 快捷键说明

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