uint8_t *EthernetClass::localMAC() { int i; void *mac_io; if (io_Init() == false) ; else if ((mac_io = io_Alloc(IO_USE_MMIO, 0xFFFFFFB0UL, 0x06UL)) == NULL) io_Close(); else { for (i = 0; i < 6; i++) MAC_address[i] = io_In8(mac_io, i); io_Free(mac_io); io_Close(); } return &MAC_address[0]; }
DMPAPI(bool) uart_Init(void *vport) { SerialPort *port = (SerialPort *)vport; if (vx86_uart_Init(port->com) == false) { err_print((char*)"%s: Vertex86 COM%d init fail!!\n", __FUNCTION__, port->com + 1); goto VX86_UART_INIT_FAIL; } if (uart_IsOK(port) == false) { err_print((char*)"%s: COM%d doesn't exist.\n", __FUNCTION__, port->com + 1); goto CHECK_EXIST_FAIL; } if (irq_Init() == false) { err_print((char*)"%s: IRQ init fail.\n", __FUNCTION__); goto IRQ_INIT_FAIL; } // save old UART config port->old_lsb = port->lsb = _16550_DLAB_In(port, port->DLLSB); port->old_msb = port->msb = _16550_DLAB_In(port, port->DLMSB); port->old_ier = port->ier = io_inpb(port->IER); port->old_lcr = port->lcr = io_inpb(port->LCR); port->old_mcr = port->mcr = io_inpb(port->MCR); port->old_TimeOut = port->TimeOut = UART_NO_TIMEOUT; // UART initial uart_SetBaud(vport, UARTBAUD_115200BPS); uart_SetFormat(vport, BYTESIZE8 + STOPBIT1 + NOPARITY); uart_SetTimeOut(vport, UART_NO_TIMEOUT); // flush rx & tx queue uart_FlushRxQueue(vport); uart_FlushTxQueue(vport); // clear Hardware RX & TX FIFO uart_ClearRFIFO(vport); uart_ClearWFIFO(vport); // enable FIFO mode uart_SetHWFIFO(vport, ENABLE_HW_FIFO_16); // enable receive data interrupt uart_IntEnable(vport, IERALL); // set flow control uart_SetFlowControl(vport, NO_CONTROL); port->InUse = 1; return true; IRQ_INIT_FAIL: CHECK_EXIST_FAIL: vx86_uart_Close(port->com); VX86_UART_INIT_FAIL: if (io_Close() == false) err_print((char*)"Close IO lib error!!\n"); return false; }
DMPAPI(void) uart_Close(void *vport) { unsigned char lcr; SerialPort *port = (SerialPort *)vport; if (port == NULL) { err_print((char*)"%s: port is null.\n", __FUNCTION__); return; } if (port->InUse != 0) { // restore old IER & MCR uart_IntDisable(vport); io_DisableINT(); { io_outpb(port->MCR, port->old_mcr); } io_RestoreINT(); irq_Close(); // restore old LSB & MSB #ifdef _VORTEX86EXC_UART_WORKAROUND /* To avoid Vortex86EX(D) write sync. issue. */ io_DisableINT(); { lcr = io_inpb(port->LCR); io_outpb(port->LCR, 0x80); do { io_outpb(port->DLLSB, port->old_lsb); } while (io_inpb(port->DLLSB) != port->old_lsb); do { io_outpb(port->DLMSB, port->old_msb); } while (io_inpb(port->DLMSB) != port->old_msb); io_inpb(0x80); // do IO delay io_outpb(port->LCR, lcr); } io_RestoreINT(); #else _16550_DLAB_Out(port, port->DLLSB, port->old_lsb); _16550_DLAB_Out(port, port->DLMSB, port->old_msb); #endif // restore old LCR & timeout uart_SetFormat(vport, port->old_lcr); uart_SetTimeOut(vport, port->old_TimeOut); vx86_uart_Close(port->com); if (io_Close() == false) err_print((char*)"Close IO lib error!!\n"); } uart_Free(port); }
RBAPI(void) spi_Close(void) { if (SPISW_active == true) return; if (SPI_ioSection == -1) return; spidx_DisableCS(); spi_ClearErrors(); // restore GPIO3[3:0]/external SPI switch setting if (OLD_SPIGPIO3FLAG != 0xffffffffL) write_sb_reg(SB_MULTIFUNC_REG, (read_sb_reg(SB_MULTIFUNC_REG) & 0xfffffffeL) + OLD_SPIGPIO3FLAG); io_Close(SPI_ioSection); SPI_ioSection = -1; OLD_SPIGPIO3FLAG = 0xffffffffL; }
RBAPI(void) spi_CloseSW(void) { if (spi_InUse() == false) return; if (SPISW_active == false) return; if (OLD_SPIGPIO3FLAG != 0xffffffffL) write_sb_reg(SB_MULTIFUNC_REG, (read_sb_reg(SB_MULTIFUNC_REG) & 0xfffffffeL) + OLD_SPIGPIO3FLAG); // restore GPIO3[3:0] io_outpb(GPIO3_DIR, (io_inpb(GPIO3_DIR) & 0xf0) + OLD_SPIGPIODIR); io_outpb(GPIO3_DATA, (io_inpb(GPIO3_DATA) & 0xf0) + OLD_SPIGPIODATA); io_Close(SPI_ioSection); SPI_ioSection = -1; OLD_SPIGPIO3FLAG = 0xffffffffL; SPISW_active = false; }
static bool set_gpio_config_addr(unsigned short addr) { if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); return false; } if ((gpio_config_addr = sb_Read16(0x62) & 0xfffe) == 0x0000) sb_Write16(0x62, sb_Read16(0x62) | (gpio_config_addr = GPIO_CONFIG_ADDR)); sb_Write16(0x62, sb_Read16(0x62) | 0x0001); // io_outpdw(GPIO_CONFIG_ADDR + 0x00, io_inpdw(GPIO_CONFIG_ADDR + 0x00) | 0x00000004L); if (io_Close() == false) err_print((char*)"%s: Close IO lib error!!\n", __FUNCTION__); return true; }
bool cm_init(void) { if (io_Init() == false) return false; sb_Write(0xc0, sb_Read(0xc0) & 0x7fffffffL | ((unsigned long)1UL << 31)); io_Close(); //com_SetUSBPins(2, 0, 2, 1); if ((Serial = com_Init(COM4)) == NULL) return false; com_SetTimeOut(Serial, 0); com_SetBPS(Serial, COM_UARTBAUD_1000000BPS); com_SetFormat(Serial, BYTESIZE8 + STOPBIT1 + NOPARITY); com_SetFlowControl(Serial, NO_CONTROL); com_EnableFIFO(Serial, FIFO_032); return true; }
RBAPI(void) i2c_Close(void) { int i; if (I2C_ioSection == -1) return; for (i=0; i<2; i++) //restore GPIO3/I2C switch setting { if (I2C_action[i] == I2CACT_DISABLE) continue; io_outpb(GPIO3_DIR, (io_inpb(GPIO3_DIR) & (~(0x03 << (i*2+4)))) | OLD_I2CGPIO3DIR[i]); //restore GPIO3 DIR io_outpb(GPIO3_DATA, (io_inpb(GPIO3_DATA) & (~(0x03 << (i*2+4)))) | OLD_I2CGPIO3VAL[i]); //restore GPIO3 VAL write_sb_reg(SB_IPFCTRL3_REG, (read_sb_reg(SB_IPFCTRL3_REG) & (~OLD_I2CGPIO3MASK[i])) | OLD_I2CGPIO3FLAG[i]); //restore GPIO/I2C switch flag I2C_action[i] = I2CACT_DISABLE; } io_Close(I2C_ioSection); I2C_ioSection = -1; }
RBAPI(void) com_Close(int com) { if(com_InUse(com) == false) return; #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) SetCommTimeouts(COM_info[com].fp, &COM_info[com].oldtimeouts); SetCommState(COM_info[com].fp, &(COM_info[com].oldstate)); CloseHandle(COM_info[com].fp); #elif defined(RB_LINUX) tcsetattr(COM_info[com].fp, TCSANOW, &(COM_info[com].oldstate)); close(COM_info[com].fp); #endif if (COM_oldTMode[com] == true) com_EnableTurboMode(com); else com_DisableTurboMode(com); if (COM_oldFMode[com] == true) com_EnableFIFO32(com); else com_DisableFIFO32(com); io_Close(COM_ioSection[com]); COM_ioSection[com] = -1; }
DMPAPI(void) usb_Close(void *vusb) { USB_Device *usb = (USB_Device *)vusb; if (usb == NULL) { err_print((char*)"%s: USB device is null.\n", __FUNCTION__); return; } if (usb->InUse != 0) { io_DisableINT(); { io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); irq_UninstallISR(usb->nIRQ, (void *)usb); } io_RestoreINT(); irq_Close(); io_outpb(usb->CFR, 0x02); // Soft reset while (io_inpb(usb->CFR) & 0x02); #if defined DMP_DOS_DJGPP if (dma_Free(dma_handle) == false) err_print((char*)"%s: Free DMA buffer fail!!\n", __FUNCTION__); #endif ker_Mfree(usb->EP[0].SetupBuf); ker_Mfree(usb->EP[0].InBuf); ker_Mfree(usb->EP[0].OutBuf); ker_Mfree(usb->EP[1].InBuf); ker_Mfree(usb->EP[2].InBuf); ker_Mfree(usb->EP[2].OutBuf); if (io_Close() == false) err_print((char*)"%s: Close IO lib error!!\n", __FUNCTION__); usb->state = USB_DEV_POWERED; usb->InUse = 0; USB_Disconnect(); } DestoryQueue(usb->rcvd); DestoryQueue(usb->xmit); ker_Mfree((void *)usb); }
DMPAPI(void *) CreateUART(int com) { SerialPort *port; if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); return NULL; } if ((port = (SerialPort *)ker_Malloc(sizeof(SerialPort))) == NULL) { io_Close(); return NULL; } port->com = com; port->addr = vx86_uart_GetBaseAddr(com); port->nIRQ = vx86_uart_GetIRQ(com); if (port->addr == 0x00 || port->nIRQ == 0) { err_print((char*)"%s: COM%d is null. [Base address: 0x%04x, IRQ: %d]\n", __FUNCTION__, com+1, port->addr, port->nIRQ); ker_Mfree((void*)port); io_Close(); return NULL; } port->nBuad = 115200L; port->nData = BYTESIZE8; port->nStop = STOPBIT1; port->nParity = NOPARITY; port->control = NO_CONTROL; port->InUse = 0; port->INT_InUse = 0; port->rts = 0; port->cts = CTS_OFF; port->xonxoff_rcvd = XOFF_RCVD; port->xonxoff_xmit = XOFF_XMIT; port->xon = 0; port->xoff = 0; port->old_lsb = port->lsb = 0; port->old_msb = port->msb = 0; port->old_ier = port->ier = 0; port->old_lcr = port->lcr = 0; port->old_mcr = port->mcr = 0; port->old_TimeOut = port->TimeOut = UART_NO_TIMEOUT; port->fcr = 0; port->RFIFO_Size = 0; port->WFIFO_Size = 0; if ((port->rcvd = CreateQueue(RX_QUEUE_SIZE)) == NULL) goto CREATE_RX_QUEUE_FAIL; if ((port->xmit = CreateQueue(TX_QUEUE_SIZE)) == NULL) goto CREATE_TX_QUEUE_FAIL; port->msr_handler = NULL; port->lsr_handler = NULL; port->TXDB = port->addr + 0; port->RXDB = port->addr + 0; port->DLLSB = port->addr + 0; port->DLMSB = port->addr + 1; port->IER = port->addr + 1; port->IIR = port->addr + 2; port->FCR = port->addr + 2; port->LCR = port->addr + 3; port->MCR = port->addr + 4; port->LSR = port->addr + 5; port->MSR = port->addr + 6; port->SCR = port->addr + 7; return (void *)port; CREATE_TX_QUEUE_FAIL: DestoryQueue(port->rcvd); CREATE_RX_QUEUE_FAIL: ker_Mfree((void*)port); io_Close(); return NULL; }
RBAPI(bool) com_Init(int com, int duplex) { if (com_InUse(com) == true) { err_SetMsg(ERROR_COM_INUSE, "COM%d was already opened", com); return false; } #ifdef ROBOIO duplex = (duplex == COM_ADUPLEX)? COM_FDUPLEX : duplex; switch (roboio_GetRBVer()) { case RB_100b1: switch (com) { case COM_PORT1: COM_duplex[com] = (duplex != COM_HDUPLEX_TXDEN)? duplex : COM_HDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX_RTS; break; } break; case RB_100b2: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX_RTS; break; } break; case RB_100b3: switch (com) { case COM_PORT1: COM_duplex[com] = (duplex != COM_HDUPLEX_TXDEN)? duplex : COM_HDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX; break; } break; case RB_100: case RB_100RD: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX; break; } break; case RB_110: case RB_050: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; } break; default: err_SetMsg(ERROR_RBVER_UNKNOWN, "unrecognized RoBoard"); return false; } #else COM_duplex[com] = duplex; #endif if((COM_ioSection[com] = io_Init()) == -1) return false; if(uart_isenabled(com) == false) { err_SetMsg(ERROR_COM_INVALID, "COM%d isn't enabled in BIOS", com); goto COMINIT_FAIL; } COM_baseaddr[com] = uart_getbaseaddr(com); COM_oldTMode[com] = com_IsTurboMode(com); COM_oldFMode[com] = com_IsFIFO32Mode(com); #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) { #ifdef RB_MSVC_WINCE int idx = com; #else int i, idx; // find the device name of the COM port for (idx=0, i=0; i<com; i++) if (uart_isenabled(i) == true) idx++; #endif COM_info[com].fp = CreateFile( COM_portname[idx], // device name of COM port GENERIC_READ | GENERIC_WRITE, // access mode 0, // share mode 0, // security attributes OPEN_EXISTING, // opens a device only if it exists 0, // non-overlapped NULL); // NULL when opening an existing file if (COM_info[com].fp == INVALID_HANDLE_VALUE) { err_SetMsg(ERROR_COM_FAIL, "cannot open COM%d device driver", com); goto COMINIT_FAIL; } // backup the old DCB if (GetCommState(COM_info[com].fp, &(COM_info[com].oldstate)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to get DCB settings"); goto COMINIT_FAIL2; } memcpy(&(COM_info[com].newstate), &(COM_info[com].oldstate), sizeof(DCB)); // set new DCB COM_info[com].newstate.fBinary = TRUE; // binary mode COM_info[com].newstate.fOutxCtsFlow = FALSE; // no CTS output control COM_info[com].newstate.fOutxDsrFlow = FALSE; // no DSR output control COM_info[com].newstate.fDtrControl = DTR_CONTROL_DISABLE; // no DRT control COM_info[com].newstate.fDsrSensitivity = FALSE; // no sensitive to DSR COM_info[com].newstate.fOutX = FALSE; // no S/W output flow control COM_info[com].newstate.fInX = FALSE; // no S/W input flow control COM_info[com].newstate.fErrorChar = FALSE; // no replace parity-error byte COM_info[com].newstate.fNull = FALSE; // no discard NULL byte COM_info[com].newstate.fRtsControl = RTS_CONTROL_DISABLE; // no S/W input flow control COM_info[com].newstate.fAbortOnError = FALSE; // no terminate on errors if (SetCommState(COM_info[com].fp, &(COM_info[com].newstate)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to set DCB settings"); goto COMINIT_FAIL2; } // get old timeout parameters if (GetCommTimeouts(COM_info[com].fp, &(COM_info[com].oldtimeouts)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to get TIMEOUTS settings"); goto COMINIT_FAIL3; } // set timeout parameters (no waiting on read/write) COM_info[com].newtimeouts.ReadIntervalTimeout = MAXDWORD; COM_info[com].newtimeouts.ReadTotalTimeoutConstant = 0; COM_info[com].newtimeouts.ReadTotalTimeoutMultiplier = 0; COM_info[com].newtimeouts.WriteTotalTimeoutConstant = 0; COM_info[com].newtimeouts.WriteTotalTimeoutMultiplier = 0; if (SetCommTimeouts(COM_info[com].fp, &(COM_info[com].newtimeouts)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to set TIMEOUT parameters"); goto COMINIT_FAIL3; } ClearCommBreak(COM_info[com].fp); ClearCommError(COM_info[com].fp, NULL, NULL); // clear all communication errors SetupComm(COM_info[com].fp, 8192, 8192); // set read/write FIFO to 8KB PurgeComm(COM_info[com].fp, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); // clear all communication buffers } #elif defined(RB_LINUX) if ((COM_info[com].fp = open(COM_portname[com], O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) { err_SetMsg(ERROR_COM_FAIL, "cannot open COM%d device driver", com); goto COMINIT_FAIL; } // backup the old termios settings if (tcgetattr(COM_info[com].fp, &(COM_info[com].oldstate)) < 0) { err_SetMsg(ERROR_COM_FAIL, "fail to get termios settings"); goto COMINIT_FAIL2; } memcpy(&(COM_info[com].newstate), &(COM_info[com].oldstate), sizeof(termios)); // set new termios settings COM_info[com].newstate.c_cflag |= CLOCAL | CREAD; COM_info[com].newstate.c_cflag &= ~CRTSCTS; // disable H/W flow control COM_info[com].newstate.c_lflag &= ~(ICANON | // raw mode ISIG | // disable SIGxxxx signals IEXTEN | // disable extended functions ECHO | ECHOE); // disable all auto-echo functions COM_info[com].newstate.c_iflag &= ~(IXON | IXOFF | IXANY); // disable S/W flow control COM_info[com].newstate.c_oflag &= ~OPOST; // raw output COM_info[com].newstate.c_cc[VTIME] = 0; // no waiting to read COM_info[com].newstate.c_cc[VMIN] = 0; if(tcsetattr(COM_info[com].fp, TCSANOW, &(COM_info[com].newstate)) < 0) { err_SetMsg(ERROR_COM_FAIL, "fail to set termios settings"); goto COMINIT_FAIL2; } // clear input/output buffers tcflush(COM_info[com].fp, TCIOFLUSH); #else // TODO ... err_SetMsg(ERROR_COM_INVALID, "unsupported platform"); goto COMINIT_FAIL; #endif if (COM_duplex[com] == COM_HDUPLEX_RTS) clear_rts(com); // set COM direction as input com_SetFormat(com, COM_BYTESIZE8, COM_STOPBIT1, COM_NOPARITY); // default data format: 8 bits, 1 stop bit, no parity com_SetBaud(com, COMBAUD_115200BPS); // default baudrate: 115200 bps com_EnableFIFO32(com); // set Vortex86DX's UART FIFO as 32 bytes return true; #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) COMINIT_FAIL3: SetCommState(COM_info[com].fp, &(COM_info[com].oldstate)); COMINIT_FAIL2: CloseHandle(COM_info[com].fp); #elif defined(RB_LINUX) COMINIT_FAIL2: close(COM_info[com].fp); #endif COMINIT_FAIL: io_Close(COM_ioSection[com]); COM_ioSection[com] = -1; return false; }
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; }
DMPAPI(void *) CreateUSBDevice(void) { USB_Device *usb = NULL; if ((usb = (USB_Device *)ker_Malloc(sizeof(USB_Device))) == NULL) return NULL; if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); ker_Mfree((void *)usb); return NULL; } usb->addr = vx86_GetUSBDevAddr(); usb->nIRQ = vx86_GetUSBDevIRQ(); if (usb->addr == 0x0000 || usb->nIRQ == 0) { io_Close(); ker_Mfree((void *)usb); return NULL; } usb->DevAddr = 0x00; usb->ReadySetAddr = false; usb->state = USB_DEV_NOT_ATTACHED; usb->stall = false; usb->InUse = 0; usb->IsSet = 0; usb->setup_in_handled = false; usb->setup_out_handled = false; usb->bulk_in_transmitting = false; usb->TimeOut = USB_NO_TIMEOUT; usb->InDataPtr = NULL; usb->OutDataPtr = NULL; usb->InDataSize = 0; usb->OutDataSize = 0; if ((usb->rcvd = CreateQueue(RX_QUEUE_SIZE)) == NULL) goto CREATE_RX_QUEUE_FAIL; if ((usb->xmit = CreateQueue(TX_QUEUE_SIZE)) == NULL) goto CREATE_TX_QUEUE_FAIL; usb->Setup.bmRequestType = 0; usb->Setup.bRequest = 0; usb->Setup.wValue.Value = 0; usb->Setup.wIndex.Value = 0; usb->Setup.wLength = 0; usb->ling_coding.dwDTERate = 0; usb->ling_coding.bCharFormat = 0; usb->ling_coding.bParityType = 0; usb->ling_coding.bDataBits = 0; usb->control_line_state = 0; usb->serial_state = 0; usb->DAR = usb->addr + 0x00; usb->CFR = usb->addr + 0x02; usb->FNR = usb->addr + 0x06; usb->IER = usb->addr + 0x08; usb->ISR = usb->addr + 0x0C; usb->TMR = usb->addr + 0x68; memset((Endpoint *)usb->EP, 0, sizeof(usb->EP)); usb->EP[0].CtrlTR = usb->addr + 0x10; usb->EP[1].OutTR = usb->addr + 0x12; usb->EP[1].InTR = usb->addr + 0x14; usb->EP[2].OutTR = usb->addr + 0x16; usb->EP[2].InTR = usb->addr + 0x18; usb->EP[3].OutTR = usb->addr + 0x1A; usb->EP[3].InTR = usb->addr + 0x1C; usb->EP[0].SetupDLR = usb->addr + 0x20; usb->EP[0].OutDLR = usb->addr + 0x24; usb->EP[0].InDLR = usb->addr + 0x28; usb->EP[1].OutDLR = usb->addr + 0x2C; usb->EP[1].InDLR = usb->addr + 0x30; usb->EP[2].OutDLR = usb->addr + 0x34; usb->EP[2].InDLR = usb->addr + 0x38; usb->EP[3].OutDLR = usb->addr + 0x3C; usb->EP[3].InDLR = usb->addr + 0x40; usb->EP[0].SetupDSR = usb->addr + 0x44; usb->EP[0].OutDSR = usb->addr + 0x48; usb->EP[0].InDSR = usb->addr + 0x4C; usb->EP[1].OutDSR = usb->addr + 0x50; usb->EP[1].InDSR = usb->addr + 0x54; usb->EP[2].OutDSR = usb->addr + 0x58; usb->EP[2].InDSR = usb->addr + 0x5C; usb->EP[3].OutDSR = usb->addr + 0x60; usb->EP[3].InDSR = usb->addr + 0x64; #ifdef DMP_86DUINO_MODE set_gpio_config_addr(GPIO_CONFIG_ADDR); // for 86duino set_tx_led(7, 2); set_rx_led(7, 3); TX_LED_OFF(); RX_LED_OFF(); #endif return (void *)usb; CREATE_TX_QUEUE_FAIL: DestoryQueue(usb->rcvd); CREATE_RX_QUEUE_FAIL: io_Close(); ker_Mfree((void *)usb); return NULL; }