usb通讯时readfile没有响应。
上位机和单片机使用标准usb协议通讯,上位机的相关代码:
//这里的详细代码没有给出。openDevice();openHandles();//重点: int result = FileIOApiDeclarations.WriteFile(this._WriteHandle,ref command[0], command.Length,ref TxdBytes, 0); if (result == 0) { int error = Usb.DebuggingDeclarations.GetLastError(); string str = ""; long size = 0; Usb.DebuggingDeclarations.FormatMessage(Usb.DebuggingDeclarations.FORMAT_MESSAGE_ALLOCATE_BUFFER | Usb.DebuggingDeclarations.FORMAT_MESSAGE_FROM_SYSTEM | Usb.DebuggingDeclarations.FORMAT_MESSAGE_IGNORE_INSERTS, ref size, error, 0, str, 0, 0); Log.WriteLog("error when set read command" + str); this.closeHandles(); return retResult; } //FileIOApiDeclarations.CloseHandle(this._WriteHandle); int relReaded = 0; Usb.FileIOApiDeclarations.OVERLAPPED overlapped = new Usb.FileIOApiDeclarations.OVERLAPPED(); this.PrepareForOverlappedTransfer(ref overlapped); System.Threading.Thread.Sleep(500); result = FileIOApiDeclarations.ReadFile(this._ReadHandle, ref data[0], length, ref relReaded, ref overlapped); /*[color=#FF0000]错误出现在此处,每次到这里程序完全没有响应了,拔掉USB设备时程序下行,GetLastError会返回31 前边的获取GUID,writeFlie都是正确的 [/color]*/ if (result == 0) // error handle { int error = Usb.DebuggingDeclarations.GetLastError(); if (error == FileIOApiDeclarations.ERROR_IO_PENDING) { FileIOApiDeclarations.WaitForSingleObject(overlapped.hEvent, 30000); int readBytes = 0; result = FileIOApiDeclarations.GetOverlappedResult(this._ReadHandle, ref overlapped, ref readBytes, false); if (result != 0) retResult = true; }
init_USBused(); init_TimerA(); init_TimerB(); RWsw = P2IN & BIT5; // for USB Write Data if(RWsw == 0) { FCTL2 = FWKEY + FSSEL0 + FN0; FlashErase(ipatient); FlashErase(isetup); } MCU_D12RST_0; //片选usb _NOP(); MCU_D12RST_1; //reset usb 芯片 bEPPflags.value = 0; _EINT(); reconnect_USB(); P1IE = 0X04; opcode = 0xff; index = 0; while(1) { if (bEPPflags.bits.ep2_rxdone) //端点2接收响应 { bEPPflags.bits.ep2_rxdone = 0; //flag set zero if(opcode == 0xff) { //D12_ReadEndpoint(4, 6, EpBuf); if(EpBuf[0] == 0x01)//if(EpBuf[0] == 0x01) { opcode = 1; addr = EpBuf[2]*0x100 + EpBuf[3]; length = EpBuf[4]*0x100 + EpBuf[5]; cur_addr = addr; fileoff = 0; } else if(EpBuf[0] == 0x00) { opcode = 0; addr = EpBuf[2]*0x100 + EpBuf[3]; length = EpBuf[4]*0x100 + EpBuf[5]; cur_addr = addr; fileoff = 0; } } else if(opcode == 1) //将usb接到的数据写入寄存器 { if(index == 256) { index = 0; for(ptr = 0; ptr < 256; ptr++,cur_addr++) FlashWB(cur_addr,data[ptr]); fileoff += 256; if(fileoff == length) { opcode = 0xff; fileoff = 0; index = 0; } } } }//if bEPPflags.bits.ep2_rxdone if(opcode == 0) //read operation { if(rindex == 0) { for(ptr = 0; ptr < 256; ptr++,cur_addr++) data[ptr] = *(char *)(cur_addr); } _DINT(); D12_WriteEndpoint(5,64,&data[rindex]); //通过point5传输数据 _EINT(); rindex = (rindex + 64) % 256; fileoff += 64; //D12_WriteEndpoint(5,64,&data[index]); //index = (index + 64) % 256; if(fileoff == length) { opcode = 0xff; fileoff = 0; rindex = 0; index = 0; icount = 0; } } if (bEPPflags.bits.timer) { //计时器中断响应 bEPPflags.bits.timer = 0; } if (bEPPflags.bits.bus_reset) { //总线重起中断响应 _DINT(); bEPPflags.bits.bus_reset = 0; _EINT(); } // if bus reset if (bEPPflags.bits.suspend) { //挂起中断响应 _DINT(); bEPPflags.bits.suspend= 0; _EINT(); if((P4OUT & D12_SUSPD) == D12_SUSPD) { D12_SUSPD_0; P5DIR = 0XFF; P5OUT = 0xFF; P5DIR = 0X0; P4DIR = 0XFF; P4OUT = 0xFF; D12_SetDMA(0xC3); D12_SUSPD_1; } } // if suspend change if (bEPPflags.bits.setup_packet) { //接收到设置包中断处理 _DINT(); bEPPflags.bits.setup_packet = 0; control_handler(); _EINT(); //P1OUT |=D12SUSPD; } // if setup_packet } // Main Loop