DMP_INLINE(void) EP0_SetupHandler(USB_Device *usb) { #if defined DMP_DOS_DJGPP dosmemget(usb->EP[0].SetupPhysical, EP0_MAX_PACKET_SIZE, usb->EP[0].SetupBuf); #endif usb->Setup.bmRequestType = usb->EP[0].SetupBuf[0]; usb->Setup.bRequest = usb->EP[0].SetupBuf[1]; usb->Setup.wValue.Value = ((WORD)usb->EP[0].SetupBuf[3] << 8) + (WORD)usb->EP[0].SetupBuf[2]; usb->Setup.wIndex.Value = ((WORD)usb->EP[0].SetupBuf[5] << 8) + (WORD)usb->EP[0].SetupBuf[4]; usb->Setup.wLength = ((WORD)usb->EP[0].SetupBuf[7] << 8) + (WORD)usb->EP[0].SetupBuf[6]; if (usb->stall == true) { io_outpdw(usb->EP[0].CtrlTR, io_inpdw(usb->EP[0].CtrlTR) & 0xDFFF); io_outpdw(usb->EP[0].CtrlTR, io_inpdw(usb->EP[0].CtrlTR) | 0x2000); usb->stall = false; } switch (usb->Setup.bmRequestType) { case 0x00: case 0x01: case 0x02: case 0x80: case 0x81: case 0x82: USB_Standard_Request(usb); break; case 0x21: case 0xA1: USB_CDC_Request(usb); break; default: break; }; SetEPnDLR(usb, EP0, SETUP, ENABLE); }
DMP_INLINE(void) SetEPnDLR(USB_Device *usb, int ep, int dir, DWORD flag) { switch (dir) { case IN : io_outpdw(usb->EP[ep].InDLR , flag); break; case OUT : io_outpdw(usb->EP[ep].OutDLR , flag); break; case SETUP: io_outpdw(usb->EP[ep].SetupDLR, flag); break; default: break; } }
bool init() { int i; if(io_Init() == false) return false; //set corssbar Base Address sb_Write16(0x64,0x0A01); //printf("SB C0~C3 REG: 0x%08X\n", sb_Read(0xc0)); sb_Write(0xc0, sb_Read(0xc0) | 0x8000C000L); //printf("SB C0~C3 REG: 0x%08X\n", sb_Read(0xc0)); //set GPIO Base Address sb_Write16(0x62,0xf101); // GPIO enable io_outpdw(0xf100,0x00ff); //set GPIO P0~9 dircetion & data Address //io_outpdw(0xf100,0x03ff); for(i=0;i<8;i++) { io_outpdw(0xf100 + (i+1)*4,((0xf202 + i*4)<<16) + 0xf200 + i*4); //io_outpb((sb_Read16(0x64)&0xfffe)+i,0x00); } //setADC Base Address sb_Write(0xbc, sb_Read(0xbc) & (~(1L<<28))); // active adc sb1_Write16(0xde, sb1_Read16(0xde) | 0x02); // not Available for 8051A Access ADC sb1_Write(0xe0, 0x0010fe00L); // baseaddr = 0xfe00, disable irq //CDC USBDEV = CreateUSBDevice(); if(USBDEV == NULL) { printf("init error\n"); return false; } usb_SetUSBPins(USBDEV, 7, 0, 7, 1); if(usb_Init(USBDEV) == false) { printf("init2 error\n"); return false; } //io_Close(); return true; }
static void set_pin_in(char port, char pin) { io_outpdw(gpio_config_addr + 0x00, io_inpdw(gpio_config_addr + 0x00) | 0x01L << port); // set data address if ((usb_detect_data = io_inpw(gpio_config_addr + (4 + 4*port))) == 0x0000) io_outpw(gpio_config_addr + (4 + 4*port), (usb_detect_data = (GPIO_BASE_ADDR + port))); // set direction address if ((usb_detect_dir = io_inpw(gpio_config_addr + (6 + 4*port))) == 0x0000) io_outpw(gpio_config_addr + (6 + 4*port), (usb_detect_dir = (GPIO_BASE_ADDR + port + 10))); // set USB-Detect pin -> IN io_outpb(usb_detect_dir, io_inpb(usb_detect_dir) & ~(1 << pin)); usb_detect_pin = pin; }
static void set_rx_led(char port, char pin) { io_outpdw(gpio_config_addr + 0x00, io_inpdw(gpio_config_addr + 0x00) | 0x01L << port); // set data address if ((rx_led_data = io_inpw(gpio_config_addr + (4 + 4*port))) == 0x0000) io_outpw(gpio_config_addr + (4 + 4*port), (rx_led_data = (GPIO_BASE_ADDR + port))); // set direction address if ((rx_led_dir = io_inpw(gpio_config_addr + (6 + 4*port))) == 0x0000) io_outpw(gpio_config_addr + (6 + 4*port), (rx_led_dir = (GPIO_BASE_ADDR + port + 10))); // set RX LED pin -> OUT io_outpb(rx_led_dir, io_inpb(rx_led_dir) | (1 << pin)); rx_led_pin = pin; }
static void set_pin_out(char port, char pin) { io_outpdw(gpio_config_addr + 0x00, io_inpdw(gpio_config_addr + 0x00) | 0x01L << port); // set data address if ((usb_on_off_data = io_inpw(gpio_config_addr + (4 + 4*port))) == 0x0000) io_outpw(gpio_config_addr + (4 + 4*port), (usb_on_off_data = (GPIO_BASE_ADDR + port))); // set direction address if ((usb_on_off_dir = io_inpw(gpio_config_addr + (6 + 4*port))) == 0x0000) io_outpw(gpio_config_addr + (6 + 4*port), (usb_on_off_dir = (GPIO_BASE_ADDR + port + 10))); // set USB-ONOFF pin -> OUT io_outpb(usb_on_off_dir, io_inpb(usb_on_off_dir) | (1 << pin)); io_outpb(usb_on_off_data, io_inpb(usb_on_off_data) | (1 << pin)); usb_on_off_pin = pin; }
bool init() { int i, crossbarBase, gpioBase; if(io_Init() == false) return false; timer_NowTime(); // initialize timer CLOCKS_PER_MICROSEC = vx86_CpuCLK(); VORTEX86EX_CLOCKS_PER_MS = CLOCKS_PER_MICROSEC*1000UL; // Set IRQ4 as level-trigger io_outpb(0x4D0, io_inpb(0x4D0) | 0x10); //set corssbar Base Address crossbarBase = sb_Read16(SB_CROSSBASE) & 0xfffe; if(crossbarBase == 0 || crossbarBase == 0xfffe) sb_Write16(SB_CROSSBASE, CROSSBARBASE | 0x01); // Force set HIGH speed ISA on SB sb_Write(SB_FCREG, sb_Read(SB_FCREG) | 0x8000C000L); //set SB GPIO Base Address gpioBase = sb_Read16(SB_GPIOBASE) & 0xfffe; if(gpioBase == 0 || gpioBase == 0xfffe) { sb_Write16(SB_GPIOBASE, GPIOCTRLBASE | 0x01); gpioBase = GPIOCTRLBASE; } // Enable GPIO 0 ~ 7 io_outpdw(gpioBase, 0x00ff); // set GPIO Port 0~7 dircetion & data Address for(i=0;i<8;i++) io_outpdw(gpioBase + (i+1)*4,((GPIODIRBASE + i*4)<<16) + GPIODATABASE + i*4); // set ADC Base Address sb_Write(0xbc, sb_Read(0xbc) & (~(1L<<28))); // active adc sb1_Write16(0xde, sb1_Read16(0xde) | 0x02); // not Available for 8051A Access ADC sb1_Write(0xe0, 0x0050fe00L); // baseaddr = 0xfe00, disable irq // set MCM Base Address set_MMIO(); mc_setbaseaddr(); for(i=0; i<4; i++) mc_SetMode(i, MCMODE_PWM_SIFB); if(Global_irq_Init == false) { // set MCM IRQ if(irq_Init() == false) { printf("MCM IRQ init fail\n"); return false; } if(irq_Setting(GetMCIRQ(), IRQ_LEVEL_TRIGGER + IRQ_DISABLE_INTR) == false) { printf("MCM IRQ Setting fail\n"); return false; } Set_MCIRQ(GetMCIRQ()); Global_irq_Init = true; } //CDC USBDEV = CreateUSBDevice(); if(USBDEV == NULL) { printf("init error\n"); return false; } usb_SetUSBPins(USBDEV, 7, 0, 7, 1); usb_SetTimeOut(USBDEV, 0L, 500L); // USB RX timerout is 0ms and TX timeout is 500ms if(usb_Init(USBDEV) == false) { printf("init2 error\n"); return false; } //io_Close(); return true; }
static int USB_ISR(int irq, void* data) { DWORD isr; USB_Device *usb; usb = (USB_Device *)data; if ((isr = io_inpdw(usb->ISR)) != 0x00L) { // Bus reset interrupt if (isr & IBRST) { io_outpdw(usb->ISR, IALL); usb_Reset(usb); } // SOF interrupt if (isr & ISOF) { io_outpdw(usb->ISR, ISOF); if (usb->ReadySetAddr == true) { io_outpb(usb->DAR, usb->DevAddr | 0x80); usb->ReadySetAddr = false; } #ifdef DMP_86DUINO_MODE if(tx_led_count && !(--tx_led_count)) TX_LED_OFF(); if(rx_led_count && !(--rx_led_count)) RX_LED_OFF(); #endif } // Suspend interrupt if (isr & ISUSP) { io_outpdw(usb->ISR, ISUSP); usb->IsSet = 0; usb->state = USB_DEV_POWERED; #ifdef DMP_86DUINO_MODE TX_LED_OFF(); RX_LED_OFF(); #endif } // Resume interrupt if (isr & IRESM) { io_outpdw(usb->ISR, IRESM); } // System error interrupt if (isr & SYSERR) { io_outpdw(usb->ISR, SYSERR); } // EP0 Setup interrupt if (isr & IEP0SETUP) { io_outpdw(usb->ISR, IEP0SETUP); EP0_SetupHandler(usb); } // EP0 RX interrupt if (isr & IEP0RX) { io_outpdw(usb->ISR, IEP0RX); EP0_OutHandler(usb); } // EP0 TX interrupt if (isr & IEP0TX) { io_outpdw(usb->ISR, IEP0TX); EP0_InHandler(usb); } // EP1 RX interrupt if (isr & IEP1RX) { io_outpdw(usb->ISR, IEP1RX); } // EP1 TX interrupt if (isr & IEP1TX) { io_outpdw(usb->ISR, IEP1TX); EP1_InHandler(usb); } // EP2 RX interrupt if (isr & IEP2RX) { io_outpdw(usb->ISR, IEP2RX); EP2_OutHandler(usb); } // EP2 TX interrupt if (isr & IEP2TX) { io_outpdw(usb->ISR, IEP2TX); usb->bulk_in_transmitting = false; EP2_InHandler(usb); } // EP3 RX interrupt if (isr & IEP3RX) { io_outpdw(usb->ISR, IEP3RX); } // EP3 TX interrupt if (isr & IEP3TX) { io_outpdw(usb->ISR, IEP3TX); } #ifdef DMP_86DUINO_MODE // the below behavier is only for compatible of Arduino Leonado (windows) if(usb->ling_coding.dwDTERate == 1200 && (usb->control_line_state & 0x01) == 0) { io_outpb(usb_on_off_data, io_inpb(usb_on_off_data) | (1 << usb_on_off_pin)); io_outpb(0xf21A, 0x5a); // write soft reset key io_outpb(0x64, 0xfe); // reboot } #endif } else return ISR_NONE; return ISR_HANDLED; }
DMPAPI(bool) usb_Init(void *vusb) { #if defined DMP_DOS_DJGPP static bool locked = false; int size_temp; #endif USB_Device *usb = (USB_Device *)vusb; if (usb->InUse == 1) return true; // if (USB_IsAttached() == false) return false; USB_Connect(); if (irq_Init() == false) { err_print((char*)"%s: IRQ init fail.\n", __FUNCTION__); io_Close(); return false; } #if defined DMP_DOS_DJGPP if (locked == false) { int i, str_size; DPMI_LOCK_FUNC(SetEPnDLR); DPMI_LOCK_FUNC(Set_Address); DPMI_LOCK_FUNC(Get_Descriptor); DPMI_LOCK_FUNC(Set_Descriptor); DPMI_LOCK_FUNC(Get_Configuration); DPMI_LOCK_FUNC(Set_Configuration); DPMI_LOCK_FUNC(Get_Interface); DPMI_LOCK_FUNC(Set_Interface); DPMI_LOCK_FUNC(Synch_Frame); DPMI_LOCK_FUNC(USB_Standard_Request); DPMI_LOCK_FUNC(Set_Line_Coding); DPMI_LOCK_FUNC(Get_Line_Coding); DPMI_LOCK_FUNC(Set_Control_Line_State); DPMI_LOCK_FUNC(Send_Break); DPMI_LOCK_FUNC(USB_CDC_Request); DPMI_LOCK_FUNC(EP0_SetupHandler); DPMI_LOCK_FUNC(EP0_InHandler); DPMI_LOCK_FUNC(EP0_OutHandler); DPMI_LOCK_FUNC(EP1_InHandler); DPMI_LOCK_FUNC(EP2_InHandler); DPMI_LOCK_FUNC(EP2_OutHandler); DPMI_LOCK_FUNC(usb_Reset); DPMI_LOCK_FUNC(USB_ISR); DPMI_LOCK_VAR(desc_Device); DPMI_LOCK_VAR(desc_Config_Set); DPMI_LOCK_VAR(StringDescTable[0]); DPMI_LOCK_VAR(StringDescTable[1]); DPMI_LOCK_VAR(StringDescTable[2]); DPMI_LOCK_VAR(StringDescTable[3]); DPMI_LOCK_VAR(StringDescTable[4]); DPMI_LOCK_VAR(StringDescTable[5]); DPMI_LOCK_VAR(StringDescTable[6]); locked = true; } #endif io_outpb(usb->CFR, 0x02); // Soft reset while (io_inpb(usb->CFR) & 0x02); if ((usb->EP[0].SetupBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP0_MAX_PACKET_SIZE)) == NULL) goto EP0_SETUP_FAIL; if ((usb->EP[0].InBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP0_MAX_PACKET_SIZE)) == NULL) goto EP0_IN_FAIL; if ((usb->EP[0].OutBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP0_MAX_PACKET_SIZE)) == NULL) goto EP0_OUT_FAIL; if ((usb->EP[1].InBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP1_MAX_PACKET_SIZE_IN)) == NULL) goto EP1_IN_FAIL; if ((usb->EP[2].InBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP2_MAX_PACKET_SIZE_IN)) == NULL) goto EP2_IN_FAIL; if ((usb->EP[2].OutBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP2_MAX_PACKET_SIZE_OUT)) == NULL) goto EP2_OUT_FAIL; #if defined DMP_DOS_DJGPP if ((dma_handle = dma_Alloc(EP0_MAX_PACKET_SIZE + EP0_MAX_PACKET_SIZE + EP0_MAX_PACKET_SIZE + EP1_MAX_PACKET_SIZE_IN + EP2_MAX_PACKET_SIZE_IN + EP2_MAX_PACKET_SIZE_OUT, &dma_addr)) == DMA_FAIL) goto EP2_OUT_FAIL; size_temp = 0; usb->EP[0].SetupPhysical = dma_addr; usb->EP[0].InPhysical = dma_addr + (size_temp += EP0_MAX_PACKET_SIZE); usb->EP[0].OutPhysical = dma_addr + (size_temp += EP0_MAX_PACKET_SIZE); usb->EP[1].InPhysical = dma_addr + (size_temp += EP0_MAX_PACKET_SIZE); usb->EP[2].InPhysical = dma_addr + (size_temp += EP1_MAX_PACKET_SIZE_IN); usb->EP[2].OutPhysical = dma_addr + (size_temp += EP2_MAX_PACKET_SIZE_IN); #else usb->EP[0].SetupPhysical = GrabPhysicalMEM((void *)usb->EP[0].SetupBuf); usb->EP[0].InPhysical = GrabPhysicalMEM((void *)usb->EP[0].InBuf); usb->EP[0].OutPhysical = GrabPhysicalMEM((void *)usb->EP[0].OutBuf); usb->EP[1].InPhysical = GrabPhysicalMEM((void *)usb->EP[1].InBuf); usb->EP[2].InPhysical = GrabPhysicalMEM((void *)usb->EP[2].InBuf); usb->EP[2].OutPhysical = GrabPhysicalMEM((void *)usb->EP[2].OutBuf); #endif // usb->DevAddr = 0x00; // usb->ReadySetAddr = false; // io_outpb(usb->DAR, 0x00); // enable USB device io_outpdw(usb->EP[0].SetupDSR, usb->EP[0].SetupPhysical); io_outpdw(usb->EP[0].InDSR , usb->EP[0].InPhysical); io_outpdw(usb->EP[0].OutDSR , usb->EP[0].OutPhysical); io_outpdw(usb->EP[1].InDSR , usb->EP[1].InPhysical); io_outpdw(usb->EP[2].InDSR , usb->EP[2].InPhysical); io_outpdw(usb->EP[2].OutDSR , usb->EP[2].OutPhysical); io_outpw(usb->EP[0].CtrlTR , 0x2000 | EP0_MAX_PACKET_SIZE); io_outpw(usb->EP[1].InTR , 0x3800 | EP1_MAX_PACKET_SIZE_IN); io_outpw(usb->EP[2].InTR , 0x3000 | EP2_MAX_PACKET_SIZE_IN); io_outpw(usb->EP[2].OutTR , 0x3000 | EP2_MAX_PACKET_SIZE_OUT); SetEPnDLR(usb, EP0, SETUP, ENABLE); // io_outpb(usb->DAR, 0x80); // enable USB device // while (!(io_inpb(usb->DAR) & 0x80)); // ClearQueue(usb->rcvd); // ClearQueue(usb->xmit); // io_DisableINT(); // { // io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); // io_outpdw(usb->IER, ISOF + IBRST + ISUSP + IRESM + SYSERR + // IEP0SETUP + IEP0RX + IEP0TX + IEP1TX + IEP2RX + IEP2TX); // io_outpb(usb->CFR, io_inpb(usb->CFR) | 0x01); // } // io_RestoreINT(); // usb->state = USB_DEV_DEFAULT; io_outpb(usb->DAR, 0x80); // enable USB device while (!(io_inpb(usb->DAR) & 0x80)); io_outpdw(usb->ISR, 0xFFFFFFFFL); io_DisableINT(); { io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); irq_Setting(usb->nIRQ, IRQ_LEVEL_TRIGGER); irq_InstallISR(usb->nIRQ, USB_ISR, (void *)usb); io_outpdw(usb->IER, ISOF + IBRST + ISUSP + IRESM + SYSERR + IEP0SETUP + IEP0RX + IEP0TX + IEP1TX + IEP2RX + IEP2TX); io_outpb(usb->CFR, io_inpb(usb->CFR) | 0x01); } io_RestoreINT(); usb->state = USB_DEV_POWERED; usb->InUse = 1; return true; EP2_OUT_FAIL: ker_Mfree(usb->EP[2].InBuf); EP2_IN_FAIL: ker_Mfree(usb->EP[1].InBuf); EP1_IN_FAIL: ker_Mfree(usb->EP[0].OutBuf); EP0_OUT_FAIL: ker_Mfree(usb->EP[0].InBuf); EP0_IN_FAIL: ker_Mfree(usb->EP[0].SetupBuf); EP0_SETUP_FAIL: err_print((char*)"%s: Alloc endpoint buffers error!!\n", __FUNCTION__); irq_Close(); io_Close(); return false; }